]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 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 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
6300502b | 22 | #include <fcntl.h> |
663996b3 | 23 | #include <getopt.h> |
663996b3 | 24 | #include <stdio.h> |
6300502b | 25 | #include <stdlib.h> |
663996b3 MS |
26 | #include <unistd.h> |
27 | ||
60f067b4 | 28 | #include "sd-bus.h" |
6300502b | 29 | |
60f067b4 | 30 | #include "bus-error.h" |
6300502b | 31 | #include "bus-util.h" |
e3bff60a MP |
32 | #include "formats-util.h" |
33 | #include "process-util.h" | |
86f210e9 | 34 | #include "signal-util.h" |
6300502b MP |
35 | #include "strv.h" |
36 | #include "util.h" | |
663996b3 MS |
37 | |
38 | static const char* arg_what = "idle:sleep:shutdown"; | |
39 | static const char* arg_who = NULL; | |
40 | static const char* arg_why = "Unknown reason"; | |
f47781d8 | 41 | static const char* arg_mode = NULL; |
663996b3 MS |
42 | |
43 | static enum { | |
44 | ACTION_INHIBIT, | |
45 | ACTION_LIST | |
46 | } arg_action = ACTION_INHIBIT; | |
47 | ||
60f067b4 JS |
48 | static int inhibit(sd_bus *bus, sd_bus_error *error) { |
49 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
663996b3 | 50 | int r; |
60f067b4 | 51 | int fd; |
663996b3 | 52 | |
60f067b4 | 53 | r = sd_bus_call_method( |
663996b3 MS |
54 | bus, |
55 | "org.freedesktop.login1", | |
56 | "/org/freedesktop/login1", | |
57 | "org.freedesktop.login1.Manager", | |
58 | "Inhibit", | |
60f067b4 | 59 | error, |
663996b3 | 60 | &reply, |
60f067b4 | 61 | "ssss", arg_what, arg_who, arg_why, arg_mode); |
663996b3 MS |
62 | if (r < 0) |
63 | return r; | |
64 | ||
60f067b4 JS |
65 | r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd); |
66 | if (r < 0) | |
67 | return r; | |
68 | ||
69 | r = fcntl(fd, F_DUPFD_CLOEXEC, 3); | |
70 | if (r < 0) | |
71 | return -errno; | |
663996b3 MS |
72 | |
73 | return r; | |
74 | } | |
75 | ||
60f067b4 JS |
76 | static int print_inhibitors(sd_bus *bus, sd_bus_error *error) { |
77 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
78 | const char *what, *who, *why, *mode; | |
79 | unsigned int uid, pid; | |
663996b3 | 80 | unsigned n = 0; |
663996b3 MS |
81 | int r; |
82 | ||
60f067b4 | 83 | r = sd_bus_call_method( |
663996b3 MS |
84 | bus, |
85 | "org.freedesktop.login1", | |
86 | "/org/freedesktop/login1", | |
87 | "org.freedesktop.login1.Manager", | |
88 | "ListInhibitors", | |
60f067b4 | 89 | error, |
663996b3 | 90 | &reply, |
60f067b4 | 91 | ""); |
663996b3 MS |
92 | if (r < 0) |
93 | return r; | |
94 | ||
60f067b4 JS |
95 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
96 | if (r < 0) | |
97 | return bus_log_parse_error(r); | |
663996b3 | 98 | |
60f067b4 | 99 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
663996b3 | 100 | _cleanup_free_ char *comm = NULL, *u = NULL; |
663996b3 | 101 | |
f47781d8 MP |
102 | if (arg_mode && !streq(mode, arg_mode)) |
103 | continue; | |
104 | ||
663996b3 MS |
105 | get_process_comm(pid, &comm); |
106 | u = uid_to_name(uid); | |
107 | ||
60f067b4 | 108 | printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n" |
663996b3 MS |
109 | " What: %s\n" |
110 | " Why: %s\n" | |
111 | " Mode: %s\n\n", | |
60f067b4 | 112 | who, uid, strna(u), pid, strna(comm), |
663996b3 MS |
113 | what, |
114 | why, | |
115 | mode); | |
116 | ||
663996b3 MS |
117 | n++; |
118 | } | |
60f067b4 JS |
119 | if (r < 0) |
120 | return bus_log_parse_error(r); | |
121 | ||
122 | r = sd_bus_message_exit_container(reply); | |
123 | if (r < 0) | |
124 | return bus_log_parse_error(r); | |
663996b3 MS |
125 | |
126 | printf("%u inhibitors listed.\n", n); | |
127 | return 0; | |
128 | } | |
129 | ||
5eef597e | 130 | static void help(void) { |
663996b3 MS |
131 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
132 | "Execute a process while inhibiting shutdown/sleep/idle.\n\n" | |
133 | " -h --help Show this help\n" | |
134 | " --version Show package version\n" | |
135 | " --what=WHAT Operations to inhibit, colon separated list of:\n" | |
136 | " shutdown, sleep, idle, handle-power-key,\n" | |
137 | " handle-suspend-key, handle-hibernate-key,\n" | |
138 | " handle-lid-switch\n" | |
139 | " --who=STRING A descriptive string who is inhibiting\n" | |
140 | " --why=STRING A descriptive string why is being inhibited\n" | |
141 | " --mode=MODE One of block or delay\n" | |
5eef597e MP |
142 | " --list List active inhibitors\n" |
143 | , program_invocation_short_name); | |
663996b3 MS |
144 | } |
145 | ||
146 | static int parse_argv(int argc, char *argv[]) { | |
147 | ||
148 | enum { | |
149 | ARG_VERSION = 0x100, | |
150 | ARG_WHAT, | |
151 | ARG_WHO, | |
152 | ARG_WHY, | |
153 | ARG_MODE, | |
154 | ARG_LIST, | |
155 | }; | |
156 | ||
157 | static const struct option options[] = { | |
158 | { "help", no_argument, NULL, 'h' }, | |
159 | { "version", no_argument, NULL, ARG_VERSION }, | |
160 | { "what", required_argument, NULL, ARG_WHAT }, | |
161 | { "who", required_argument, NULL, ARG_WHO }, | |
162 | { "why", required_argument, NULL, ARG_WHY }, | |
163 | { "mode", required_argument, NULL, ARG_MODE }, | |
164 | { "list", no_argument, NULL, ARG_LIST }, | |
60f067b4 | 165 | {} |
663996b3 MS |
166 | }; |
167 | ||
168 | int c; | |
169 | ||
170 | assert(argc >= 0); | |
171 | assert(argv); | |
172 | ||
5eef597e | 173 | while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) |
663996b3 MS |
174 | |
175 | switch (c) { | |
176 | ||
177 | case 'h': | |
5eef597e MP |
178 | help(); |
179 | return 0; | |
663996b3 MS |
180 | |
181 | case ARG_VERSION: | |
6300502b | 182 | return version(); |
663996b3 MS |
183 | |
184 | case ARG_WHAT: | |
185 | arg_what = optarg; | |
186 | break; | |
187 | ||
188 | case ARG_WHO: | |
189 | arg_who = optarg; | |
190 | break; | |
191 | ||
192 | case ARG_WHY: | |
193 | arg_why = optarg; | |
194 | break; | |
195 | ||
196 | case ARG_MODE: | |
197 | arg_mode = optarg; | |
198 | break; | |
199 | ||
200 | case ARG_LIST: | |
201 | arg_action = ACTION_LIST; | |
202 | break; | |
203 | ||
60f067b4 | 204 | case '?': |
663996b3 | 205 | return -EINVAL; |
60f067b4 JS |
206 | |
207 | default: | |
208 | assert_not_reached("Unhandled option"); | |
663996b3 | 209 | } |
663996b3 | 210 | |
f47781d8 | 211 | if (arg_action == ACTION_INHIBIT && optind == argc) |
663996b3 MS |
212 | arg_action = ACTION_LIST; |
213 | ||
214 | else if (arg_action == ACTION_INHIBIT && optind >= argc) { | |
215 | log_error("Missing command line to execute."); | |
216 | return -EINVAL; | |
217 | } | |
218 | ||
219 | return 1; | |
220 | } | |
221 | ||
222 | int main(int argc, char *argv[]) { | |
60f067b4 | 223 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
fb183854 | 224 | _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL; |
60f067b4 | 225 | int r; |
663996b3 MS |
226 | |
227 | log_parse_environment(); | |
228 | log_open(); | |
229 | ||
230 | r = parse_argv(argc, argv); | |
60f067b4 JS |
231 | if (r < 0) |
232 | return EXIT_FAILURE; | |
233 | if (r == 0) | |
234 | return EXIT_SUCCESS; | |
235 | ||
236 | r = sd_bus_default_system(&bus); | |
237 | if (r < 0) { | |
f47781d8 | 238 | log_error_errno(r, "Failed to connect to bus: %m"); |
60f067b4 | 239 | return EXIT_FAILURE; |
663996b3 MS |
240 | } |
241 | ||
242 | if (arg_action == ACTION_LIST) { | |
243 | ||
244 | r = print_inhibitors(bus, &error); | |
245 | if (r < 0) { | |
60f067b4 JS |
246 | log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r)); |
247 | return EXIT_FAILURE; | |
663996b3 MS |
248 | } |
249 | ||
250 | } else { | |
60f067b4 JS |
251 | _cleanup_close_ int fd = -1; |
252 | _cleanup_free_ char *w = NULL; | |
663996b3 MS |
253 | pid_t pid; |
254 | ||
255 | if (!arg_who) | |
256 | arg_who = w = strv_join(argv + optind, " "); | |
257 | ||
f47781d8 MP |
258 | if (!arg_mode) |
259 | arg_mode = "block"; | |
260 | ||
663996b3 | 261 | fd = inhibit(bus, &error); |
663996b3 | 262 | if (fd < 0) { |
e3bff60a | 263 | log_error("Failed to inhibit: %s", bus_error_message(&error, fd)); |
60f067b4 | 264 | return EXIT_FAILURE; |
663996b3 MS |
265 | } |
266 | ||
267 | pid = fork(); | |
268 | if (pid < 0) { | |
f47781d8 | 269 | log_error_errno(errno, "Failed to fork: %m"); |
60f067b4 | 270 | return EXIT_FAILURE; |
663996b3 MS |
271 | } |
272 | ||
273 | if (pid == 0) { | |
274 | /* Child */ | |
275 | ||
86f210e9 MP |
276 | (void) reset_all_signal_handlers(); |
277 | (void) reset_signal_mask(); | |
278 | ||
663996b3 MS |
279 | close_all_fds(NULL, 0); |
280 | ||
281 | execvp(argv[optind], argv + optind); | |
f47781d8 | 282 | log_error_errno(errno, "Failed to execute %s: %m", argv[optind]); |
663996b3 MS |
283 | _exit(EXIT_FAILURE); |
284 | } | |
285 | ||
f47781d8 | 286 | r = wait_for_terminate_and_warn(argv[optind], pid, true); |
60f067b4 | 287 | return r < 0 ? EXIT_FAILURE : r; |
663996b3 MS |
288 | } |
289 | ||
60f067b4 | 290 | return 0; |
663996b3 | 291 | } |