chiark / gitweb /
cgroup: kill processes, not tasks and other cgroup changes
[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 "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 (cg_init() < 0) {
87                 log_error("Failed to initialize libcg: %s", strerror(-r));
88                 goto finish;
89         }
90
91         if (optind < argc) {
92                 unsigned i;
93
94                 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
95                         int q;
96                         printf("%s:\n", argv[i]);
97
98                         if ((q = show_cgroup_recursive(argv[i], NULL, 0)) < 0)
99                                 r = q;
100                 }
101
102         } else {
103                 char *p;
104
105                 if (!(p = get_current_dir_name())) {
106                         log_error("Cannot determine current working directory: %m");
107                         goto finish;
108                 }
109
110                 if (path_startswith(p, "/cgroup")) {
111                         printf("Working Directory %s:\n", p);
112                         r = show_cgroup_recursive(p, NULL, 0);
113                 } else
114                         r = show_cgroup_recursive(NULL, NULL, 0);
115
116                 free(p);
117         }
118
119         if (r < 0)
120                 log_error("Failed to list cgroup tree: %s", strerror(-r));
121
122         retval = 0;
123
124 finish:
125
126         return retval;
127 }