cgls.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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
(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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "cgroup-show.h"
#include "cgroup-util.h"
#include "fileio.h"
#include "log.h"
#include "output-mode.h"
#include "pager.h"
#include "path-util.h"
#include "unit-name.h"
#include "util.h"
static bool arg_no_pager = false;
static bool arg_kernel_threads = false;
static bool arg_all = false;
static int arg_full = -1;
static char* arg_machine = NULL;
static void help(void) {
printf("%s [OPTIONS...] [CGROUP...]\n\n"
"Recursively show control group contents.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" -a --all Show all groups, including empty\n"
" -l --full Do not ellipsize output\n"
" -k Include kernel threads in output\n"
" -M --machine= Show container\n"
}
enum {
ARG_NO_PAGER = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 'a':
arg_all = true;
break;
case 'l':
arg_full = true;
break;
case 'k':
arg_kernel_threads = true;
break;
case 'M':
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
static int get_cgroup_root(char **ret) {
const char *m;
int r;
if (!arg_machine) {
r = cg_get_root_path(ret);
if (r == -ENOMEDIUM)
return log_error_errno(r, "Failed to get root control group path: No cgroup filesystem mounted on /sys/fs/cgroup");
else if (r < 0)
return log_error_errno(r, "Failed to get root control group path: %m");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to load machine data: %m");
if (!path)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to create bus connection: %m");
bus,
"org.freedesktop.systemd1",
path,
"ControlGroup",
&error,
ret);
if (r < 0)
return log_error_errno(r, "Failed to query unit control group path: %s", bus_error_message(&error, r));
return 0;
}
}
int r, output_flags;
log_open();
if (r <= 0)
goto finish;
if (!arg_no_pager) {
r = pager_open(false);
if (r > 0 && arg_full < 0)
arg_full = true;
}
(arg_full > 0) * OUTPUT_FULL_WIDTH;
int i;
r = get_cgroup_root(&root);
if (r < 0)
goto finish;
int q;
} else {
const char *controller, *path;
r = cg_split_spec(argv[i], &c, &p);
if (r < 0) {
goto finish;
}
controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
if (p) {
if (!j) {
r = log_oom();
goto finish;
}
path = j;
} else
}
if (q < 0)
r = q;
}
} else {
bool done = false;
if (!arg_machine) {
cwd = get_current_dir_name();
if (!cwd) {
goto finish;
}
done = true;
}
}
if (!done) {
r = get_cgroup_root(&root);
if (r < 0)
goto finish;
printf("-.slice\n");
}
}
if (r < 0)
log_error_errno(r, "Failed to list cgroup tree: %m");
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}