From 1e5678d023ab57807d3de08e10304e36bf9a1950 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 22 Jan 2012 18:18:51 +0100 Subject: [PATCH] cgls: don't show kernel threads by default --- man/systemd-cgls.xml | 14 ++++++++++++++ src/cgls.c | 26 ++++++++++++++++++-------- src/cgroup-show.c | 22 +++++++++++++--------- src/cgroup-show.h | 6 ++++-- src/login/loginctl.c | 4 ++-- src/systemctl.c | 2 +- src/util.c | 31 +++++++++++++++++++++++++++++++ src/util.h | 2 ++ 8 files changed, 85 insertions(+), 22 deletions(-) diff --git a/man/systemd-cgls.xml b/man/systemd-cgls.xml index 8546e394..569764a6 100644 --- a/man/systemd-cgls.xml +++ b/man/systemd-cgls.xml @@ -84,6 +84,20 @@ text and exits. + + + + Do not pipe output into a + pager. + + + + + + Includes kernel + threads in output. + + diff --git a/src/cgls.c b/src/cgls.c index 20d65311..d5417f95 100644 --- a/src/cgls.c +++ b/src/cgls.c @@ -33,13 +33,15 @@ #include "pager.h" static bool arg_no_pager = false; +static bool arg_kernel_threads = false; static void help(void) { printf("%s [OPTIONS...] [CGROUP...]\n\n" "Recursively show control group contents.\n\n" " -h --help Show this help\n" - " --no-pager Do not pipe output into a pager\n", + " --no-pager Do not pipe output into a pager\n" + " -k Include kernel threads in output\n", program_invocation_short_name); } @@ -60,7 +62,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 1); assert(argv); - while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "hk", options, NULL)) >= 0) { switch (c) { @@ -72,6 +74,10 @@ static int parse_argv(int argc, char *argv[]) { arg_no_pager = true; break; + case 'k': + arg_kernel_threads = true; + break; + case '?': return -EINVAL; @@ -90,7 +96,8 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); - if ((r = parse_argv(argc, argv)) < 0) + r = parse_argv(argc, argv); + if (r < 0) goto finish; else if (r == 0) { retval = EXIT_SUCCESS; @@ -107,26 +114,29 @@ int main(int argc, char *argv[]) { int q; printf("%s:\n", argv[i]); - if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0) + q = show_cgroup_by_path(argv[i], NULL, 0, arg_kernel_threads); + if (q < 0) r = q; } } else { char *p; - if (!(p = get_current_dir_name())) { + p = get_current_dir_name(); + if (!p) { log_error("Cannot determine current working directory: %m"); goto finish; } if (path_startswith(p, "/sys/fs/cgroup")) { printf("Working Directory %s:\n", p); - r = show_cgroup_by_path(p, NULL, 0); + r = show_cgroup_by_path(p, NULL, 0, arg_kernel_threads); } else { char *root = NULL; const char *t = NULL; - if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root)) < 0) + r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root); + if (r < 0) t = "/"; else { if (endswith(root, "/system")) @@ -135,7 +145,7 @@ int main(int argc, char *argv[]) { t = root[0] ? root : "/"; } - r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0); + r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0, arg_kernel_threads); free(root); } diff --git a/src/cgroup-show.c b/src/cgroup-show.c index 03c942c6..ee2a241c 100644 --- a/src/cgroup-show.c +++ b/src/cgroup-show.c @@ -50,7 +50,7 @@ static unsigned ilog10(unsigned long ul) { return n; } -static int show_cgroup_one_by_path(const char *path, 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; @@ -82,6 +82,9 @@ static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigne while ((r = cg_read_pid(f, &pid)) > 0) { + if (!kernel_threads && is_kernel_thread(pid) > 0) + continue; + if (n >= n_allocated) { pid_t *npids; @@ -157,7 +160,7 @@ finish: return r; } -int show_cgroup_by_path(const char *path, 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, *gn = NULL; @@ -181,7 +184,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns while ((r = cg_read_subgroup(d, &gn)) > 0) { if (!shown_pids) { - show_cgroup_one_by_path(path, prefix, n_columns, true); + show_cgroup_one_by_path(path, prefix, n_columns, true, kernel_threads); shown_pids = true; } @@ -194,7 +197,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns goto finish; } - show_cgroup_by_path(last, p1, n_columns-2); + show_cgroup_by_path(last, p1, n_columns-2, kernel_threads); free(last); last = NULL; @@ -213,7 +216,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns goto finish; if (!shown_pids) - show_cgroup_one_by_path(path, 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)); @@ -224,7 +227,7 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns goto finish; } - show_cgroup_by_path(last, p2, n_columns-2); + show_cgroup_by_path(last, p2, n_columns-2, kernel_threads); } r = 0; @@ -240,17 +243,18 @@ finish: return r; } -int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns) { +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); - if ((r = cg_get_path(controller, path, NULL, &p)) < 0) + r = cg_get_path(controller, path, NULL, &p); + if (r < 0) return r; - r = show_cgroup_by_path(p, prefix, n_columns); + r = show_cgroup_by_path(p, prefix, n_columns, kernel_threads); free(p); return r; diff --git a/src/cgroup-show.h b/src/cgroup-show.h index ae5b13b5..992e17b9 100644 --- a/src/cgroup-show.h +++ b/src/cgroup-show.h @@ -22,7 +22,9 @@ along with systemd; If not, see . ***/ -int show_cgroup_by_path(const char *path, const char *prefix, unsigned columns); -int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned columns); +#include + +int show_cgroup_by_path(const char *path, const char *prefix, unsigned columns, bool kernel_threads); +int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned columns, bool kernel_threads); #endif diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 1be47c8d..7e0056c5 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -449,7 +449,7 @@ static void print_session_status_info(SessionStatusInfo *i) { else c = 0; - show_cgroup_by_path(i->control_group, "\t\t ", c); + show_cgroup_by_path(i->control_group, "\t\t ", c, false); } } } @@ -501,7 +501,7 @@ static void print_user_status_info(UserStatusInfo *i) { else c = 0; - show_cgroup_by_path(i->control_group, "\t\t ", c); + show_cgroup_by_path(i->control_group, "\t\t ", c, false); } } } diff --git a/src/systemctl.c b/src/systemctl.c index cb59fd6d..5ad81fd6 100644 --- a/src/systemctl.c +++ b/src/systemctl.c @@ -2263,7 +2263,7 @@ static void print_status_info(UnitStatusInfo *i) { else c = 0; - show_cgroup_by_path(i->default_control_group, "\t\t ", c); + show_cgroup_by_path(i->default_control_group, "\t\t ", c, false); } } diff --git a/src/util.c b/src/util.c index de5feeb8..ef6eea4b 100644 --- a/src/util.c +++ b/src/util.c @@ -1100,6 +1100,37 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char * return 0; } +int is_kernel_thread(pid_t pid) { + char *p; + size_t count; + char c; + bool eof; + FILE *f; + + if (pid == 0) + return 0; + + if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0) + return -ENOMEM; + + f = fopen(p, "re"); + free(p); + + if (!f) + return -errno; + + count = fread(&c, 1, 1, f); + eof = feof(f); + fclose(f); + + /* Kernel threads have an empty cmdline */ + + if (count <= 0) + return eof ? 1 : -errno; + + return 0; +} + int get_process_exe(pid_t pid, char **name) { int r; diff --git a/src/util.h b/src/util.h index 114b24c3..1ce2440f 100644 --- a/src/util.h +++ b/src/util.h @@ -531,4 +531,6 @@ void* memdup(const void *p, size_t l); int rtc_open(int flags); +int is_kernel_thread(pid_t pid); + #endif -- 2.39.5