]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
eecd1362 | 2 | |
3f6fd1ba | 3 | #include <fcntl.h> |
eecd1362 | 4 | #include <getopt.h> |
eecd1362 | 5 | #include <stdio.h> |
eecd1362 LP |
6 | #include <unistd.h> |
7 | ||
0fb0c56f | 8 | #include "sd-bus.h" |
3f6fd1ba | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
d6b4d1c7 | 11 | #include "build.h" |
0fb0c56f | 12 | #include "bus-error.h" |
d962e737 | 13 | #include "bus-locator.h" |
3f6fd1ba | 14 | #include "bus-util.h" |
6ad1d1ed | 15 | #include "errno-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
a9426617 | 17 | #include "format-table.h" |
93a1f792 | 18 | #include "log.h" |
5e332028 | 19 | #include "main-func.h" |
f3c9133c | 20 | #include "pager.h" |
76d62b63 | 21 | #include "polkit-agent.h" |
294bf0c3 | 22 | #include "pretty-print.h" |
0b452006 | 23 | #include "process-util.h" |
6ad1d1ed | 24 | #include "runtime-scope.h" |
ce30c8dc | 25 | #include "signal-util.h" |
6ad1d1ed | 26 | #include "string-util.h" |
3f6fd1ba | 27 | #include "strv.h" |
353b2baa | 28 | #include "terminal-util.h" |
b1d4f8e1 | 29 | #include "user-util.h" |
eecd1362 | 30 | |
a87b7aa1 ZJS |
31 | static const char *arg_what = "idle:sleep:shutdown"; |
32 | static const char *arg_who = NULL; | |
33 | static const char *arg_why = "Unknown reason"; | |
34 | static const char *arg_mode = NULL; | |
35 | static bool arg_ask_password = true; | |
0221d68a | 36 | static PagerFlags arg_pager_flags = 0; |
a9426617 | 37 | static bool arg_legend = true; |
eecd1362 LP |
38 | |
39 | static enum { | |
40 | ACTION_INHIBIT, | |
41 | ACTION_LIST | |
42 | } arg_action = ACTION_INHIBIT; | |
43 | ||
0fb0c56f | 44 | static int inhibit(sd_bus *bus, sd_bus_error *error) { |
4afd3348 | 45 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
3137e0bd | 46 | int r; |
0fb0c56f | 47 | int fd; |
eecd1362 | 48 | |
a87b7aa1 ZJS |
49 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
50 | ||
d962e737 | 51 | r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode); |
3137e0bd LP |
52 | if (r < 0) |
53 | return r; | |
eecd1362 | 54 | |
0fb0c56f TG |
55 | r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd); |
56 | if (r < 0) | |
5b30bef8 | 57 | return r; |
eecd1362 | 58 | |
7c248223 | 59 | return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3)); |
eecd1362 LP |
60 | } |
61 | ||
0e6872cd LP |
62 | static int print_inhibitors(sd_bus *bus) { |
63 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4afd3348 | 64 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
a9426617 | 65 | _cleanup_(table_unrefp) Table *table = NULL; |
eecd1362 LP |
66 | int r; |
67 | ||
384c2c32 | 68 | pager_open(arg_pager_flags); |
f3c9133c | 69 | |
6ce68c7c | 70 | r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL); |
3137e0bd | 71 | if (r < 0) |
0e6872cd | 72 | return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r)); |
eecd1362 | 73 | |
9969b542 | 74 | table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode"); |
a9426617 LP |
75 | if (!table) |
76 | return log_oom(); | |
77 | ||
78 | /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */ | |
79 | (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20); | |
619b0c07 | 80 | (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2); |
a9426617 | 81 | |
0fb0c56f TG |
82 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
83 | if (r < 0) | |
40be0704 | 84 | return bus_log_parse_error(r); |
eecd1362 | 85 | |
a9426617 | 86 | for (;;) { |
391a4f72 | 87 | _cleanup_free_ char *comm = NULL, *u = NULL; |
a9426617 LP |
88 | const char *what, *who, *why, *mode; |
89 | uint32_t uid, pid; | |
90 | ||
91 | r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid); | |
92 | if (r < 0) | |
93 | return bus_log_parse_error(r); | |
94 | if (r == 0) | |
95 | break; | |
eecd1362 | 96 | |
ca5447c0 MM |
97 | if (arg_mode && !streq(mode, arg_mode)) |
98 | continue; | |
99 | ||
d7d74854 | 100 | (void) pid_get_comm(pid, &comm); |
391a4f72 LP |
101 | u = uid_to_name(uid); |
102 | ||
a9426617 LP |
103 | r = table_add_many(table, |
104 | TABLE_STRING, who, | |
805f2df1 | 105 | TABLE_UID, (uid_t) uid, |
a9426617 | 106 | TABLE_STRING, strna(u), |
805f2df1 | 107 | TABLE_PID, (pid_t) pid, |
a9426617 LP |
108 | TABLE_STRING, strna(comm), |
109 | TABLE_STRING, what, | |
110 | TABLE_STRING, why, | |
111 | TABLE_STRING, mode); | |
112 | if (r < 0) | |
bd17fa8c | 113 | return table_log_add_error(r); |
eecd1362 | 114 | } |
0fb0c56f TG |
115 | |
116 | r = sd_bus_message_exit_container(reply); | |
117 | if (r < 0) | |
40be0704 | 118 | return bus_log_parse_error(r); |
eecd1362 | 119 | |
2413a0fa | 120 | if (!table_isempty(table)) { |
ef1e0b9a | 121 | r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6); |
a9426617 | 122 | if (r < 0) |
df83eb54 | 123 | return table_log_sort_error(r); |
a9426617 LP |
124 | |
125 | table_set_header(table, arg_legend); | |
126 | ||
127 | r = table_print(table, NULL); | |
128 | if (r < 0) | |
4b6607d9 | 129 | return table_log_print_error(r); |
a9426617 LP |
130 | } |
131 | ||
132 | if (arg_legend) { | |
2413a0fa | 133 | if (table_isempty(table)) |
a9426617 | 134 | printf("No inhibitors.\n"); |
2413a0fa MY |
135 | else |
136 | printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1); | |
a9426617 LP |
137 | } |
138 | ||
4654e558 | 139 | return 0; |
eecd1362 LP |
140 | } |
141 | ||
37ec0fdd LP |
142 | static int help(void) { |
143 | _cleanup_free_ char *link = NULL; | |
144 | int r; | |
145 | ||
146 | r = terminal_urlify_man("systemd-inhibit", "1", &link); | |
147 | if (r < 0) | |
148 | return log_oom(); | |
149 | ||
353b2baa LP |
150 | printf("%s [OPTIONS...] COMMAND ...\n" |
151 | "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n" | |
eecd1362 LP |
152 | " -h --help Show this help\n" |
153 | " --version Show package version\n" | |
a87b7aa1 | 154 | " --no-ask-password Do not attempt interactive authorization\n" |
f3c9133c | 155 | " --no-pager Do not pipe output into a pager\n" |
a9426617 | 156 | " --no-legend Do not show the headers and footers\n" |
12a1309e LP |
157 | " --what=WHAT Operations to inhibit, colon separated list of:\n" |
158 | " shutdown, sleep, idle, handle-power-key,\n" | |
2f1bb513 СНА |
159 | " handle-suspend-key, handle-hibernate-key,\n" |
160 | " handle-lid-switch\n" | |
eecd1362 LP |
161 | " --who=STRING A descriptive string who is inhibiting\n" |
162 | " --why=STRING A descriptive string why is being inhibited\n" | |
5360db2a | 163 | " --mode=MODE One of block, block-weak, or delay\n" |
601185b4 | 164 | " --list List active inhibitors\n" |
bc556335 DDM |
165 | "\nSee the %s for details.\n", |
166 | program_invocation_short_name, | |
167 | ansi_highlight(), | |
168 | ansi_normal(), | |
169 | link); | |
37ec0fdd LP |
170 | |
171 | return 0; | |
eecd1362 LP |
172 | } |
173 | ||
174 | static int parse_argv(int argc, char *argv[]) { | |
175 | ||
176 | enum { | |
177 | ARG_VERSION = 0x100, | |
178 | ARG_WHAT, | |
179 | ARG_WHO, | |
180 | ARG_WHY, | |
181 | ARG_MODE, | |
182 | ARG_LIST, | |
a87b7aa1 | 183 | ARG_NO_ASK_PASSWORD, |
f3c9133c | 184 | ARG_NO_PAGER, |
a9426617 | 185 | ARG_NO_LEGEND, |
eecd1362 LP |
186 | }; |
187 | ||
188 | static const struct option options[] = { | |
a87b7aa1 ZJS |
189 | { "help", no_argument, NULL, 'h' }, |
190 | { "version", no_argument, NULL, ARG_VERSION }, | |
191 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
192 | { "what", required_argument, NULL, ARG_WHAT }, | |
193 | { "who", required_argument, NULL, ARG_WHO }, | |
194 | { "why", required_argument, NULL, ARG_WHY }, | |
195 | { "mode", required_argument, NULL, ARG_MODE }, | |
196 | { "list", no_argument, NULL, ARG_LIST }, | |
197 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
198 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
eb9da376 | 199 | {} |
eecd1362 LP |
200 | }; |
201 | ||
202 | int c; | |
203 | ||
204 | assert(argc >= 0); | |
205 | assert(argv); | |
206 | ||
ef9c12b1 YW |
207 | /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() |
208 | * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ | |
209 | optind = 0; | |
601185b4 | 210 | while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) |
eecd1362 LP |
211 | |
212 | switch (c) { | |
213 | ||
214 | case 'h': | |
37ec0fdd | 215 | return help(); |
eecd1362 LP |
216 | |
217 | case ARG_VERSION: | |
3f6fd1ba | 218 | return version(); |
eecd1362 LP |
219 | |
220 | case ARG_WHAT: | |
221 | arg_what = optarg; | |
222 | break; | |
223 | ||
224 | case ARG_WHO: | |
225 | arg_who = optarg; | |
226 | break; | |
227 | ||
228 | case ARG_WHY: | |
229 | arg_why = optarg; | |
230 | break; | |
231 | ||
232 | case ARG_MODE: | |
233 | arg_mode = optarg; | |
234 | break; | |
235 | ||
236 | case ARG_LIST: | |
237 | arg_action = ACTION_LIST; | |
238 | break; | |
239 | ||
a87b7aa1 ZJS |
240 | case ARG_NO_ASK_PASSWORD: |
241 | arg_ask_password = false; | |
242 | break; | |
243 | ||
f3c9133c | 244 | case ARG_NO_PAGER: |
0221d68a | 245 | arg_pager_flags |= PAGER_DISABLE; |
f3c9133c DT |
246 | break; |
247 | ||
a9426617 LP |
248 | case ARG_NO_LEGEND: |
249 | arg_legend = false; | |
250 | break; | |
251 | ||
eb9da376 | 252 | case '?': |
eecd1362 | 253 | return -EINVAL; |
eb9da376 LP |
254 | |
255 | default: | |
04499a70 | 256 | assert_not_reached(); |
eecd1362 | 257 | } |
eecd1362 | 258 | |
d9130355 | 259 | if (arg_action == ACTION_INHIBIT && optind == argc) |
2f2343c6 KS |
260 | arg_action = ACTION_LIST; |
261 | ||
baaa35ad ZJS |
262 | else if (arg_action == ACTION_INHIBIT && optind >= argc) |
263 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
264 | "Missing command line to execute."); | |
eecd1362 LP |
265 | |
266 | return 1; | |
267 | } | |
268 | ||
b453c447 | 269 | static int run(int argc, char *argv[]) { |
4afd3348 | 270 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
5220a6f3 | 271 | int r; |
eecd1362 | 272 | |
aa976d87 | 273 | log_setup(); |
eecd1362 LP |
274 | |
275 | r = parse_argv(argc, argv); | |
b453c447 ZJS |
276 | if (r <= 0) |
277 | return r; | |
eecd1362 | 278 | |
76b54375 | 279 | r = sd_bus_default_system(&bus); |
b453c447 | 280 | if (r < 0) |
d8a77d55 | 281 | return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM); |
eecd1362 | 282 | |
a87b7aa1 ZJS |
283 | (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); |
284 | ||
a34c79d0 ZJS |
285 | if (arg_action == ACTION_LIST) |
286 | return print_inhibitors(bus); | |
a34c79d0 | 287 | else { |
0e6872cd | 288 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b473691d | 289 | _cleanup_strv_free_ char **arguments = NULL; |
5220a6f3 | 290 | _cleanup_free_ char *w = NULL; |
254d1313 | 291 | _cleanup_close_ int fd = -EBADF; |
eecd1362 LP |
292 | pid_t pid; |
293 | ||
106f12a0 | 294 | /* Ignore SIGINT and allow the forked process to receive it */ |
9c274488 | 295 | (void) ignore_signals(SIGINT); |
106f12a0 | 296 | |
1b5e34fe LP |
297 | if (!arg_who) { |
298 | w = strv_join(argv + optind, " "); | |
299 | if (!w) | |
300 | return log_oom(); | |
301 | ||
302 | arg_who = w; | |
303 | } | |
eecd1362 | 304 | |
ca5447c0 MM |
305 | if (!arg_mode) |
306 | arg_mode = "block"; | |
307 | ||
eecd1362 | 308 | fd = inhibit(bus, &error); |
b453c447 | 309 | if (fd < 0) |
6bc7a6ac | 310 | return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd)); |
eecd1362 | 311 | |
b473691d LP |
312 | arguments = strv_copy(argv + optind); |
313 | if (!arguments) | |
314 | return log_oom(); | |
315 | ||
e9ccae31 | 316 | r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
b6e1fff1 | 317 | if (r < 0) |
b453c447 | 318 | return r; |
4c253ed1 | 319 | if (r == 0) { |
eecd1362 | 320 | /* Child */ |
b473691d | 321 | execvp(arguments[0], arguments); |
0b1f3c76 | 322 | log_open(); |
56f64d95 | 323 | log_error_errno(errno, "Failed to execute %s: %m", argv[optind]); |
eecd1362 LP |
324 | _exit(EXIT_FAILURE); |
325 | } | |
326 | ||
b453c447 | 327 | return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG); |
eecd1362 | 328 | } |
eecd1362 | 329 | } |
b453c447 ZJS |
330 | |
331 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |