]>
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 "formats-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(pid_t pids
[], unsigned n_pids
, const char *prefix
, unsigned n_columns
, bool extra
, bool more
, bool kernel_threads
, OutputFlags flags
) {
41 unsigned i
, j
, pid_width
;
46 qsort(pids
, n_pids
, sizeof(pid_t
), pid_compare_func
);
48 /* Filter duplicates */
49 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
50 if (pids
[i
] == pids
[j
])
55 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
57 if (flags
& OUTPUT_FULL_WIDTH
)
60 if (n_columns
> pid_width
+2)
61 n_columns
-= pid_width
+2;
65 for (i
= 0; i
< n_pids
; i
++) {
66 _cleanup_free_
char *t
= NULL
;
68 get_process_cmdline(pids
[i
], n_columns
, true, &t
);
71 printf("%s%s ", prefix
, draw_special_char(DRAW_TRIANGULAR_BULLET
));
73 printf("%s%s", prefix
, draw_special_char(((more
|| i
< n_pids
-1) ? DRAW_TREE_BRANCH
: DRAW_TREE_RIGHT
)));
75 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
79 static int show_cgroup_one_by_path(
88 _cleanup_fclose_
FILE *f
= NULL
;
89 size_t n
= 0, n_allocated
= 0;
90 _cleanup_free_ pid_t
*pids
= NULL
;
91 _cleanup_free_
char *p
= NULL
;
95 r
= cg_mangle_path(path
, &p
);
99 fn
= strjoina(p
, "/cgroup.procs");
104 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
106 if (!kernel_threads
&& is_kernel_thread(pid
) > 0)
109 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
112 assert(n
< n_allocated
);
119 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, kernel_threads
, flags
);
124 int show_cgroup_by_path(
131 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
132 _cleanup_closedir_
DIR *d
= NULL
;
134 bool shown_pids
= false;
140 n_columns
= columns();
142 prefix
= strempty(prefix
);
144 r
= cg_mangle_path(path
, &fn
);
152 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
153 _cleanup_free_
char *k
= NULL
;
155 k
= strjoin(fn
, "/", gn
, NULL
);
160 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
164 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, kernel_threads
, flags
);
169 printf("%s%s%s\n", prefix
, draw_special_char(DRAW_TREE_BRANCH
), cg_unescape(basename(last
)));
172 p1
= strappend(prefix
, draw_special_char(DRAW_TREE_VERTICAL
));
177 show_cgroup_by_path(last
, p1
, n_columns
-2, kernel_threads
, flags
);
189 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, kernel_threads
, flags
);
192 printf("%s%s%s\n", prefix
, draw_special_char(DRAW_TREE_RIGHT
), cg_unescape(basename(last
)));
195 p2
= strappend(prefix
, " ");
200 show_cgroup_by_path(last
, p2
, n_columns
-2, kernel_threads
, flags
);
206 int show_cgroup(const char *controller
,
213 _cleanup_free_
char *p
= NULL
;
218 r
= cg_get_path(controller
, path
, NULL
, &p
);
222 return show_cgroup_by_path(p
, prefix
, n_columns
, kernel_threads
, flags
);
225 static int show_extra_pids(
226 const char *controller
,
234 _cleanup_free_ pid_t
*copy
= NULL
;
244 n_columns
= columns();
246 prefix
= strempty(prefix
);
248 copy
= new(pid_t
, n_pids
);
252 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
253 _cleanup_free_
char *k
= NULL
;
255 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
259 if (path_startswith(k
, path
))
265 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, false, flags
);
270 int show_cgroup_and_extra(
271 const char *controller
,
276 const pid_t extra_pids
[],
277 unsigned n_extra_pids
,
284 r
= show_cgroup(controller
, path
, prefix
, n_columns
, kernel_threads
, flags
);
288 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
291 int show_cgroup_and_extra_by_spec(
296 const pid_t extra_pids
[],
297 unsigned n_extra_pids
,
300 _cleanup_free_
char *controller
= NULL
, *path
= NULL
;
305 r
= cg_split_spec(spec
, &controller
, &path
);
309 return show_cgroup_and_extra(controller
, path
, prefix
, n_columns
, kernel_threads
, extra_pids
, n_extra_pids
, flags
);