Debian Codegraph @

src/cgroups/gsm-cgroups.c

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include "config.h"
6
7 #include <unistd.h>
8
9 #include <glib.h>
10 #include <glib/gstdio.h>
11
12 #include "gsm-cgroups.h"
13
14
15 gboolean
16 gsm_cgroups_is_enabled (void)
17 {
18 enum {
19 UNINIT = 0,
20 ENABLED,
21 NOT_ENABLED,
22 };
23 static size_t status = UNINIT;
24
25 if (g_once_init_enter (&status)) {
26 gboolean has_cgroups = g_access ("/proc/cgroups", F_OK) >= 0;
27
28 g_once_init_leave (&status, has_cgroups ? ENABLED : NOT_ENABLED);
29 }
30
31 return status == ENABLED;
32 }
33
34
35 static inline gboolean
36 str_non_empty (const char *str, ssize_t len)
37 {
38 const char *first_non_space = str;
39
40 if (first_non_space == NULL || len == 0) {
41 return FALSE;
42 }
43
44 while (*first_non_space != '\0' &&
45 g_ascii_isspace (*first_non_space) &&
46 (len < 0 || first_non_space - str < len)) {
47 first_non_space++;
48 }
49
50 return *first_non_space != '\0';
51 }
52
53
54 typedef struct _BorrowedString BorrowedString;
55 struct _BorrowedString {
56 /* This IS NOT null terminated! */
57 const char *ptr;
58 size_t len;
59 };
60
61
62 static int
63 borrowed_cmp (gconstpointer a, gconstpointer b)
64 {
65 return strncmp (((BorrowedString *) a)->ptr,
66 ((BorrowedString *) b)->ptr,
67 ((BorrowedString *) a)->len);
68 }
69
70
71 static inline gboolean
72 parse_cgroup_line (const char *line,
73 size_t line_len,
74 BorrowedString *path,
75 BorrowedString *controller)
76 {
77 /*
78 * cgroups(7):
79 * For each cgroup hierarchy of which the process is a member,
80 * there is one entry containing three colon-separated fields:
81 * hierarchy-ID:controller-list:cgroup-path
82 * For example:
83 * 5:cpuacct,cpu,cpuset:/daemons
84 */
85 const char *path_prefix = "name=";
86 const char *path_start = NULL;
87 const char *con_start = NULL;
88 const char *ptr = line;
89 size_t remaining_len = line_len;
90
91 /* Jump past the hierarchy id */
92 con_start = g_strstr_len (ptr, remaining_len, ":");
93 if (con_start == NULL) {
94 return FALSE;
95 }
96
97 ptr = controller->ptr = con_start + 1;
98 remaining_len = line_len - (ptr - line);
99
100 /* Jump past the controller list */
101 path_start = g_strstr_len (ptr, remaining_len, ":");
102 if (path_start == NULL) {
103 return FALSE;
104 }
105
106 /* What remains is the path */
107 ptr = path->ptr = path_start + 1;
108 remaining_len = path->len = line_len - (ptr - line);
109
110 if (!str_non_empty (path->ptr, remaining_len) || strncmp (path->ptr, "/", remaining_len) == 0) {
111 return FALSE;
112 }
113
114 controller->len = path_start - con_start - 1;
115 if (g_str_has_prefix (controller->ptr, path_prefix)) {
116 controller->ptr += strlen (path_prefix);
117 controller->len -= strlen (path_prefix);
118 }
119
120 return TRUE;
121 }
122
123
124 typedef struct _CGroupPath CGroupPath;
125 struct _CGroupPath {
126 BorrowedString name;
127 GArray *controllers;
128 };
129
130
131 static int
132 path_cmp (gconstpointer a, gconstpointer b)
133 {
134 return strncmp (((CGroupPath *) a)->name.ptr,
135 ((CGroupPath *) b)->name.ptr,
136 ((CGroupPath *) a)->name.len);
137 }
138
139
140 static void
141 clear_path (gpointer data)
142 {
143 CGroupPath *path = (CGroupPath *) data;
144
145 if (path->controllers) {
146 g_array_free (path->controllers, TRUE);
147 }
148 }
149
150
151 static inline void
152 add_controller (CGroupPath *path, const char *name, size_t len)
153 {
154 BorrowedString controller = {
155 .ptr = name,
156 .len = len,
157 };
158
159 if (!str_non_empty (name, len)) {
160 return;
161 }
162
163 if (!path->controllers) {
164 path->controllers = g_array_new (TRUE, TRUE, sizeof (BorrowedString));
165 }
166
167 g_array_append_val (path->controllers, controller);
168 }
169
170
171 static char *
172 extract_name (const char *file_text)
173 {
174 g_autoptr (GString) groups = g_string_sized_new (150);
175 g_autoptr (GArray) paths = g_array_new (TRUE, TRUE, sizeof (CGroupPath));
176 const char *ptr = file_text;
177 const char *next_newline = NULL;
178 size_t line_len;
179 gboolean final_line = FALSE;
180
181 g_array_set_clear_func (paths, clear_path);
182
183 if (!str_non_empty (file_text, -1)) {
184 goto out;
185 }
186
187 do {
188 BorrowedString path;
189 BorrowedString controller;
190 guint index;
191
192 next_newline = strstr (ptr, "\n");
193 if (next_newline) {
194 line_len = next_newline - ptr;
195 } else {
196 line_len = strlen (ptr);
197 final_line = TRUE;
198 }
199
200 if (line_len < 1) {
201 goto next;
202 }
203
204 if (!parse_cgroup_line (ptr, line_len, &path, &controller)) {
205 goto next;
206 }
207
208 if (g_array_binary_search (paths, &path, borrowed_cmp, &index)) {
209 add_controller (&g_array_index (paths, CGroupPath, index),
210 controller.ptr,
211 controller.len);
212 } else {
213 CGroupPath path_info = {
214 .name = path,
215 .controllers = NULL,
216 };
217 add_controller (&path_info, controller.ptr, controller.len);
218 g_array_append_val (paths, path_info);
219 g_array_sort (paths, path_cmp);
220 }
221
222 next:
223 ptr = next_newline + 1;
224 } while (!final_line);
225
226 for (size_t i = 0; i < paths->len; i++) {
227 CGroupPath *path = &g_array_index (paths, CGroupPath, i);
228
229 if (groups->len > 0) {
230 g_string_append (groups, ", ");
231 }
232 g_string_append_len (groups, path->name.ptr, path->name.len);
233
234 if (!path->controllers) {
235 continue;
236 }
237
238 g_array_sort (path->controllers, borrowed_cmp);
239
240 g_string_append (groups, " (");
241 for (size_t j = 0; j < path->controllers->len; j++) {
242 BorrowedString *controller =
243 &g_array_index (path->controllers, BorrowedString, j);
244
245 if (j != 0) {
246 g_string_append (groups, ", ");
247 }
248
249 g_string_append_len (groups, controller->ptr, controller->len);
250 }
251 g_string_append_c (groups, ')');
252 }
253
254 out:
255 return g_string_free_and_steal (g_steal_pointer (&groups));
256 }
257
258
259 static const char *
260 extract_name_cached (const char *file_text)
261 {
262 static GHashTable *cache = NULL;
263 g_autofree char *calculated = NULL;
264 const char *name = NULL;
265 guint hash = g_str_hash (file_text);
266
267 if (g_once_init_enter_pointer (&cache)) {
268 GHashTable *table =
269 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
270
271 g_once_init_leave_pointer (&cache, g_steal_pointer (&table));
272 }
273
274 name = (const char *) g_hash_table_lookup (cache, GUINT_TO_POINTER (hash));
275 if (name) {
276 return name;
277 }
278
279 calculated = extract_name (file_text);
280 name = calculated;
281 g_hash_table_insert (cache,
282 GUINT_TO_POINTER (hash),
283 g_steal_pointer (&calculated));
284
285 return name;
286 }
287
288
289 const char *
290 gsm_cgroups_get_name (pid_t pid)
291 {
292 g_autoptr (GError) error = NULL;
293 g_autofree char *path = NULL;
294 g_autofree char *text = NULL;
295
296 if (!gsm_cgroups_is_enabled ()) {
297 return NULL;
298 }
299
300 path = g_strdup_printf ("/proc/%i/cgroup", pid);
301
302 g_file_get_contents (path, &text, NULL, &error);
303 if (error) {
304 return NULL;
305 }
306
307 return extract_name_cached (text);
308 }