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>
#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);
}
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) {
arg_no_pager = true;
break;
+ case 'k':
+ arg_kernel_threads = true;
+ break;
+
case '?':
return -EINVAL;
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;
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"))
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);
}
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;
while ((r = cg_read_pid(f, &pid)) > 0) {
+ if (!kernel_threads && is_kernel_thread(pid) > 0)
+ continue;
+
if (n >= n_allocated) {
pid_t *npids;
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;
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;
}
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;
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));
goto finish;
}
- show_cgroup_by_path(last, p2, n_columns-2);
+ show_cgroup_by_path(last, p2, n_columns-2, kernel_threads);
}
r = 0;
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;
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
else
c = 0;
- show_cgroup_by_path(i->control_group, "\t\t ", c);
+ show_cgroup_by_path(i->control_group, "\t\t ", c, false);
}
}
}
else
c = 0;
- show_cgroup_by_path(i->control_group, "\t\t ", c);
+ show_cgroup_by_path(i->control_group, "\t\t ", c, false);
}
}
}
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);
}
}
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;
int rtc_open(int flags);
+int is_kernel_thread(pid_t pid);
+
#endif