chiark / gitweb /
service: calculate sysv startup priority only in start runlevels
[elogind.git] / src / cgroup-show.c
index b44180e1fb8ab2d9fe018e5b2471e17ef24fedca..bc9c2163291d128504b0624564ffc6443cf534be 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
@@ -26,6 +26,7 @@
 
 #include "util.h"
 #include "macro.h"
+#include "cgroup-util.h"
 #include "cgroup-show.h"
 
 static int compare(const void *a, const void *b) {
@@ -38,17 +39,6 @@ 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;
 
@@ -60,14 +50,14 @@ static unsigned ilog10(unsigned long ul) {
         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) {
         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();
@@ -75,8 +65,8 @@ static int show_cgroup_full(const char *name, const char *prefix, unsigned n_col
         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);
@@ -90,14 +80,7 @@ static int show_cgroup_full(const char *name, const char *prefix, unsigned n_col
         if (!f)
                 return -errno;
 
-        while (!feof(f)) {
-                unsigned long ul;
-
-                if (fscanf(f, "%lu", &ul) != 1)
-                        break;
-
-                if (ul <= 0)
-                        continue;
+        while ((r = cg_read_pid(f, &pid)) > 0) {
 
                 if (n >= n_allocated) {
                         pid_t *npids;
@@ -113,12 +96,15 @@ static int show_cgroup_full(const char *name, const char *prefix, unsigned n_col
                 }
 
                 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;
 
@@ -152,7 +138,7 @@ static int show_cgroup_full(const char *name, const char *prefix, unsigned n_col
                         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));
 
@@ -171,44 +157,31 @@ finish:
         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) {
         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);
                         shown_pids = true;
                 }
 
@@ -221,20 +194,26 @@ int show_cgroup_recursive(const char *name, const char *prefix, unsigned n_colum
                                         goto finish;
                                 }
 
-                        show_cgroup_recursive(last, p1, n_columns-2);
+                        show_cgroup_by_path(last, p1, n_columns-2);
 
                         free(last);
                         last = NULL;
                 }
 
-                if (asprintf(&last, "%s/%s", name, de->d_name) < 0) {
-                        log_error("Out of memory");
+                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);
 
         if (last) {
                 printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
@@ -245,7 +224,7 @@ int show_cgroup_recursive(const char *name, const char *prefix, unsigned n_colum
                                 goto finish;
                         }
 
-                show_cgroup_recursive(last, p2, n_columns-2);
+                show_cgroup_by_path(last, p2, n_columns-2);
         }
 
         r = 0;
@@ -260,3 +239,19 @@ finish:
 
         return r;
 }
+
+int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns) {
+        char *p;
+        int r;
+
+        assert(controller);
+        assert(path);
+
+        if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
+                return r;
+
+        r = show_cgroup_by_path(p, prefix, n_columns);
+        free(p);
+
+        return r;
+}