Debian Codegraph @

src/login/inhibit.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <fnmatch.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 "options.h"
21 #include "pager.h"
22 #include "parse-argument.h"
23 #include "pidref.h"
24 #include "polkit-agent.h"
25 #include "pretty-print.h"
26 #include "process-util.h"
27 #include "runtime-scope.h"
28 #include "signal-util.h"
29 #include "string-util.h"
30 #include "strv.h"
31 #include "terminal-util.h"
32 #include "user-util.h"
33
34 static const char *arg_what = NULL;
35 static const char *arg_who = NULL;
36 static const char *arg_why = NULL;
37 static const char *arg_mode = NULL;
38 static bool arg_ask_password = true;
39 static PagerFlags arg_pager_flags = 0;
40 static bool arg_legend = true;
41 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
42
43 static enum {
44 ACTION_INHIBIT,
45 ACTION_LIST
46 } arg_action = ACTION_INHIBIT;
47
48 static int inhibit(sd_bus *bus, sd_bus_error *error) {
49 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
50 int r;
51 int fd;
52
53 (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
54
55 r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode);
56 if (r < 0)
57 return r;
58
59 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
60 if (r < 0)
61 return r;
62
63 return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
64 }
65
66 static int print_inhibitors(sd_bus *bus) {
67 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
68 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
69 _cleanup_(table_unrefp) Table *table = NULL;
70 _cleanup_strv_free_ char **what_filter = NULL;
71
72 int r;
73
74 pager_open(arg_pager_flags);
75
76 r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL);
77 if (r < 0)
78 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
79
80 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
81 if (!table)
82 return log_oom();
83
84 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
85 (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
86 (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2);
87
88 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
89 if (r < 0)
90 return bus_log_parse_error(r);
91
92 if (arg_what) {
93 what_filter = strv_split(arg_what, ":");
94 if (!what_filter)
95 return log_oom();
96 }
97
98 for (;;) {
99 _cleanup_free_ char *comm = NULL, *u = NULL;
100 const char *what, *who, *why, *mode;
101 uint32_t uid, pid;
102
103 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
104 if (r < 0)
105 return bus_log_parse_error(r);
106 if (r == 0)
107 break;
108
109 if (what_filter) {
110 bool skip = false;
111
112 STRV_FOREACH(op, what_filter)
113 if (!string_contains_word(what, ":", *op)) {
114 skip = true;
115 break;
116 }
117
118 if (skip)
119 continue;
120 }
121
122 if (arg_who && !streq(who, arg_who))
123 continue;
124
125 if (arg_why && fnmatch(arg_why, why, FNM_CASEFOLD) != 0)
126 continue;
127
128 if (arg_mode && !streq(mode, arg_mode))
129 continue;
130
131 (void) pid_get_comm(pid, &comm);
132 u = uid_to_name(uid);
133
134 r = table_add_many(table,
135 TABLE_STRING, who,
136 TABLE_UID, (uid_t) uid,
137 TABLE_STRING, strna(u),
138 TABLE_PID, (pid_t) pid,
139 TABLE_STRING, strna(comm),
140 TABLE_STRING, what,
141 TABLE_STRING, why,
142 TABLE_STRING, mode);
143 if (r < 0)
144 return table_log_add_error(r);
145 }
146
147 r = sd_bus_message_exit_container(reply);
148 if (r < 0)
149 return bus_log_parse_error(r);
150
151 if (!table_isempty(table)) {
152 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
153 if (r < 0)
154 return table_log_sort_error(r);
155
156 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
157 if (r < 0)
158 return r;
159 }
160
161 if (arg_legend && !sd_json_format_enabled(arg_json_format_flags)) {
162 if (table_isempty(table))
163 printf("No inhibitors.\n");
164 else
165 printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
166 }
167
168 return 0;
169 }
170
171 static int help(void) {
172 _cleanup_free_ char *link = NULL;
173 _cleanup_(table_unrefp) Table *options = NULL;
174 int r;
175
176 r = terminal_urlify_man("systemd-inhibit", "1", &link);
177 if (r < 0)
178 return log_oom();
179
180 r = option_parser_get_help_table(&options);
181 if (r < 0)
182 return r;
183
184 printf("%s [OPTIONS...] COMMAND ...\n\n"
185 "%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n",
186 program_invocation_short_name,
187 ansi_highlight(),
188 ansi_normal());
189
190 r = table_print_or_warn(options);
191 if (r < 0)
192 return r;
193
194 printf("\nSee the %s for details.\n", link);
195 return 0;
196 }
197
198 static int parse_argv(int argc, char *argv[], char ***remaining_args) {
199 assert(argc >= 0);
200 assert(argv);
201 assert(remaining_args);
202
203 OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION };
204 int r;
205
206 FOREACH_OPTION_OR_RETURN(c, &opts)
207 switch (c) {
208
209 OPTION_COMMON_HELP:
210 return help();
211
212 OPTION_COMMON_VERSION:
213 return version();
214
215 OPTION_COMMON_NO_ASK_PASSWORD:
216 arg_ask_password = false;
217 break;
218
219 OPTION_COMMON_NO_PAGER:
220 arg_pager_flags |= PAGER_DISABLE;
221 break;
222
223 OPTION_COMMON_NO_LEGEND:
224 arg_legend = false;
225 break;
226
227 OPTION_COMMON_JSON:
228 r = parse_json_argument(opts.arg, &arg_json_format_flags);
229 if (r <= 0)
230 return r;
231 break;
232
233 OPTION_LONG("what", "WHAT",
234 "Operations to inhibit, colon separated list "
235 "(shutdown, sleep, idle, handle-power-key, "
236 "handle-suspend-key, handle-hibernate-key, "
237 "handle-lid-switch)"):
238 arg_what = opts.arg;
239 break;
240
241 OPTION_LONG("who", "STRING",
242 "A descriptive string who is inhibiting"):
243 arg_who = opts.arg;
244 break;
245
246 OPTION_LONG("why", "STRING",
247 "A descriptive string why is being inhibited"):
248 arg_why = opts.arg;
249 break;
250
251 OPTION_LONG("mode", "MODE", "One of block, block-weak, or delay"):
252 arg_mode = opts.arg;
253 break;
254
255 OPTION_LONG("list", NULL, "List active inhibitors"):
256 arg_action = ACTION_LIST;
257 break;
258 }
259
260 char **args = option_parser_get_args(&opts);
261
262 if (arg_action == ACTION_INHIBIT && strv_isempty(args))
263 arg_action = ACTION_LIST;
264
265 *remaining_args = args;
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 char **args = NULL;
276 r = parse_argv(argc, argv, &args);
277 if (r <= 0)
278 return r;
279
280 r = sd_bus_default_system(&bus);
281 if (r < 0)
282 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
283
284 (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
285
286 if (arg_action == ACTION_LIST)
287 return print_inhibitors(bus);
288 else {
289 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
290 _cleanup_strv_free_ char **arguments = NULL;
291 _cleanup_free_ char *w = NULL;
292 _cleanup_close_ int fd = -EBADF;
293
294 /* Ignore SIGINT and allow the forked process to receive it */
295 (void) ignore_signals(SIGINT);
296
297 if (!arg_what)
298 arg_what = "idle:sleep:shutdown";
299
300 if (!arg_who) {
301 w = strv_join(args, " ");
302 if (!w)
303 return log_oom();
304
305 arg_who = w;
306 }
307
308 if (!arg_why)
309 arg_why = "Unknown reason";
310
311 if (!arg_mode)
312 arg_mode = "block";
313
314 fd = inhibit(bus, &error);
315 if (fd < 0)
316 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
317
318 arguments = strv_copy(args);
319 if (!arguments)
320 return log_oom();
321
322 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
323 r = pidref_safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pidref);
324 if (r < 0)
325 return r;
326 if (r == 0) {
327 /* Child */
328 execvp(arguments[0], arguments);
329 log_open();
330 log_error_errno(errno, "Failed to execute '%s': %m", arguments[0]);
331 _exit(EXIT_FAILURE);
332 }
333
334 return pidref_wait_for_terminate_and_check(args[0], &pidref, WAIT_LOG_ABNORMAL);
335 }
336 }
337
338 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);