]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/cgroup-show.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "alloc-util.h"
28 #include "cgroup-show.h"
29 #include "cgroup-util.h"
31 #include "format-util.h"
32 #include "locale-util.h"
34 #include "output-mode.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "string-util.h"
38 #include "terminal-util.h"
40 static void show_pid_array(
49 unsigned i
, j
, pid_width
;
54 qsort(pids
, n_pids
, sizeof(pid_t
), pid_compare_func
);
56 /* Filter duplicates */
57 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
58 if (pids
[i
] == pids
[j
])
63 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
65 if (flags
& OUTPUT_FULL_WIDTH
)
68 if (n_columns
> pid_width
+2)
69 n_columns
-= pid_width
+2;
73 for (i
= 0; i
< n_pids
; i
++) {
74 _cleanup_free_
char *t
= NULL
;
76 get_process_cmdline(pids
[i
], n_columns
, true, &t
);
79 printf("%s%s ", prefix
, special_glyph(TRIANGULAR_BULLET
));
81 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? TREE_BRANCH
: TREE_RIGHT
)));
83 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
87 static int show_cgroup_one_by_path(
95 _cleanup_fclose_
FILE *f
= NULL
;
96 size_t n
= 0, n_allocated
= 0;
97 _cleanup_free_ pid_t
*pids
= NULL
;
98 _cleanup_free_
char *p
= NULL
;
102 r
= cg_mangle_path(path
, &p
);
106 fn
= strjoina(p
, "/cgroup.procs");
111 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
113 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
116 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
119 assert(n
< n_allocated
);
126 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
131 int show_cgroup_by_path(
137 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
138 _cleanup_closedir_
DIR *d
= NULL
;
140 bool shown_pids
= false;
146 n_columns
= columns();
148 prefix
= strempty(prefix
);
150 r
= cg_mangle_path(path
, &fn
);
158 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
159 _cleanup_free_
char *k
= NULL
;
161 k
= strjoin(fn
, "/", gn
);
166 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
170 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
175 printf("%s%s%s\n", prefix
, special_glyph(TREE_BRANCH
), cg_unescape(basename(last
)));
178 p1
= strappend(prefix
, special_glyph(TREE_VERTICAL
));
183 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
195 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
198 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), cg_unescape(basename(last
)));
201 p2
= strappend(prefix
, " ");
206 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
212 int show_cgroup(const char *controller
,
217 _cleanup_free_
char *p
= NULL
;
222 r
= cg_get_path(controller
, path
, NULL
, &p
);
226 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
229 static int show_extra_pids(
230 const char *controller
,
238 _cleanup_free_ pid_t
*copy
= NULL
;
248 n_columns
= columns();
250 prefix
= strempty(prefix
);
252 copy
= new(pid_t
, n_pids
);
256 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
257 _cleanup_free_
char *k
= NULL
;
259 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
263 if (path_startswith(k
, path
))
269 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
274 int show_cgroup_and_extra(
275 const char *controller
,
279 const pid_t extra_pids
[],
280 unsigned n_extra_pids
,
287 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
291 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
294 int show_cgroup_and_extra_by_spec(
298 const pid_t extra_pids
[],
299 unsigned n_extra_pids
,
302 _cleanup_free_
char *controller
= NULL
, *path
= NULL
;
307 r
= cg_split_spec(spec
, &controller
, &path
);
311 return show_cgroup_and_extra(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);