chiark / gitweb /
manager: serialize/deserialize max job id and /usr taint flag
[elogind.git] / src / cgls.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 = EXIT_FAILURE;
76
77         log_parse_environment();
78         log_open();
79
80         if ((r = parse_argv(argc, argv)) < 0)
81                 goto finish;
82         else if (r == 0) {
83                 retval = EXIT_SUCCESS;
84                 goto finish;
85         }
86
87         if (optind < argc) {
88                 unsigned i;
89
90                 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
91                         int q;
92                         printf("%s:\n", argv[i]);
93
94                         if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0)
95                                 r = q;
96                 }
97
98         } else {
99                 char *p;
100
101                 if (!(p = get_current_dir_name())) {
102                         log_error("Cannot determine current working directory: %m");
103                         goto finish;
104                 }
105
106                 if (path_startswith(p, "/sys/fs/cgroup")) {
107                         printf("Working Directory %s:\n", p);
108                         r = show_cgroup_by_path(p, NULL, 0);
109                 } else {
110                         char *root = NULL;
111                         const char *t = NULL;
112
113                         if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root)) < 0)
114                                 t = "/";
115                         else {
116                                 if (endswith(root, "/system"))
117                                         root[strlen(root)-7] = 0;
118
119                                 t = root[0] ? root : "/";
120                         }
121
122                         r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0);
123                         free(root);
124                 }
125
126                 free(p);
127         }
128
129         if (r < 0)
130                 log_error("Failed to list cgroup tree: %s", strerror(-r));
131
132         retval = EXIT_SUCCESS;
133
134 finish:
135
136         return retval;
137 }