]> err.no Git - systemd/commitdiff
cgls: don't show kernel threads by default
authorLennart Poettering <lennart@poettering.net>
Sun, 22 Jan 2012 17:18:51 +0000 (18:18 +0100)
committerLennart Poettering <lennart@poettering.net>
Sun, 22 Jan 2012 17:22:26 +0000 (18:22 +0100)
man/systemd-cgls.xml
src/cgls.c
src/cgroup-show.c
src/cgroup-show.h
src/login/loginctl.c
src/systemctl.c
src/util.c
src/util.h

index 8546e39433652c6446c7f353219ff8f163302246..569764a6c2ffd3f0b2f0ea83660001b477c05cfb 100644 (file)
                                 text and exits.</para></listitem>
                         </varlistentry>
 
+                        <varlistentry>
+                                <term><option>--no-pager</option></term>
+
+                               <listitem><para>Do not pipe output into a
+                               pager.</para></listitem>
+                       </varlistentry>
+
+                        <varlistentry>
+                                <term><option>-k</option></term>
+
+                                <listitem><para>Includes kernel
+                                threads in output.</para></listitem>
+                        </varlistentry>
+
                 </variablelist>
 
         </refsect1>
index 20d6531123c8a69cc5293f8e72d996ebcbba0b03..d5417f9505e68de6a3f9d85757b847c1f2d11a48 100644 (file)
 #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);
                 }
 
index 03c942c66e23bd318b47bfbd9727cd7781539ea4..ee2a241c9fdfb0a8ca8e6892dff7f35b4872f111 100644 (file)
@@ -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;
index ae5b13b5647595865a8764d68600fa574f920ed3..992e17b98697dffdf674fe55c498415deaba1ef4 100644 (file)
@@ -22,7 +22,9 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-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 <stdbool.h>
+
+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
index 1be47c8dde9b8a3340c251a947ebbf49d5a3a2f6..7e0056c589d68ba6273491d4dc4553083505ef48 100644 (file)
@@ -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);
                 }
         }
 }
index cb59fd6db2a35f750c969a426b5291c9583528ea..5ad81fd6ca680f81dea73b8a4a0b0642574659b2 100644 (file)
@@ -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);
                 }
         }
 
index de5feeb8d03eb4a2db31f929d167a5b99c29a63b..ef6eea4b9f591321a430a67920c4673251945b3d 100644 (file)
@@ -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;
 
index 114b24c3e7da00ab6cdb293e7928b53b002ba652..1ce2440f3f0611569fc18fd34dc710e64f01a91f 100644 (file)
@@ -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