]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <getopt.h> | |
5 | #include <stdio.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "sd-bus.h" | |
9 | ||
10 | #include "alloc-util.h" | |
11 | #include "build.h" | |
12 | #include "bus-error.h" | |
13 | #include "bus-locator.h" | |
14 | #include "bus-util.h" | |
15 | #include "errno-util.h" | |
16 | #include "fd-util.h" | |
17 | #include "format-table.h" | |
18 | #include "log.h" | |
19 | #include "main-func.h" | |
20 | #include "pager.h" | |
21 | #include "polkit-agent.h" | |
22 | #include "pretty-print.h" | |
23 | #include "process-util.h" | |
24 | #include "runtime-scope.h" | |
25 | #include "signal-util.h" | |
26 | #include "string-util.h" | |
27 | #include "strv.h" | |
28 | #include "terminal-util.h" | |
29 | #include "user-util.h" | |
30 | ||
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; | |
36 | static PagerFlags arg_pager_flags = 0; | |
37 | static bool arg_legend = true; | |
38 | ||
39 | static enum { | |
40 | ACTION_INHIBIT, | |
41 | ACTION_LIST | |
42 | } arg_action = ACTION_INHIBIT; | |
43 | ||
44 | static int inhibit(sd_bus *bus, sd_bus_error *error) { | |
45 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
46 | int r; | |
47 | int fd; | |
48 | ||
49 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); | |
50 | ||
51 | r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode); | |
52 | if (r < 0) | |
53 | return r; | |
54 | ||
55 | r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd); | |
56 | if (r < 0) | |
57 | return r; | |
58 | ||
59 | return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3)); | |
60 | } | |
61 | ||
62 | static int print_inhibitors(sd_bus *bus) { | |
63 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
64 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
65 | _cleanup_(table_unrefp) Table *table = NULL; | |
66 | int r; | |
67 | ||
68 | pager_open(arg_pager_flags); | |
69 | ||
70 | r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL); | |
71 | if (r < 0) | |
72 | return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r)); | |
73 | ||
74 | table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode"); | |
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); | |
80 | (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2); | |
81 | ||
82 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); | |
83 | if (r < 0) | |
84 | return bus_log_parse_error(r); | |
85 | ||
86 | for (;;) { | |
87 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
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; | |
96 | ||
97 | if (arg_mode && !streq(mode, arg_mode)) | |
98 | continue; | |
99 | ||
100 | (void) pid_get_comm(pid, &comm); | |
101 | u = uid_to_name(uid); | |
102 | ||
103 | r = table_add_many(table, | |
104 | TABLE_STRING, who, | |
105 | TABLE_UID, (uid_t) uid, | |
106 | TABLE_STRING, strna(u), | |
107 | TABLE_PID, (pid_t) pid, | |
108 | TABLE_STRING, strna(comm), | |
109 | TABLE_STRING, what, | |
110 | TABLE_STRING, why, | |
111 | TABLE_STRING, mode); | |
112 | if (r < 0) | |
113 | return table_log_add_error(r); | |
114 | } | |
115 | ||
116 | r = sd_bus_message_exit_container(reply); | |
117 | if (r < 0) | |
118 | return bus_log_parse_error(r); | |
119 | ||
120 | if (!table_isempty(table)) { | |
121 | r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6); | |
122 | if (r < 0) | |
123 | return table_log_sort_error(r); | |
124 | ||
125 | table_set_header(table, arg_legend); | |
126 | ||
127 | r = table_print(table, NULL); | |
128 | if (r < 0) | |
129 | return table_log_print_error(r); | |
130 | } | |
131 | ||
132 | if (arg_legend) { | |
133 | if (table_isempty(table)) | |
134 | printf("No inhibitors.\n"); | |
135 | else | |
136 | printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1); | |
137 | } | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
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 | ||
150 | printf("%s [OPTIONS...] COMMAND ...\n" | |
151 | "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n" | |
152 | " -h --help Show this help\n" | |
153 | " --version Show package version\n" | |
154 | " --no-ask-password Do not attempt interactive authorization\n" | |
155 | " --no-pager Do not pipe output into a pager\n" | |
156 | " --no-legend Do not show the headers and footers\n" | |
157 | " --what=WHAT Operations to inhibit, colon separated list of:\n" | |
158 | " shutdown, sleep, idle, handle-power-key,\n" | |
159 | " handle-suspend-key, handle-hibernate-key,\n" | |
160 | " handle-lid-switch\n" | |
161 | " --who=STRING A descriptive string who is inhibiting\n" | |
162 | " --why=STRING A descriptive string why is being inhibited\n" | |
163 | " --mode=MODE One of block, block-weak, or delay\n" | |
164 | " --list List active inhibitors\n" | |
165 | "\nSee the %s for details.\n", | |
166 | program_invocation_short_name, | |
167 | ansi_highlight(), | |
168 | ansi_normal(), | |
169 | link); | |
170 | ||
171 | return 0; | |
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, | |
183 | ARG_NO_ASK_PASSWORD, | |
184 | ARG_NO_PAGER, | |
185 | ARG_NO_LEGEND, | |
186 | }; | |
187 | ||
188 | static const struct option options[] = { | |
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 }, | |
199 | {} | |
200 | }; | |
201 | ||
202 | int c; | |
203 | ||
204 | assert(argc >= 0); | |
205 | assert(argv); | |
206 | ||
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; | |
210 | while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) | |
211 | ||
212 | switch (c) { | |
213 | ||
214 | case 'h': | |
215 | return help(); | |
216 | ||
217 | case ARG_VERSION: | |
218 | return version(); | |
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 | ||
240 | case ARG_NO_ASK_PASSWORD: | |
241 | arg_ask_password = false; | |
242 | break; | |
243 | ||
244 | case ARG_NO_PAGER: | |
245 | arg_pager_flags |= PAGER_DISABLE; | |
246 | break; | |
247 | ||
248 | case ARG_NO_LEGEND: | |
249 | arg_legend = false; | |
250 | break; | |
251 | ||
252 | case '?': | |
253 | return -EINVAL; | |
254 | ||
255 | default: | |
256 | assert_not_reached(); | |
257 | } | |
258 | ||
259 | if (arg_action == ACTION_INHIBIT && optind == argc) | |
260 | arg_action = ACTION_LIST; | |
261 | ||
262 | else if (arg_action == ACTION_INHIBIT && optind >= argc) | |
263 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
264 | "Missing command line to execute."); | |
265 | ||
266 | return 1; | |
267 | } | |
268 | ||
269 | static int run(int argc, char *argv[]) { | |
270 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
271 | int r; | |
272 | ||
273 | log_setup(); | |
274 | ||
275 | r = parse_argv(argc, argv); | |
276 | if (r <= 0) | |
277 | return r; | |
278 | ||
279 | r = sd_bus_default_system(&bus); | |
280 | if (r < 0) | |
281 | return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM); | |
282 | ||
283 | (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); | |
284 | ||
285 | if (arg_action == ACTION_LIST) | |
286 | return print_inhibitors(bus); | |
287 | else { | |
288 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
289 | _cleanup_strv_free_ char **arguments = NULL; | |
290 | _cleanup_free_ char *w = NULL; | |
291 | _cleanup_close_ int fd = -EBADF; | |
292 | pid_t pid; | |
293 | ||
294 | /* Ignore SIGINT and allow the forked process to receive it */ | |
295 | (void) ignore_signals(SIGINT); | |
296 | ||
297 | if (!arg_who) { | |
298 | w = strv_join(argv + optind, " "); | |
299 | if (!w) | |
300 | return log_oom(); | |
301 | ||
302 | arg_who = w; | |
303 | } | |
304 | ||
305 | if (!arg_mode) | |
306 | arg_mode = "block"; | |
307 | ||
308 | fd = inhibit(bus, &error); | |
309 | if (fd < 0) | |
310 | return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd)); | |
311 | ||
312 | arguments = strv_copy(argv + optind); | |
313 | if (!arguments) | |
314 | return log_oom(); | |
315 | ||
316 | r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); | |
317 | if (r < 0) | |
318 | return r; | |
319 | if (r == 0) { | |
320 | /* Child */ | |
321 | execvp(arguments[0], arguments); | |
322 | log_open(); | |
323 | log_error_errno(errno, "Failed to execute %s: %m", argv[optind]); | |
324 | _exit(EXIT_FAILURE); | |
325 | } | |
326 | ||
327 | return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG); | |
328 | } | |
329 | } | |
330 | ||
331 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |