chiark / gitweb /
log: make internal log api log directly to the journal
[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 #include "pager.h"
34
35 static bool arg_no_pager = false;
36
37 static void help(void) {
38
39         printf("%s [OPTIONS...] [CGROUP...]\n\n"
40                "Recursively show control group contents.\n\n"
41                "  -h --help           Show this help\n"
42                "     --no-pager       Do not pipe output into a pager\n",
43                program_invocation_short_name);
44 }
45
46 static int parse_argv(int argc, char *argv[]) {
47
48         enum {
49                 ARG_NO_PAGER = 0x100
50         };
51
52         static const struct option options[] = {
53                 { "help",      no_argument,       NULL, 'h'          },
54                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER },
55                 { NULL,        0,                 NULL, 0            }
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
71                 case ARG_NO_PAGER:
72                         arg_no_pager = true;
73                         break;
74
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
87 int main(int argc, char *argv[]) {
88         int r = 0, retval = EXIT_FAILURE;
89
90         log_parse_environment();
91         log_open();
92
93         if ((r = parse_argv(argc, argv)) < 0)
94                 goto finish;
95         else if (r == 0) {
96                 retval = EXIT_SUCCESS;
97                 goto finish;
98         }
99
100         if (!arg_no_pager)
101                 pager_open();
102
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
110                         if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0)
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
122                 if (path_startswith(p, "/sys/fs/cgroup")) {
123                         printf("Working Directory %s:\n", p);
124                         r = show_cgroup_by_path(p, NULL, 0);
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 = "/";
131                         else {
132                                 if (endswith(root, "/system"))
133                                         root[strlen(root)-7] = 0;
134
135                                 t = root[0] ? root : "/";
136                         }
137
138                         r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0);
139                         free(root);
140                 }
141
142                 free(p);
143         }
144
145         if (r < 0)
146                 log_error("Failed to list cgroup tree: %s", strerror(-r));
147
148         retval = EXIT_SUCCESS;
149
150 finish:
151         pager_close();
152
153         return retval;
154 }