]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cgls.c
relicense to LGPLv2.1 (with exceptions)
[thirdparty/systemd.git] / src / cgls.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
fa776d8e
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
fa776d8e
LP
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
fa776d8e 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
fa776d8e
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <limits.h>
23#include <stdio.h>
24#include <unistd.h>
25#include <errno.h>
26#include <getopt.h>
27#include <string.h>
28
29#include "cgroup-show.h"
c6c18be3 30#include "cgroup-util.h"
fa776d8e
LP
31#include "log.h"
32#include "util.h"
1968a360
LP
33#include "pager.h"
34
35static bool arg_no_pager = false;
1e5678d0 36static bool arg_kernel_threads = false;
fa776d8e
LP
37
38static void help(void) {
39
40 printf("%s [OPTIONS...] [CGROUP...]\n\n"
41 "Recursively show control group contents.\n\n"
69fc152f 42 " -h --help Show this help\n"
1e5678d0
LP
43 " --no-pager Do not pipe output into a pager\n"
44 " -k Include kernel threads in output\n",
fa776d8e
LP
45 program_invocation_short_name);
46}
47
48static int parse_argv(int argc, char *argv[]) {
49
1968a360
LP
50 enum {
51 ARG_NO_PAGER = 0x100
52 };
53
fa776d8e 54 static const struct option options[] = {
1968a360
LP
55 { "help", no_argument, NULL, 'h' },
56 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
57 { NULL, 0, NULL, 0 }
fa776d8e
LP
58 };
59
60 int c;
61
62 assert(argc >= 1);
63 assert(argv);
64
1e5678d0 65 while ((c = getopt_long(argc, argv, "hk", options, NULL)) >= 0) {
fa776d8e
LP
66
67 switch (c) {
68
69 case 'h':
70 help();
71 return 0;
72
1968a360
LP
73 case ARG_NO_PAGER:
74 arg_no_pager = true;
75 break;
76
1e5678d0
LP
77 case 'k':
78 arg_kernel_threads = true;
79 break;
80
fa776d8e
LP
81 case '?':
82 return -EINVAL;
83
84 default:
85 log_error("Unknown option code %c", c);
86 return -EINVAL;
87 }
88 }
89
90 return 1;
91}
92
93int main(int argc, char *argv[]) {
22f4096c 94 int r = 0, retval = EXIT_FAILURE;
fa776d8e
LP
95
96 log_parse_environment();
2396fb04 97 log_open();
fa776d8e 98
1e5678d0
LP
99 r = parse_argv(argc, argv);
100 if (r < 0)
fa776d8e
LP
101 goto finish;
102 else if (r == 0) {
22f4096c 103 retval = EXIT_SUCCESS;
fa776d8e
LP
104 goto finish;
105 }
106
1968a360
LP
107 if (!arg_no_pager)
108 pager_open();
109
fa776d8e
LP
110 if (optind < argc) {
111 unsigned i;
112
113 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
114 int q;
115 printf("%s:\n", argv[i]);
116
1e5678d0
LP
117 q = show_cgroup_by_path(argv[i], NULL, 0, arg_kernel_threads);
118 if (q < 0)
fa776d8e
LP
119 r = q;
120 }
121
122 } else {
123 char *p;
124
1e5678d0
LP
125 p = get_current_dir_name();
126 if (!p) {
fa776d8e
LP
127 log_error("Cannot determine current working directory: %m");
128 goto finish;
129 }
130
77d5f105 131 if (path_startswith(p, "/sys/fs/cgroup")) {
fa776d8e 132 printf("Working Directory %s:\n", p);
1e5678d0 133 r = show_cgroup_by_path(p, NULL, 0, arg_kernel_threads);
1f16b4a6
LP
134 } else {
135 char *root = NULL;
136 const char *t = NULL;
137
1e5678d0
LP
138 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root);
139 if (r < 0)
1f16b4a6 140 t = "/";
b9a8e638
LP
141 else {
142 if (endswith(root, "/system"))
143 root[strlen(root)-7] = 0;
144
145 t = root[0] ? root : "/";
146 }
1f16b4a6 147
1e5678d0 148 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0, arg_kernel_threads);
1f16b4a6
LP
149 free(root);
150 }
fa776d8e
LP
151
152 free(p);
153 }
154
155 if (r < 0)
156 log_error("Failed to list cgroup tree: %s", strerror(-r));
157
22f4096c 158 retval = EXIT_SUCCESS;
fa776d8e
LP
159
160finish:
1968a360 161 pager_close();
fa776d8e
LP
162
163 return retval;
164}