]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cgls/cgls.c
util: split-out hwclock.[ch]
[thirdparty/systemd.git] / src / cgls / 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 33#include "pager.h"
c3175a7f 34#include "build.h"
1968a360
LP
35
36static bool arg_no_pager = false;
1e5678d0 37static bool arg_kernel_threads = false;
c3175a7f 38static bool arg_all = false;
fa776d8e
LP
39
40static void help(void) {
41
42 printf("%s [OPTIONS...] [CGROUP...]\n\n"
43 "Recursively show control group contents.\n\n"
69fc152f 44 " -h --help Show this help\n"
c3175a7f 45 " --version Show package version\n"
1e5678d0 46 " --no-pager Do not pipe output into a pager\n"
c3175a7f 47 " -a --all Show all groups, including empty\n"
1e5678d0 48 " -k Include kernel threads in output\n",
fa776d8e
LP
49 program_invocation_short_name);
50}
51
52static int parse_argv(int argc, char *argv[]) {
53
1968a360 54 enum {
c3175a7f
LP
55 ARG_NO_PAGER = 0x100,
56 ARG_VERSION
1968a360
LP
57 };
58
fa776d8e 59 static const struct option options[] = {
1968a360 60 { "help", no_argument, NULL, 'h' },
c3175a7f 61 { "version", no_argument, NULL, ARG_VERSION },
1968a360 62 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
c3175a7f 63 { "all", no_argument, NULL, 'a' },
1968a360 64 { NULL, 0, NULL, 0 }
fa776d8e
LP
65 };
66
67 int c;
68
69 assert(argc >= 1);
70 assert(argv);
71
c3175a7f 72 while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) {
fa776d8e
LP
73
74 switch (c) {
75
76 case 'h':
77 help();
78 return 0;
79
c3175a7f
LP
80 case ARG_VERSION:
81 puts(PACKAGE_STRING);
82 puts(DISTRIBUTION);
83 puts(SYSTEMD_FEATURES);
84 return 0;
85
1968a360
LP
86 case ARG_NO_PAGER:
87 arg_no_pager = true;
88 break;
89
c3175a7f
LP
90 case 'a':
91 arg_all = true;
92 break;
93
1e5678d0
LP
94 case 'k':
95 arg_kernel_threads = true;
96 break;
97
fa776d8e
LP
98 case '?':
99 return -EINVAL;
100
101 default:
102 log_error("Unknown option code %c", c);
103 return -EINVAL;
104 }
105 }
106
107 return 1;
108}
109
110int main(int argc, char *argv[]) {
22f4096c 111 int r = 0, retval = EXIT_FAILURE;
fa776d8e
LP
112
113 log_parse_environment();
2396fb04 114 log_open();
fa776d8e 115
1e5678d0
LP
116 r = parse_argv(argc, argv);
117 if (r < 0)
fa776d8e
LP
118 goto finish;
119 else if (r == 0) {
22f4096c 120 retval = EXIT_SUCCESS;
fa776d8e
LP
121 goto finish;
122 }
123
1968a360
LP
124 if (!arg_no_pager)
125 pager_open();
126
fa776d8e
LP
127 if (optind < argc) {
128 unsigned i;
129
130 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
131 int q;
132 printf("%s:\n", argv[i]);
133
c3175a7f 134 q = show_cgroup_by_path(argv[i], NULL, 0, arg_kernel_threads, arg_all);
1e5678d0 135 if (q < 0)
fa776d8e
LP
136 r = q;
137 }
138
139 } else {
140 char *p;
141
1e5678d0
LP
142 p = get_current_dir_name();
143 if (!p) {
fa776d8e
LP
144 log_error("Cannot determine current working directory: %m");
145 goto finish;
146 }
147
77d5f105 148 if (path_startswith(p, "/sys/fs/cgroup")) {
fa776d8e 149 printf("Working Directory %s:\n", p);
c3175a7f 150 r = show_cgroup_by_path(p, NULL, 0, arg_kernel_threads, arg_all);
1f16b4a6
LP
151 } else {
152 char *root = NULL;
153 const char *t = NULL;
154
1e5678d0
LP
155 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root);
156 if (r < 0)
1f16b4a6 157 t = "/";
b9a8e638
LP
158 else {
159 if (endswith(root, "/system"))
160 root[strlen(root)-7] = 0;
161
162 t = root[0] ? root : "/";
163 }
1f16b4a6 164
c3175a7f 165 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0, arg_kernel_threads, arg_all);
1f16b4a6
LP
166 free(root);
167 }
fa776d8e
LP
168
169 free(p);
170 }
171
172 if (r < 0)
173 log_error("Failed to list cgroup tree: %s", strerror(-r));
174
22f4096c 175 retval = EXIT_SUCCESS;
fa776d8e
LP
176
177finish:
1968a360 178 pager_close();
fa776d8e
LP
179
180 return retval;
181}