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