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/>.
27 #include "cgroup-show.h"
29 static int compare(const void *a, const void *b) {
30 const pid_t *p = a, *q = b;
39 void show_cgroup(const char *name, const char *prefix, unsigned columns) {
42 size_t n = 0, n_allocated = 0;
45 if (!startswith(name, "name=systemd:"))
48 if (asprintf(&fn, "/cgroup/systemd/%s/cgroup.procs", name + 13) < 0) {
49 log_error("Out of memory");
62 if (fscanf(f, "%lu", &ul) != 1)
68 if (n >= n_allocated) {
71 n_allocated = MAX(16U, n*2U);
73 if (!(npids = realloc(pids, sizeof(pid_t) * n_allocated))) {
74 log_error("Out of memory");
81 assert(n < n_allocated);
82 pids[n++] = (pid_t) ul;
88 /* Filter duplicates */
90 for (i = 0; i < n; i++) {
93 for (j = i+1; j < n; j++)
94 if (pids[i] == pids[j])
103 qsort(pids, n, sizeof(pid_t), compare);
113 printf("%s\342\224\202\n", prefix);
115 for (i = 0; i < n; i++) {
118 get_process_cmdline(pids[i], columns, &t);
120 printf("%s%s %5lu %s\n",
122 i < n-1 ? "\342\224\234" : "\342\224\224",
123 (unsigned long) pids[i], strna(t));