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-show.h"
31 static int compare(const void *a, const void *b) {
32 const pid_t *p = a, *q = b;
41 static char *get_cgroup_path(const char *name) {
43 if (startswith(name, "name=systemd:"))
46 if (path_startswith(name, "/cgroup"))
49 return strappend("/cgroup/systemd/", name);
52 static unsigned ilog10(unsigned long ul) {
63 static int show_cgroup_full(const char *name, const char *prefix, unsigned n_columns, bool more) {
66 size_t n = 0, n_allocated = 0;
70 unsigned long biggest = 0;
73 n_columns = columns();
78 if (!(p = get_cgroup_path(name)))
81 r = asprintf(&fn, "%s/cgroup.procs", p);
96 if (fscanf(f, "%lu", &ul) != 1)
102 if (n >= n_allocated) {
105 n_allocated = MAX(16U, n*2U);
107 if (!(npids = realloc(pids, sizeof(pid_t) * n_allocated))) {
115 assert(n < n_allocated);
116 pids[n++] = (pid_t) ul;
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 *name, const char *prefix, unsigned n_columns) {
175 return show_cgroup_full(name, prefix, n_columns, false);
178 int show_cgroup_recursive(const char *name, const char *prefix, unsigned n_columns) {
181 char *p1 = NULL, *p2 = NULL, *fn = NULL;
183 bool shown_pids = false;
189 n_columns = columns();
194 if (!(fn = get_cgroup_path(name)))
197 if (!(d = opendir(fn))) {
202 while ((de = readdir(d))) {
204 if (de->d_type != DT_DIR)
207 if (ignore_file(de->d_name))
211 show_cgroup_full(name, prefix, n_columns, true);
216 printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last));
219 if (!(p1 = strappend(prefix, "\342\224\202 "))) {
224 show_cgroup_recursive(last, p1, n_columns-2);
230 if (asprintf(&last, "%s/%s", name, de->d_name) < 0) {
237 show_cgroup_full(name, prefix, n_columns, !!last);
240 printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
243 if (!(p2 = strappend(prefix, " "))) {
248 show_cgroup_recursive(last, p2, n_columns-2);