1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 unsigned ilog10(unsigned long ul) {
53 static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigned n_columns, bool more) {
56 size_t n = 0, n_allocated = 0;
59 pid_t pid, biggest = 0;
63 n_columns = columns();
68 if ((r = cg_fix_path(path, &p)) < 0)
71 r = asprintf(&fn, "%s/cgroup.procs", p);
83 while ((r = cg_read_pid(f, &pid)) > 0) {
85 if (n >= n_allocated) {
88 n_allocated = MAX(16U, n*2U);
90 if (!(npids = realloc(pids, sizeof(pid_t) * n_allocated))) {
98 assert(n < n_allocated);
111 /* Filter duplicates */
113 for (i = 0; i < n; i++) {
116 for (j = i+1; j < n; j++)
117 if (pids[i] == pids[j])
126 qsort(pids, n, sizeof(pid_t), compare);
133 for (i = 0; i < n; i++) {
136 get_process_cmdline(pids[i], n_columns, &t);
138 printf("%s%s %*lu %s\n",
140 (more || i < n-1) ? "\342\224\234" : "\342\224\224",
141 (int) ilog10(biggest),
142 (unsigned long) pids[i],
160 int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns) {
163 char *p1 = NULL, *p2 = NULL, *fn = NULL, *gn = NULL;
164 bool shown_pids = false;
168 n_columns = columns();
173 if ((r = cg_fix_path(path, &fn)) < 0)
176 if (!(d = opendir(fn))) {
181 while ((r = cg_read_subgroup(d, &gn)) > 0) {
184 show_cgroup_one_by_path(path, prefix, n_columns, true);
189 printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last));
192 if (!(p1 = strappend(prefix, "\342\224\202 "))) {
197 show_cgroup_by_path(last, p1, n_columns-2);
203 r = asprintf(&last, "%s/%s", fn, gn);
216 show_cgroup_one_by_path(path, prefix, n_columns, !!last);
219 printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
222 if (!(p2 = strappend(prefix, " "))) {
227 show_cgroup_by_path(last, p2, n_columns-2);
243 int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns) {
250 if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
253 r = show_cgroup_by_path(p, prefix, n_columns);