-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
#include "util.h"
#include "macro.h"
+#include "cgroup-util.h"
#include "cgroup-show.h"
static int compare(const void *a, const void *b) {
return 0;
}
-static char *get_cgroup_path(const char *name) {
-
- if (startswith(name, "name=systemd:"))
- name += 13;
-
- if (path_startswith(name, "/cgroup"))
- return strdup(name);
-
- return strappend("/cgroup/systemd/", name);
-}
-
static unsigned ilog10(unsigned long ul) {
int n = 0;
return n;
}
-static int show_cgroup_full(const char *name, const char *prefix, unsigned n_columns, bool more) {
+static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigned n_columns, bool more, bool kernel_threads) {
char *fn;
FILE *f;
size_t n = 0, n_allocated = 0;
pid_t *pids = NULL;
char *p;
+ pid_t pid, biggest = 0;
int r;
- unsigned long biggest = 0;
if (n_columns <= 0)
n_columns = columns();
if (!prefix)
prefix = "";
- if (!(p = get_cgroup_path(name)))
- return -ENOMEM;
+ if ((r = cg_fix_path(path, &p)) < 0)
+ return r;
r = asprintf(&fn, "%s/cgroup.procs", p);
free(p);
if (!f)
return -errno;
- while (!feof(f)) {
- unsigned long ul;
+ while ((r = cg_read_pid(f, &pid)) > 0) {
- if (fscanf(f, "%lu", &ul) != 1)
- break;
-
- if (ul <= 0)
+ if (!kernel_threads && is_kernel_thread(pid) > 0)
continue;
if (n >= n_allocated) {
}
assert(n < n_allocated);
- pids[n++] = (pid_t) ul;
+ pids[n++] = pid;
- if (ul > biggest)
- biggest = ul;
+ if (pid > biggest)
+ biggest = pid;
}
+ if (r < 0)
+ goto finish;
+
if (n > 0) {
unsigned i, m;
for (i = 0; i < n; i++) {
char *t = NULL;
- get_process_cmdline(pids[i], n_columns, &t);
+ get_process_cmdline(pids[i], n_columns, true, &t);
printf("%s%s %*lu %s\n",
prefix,
(more || i < n-1) ? "\342\224\234" : "\342\224\224",
- ilog10(biggest),
+ (int) ilog10(biggest),
(unsigned long) pids[i],
strna(t));
return r;
}
-int show_cgroup(const char *name, const char *prefix, unsigned n_columns) {
- return show_cgroup_full(name, prefix, n_columns, false);
-}
-
-int show_cgroup_recursive(const char *name, const char *prefix, unsigned n_columns) {
+int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns, bool kernel_threads) {
DIR *d;
char *last = NULL;
- char *p1 = NULL, *p2 = NULL, *fn = NULL;
- struct dirent *de;
+ char *p1 = NULL, *p2 = NULL, *fn = NULL, *gn = NULL;
bool shown_pids = false;
int r;
- assert(name);
-
if (n_columns <= 0)
n_columns = columns();
if (!prefix)
prefix = "";
- if (!(fn = get_cgroup_path(name)))
- return -ENOMEM;
+ if ((r = cg_fix_path(path, &fn)) < 0)
+ return r;
if (!(d = opendir(fn))) {
free(fn);
return -errno;
}
- while ((de = readdir(d))) {
-
- if (de->d_type != DT_DIR)
- continue;
-
- if (ignore_file(de->d_name))
- continue;
+ while ((r = cg_read_subgroup(d, &gn)) > 0) {
if (!shown_pids) {
- show_cgroup_full(name, prefix, n_columns, true);
+ show_cgroup_one_by_path(path, prefix, n_columns, true, kernel_threads);
shown_pids = true;
}
goto finish;
}
- show_cgroup_recursive(last, p1, n_columns-2);
+ show_cgroup_by_path(last, p1, n_columns-2, kernel_threads);
free(last);
last = NULL;
}
- if (asprintf(&last, "%s/%s", name, de->d_name) < 0) {
+ r = asprintf(&last, "%s/%s", fn, gn);
+ free(gn);
+
+ if (r < 0) {
r = -ENOMEM;
goto finish;
}
}
+ if (r < 0)
+ goto finish;
+
if (!shown_pids)
- show_cgroup_full(name, prefix, n_columns, !!last);
+ show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads);
if (last) {
printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
goto finish;
}
- show_cgroup_recursive(last, p2, n_columns-2);
+ show_cgroup_by_path(last, p2, n_columns-2, kernel_threads);
}
r = 0;
return r;
}
+
+int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns, bool kernel_threads) {
+ char *p;
+ int r;
+
+ assert(controller);
+ assert(path);
+
+ r = cg_get_path(controller, path, NULL, &p);
+ if (r < 0)
+ return r;
+
+ r = show_cgroup_by_path(p, prefix, n_columns, kernel_threads);
+ free(p);
+
+ return r;
+}