chiark / gitweb /
service: read special startup dirs only on the respective distros
[elogind.git] / src / cgls.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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"
30 #include "cgroup-util.h"
31 #include "log.h"
32 #include "util.h"
33
34 static void help(void) {
35
36         printf("%s [OPTIONS...] [CGROUP...]\n\n"
37                "Recursively show control group contents.\n\n"
38                "  -h --help         Show this help\n",
39                program_invocation_short_name);
40 }
41
42 static int parse_argv(int argc, char *argv[]) {
43
44         static const struct option options[] = {
45                 { "help",      no_argument,       NULL, 'h'         },
46                 { NULL,        0,                 NULL, 0           }
47         };
48
49         int c;
50
51         assert(argc >= 1);
52         assert(argv);
53
54         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
55
56                 switch (c) {
57
58                 case 'h':
59                         help();
60                         return 0;
61
62                 case '?':
63                         return -EINVAL;
64
65                 default:
66                         log_error("Unknown option code %c", c);
67                         return -EINVAL;
68                 }
69         }
70
71         return 1;
72 }
73
74 int main(int argc, char *argv[]) {
75         int r = 0, retval = 1;
76
77         log_parse_environment();
78
79         if ((r = parse_argv(argc, argv)) < 0)
80                 goto finish;
81         else if (r == 0) {
82                 retval = 0;
83                 goto finish;
84         }
85
86         if (optind < argc) {
87                 unsigned i;
88
89                 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
90                         int q;
91                         printf("%s:\n", argv[i]);
92
93                         if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0)
94                                 r = q;
95                 }
96
97         } else {
98                 char *p;
99
100                 if (!(p = get_current_dir_name())) {
101                         log_error("Cannot determine current working directory: %m");
102                         goto finish;
103                 }
104
105                 if (path_startswith(p, "/cgroup")) {
106                         printf("Working Directory %s:\n", p);
107                         r = show_cgroup_by_path(p, NULL, 0);
108                 } else
109                         r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, "/", NULL, 0);
110
111                 free(p);
112         }
113
114         if (r < 0)
115                 log_error("Failed to list cgroup tree: %s", strerror(-r));
116
117         retval = 0;
118
119 finish:
120
121         return retval;
122 }