From c59760eedae9d9de3be1572b9b612dfd8cc37547 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 5 Jul 2010 03:06:02 +0200 Subject: [PATCH] systemctl: show cgroup contents in status --- src/systemctl.c | 41 +++++++++++++++++++++++++++++++++- src/util.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 1 + 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/systemctl.c b/src/systemctl.c index d32a688d..76f6b84a 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -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) { diff --git a/src/util.c b/src/util.c index ea8bfc19..8c22dbef 100644 --- a/src/util.c +++ b/src/util.c @@ -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; diff --git a/src/util.h b/src/util.h index 8c917148..cb47c7a0 100644 --- a/src/util.h +++ b/src/util.h @@ -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); -- 2.39.5