1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "bus-unit-procs.h"
4 #include "glyph-util.h"
5 #include "hashmap.h"
6 #include "list.h"
7 #include "macro.h"
8 #include "path-util.h"
9 #include "process-util.h"
10 #include "sort-util.h"
11 #include "string-util.h"
12 #include "terminal-util.h"
13 
14 struct CGroupInfo {
15         char *cgroup_path;
16         bool is_const; /* If false, cgroup_path should be free()'d */
17 
18         Hashmap *pids; /* PID → process name */
19         bool done;
20 
21         struct CGroupInfo *parent;
22         LIST_FIELDS(struct CGroupInfo, siblings);
23         LIST_HEAD(struct CGroupInfo, children);
24         size_t n_children;
25 };
26 
add_cgroup(Hashmap * cgroups,const char * path,bool is_const,struct CGroupInfo ** ret)27 static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
28         struct CGroupInfo *parent = NULL, *cg;
29         int r;
30 
31         assert(cgroups);
32         assert(ret);
33 
34         path = empty_to_root(path);
35 
36         cg = hashmap_get(cgroups, path);
37         if (cg) {
38                 *ret = cg;
39                 return 0;
40         }
41 
42         if (!empty_or_root(path)) {
43                 const char *e, *pp;
44 
45                 e = strrchr(path, '/');
46                 if (!e)
47                         return -EINVAL;
48 
49                 pp = strndupa_safe(path, e - path);
50 
51                 r = add_cgroup(cgroups, pp, false, &parent);
52                 if (r < 0)
53                         return r;
54         }
55 
56         cg = new0(struct CGroupInfo, 1);
57         if (!cg)
58                 return -ENOMEM;
59 
60         if (is_const)
61                 cg->cgroup_path = (char*) path;
62         else {
63                 cg->cgroup_path = strdup(path);
64                 if (!cg->cgroup_path) {
65                         free(cg);
66                         return -ENOMEM;
67                 }
68         }
69 
70         cg->is_const = is_const;
71         cg->parent = parent;
72 
73         r = hashmap_put(cgroups, cg->cgroup_path, cg);
74         if (r < 0) {
75                 if (!is_const)
76                         free(cg->cgroup_path);
77                 free(cg);
78                 return r;
79         }
80 
81         if (parent) {
82                 LIST_PREPEND(siblings, parent->children, cg);
83                 parent->n_children++;
84         }
85 
86         *ret = cg;
87         return 1;
88 }
89 
add_process(Hashmap * cgroups,const char * path,pid_t pid,const char * name)90 static int add_process(
91                 Hashmap *cgroups,
92                 const char *path,
93                 pid_t pid,
94                 const char *name) {
95 
96         struct CGroupInfo *cg;
97         int r;
98 
99         assert(cgroups);
100         assert(name);
101         assert(pid > 0);
102 
103         r = add_cgroup(cgroups, path, true, &cg);
104         if (r < 0)
105                 return r;
106 
107         return hashmap_ensure_put(&cg->pids, &trivial_hash_ops, PID_TO_PTR(pid), (void*) name);
108 }
109 
remove_cgroup(Hashmap * cgroups,struct CGroupInfo * cg)110 static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
111         assert(cgroups);
112         assert(cg);
113 
114         while (cg->children)
115                 remove_cgroup(cgroups, cg->children);
116 
117         hashmap_remove(cgroups, cg->cgroup_path);
118 
119         if (!cg->is_const)
120                 free(cg->cgroup_path);
121 
122         hashmap_free(cg->pids);
123 
124         if (cg->parent)
125                 LIST_REMOVE(siblings, cg->parent->children, cg);
126 
127         free(cg);
128 }
129 
cgroup_info_compare_func(struct CGroupInfo * const * a,struct CGroupInfo * const * b)130 static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
131         return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
132 }
133 
dump_processes(Hashmap * cgroups,const char * cgroup_path,const char * prefix,unsigned n_columns,OutputFlags flags)134 static int dump_processes(
135                 Hashmap *cgroups,
136                 const char *cgroup_path,
137                 const char *prefix,
138                 unsigned n_columns,
139                 OutputFlags flags) {
140 
141         struct CGroupInfo *cg;
142         int r;
143 
144         assert(prefix);
145 
146         cgroup_path = empty_to_root(cgroup_path);
147 
148         cg = hashmap_get(cgroups, cgroup_path);
149         if (!cg)
150                 return 0;
151 
152         if (!hashmap_isempty(cg->pids)) {
153                 const char *name;
154                 size_t n = 0, i;
155                 pid_t *pids;
156                 void *pidp;
157                 int width;
158 
159                 /* Order processes by their PID */
160                 pids = newa(pid_t, hashmap_size(cg->pids));
161 
162                 HASHMAP_FOREACH_KEY(name, pidp, cg->pids)
163                         pids[n++] = PTR_TO_PID(pidp);
164 
165                 assert(n == hashmap_size(cg->pids));
166                 typesafe_qsort(pids, n, pid_compare_func);
167 
168                 width = DECIMAL_STR_WIDTH(pids[n-1]);
169 
170                 for (i = 0; i < n; i++) {
171                         _cleanup_free_ char *e = NULL;
172                         const char *special;
173                         bool more;
174 
175                         name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
176                         assert(name);
177 
178                         if (n_columns != 0) {
179                                 unsigned k;
180 
181                                 k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
182 
183                                 e = ellipsize(name, k, 100);
184                                 if (e)
185                                         name = e;
186                         }
187 
188                         more = i+1 < n || cg->children;
189                         special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
190 
191                         fprintf(stdout, "%s%s%s%*"PID_PRI" %s%s\n",
192                                 prefix,
193                                 special,
194                                 ansi_grey(),
195                                 width, pids[i],
196                                 name,
197                                 ansi_normal());
198                 }
199         }
200 
201         if (cg->children) {
202                 struct CGroupInfo **children;
203                 size_t n = 0, i;
204 
205                 /* Order subcgroups by their name */
206                 children = newa(struct CGroupInfo*, cg->n_children);
207                 LIST_FOREACH(siblings, child, cg->children)
208                         children[n++] = child;
209                 assert(n == cg->n_children);
210                 typesafe_qsort(children, n, cgroup_info_compare_func);
211 
212                 if (n_columns != 0)
213                         n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
214 
215                 for (i = 0; i < n; i++) {
216                         _cleanup_free_ char *pp = NULL;
217                         const char *name, *special;
218                         bool more;
219 
220                         name = strrchr(children[i]->cgroup_path, '/');
221                         if (!name)
222                                 return -EINVAL;
223                         name++;
224 
225                         more = i+1 < n;
226                         special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
227 
228                         fputs(prefix, stdout);
229                         fputs(special, stdout);
230                         fputs(name, stdout);
231                         fputc('\n', stdout);
232 
233                         special = special_glyph(more ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE);
234 
235                         pp = strjoin(prefix, special);
236                         if (!pp)
237                                 return -ENOMEM;
238 
239                         r = dump_processes(cgroups, children[i]->cgroup_path, pp, n_columns, flags);
240                         if (r < 0)
241                                 return r;
242                 }
243         }
244 
245         cg->done = true;
246         return 0;
247 }
248 
dump_extra_processes(Hashmap * cgroups,const char * prefix,unsigned n_columns,OutputFlags flags)249 static int dump_extra_processes(
250                 Hashmap *cgroups,
251                 const char *prefix,
252                 unsigned n_columns,
253                 OutputFlags flags) {
254 
255         _cleanup_free_ pid_t *pids = NULL;
256         _cleanup_hashmap_free_ Hashmap *names = NULL;
257         struct CGroupInfo *cg;
258         size_t n = 0, k;
259         int width, r;
260 
261         /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
262          * combined, sorted, linear list. */
263 
264         HASHMAP_FOREACH(cg, cgroups) {
265                 const char *name;
266                 void *pidp;
267 
268                 if (cg->done)
269                         continue;
270 
271                 if (hashmap_isempty(cg->pids))
272                         continue;
273 
274                 r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
275                 if (r < 0)
276                         return r;
277 
278                 if (!GREEDY_REALLOC(pids, n + hashmap_size(cg->pids)))
279                         return -ENOMEM;
280 
281                 HASHMAP_FOREACH_KEY(name, pidp, cg->pids) {
282                         pids[n++] = PTR_TO_PID(pidp);
283 
284                         r = hashmap_put(names, pidp, (void*) name);
285                         if (r < 0)
286                                 return r;
287                 }
288         }
289 
290         if (n == 0)
291                 return 0;
292 
293         typesafe_qsort(pids, n, pid_compare_func);
294         width = DECIMAL_STR_WIDTH(pids[n-1]);
295 
296         for (k = 0; k < n; k++) {
297                 _cleanup_free_ char *e = NULL;
298                 const char *name;
299 
300                 name = hashmap_get(names, PID_TO_PTR(pids[k]));
301                 assert(name);
302 
303                 if (n_columns != 0) {
304                         unsigned z;
305 
306                         z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
307 
308                         e = ellipsize(name, z, 100);
309                         if (e)
310                                 name = e;
311                 }
312 
313                 fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
314                         prefix,
315                         special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET),
316                         width, pids[k],
317                         name);
318         }
319 
320         return 0;
321 }
322 
unit_show_processes(sd_bus * bus,const char * unit,const char * cgroup_path,const char * prefix,unsigned n_columns,OutputFlags flags,sd_bus_error * error)323 int unit_show_processes(
324                 sd_bus *bus,
325                 const char *unit,
326                 const char *cgroup_path,
327                 const char *prefix,
328                 unsigned n_columns,
329                 OutputFlags flags,
330                 sd_bus_error *error) {
331 
332         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
333         Hashmap *cgroups = NULL;
334         struct CGroupInfo *cg;
335         int r;
336 
337         assert(bus);
338         assert(unit);
339 
340         if (flags & OUTPUT_FULL_WIDTH)
341                 n_columns = 0;
342         else if (n_columns <= 0)
343                 n_columns = columns();
344 
345         prefix = strempty(prefix);
346 
347         r = sd_bus_call_method(
348                         bus,
349                         "org.freedesktop.systemd1",
350                         "/org/freedesktop/systemd1",
351                         "org.freedesktop.systemd1.Manager",
352                         "GetUnitProcesses",
353                         error,
354                         &reply,
355                         "s",
356                         unit);
357         if (r < 0)
358                 return r;
359 
360         cgroups = hashmap_new(&path_hash_ops);
361         if (!cgroups)
362                 return -ENOMEM;
363 
364         r = sd_bus_message_enter_container(reply, 'a', "(sus)");
365         if (r < 0)
366                 goto finish;
367 
368         for (;;) {
369                 const char *path = NULL, *name = NULL;
370                 uint32_t pid;
371 
372                 r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
373                 if (r < 0)
374                         goto finish;
375                 if (r == 0)
376                         break;
377 
378                 r = add_process(cgroups, path, pid, name);
379                 if (r == -ENOMEM)
380                         goto finish;
381                 if (r < 0)
382                         log_warning_errno(r, "Invalid process description in GetUnitProcesses reply: cgroup=\"%s\" pid="PID_FMT" command=\"%s\", ignoring: %m",
383                                           path, pid, name);
384         }
385 
386         r = sd_bus_message_exit_container(reply);
387         if (r < 0)
388                 goto finish;
389 
390         r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
391         if (r < 0)
392                 goto finish;
393 
394         r = dump_extra_processes(cgroups, prefix, n_columns, flags);
395 
396 finish:
397         while ((cg = hashmap_first(cgroups)))
398                remove_cgroup(cgroups, cg);
399 
400         hashmap_free(cgroups);
401 
402         return r;
403 }
404