2010-08-14 19:59:25 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2010-07-08 21:01:42 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-07-08 21:01:42 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "cgroup-show.h"
|
2010-07-11 00:50:49 +02:00
|
|
|
#include "cgroup-util.h"
|
2010-07-08 21:01:42 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
2011-07-07 02:34:35 +02:00
|
|
|
#include "pager.h"
|
2012-04-16 17:35:58 +02:00
|
|
|
#include "build.h"
|
2011-07-07 02:34:35 +02:00
|
|
|
|
|
|
|
static bool arg_no_pager = false;
|
2012-01-22 18:18:51 +01:00
|
|
|
static bool arg_kernel_threads = false;
|
2012-04-16 17:35:58 +02:00
|
|
|
static bool arg_all = false;
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
static void help(void) {
|
|
|
|
|
|
|
|
printf("%s [OPTIONS...] [CGROUP...]\n\n"
|
|
|
|
"Recursively show control group contents.\n\n"
|
2011-07-07 04:19:03 +02:00
|
|
|
" -h --help Show this help\n"
|
2012-04-16 17:35:58 +02:00
|
|
|
" --version Show package version\n"
|
2012-01-22 18:18:51 +01:00
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2012-04-16 17:35:58 +02:00
|
|
|
" -a --all Show all groups, including empty\n"
|
2012-01-22 18:18:51 +01:00
|
|
|
" -k Include kernel threads in output\n",
|
2010-07-08 21:01:42 +02:00
|
|
|
program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
2011-07-07 02:34:35 +02:00
|
|
|
enum {
|
2012-04-16 17:35:58 +02:00
|
|
|
ARG_NO_PAGER = 0x100,
|
|
|
|
ARG_VERSION
|
2011-07-07 02:34:35 +02:00
|
|
|
};
|
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
static const struct option options[] = {
|
2011-07-07 02:34:35 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-04-16 17:35:58 +02:00
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2011-07-07 02:34:35 +02:00
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2012-04-16 17:35:58 +02:00
|
|
|
{ "all", no_argument, NULL, 'a' },
|
2011-07-07 02:34:35 +02:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2010-07-08 21:01:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 1);
|
|
|
|
assert(argv);
|
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) {
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
case ARG_VERSION:
|
|
|
|
puts(PACKAGE_STRING);
|
|
|
|
puts(DISTRIBUTION);
|
|
|
|
puts(SYSTEMD_FEATURES);
|
|
|
|
return 0;
|
|
|
|
|
2011-07-07 02:34:35 +02:00
|
|
|
case ARG_NO_PAGER:
|
|
|
|
arg_no_pager = true;
|
|
|
|
break;
|
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
case 'a':
|
|
|
|
arg_all = true;
|
|
|
|
break;
|
|
|
|
|
2012-01-22 18:18:51 +01:00
|
|
|
case 'k':
|
|
|
|
arg_kernel_threads = true;
|
|
|
|
break;
|
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_error("Unknown option code %c", c);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2010-08-31 21:05:54 +02:00
|
|
|
int r = 0, retval = EXIT_FAILURE;
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
log_parse_environment();
|
2010-08-16 22:39:02 +02:00
|
|
|
log_open();
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2012-01-22 18:18:51 +01:00
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r < 0)
|
2010-07-08 21:01:42 +02:00
|
|
|
goto finish;
|
|
|
|
else if (r == 0) {
|
2010-08-31 21:05:54 +02:00
|
|
|
retval = EXIT_SUCCESS;
|
2010-07-08 21:01:42 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-07-07 02:34:35 +02:00
|
|
|
if (!arg_no_pager)
|
|
|
|
pager_open();
|
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
if (optind < argc) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = (unsigned) optind; i < (unsigned) argc; i++) {
|
|
|
|
int q;
|
|
|
|
printf("%s:\n", argv[i]);
|
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
q = show_cgroup_by_path(argv[i], NULL, 0, arg_kernel_threads, arg_all);
|
2012-01-22 18:18:51 +01:00
|
|
|
if (q < 0)
|
2010-07-08 21:01:42 +02:00
|
|
|
r = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
char *p;
|
|
|
|
|
2012-01-22 18:18:51 +01:00
|
|
|
p = get_current_dir_name();
|
|
|
|
if (!p) {
|
2010-07-08 21:01:42 +02:00
|
|
|
log_error("Cannot determine current working directory: %m");
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2010-08-25 04:58:16 +02:00
|
|
|
if (path_startswith(p, "/sys/fs/cgroup")) {
|
2010-07-08 21:01:42 +02:00
|
|
|
printf("Working Directory %s:\n", p);
|
2012-04-16 17:35:58 +02:00
|
|
|
r = show_cgroup_by_path(p, NULL, 0, arg_kernel_threads, arg_all);
|
2011-03-14 23:40:41 +01:00
|
|
|
} else {
|
|
|
|
char *root = NULL;
|
|
|
|
const char *t = NULL;
|
|
|
|
|
2012-01-22 18:18:51 +01:00
|
|
|
r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root);
|
|
|
|
if (r < 0)
|
2011-03-14 23:40:41 +01:00
|
|
|
t = "/";
|
2011-03-15 18:43:47 +01:00
|
|
|
else {
|
|
|
|
if (endswith(root, "/system"))
|
|
|
|
root[strlen(root)-7] = 0;
|
|
|
|
|
|
|
|
t = root[0] ? root : "/";
|
|
|
|
}
|
2011-03-14 23:40:41 +01:00
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0, arg_kernel_threads, arg_all);
|
2011-03-14 23:40:41 +01:00
|
|
|
free(root);
|
|
|
|
}
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
log_error("Failed to list cgroup tree: %s", strerror(-r));
|
|
|
|
|
2010-08-31 21:05:54 +02:00
|
|
|
retval = EXIT_SUCCESS;
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
finish:
|
2011-07-07 02:34:35 +02:00
|
|
|
pager_close();
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|