]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cgls.c
systemctl: don't show ln -s/rm output in 'install' mode if --quiet is passed
[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
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
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
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
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;
fa776d8e
LP
36
37static void help(void) {
38
39 printf("%s [OPTIONS...] [CGROUP...]\n\n"
40 "Recursively show control group contents.\n\n"
69fc152f
LP
41 " -h --help Show this help\n"
42 " --no-pager Do not pipe output into a pager\n",
fa776d8e
LP
43 program_invocation_short_name);
44}
45
46static int parse_argv(int argc, char *argv[]) {
47
1968a360
LP
48 enum {
49 ARG_NO_PAGER = 0x100
50 };
51
fa776d8e 52 static const struct option options[] = {
1968a360
LP
53 { "help", no_argument, NULL, 'h' },
54 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
55 { NULL, 0, NULL, 0 }
fa776d8e
LP
56 };
57
58 int c;
59
60 assert(argc >= 1);
61 assert(argv);
62
63 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
64
65 switch (c) {
66
67 case 'h':
68 help();
69 return 0;
70
1968a360
LP
71 case ARG_NO_PAGER:
72 arg_no_pager = true;
73 break;
74
fa776d8e
LP
75 case '?':
76 return -EINVAL;
77
78 default:
79 log_error("Unknown option code %c", c);
80 return -EINVAL;
81 }
82 }
83
84 return 1;
85}
86
87int main(int argc, char *argv[]) {
22f4096c 88 int r = 0, retval = EXIT_FAILURE;
fa776d8e
LP
89
90 log_parse_environment();
2396fb04 91 log_open();
fa776d8e
LP
92
93 if ((r = parse_argv(argc, argv)) < 0)
94 goto finish;
95 else if (r == 0) {
22f4096c 96 retval = EXIT_SUCCESS;
fa776d8e
LP
97 goto finish;
98 }
99
1968a360
LP
100 if (!arg_no_pager)
101 pager_open();
102
fa776d8e
LP
103 if (optind < argc) {
104 unsigned i;
105
106 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
107 int q;
108 printf("%s:\n", argv[i]);
109
35d2e7ec 110 if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0)
fa776d8e
LP
111 r = q;
112 }
113
114 } else {
115 char *p;
116
117 if (!(p = get_current_dir_name())) {
118 log_error("Cannot determine current working directory: %m");
119 goto finish;
120 }
121
77d5f105 122 if (path_startswith(p, "/sys/fs/cgroup")) {
fa776d8e 123 printf("Working Directory %s:\n", p);
35d2e7ec 124 r = show_cgroup_by_path(p, NULL, 0);
1f16b4a6
LP
125 } else {
126 char *root = NULL;
127 const char *t = NULL;
128
129 if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root)) < 0)
130 t = "/";
b9a8e638
LP
131 else {
132 if (endswith(root, "/system"))
133 root[strlen(root)-7] = 0;
134
135 t = root[0] ? root : "/";
136 }
1f16b4a6
LP
137
138 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0);
139 free(root);
140 }
fa776d8e
LP
141
142 free(p);
143 }
144
145 if (r < 0)
146 log_error("Failed to list cgroup tree: %s", strerror(-r));
147
22f4096c 148 retval = EXIT_SUCCESS;
fa776d8e
LP
149
150finish:
1968a360 151 pager_close();
fa776d8e
LP
152
153 return retval;
154}