chiark / gitweb /
systemctl: show cgroup contents in status
authorLennart Poettering <lennart@poettering.net>
Mon, 5 Jul 2010 01:06:02 +0000 (03:06 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 5 Jul 2010 01:06:02 +0000 (03:06 +0200)
src/systemctl.c
src/util.c
src/util.h

index d32a688..76f6b84 100644 (file)
@@ -959,6 +959,43 @@ finish:
         return r;
 }
 
+static void show_cgroup(const char *name) {
+        char *fn, *pids;
+        int r;
+        char *p;
+
+        if (!startswith(name, "name=systemd:"))
+                return;
+
+        if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
+                return;
+
+        r = read_one_line_file(fn, &pids);
+        free(fn);
+
+        if (r < 0)
+                return;
+
+        p = pids;
+        while (p[0]) {
+                unsigned long ul;
+                char *t = NULL;
+
+                p += strspn(p, WHITESPACE);
+
+                errno = 0;
+                ul = strtoul(p, &p, 0);
+                if (errno != 0 || ul <= 0)
+                        break;
+
+                get_process_cmdline((pid_t) ul, 60, &t);
+                printf("\t\t%lu %s\n", ul, strna(t));
+                free(t);
+        }
+
+        free(pids);
+}
+
 typedef struct UnitStatusInfo {
         const char *id;
         const char *load_state;
@@ -1079,8 +1116,10 @@ static void print_status_info(UnitStatusInfo *i) {
                 printf("\n");
         }
 
-        if (i->default_control_group)
+        if (i->default_control_group) {
                 printf("\t  CGroup: %s\n", i->default_control_group);
+                show_cgroup(i->default_control_group);
+        }
 }
 
 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
index ea8bfc1..8c22dbe 100644 (file)
@@ -566,6 +566,65 @@ int get_process_name(pid_t pid, char **name) {
         return 0;
 }
 
+int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
+        char *p, *r, *k;
+        int c;
+        bool space = false;
+        size_t left;
+        FILE *f;
+
+        assert(pid >= 1);
+        assert(max_length > 0);
+        assert(line);
+
+        if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
+                return -ENOMEM;
+
+        f = fopen(p, "r");
+        free(p);
+
+        if (!f)
+                return -errno;
+
+        if (!(r = new(char, max_length))) {
+                fclose(f);
+                return -ENOMEM;
+        }
+
+        k = r;
+        left = max_length;
+        while ((c = getc(f)) != EOF) {
+
+                if (isprint(c)) {
+                        if (space) {
+                                if (left <= 4)
+                                        break;
+
+                                *(k++) = ' ';
+                                space = false;
+                        }
+
+                        if (left <= 4)
+                                break;
+
+                        *(k++) = (char) c;
+                }  else
+                        space = true;
+        }
+
+        if (left <= 4) {
+                size_t n = MIN(left-1, 3U);
+                memcpy(k, "...", n);
+                k[n] = 0;
+        } else
+                *k = 0;
+
+        fclose(f);
+
+        *line = r;
+        return 0;
+}
+
 char *strappend(const char *s, const char *suffix) {
         size_t a, b;
         char *r;
index 8c91714..cb47c7a 100644 (file)
@@ -183,6 +183,7 @@ int mkdir_p(const char *path, mode_t mode);
 int rmdir_parents(const char *path, const char *stop);
 
 int get_process_name(pid_t pid, char **name);
+int get_process_cmdline(pid_t pid, size_t max_length, char **line);
 
 char hexchar(int x);
 int unhexchar(char c);