]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
eecd1362 | 2 | |
3f6fd1ba | 3 | #include <fcntl.h> |
eecd1362 | 4 | #include <getopt.h> |
eecd1362 | 5 | #include <stdio.h> |
3f6fd1ba | 6 | #include <stdlib.h> |
eecd1362 LP |
7 | #include <unistd.h> |
8 | ||
0fb0c56f | 9 | #include "sd-bus.h" |
3f6fd1ba | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
0fb0c56f | 12 | #include "bus-error.h" |
3f6fd1ba | 13 | #include "bus-util.h" |
3ffd4af2 | 14 | #include "fd-util.h" |
f97b34a6 | 15 | #include "format-util.h" |
f3c9133c | 16 | #include "pager.h" |
0b452006 | 17 | #include "process-util.h" |
ce30c8dc | 18 | #include "signal-util.h" |
3f6fd1ba | 19 | #include "strv.h" |
b1d4f8e1 | 20 | #include "user-util.h" |
3f6fd1ba | 21 | #include "util.h" |
eecd1362 | 22 | |
4943c1c9 | 23 | static const char* arg_what = "idle:sleep:shutdown"; |
eecd1362 LP |
24 | static const char* arg_who = NULL; |
25 | static const char* arg_why = "Unknown reason"; | |
ca5447c0 | 26 | static const char* arg_mode = NULL; |
f3c9133c | 27 | static bool arg_no_pager = false; |
eecd1362 LP |
28 | |
29 | static enum { | |
30 | ACTION_INHIBIT, | |
31 | ACTION_LIST | |
32 | } arg_action = ACTION_INHIBIT; | |
33 | ||
0fb0c56f | 34 | static int inhibit(sd_bus *bus, sd_bus_error *error) { |
4afd3348 | 35 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
3137e0bd | 36 | int r; |
0fb0c56f | 37 | int fd; |
eecd1362 | 38 | |
0fb0c56f | 39 | r = sd_bus_call_method( |
b9c26b41 | 40 | bus, |
eecd1362 LP |
41 | "org.freedesktop.login1", |
42 | "/org/freedesktop/login1", | |
43 | "org.freedesktop.login1.Manager", | |
b9c26b41 | 44 | "Inhibit", |
0fb0c56f | 45 | error, |
b9c26b41 | 46 | &reply, |
0fb0c56f | 47 | "ssss", arg_what, arg_who, arg_why, arg_mode); |
3137e0bd LP |
48 | if (r < 0) |
49 | return r; | |
eecd1362 | 50 | |
0fb0c56f TG |
51 | r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd); |
52 | if (r < 0) | |
5b30bef8 | 53 | return r; |
eecd1362 | 54 | |
ead34950 | 55 | r = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
0fb0c56f TG |
56 | if (r < 0) |
57 | return -errno; | |
58 | ||
3137e0bd | 59 | return r; |
eecd1362 LP |
60 | } |
61 | ||
0fb0c56f | 62 | static int print_inhibitors(sd_bus *bus, sd_bus_error *error) { |
4afd3348 | 63 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
0fb0c56f TG |
64 | const char *what, *who, *why, *mode; |
65 | unsigned int uid, pid; | |
eecd1362 | 66 | unsigned n = 0; |
eecd1362 LP |
67 | int r; |
68 | ||
f3c9133c DT |
69 | (void) pager_open(arg_no_pager, false); |
70 | ||
0fb0c56f | 71 | r = sd_bus_call_method( |
b9c26b41 | 72 | bus, |
eecd1362 LP |
73 | "org.freedesktop.login1", |
74 | "/org/freedesktop/login1", | |
75 | "org.freedesktop.login1.Manager", | |
b9c26b41 | 76 | "ListInhibitors", |
0fb0c56f | 77 | error, |
b9c26b41 | 78 | &reply, |
0fb0c56f | 79 | ""); |
3137e0bd | 80 | if (r < 0) |
4654e558 | 81 | return r; |
eecd1362 | 82 | |
0fb0c56f TG |
83 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
84 | if (r < 0) | |
40be0704 | 85 | return bus_log_parse_error(r); |
eecd1362 | 86 | |
0fb0c56f | 87 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
391a4f72 | 88 | _cleanup_free_ char *comm = NULL, *u = NULL; |
eecd1362 | 89 | |
ca5447c0 MM |
90 | if (arg_mode && !streq(mode, arg_mode)) |
91 | continue; | |
92 | ||
391a4f72 LP |
93 | get_process_comm(pid, &comm); |
94 | u = uid_to_name(uid); | |
95 | ||
de0671ee | 96 | printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n" |
41330ddb MM |
97 | " What: %s\n" |
98 | " Why: %s\n" | |
99 | " Mode: %s\n\n", | |
de0671ee | 100 | who, uid, strna(u), pid, strna(comm), |
41330ddb MM |
101 | what, |
102 | why, | |
103 | mode); | |
eecd1362 | 104 | |
eecd1362 LP |
105 | n++; |
106 | } | |
0fb0c56f | 107 | if (r < 0) |
40be0704 | 108 | return bus_log_parse_error(r); |
0fb0c56f TG |
109 | |
110 | r = sd_bus_message_exit_container(reply); | |
111 | if (r < 0) | |
40be0704 | 112 | return bus_log_parse_error(r); |
eecd1362 | 113 | |
41330ddb | 114 | printf("%u inhibitors listed.\n", n); |
4654e558 | 115 | return 0; |
eecd1362 LP |
116 | } |
117 | ||
601185b4 | 118 | static void help(void) { |
eecd1362 | 119 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
4943c1c9 | 120 | "Execute a process while inhibiting shutdown/sleep/idle.\n\n" |
eecd1362 LP |
121 | " -h --help Show this help\n" |
122 | " --version Show package version\n" | |
f3c9133c | 123 | " --no-pager Do not pipe output into a pager\n" |
12a1309e LP |
124 | " --what=WHAT Operations to inhibit, colon separated list of:\n" |
125 | " shutdown, sleep, idle, handle-power-key,\n" | |
2f1bb513 СНА |
126 | " handle-suspend-key, handle-hibernate-key,\n" |
127 | " handle-lid-switch\n" | |
eecd1362 LP |
128 | " --who=STRING A descriptive string who is inhibiting\n" |
129 | " --why=STRING A descriptive string why is being inhibited\n" | |
130 | " --mode=MODE One of block or delay\n" | |
601185b4 ZJS |
131 | " --list List active inhibitors\n" |
132 | , program_invocation_short_name); | |
eecd1362 LP |
133 | } |
134 | ||
135 | static int parse_argv(int argc, char *argv[]) { | |
136 | ||
137 | enum { | |
138 | ARG_VERSION = 0x100, | |
139 | ARG_WHAT, | |
140 | ARG_WHO, | |
141 | ARG_WHY, | |
142 | ARG_MODE, | |
143 | ARG_LIST, | |
f3c9133c | 144 | ARG_NO_PAGER, |
eecd1362 LP |
145 | }; |
146 | ||
147 | static const struct option options[] = { | |
148 | { "help", no_argument, NULL, 'h' }, | |
149 | { "version", no_argument, NULL, ARG_VERSION }, | |
150 | { "what", required_argument, NULL, ARG_WHAT }, | |
151 | { "who", required_argument, NULL, ARG_WHO }, | |
152 | { "why", required_argument, NULL, ARG_WHY }, | |
153 | { "mode", required_argument, NULL, ARG_MODE }, | |
154 | { "list", no_argument, NULL, ARG_LIST }, | |
f3c9133c | 155 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
eb9da376 | 156 | {} |
eecd1362 LP |
157 | }; |
158 | ||
159 | int c; | |
160 | ||
161 | assert(argc >= 0); | |
162 | assert(argv); | |
163 | ||
601185b4 | 164 | while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) |
eecd1362 LP |
165 | |
166 | switch (c) { | |
167 | ||
168 | case 'h': | |
601185b4 ZJS |
169 | help(); |
170 | return 0; | |
eecd1362 LP |
171 | |
172 | case ARG_VERSION: | |
3f6fd1ba | 173 | return version(); |
eecd1362 LP |
174 | |
175 | case ARG_WHAT: | |
176 | arg_what = optarg; | |
177 | break; | |
178 | ||
179 | case ARG_WHO: | |
180 | arg_who = optarg; | |
181 | break; | |
182 | ||
183 | case ARG_WHY: | |
184 | arg_why = optarg; | |
185 | break; | |
186 | ||
187 | case ARG_MODE: | |
188 | arg_mode = optarg; | |
189 | break; | |
190 | ||
191 | case ARG_LIST: | |
192 | arg_action = ACTION_LIST; | |
193 | break; | |
194 | ||
f3c9133c DT |
195 | case ARG_NO_PAGER: |
196 | arg_no_pager = true; | |
197 | break; | |
198 | ||
eb9da376 | 199 | case '?': |
eecd1362 | 200 | return -EINVAL; |
eb9da376 LP |
201 | |
202 | default: | |
203 | assert_not_reached("Unhandled option"); | |
eecd1362 | 204 | } |
eecd1362 | 205 | |
d9130355 | 206 | if (arg_action == ACTION_INHIBIT && optind == argc) |
2f2343c6 KS |
207 | arg_action = ACTION_LIST; |
208 | ||
209 | else if (arg_action == ACTION_INHIBIT && optind >= argc) { | |
eecd1362 LP |
210 | log_error("Missing command line to execute."); |
211 | return -EINVAL; | |
212 | } | |
213 | ||
214 | return 1; | |
215 | } | |
216 | ||
217 | int main(int argc, char *argv[]) { | |
4afd3348 LP |
218 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
219 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
5220a6f3 | 220 | int r; |
eecd1362 LP |
221 | |
222 | log_parse_environment(); | |
223 | log_open(); | |
224 | ||
225 | r = parse_argv(argc, argv); | |
45a7c6b5 | 226 | if (r < 0) |
0fb0c56f | 227 | return EXIT_FAILURE; |
45a7c6b5 LP |
228 | if (r == 0) |
229 | return EXIT_SUCCESS; | |
eecd1362 | 230 | |
76b54375 | 231 | r = sd_bus_default_system(&bus); |
0fb0c56f | 232 | if (r < 0) { |
da927ba9 | 233 | log_error_errno(r, "Failed to connect to bus: %m"); |
0fb0c56f | 234 | return EXIT_FAILURE; |
eecd1362 LP |
235 | } |
236 | ||
237 | if (arg_action == ACTION_LIST) { | |
238 | ||
239 | r = print_inhibitors(bus, &error); | |
f3c9133c | 240 | pager_close(); |
eecd1362 | 241 | if (r < 0) { |
0fb0c56f TG |
242 | log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r)); |
243 | return EXIT_FAILURE; | |
eecd1362 LP |
244 | } |
245 | ||
246 | } else { | |
5220a6f3 LP |
247 | _cleanup_close_ int fd = -1; |
248 | _cleanup_free_ char *w = NULL; | |
eecd1362 LP |
249 | pid_t pid; |
250 | ||
106f12a0 CH |
251 | /* Ignore SIGINT and allow the forked process to receive it */ |
252 | (void) ignore_signals(SIGINT, -1); | |
253 | ||
eecd1362 LP |
254 | if (!arg_who) |
255 | arg_who = w = strv_join(argv + optind, " "); | |
256 | ||
ca5447c0 MM |
257 | if (!arg_mode) |
258 | arg_mode = "block"; | |
259 | ||
eecd1362 | 260 | fd = inhibit(bus, &error); |
eecd1362 | 261 | if (fd < 0) { |
dcee0112 | 262 | log_error("Failed to inhibit: %s", bus_error_message(&error, fd)); |
0fb0c56f | 263 | return EXIT_FAILURE; |
eecd1362 LP |
264 | } |
265 | ||
b6e1fff1 LP |
266 | r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid); |
267 | if (r < 0) | |
0fb0c56f | 268 | return EXIT_FAILURE; |
4c253ed1 | 269 | if (r == 0) { |
eecd1362 | 270 | /* Child */ |
eecd1362 | 271 | execvp(argv[optind], argv + optind); |
0b1f3c76 | 272 | log_open(); |
56f64d95 | 273 | log_error_errno(errno, "Failed to execute %s: %m", argv[optind]); |
eecd1362 LP |
274 | _exit(EXIT_FAILURE); |
275 | } | |
276 | ||
7d4904fe | 277 | r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG); |
5220a6f3 | 278 | return r < 0 ? EXIT_FAILURE : r; |
eecd1362 LP |
279 | } |
280 | ||
4c0e48f9 | 281 | return EXIT_SUCCESS; |
eecd1362 | 282 | } |