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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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, bool kernel_threads) {
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 (!kernel_threads && is_kernel_thread(pid) > 0)
88 if (n >= n_allocated) {
91 n_allocated = MAX(16U, n*2U);
93 if (!(npids = realloc(pids, sizeof(pid_t) * n_allocated))) {
101 assert(n < n_allocated);
114 /* Filter duplicates */
116 for (i = 0; i < n; i++) {
119 for (j = i+1; j < n; j++)
120 if (pids[i] == pids[j])
129 qsort(pids, n, sizeof(pid_t), compare);
136 for (i = 0; i < n; i++) {
139 get_process_cmdline(pids[i], n_columns, true, &t);
141 printf("%s%s %*lu %s\n",
143 (more || i < n-1) ? "\342\224\234" : "\342\224\224",
144 (int) ilog10(biggest),
145 (unsigned long) pids[i],
163 int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns, bool kernel_threads) {
166 char *p1 = NULL, *p2 = NULL, *fn = NULL, *gn = NULL;
167 bool shown_pids = false;
171 n_columns = columns();
176 if ((r = cg_fix_path(path, &fn)) < 0)
179 if (!(d = opendir(fn))) {
184 while ((r = cg_read_subgroup(d, &gn)) > 0) {
187 show_cgroup_one_by_path(path, prefix, n_columns, true, kernel_threads);
192 printf("%s\342\224\234 %s\n", prefix, file_name_from_path(last));
195 if (!(p1 = strappend(prefix, "\342\224\202 "))) {
200 show_cgroup_by_path(last, p1, n_columns-2, kernel_threads);
206 r = asprintf(&last, "%s/%s", fn, gn);
219 show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads);
222 printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
225 if (!(p2 = strappend(prefix, " "))) {
230 show_cgroup_by_path(last, p2, n_columns-2, kernel_threads);
246 int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns, bool kernel_threads) {
253 r = cg_get_path(controller, path, NULL, &p);
257 r = show_cgroup_by_path(p, prefix, n_columns, kernel_threads);