1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "cgroup-util.h"
30 #include "cgroup-show.h"
32 static int compare(const void *a, const void *b) {
33 const pid_t *p = a, *q = b;
42 static char *get_cgroup_path(const char *name) {
45 return strdup("/cgroup/systemd");
47 if (startswith(name, SYSTEMD_CGROUP_CONTROLLER ":"))
48 name += sizeof(SYSTEMD_CGROUP_CONTROLLER);
50 if (path_startswith(name, "/cgroup"))
53 return strappend("/cgroup/systemd/", name);
56 static unsigned ilog10(unsigned long ul) {
67 static int show_cgroup_full(const char *path, const char *prefix, unsigned n_columns, bool more) {
70 size_t n = 0, n_allocated = 0;
73 pid_t pid, biggest = 0;
77 n_columns = columns();
82 if (!(p = get_cgroup_path(path)))
85 r = asprintf(&fn, "%s/cgroup.procs", p);
97 while ((r = cg_read_pid(f, &pid)) > 0) {
99 if (n >= n_allocated) {
102 n_allocated = MAX(16U, n*2U);
104 if (!(npids = realloc(pids, sizeof(pid_t) * n_allocated))) {
112 assert(n < n_allocated);
125 /* Filter duplicates */
127 for (i = 0; i < n; i++) {
130 for (j = i+1; j < n; j++)
131 if (pids[i] == pids[j])
140 qsort(pids, n, sizeof(pid_t), compare);
147 for (i = 0; i < n; i++) {
150 get_process_cmdline(pids[i], n_columns, &t);
152 printf("%s%s %*lu %s\n",
154 (more || i < n-1) ? "\342\224\234" : "\342\224\224",
156 (unsigned long) pids[i],
174 int show_cgroup(const char *path, const char *prefix, unsigned n_columns) {
175 return show_cgroup_full(path, prefix, n_columns, false);
178 int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_columns) {
181 char *p1 = NULL, *p2 = NULL, *fn = NULL;
183 bool shown_pids = false;
187 n_columns = columns();
192 if (!(fn = get_cgroup_path(path)))
195 if (!(d = opendir(fn))) {
200 while ((de = readdir(d))) {
202 if (de->d_type != DT_DIR)
205 if (ignore_file(de->d_name))
209 show_cgroup_full(path, prefix, n_columns, true);
214 printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last));
217 if (!(p1 = strappend(prefix, "\342\224\202 "))) {
222 show_cgroup_recursive(last, p1, n_columns-2);
228 if (asprintf(&last, "%s/%s", strempty(path), de->d_name) < 0) {
235 show_cgroup_full(path, prefix, n_columns, !!last);
238 printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
241 if (!(p2 = strappend(prefix, " "))) {
246 show_cgroup_recursive(last, p2, n_columns-2);