Debian Codegraph @

lib/wp/proc-utils.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2024 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <spa/utils/cleanup.h>
12
13 #include "log.h"
14 #include "proc-utils.h"
15
16 #define MAX_ARGS 1024
17
18 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-proc-utils")
19
20 /*! \defgroup wpprocutils Process Utilities */
21
22 /*!
23 * \struct WpProcInfo
24 *
25 * WpProcInfo holds information of a process.
26 */
27 struct _WpProcInfo {
28 grefcount ref;
29 pid_t pid;
30 pid_t parent;
31 gchar *cgroup;
32 gchar *args[MAX_ARGS];
33 guint n_args;
34 };
35
36 G_DEFINE_BOXED_TYPE (WpProcInfo, wp_proc_info, wp_proc_info_ref,
37 wp_proc_info_unref)
38
39 /*!
40 * \brief Increases the reference count of a process information object
41 * \ingroup wpprocutils
42 * \param self a process information object
43 * \returns (transfer full): \a self with an additional reference count on it
44 */
45 WpProcInfo *
46 wp_proc_info_ref (WpProcInfo * self)
47 {
48 g_ref_count_inc (&self->ref);
49 return self;
50 }
51
52 static void
53 wp_proc_info_free (WpProcInfo * self)
54 {
55 g_clear_pointer (&self->cgroup, g_free);
56 for (guint i = 0; i < MAX_ARGS; i++)
57 g_clear_pointer (&self->args[i], free);
58 g_slice_free (WpProcInfo, self);
59 }
60
61 /*!
62 * \brief Decreases the reference count on \a self and frees it when the ref
63 * count reaches zero.
64 * \ingroup wpprocutils
65 * \param self (transfer full): a process information object
66 */
67 void
68 wp_proc_info_unref (WpProcInfo * self)
69 {
70 if (g_ref_count_dec (&self->ref))
71 wp_proc_info_free (self);
72 }
73
74 static WpProcInfo *
75 wp_proc_info_new (pid_t pid)
76 {
77 WpProcInfo *self = g_slice_new0 (WpProcInfo);
78 g_ref_count_init (&self->ref);
79 self->pid = pid;
80 self->parent = 0;
81 self->cgroup = NULL;
82 for (guint i = 0; i < MAX_ARGS; i++)
83 self->args[i] = NULL;
84 return self;
85 }
86
87 /*!
88 * \brief Gets the PID of a process information object
89 * \ingroup wpprocutils
90 * \param self the process information object
91 * \returns the PID of the process information object
92 */
93 pid_t
94 wp_proc_info_get_pid (WpProcInfo * self)
95 {
96 return self->pid;
97 }
98
99 /*!
100 * \brief Gets the parent PID of a process information object
101 * \ingroup wpprocutils
102 * \param self the process information object
103 * \returns the parent PID of the process information object
104 */
105 pid_t
106 wp_proc_info_get_parent_pid (WpProcInfo * self)
107 {
108 return self->parent;
109 }
110
111 /*!
112 * \brief Gets the number of args of a process information object
113 * \ingroup wpprocutils
114 * \param self the process information object
115 * \returns the number of args of the process information object
116 */
117 guint
118 wp_proc_info_get_n_args (WpProcInfo * self)
119 {
120 return self->n_args;
121 }
122
123 /*!
124 * \brief Gets the indexed arg of a process information object
125 * \ingroup wpprocutils
126 * \param self the process information object
127 * \param index the index of the arg
128 * \returns the indexed arg of the process information object
129 */
130 const gchar *
131 wp_proc_info_get_arg (WpProcInfo * self, guint index)
132 {
133 if (index >= self->n_args)
134 return NULL;
135 return self->args[index];
136 }
137
138 /*!
139 * \brief Gets the systemd cgroup of a process information object
140 * \ingroup wpprocutils
141 * \param self the process information object
142 * \returns the systemd cgroup of the process information object
143 */
144 const gchar *
145 wp_proc_info_get_cgroup (WpProcInfo * self)
146 {
147 return self->cgroup;
148 }
149
150 static FILE *
151 fdopenat (int dirfd, const char *path, int flags, const char *mode, mode_t perm)
152 {
153 int fd = openat (dirfd, path, flags, perm);
154 if (fd >= 0) {
155 FILE *f = fdopen (fd, mode);
156 if (f)
157 return f;
158 close (fd);
159 }
160
161 return NULL;
162 }
163
164
165 /*!
166 * \brief Gets the process information of a given PID
167 * \ingroup wpprocutils
168 * \param pid the PID to get the process information from
169 * \returns: (transfer full): the process information of the given PID
170 */
171 WpProcInfo *
172 wp_proc_utils_get_proc_info (pid_t pid)
173 {
174 WpProcInfo *ret = wp_proc_info_new (pid);
175 char path [64];
176 spa_autoclose int base_fd = -1;
177 FILE *file;
178 g_autofree gchar *line = NULL;
179 size_t size = 0;
180
181 snprintf (path, sizeof(path), "/proc/%d", pid);
182 base_fd = open (path,
183 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY, 0);
184 if (base_fd < 0) {
185 wp_info ("Could not open process info directory %s, skipping", path);
186 return ret;
187 }
188
189 /* Get parent PID */
190 file = fdopenat (base_fd, "status",
191 O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY, "r", 0);
192 if (file) {
193 while (getline (&line, &size, file) > 1)
194 if (sscanf (line, "PPid:%d\n", &ret->parent) == 1)
195 break;
196 fclose (file);
197 }
198
199 /* Get cgroup */
200 file = fdopenat (base_fd, "cgroup",
201 O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY, "r", 0);
202 if (file) {
203 if (getline (&line, &size, file) > 1)
204 ret->cgroup = g_strstrip (g_strdup (line));
205 fclose (file);
206 }
207
208 /* Get args */
209 file = fdopenat (base_fd, "cmdline",
210 O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY, "r", 0);
211 if (file) {
212 while (getdelim (&line, &size, 0, file) > 1 && ret->n_args < MAX_ARGS)
213 ret->args[ret->n_args++] = g_strdup (line);
214 fclose (file);
215 }
216
217 return ret;
218 }