]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/inhibit.c
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / src / login / inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <getopt.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "sd-bus.h"
10
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-util.h"
14 #include "fd-util.h"
15 #include "format-table.h"
16 #include "format-util.h"
17 #include "main-func.h"
18 #include "pager.h"
19 #include "pretty-print.h"
20 #include "process-util.h"
21 #include "signal-util.h"
22 #include "strv.h"
23 #include "terminal-util.h"
24 #include "user-util.h"
25 #include "util.h"
26
27 static const char* arg_what = "idle:sleep:shutdown";
28 static const char* arg_who = NULL;
29 static const char* arg_why = "Unknown reason";
30 static const char* arg_mode = NULL;
31 static PagerFlags arg_pager_flags = 0;
32 static bool arg_legend = true;
33
34 static enum {
35 ACTION_INHIBIT,
36 ACTION_LIST
37 } arg_action = ACTION_INHIBIT;
38
39 static int inhibit(sd_bus *bus, sd_bus_error *error) {
40 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
41 int r;
42 int fd;
43
44 r = sd_bus_call_method(
45 bus,
46 "org.freedesktop.login1",
47 "/org/freedesktop/login1",
48 "org.freedesktop.login1.Manager",
49 "Inhibit",
50 error,
51 &reply,
52 "ssss", arg_what, arg_who, arg_why, arg_mode);
53 if (r < 0)
54 return r;
55
56 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
57 if (r < 0)
58 return r;
59
60 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
61 if (r < 0)
62 return -errno;
63
64 return r;
65 }
66
67 static int print_inhibitors(sd_bus *bus) {
68 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
69 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
70 _cleanup_(table_unrefp) Table *table = NULL;
71 int r;
72
73 (void) pager_open(arg_pager_flags);
74
75 r = sd_bus_call_method(
76 bus,
77 "org.freedesktop.login1",
78 "/org/freedesktop/login1",
79 "org.freedesktop.login1.Manager",
80 "ListInhibitors",
81 &error,
82 &reply,
83 "");
84 if (r < 0)
85 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
86
87 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
88 if (!table)
89 return log_oom();
90
91 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
92 (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
93 (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2);
94
95 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
96 if (r < 0)
97 return bus_log_parse_error(r);
98
99 for (;;) {
100 _cleanup_free_ char *comm = NULL, *u = NULL;
101 const char *what, *who, *why, *mode;
102 uint32_t uid, pid;
103
104 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
105 if (r < 0)
106 return bus_log_parse_error(r);
107 if (r == 0)
108 break;
109
110 if (arg_mode && !streq(mode, arg_mode))
111 continue;
112
113 (void) get_process_comm(pid, &comm);
114 u = uid_to_name(uid);
115
116 r = table_add_many(table,
117 TABLE_STRING, who,
118 TABLE_UID, (uid_t) uid,
119 TABLE_STRING, strna(u),
120 TABLE_PID, (pid_t) pid,
121 TABLE_STRING, strna(comm),
122 TABLE_STRING, what,
123 TABLE_STRING, why,
124 TABLE_STRING, mode);
125 if (r < 0)
126 return table_log_add_error(r);
127 }
128
129 r = sd_bus_message_exit_container(reply);
130 if (r < 0)
131 return bus_log_parse_error(r);
132
133 if (table_get_rows(table) > 1) {
134 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
135 if (r < 0)
136 return table_log_sort_error(r);
137
138 table_set_header(table, arg_legend);
139
140 r = table_print(table, NULL);
141 if (r < 0)
142 return table_log_print_error(r);
143 }
144
145 if (arg_legend) {
146 if (table_get_rows(table) > 1)
147 printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
148 else
149 printf("No inhibitors.\n");
150 }
151
152 return 0;
153 }
154
155 static int help(void) {
156 _cleanup_free_ char *link = NULL;
157 int r;
158
159 r = terminal_urlify_man("systemd-inhibit", "1", &link);
160 if (r < 0)
161 return log_oom();
162
163 printf("%s [OPTIONS...] COMMAND ...\n"
164 "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n"
165 " -h --help Show this help\n"
166 " --version Show package version\n"
167 " --no-pager Do not pipe output into a pager\n"
168 " --no-legend Do not show the headers and footers\n"
169 " --what=WHAT Operations to inhibit, colon separated list of:\n"
170 " shutdown, sleep, idle, handle-power-key,\n"
171 " handle-suspend-key, handle-hibernate-key,\n"
172 " handle-lid-switch\n"
173 " --who=STRING A descriptive string who is inhibiting\n"
174 " --why=STRING A descriptive string why is being inhibited\n"
175 " --mode=MODE One of block or delay\n"
176 " --list List active inhibitors\n"
177 "\nSee the %s for details.\n",
178 program_invocation_short_name,
179 ansi_highlight(),
180 ansi_normal(),
181 link);
182
183 return 0;
184 }
185
186 static int parse_argv(int argc, char *argv[]) {
187
188 enum {
189 ARG_VERSION = 0x100,
190 ARG_WHAT,
191 ARG_WHO,
192 ARG_WHY,
193 ARG_MODE,
194 ARG_LIST,
195 ARG_NO_PAGER,
196 ARG_NO_LEGEND,
197 };
198
199 static const struct option options[] = {
200 { "help", no_argument, NULL, 'h' },
201 { "version", no_argument, NULL, ARG_VERSION },
202 { "what", required_argument, NULL, ARG_WHAT },
203 { "who", required_argument, NULL, ARG_WHO },
204 { "why", required_argument, NULL, ARG_WHY },
205 { "mode", required_argument, NULL, ARG_MODE },
206 { "list", no_argument, NULL, ARG_LIST },
207 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
208 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
209 {}
210 };
211
212 int c;
213
214 assert(argc >= 0);
215 assert(argv);
216
217 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
218
219 switch (c) {
220
221 case 'h':
222 return help();
223
224 case ARG_VERSION:
225 return version();
226
227 case ARG_WHAT:
228 arg_what = optarg;
229 break;
230
231 case ARG_WHO:
232 arg_who = optarg;
233 break;
234
235 case ARG_WHY:
236 arg_why = optarg;
237 break;
238
239 case ARG_MODE:
240 arg_mode = optarg;
241 break;
242
243 case ARG_LIST:
244 arg_action = ACTION_LIST;
245 break;
246
247 case ARG_NO_PAGER:
248 arg_pager_flags |= PAGER_DISABLE;
249 break;
250
251 case ARG_NO_LEGEND:
252 arg_legend = false;
253 break;
254
255 case '?':
256 return -EINVAL;
257
258 default:
259 assert_not_reached("Unhandled option");
260 }
261
262 if (arg_action == ACTION_INHIBIT && optind == argc)
263 arg_action = ACTION_LIST;
264
265 else if (arg_action == ACTION_INHIBIT && optind >= argc)
266 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
267 "Missing command line to execute.");
268
269 return 1;
270 }
271
272 static int run(int argc, char *argv[]) {
273 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
274 int r;
275
276 log_show_color(true);
277 log_parse_environment();
278 log_open();
279
280 r = parse_argv(argc, argv);
281 if (r <= 0)
282 return r;
283
284 r = sd_bus_default_system(&bus);
285 if (r < 0)
286 return bus_log_connect_error(r);
287
288 if (arg_action == ACTION_LIST)
289 return print_inhibitors(bus);
290 else {
291 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
292 _cleanup_strv_free_ char **arguments = NULL;
293 _cleanup_free_ char *w = NULL;
294 _cleanup_close_ int fd = -1;
295 pid_t pid;
296
297 /* Ignore SIGINT and allow the forked process to receive it */
298 (void) ignore_signals(SIGINT, -1);
299
300 if (!arg_who) {
301 w = strv_join(argv + optind, " ");
302 if (!w)
303 return log_oom();
304
305 arg_who = w;
306 }
307
308 if (!arg_mode)
309 arg_mode = "block";
310
311 fd = inhibit(bus, &error);
312 if (fd < 0)
313 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
314
315 arguments = strv_copy(argv + optind);
316 if (!arguments)
317 return log_oom();
318
319 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
320 if (r < 0)
321 return r;
322 if (r == 0) {
323 /* Child */
324 execvp(arguments[0], arguments);
325 log_open();
326 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
327 _exit(EXIT_FAILURE);
328 }
329
330 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
331 }
332 }
333
334 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);