Commit 1e5678d0 authored by Lennart Poettering's avatar Lennart Poettering
Browse files

cgls: don't show kernel threads by default

parent ce3fd7e7
......@@ -84,6 +84,20 @@
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>
......
......@@ -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);
}
......
......@@ -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;
......
......@@ -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
......@@ -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);
}
}
}
......
......@@ -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);
}
}
......
......@@ -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;
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment