]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/inhibit.c
Merge pull request #12252 from keszybz/libmount-dont-unescape
[thirdparty/systemd.git] / src / login / inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "user-util.h"
24 #include "util.h"
25
26 static const char* arg_what = "idle:sleep:shutdown";
27 static const char* arg_who = NULL;
28 static const char* arg_why = "Unknown reason";
29 static const char* arg_mode = NULL;
30 static PagerFlags arg_pager_flags = 0;
31 static bool arg_legend = true;
32
33 static enum {
34 ACTION_INHIBIT,
35 ACTION_LIST
36 } arg_action = ACTION_INHIBIT;
37
38 static int inhibit(sd_bus *bus, sd_bus_error *error) {
39 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
40 int r;
41 int fd;
42
43 r = sd_bus_call_method(
44 bus,
45 "org.freedesktop.login1",
46 "/org/freedesktop/login1",
47 "org.freedesktop.login1.Manager",
48 "Inhibit",
49 error,
50 &reply,
51 "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 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
60 if (r < 0)
61 return -errno;
62
63 return r;
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 int r;
71
72 (void) pager_open(arg_pager_flags);
73
74 r = sd_bus_call_method(
75 bus,
76 "org.freedesktop.login1",
77 "/org/freedesktop/login1",
78 "org.freedesktop.login1.Manager",
79 "ListInhibitors",
80 &error,
81 &reply,
82 "");
83 if (r < 0)
84 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
85
86 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
87 if (!table)
88 return log_oom();
89
90 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
91 (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
92
93 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
94 if (r < 0)
95 return bus_log_parse_error(r);
96
97 for (;;) {
98 _cleanup_free_ char *comm = NULL, *u = NULL;
99 const char *what, *who, *why, *mode;
100 uint32_t uid, pid;
101
102 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
103 if (r < 0)
104 return bus_log_parse_error(r);
105 if (r == 0)
106 break;
107
108 if (arg_mode && !streq(mode, arg_mode))
109 continue;
110
111 (void) get_process_comm(pid, &comm);
112 u = uid_to_name(uid);
113
114 r = table_add_many(table,
115 TABLE_STRING, who,
116 TABLE_UINT32, uid,
117 TABLE_STRING, strna(u),
118 TABLE_UINT32, pid,
119 TABLE_STRING, strna(comm),
120 TABLE_STRING, what,
121 TABLE_STRING, why,
122 TABLE_STRING, mode);
123 if (r < 0)
124 return log_error_errno(r, "Failed to add table row: %m");
125 }
126
127 r = sd_bus_message_exit_container(reply);
128 if (r < 0)
129 return bus_log_parse_error(r);
130
131 if (table_get_rows(table) > 1) {
132 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
133 if (r < 0)
134 return log_error_errno(r, "Failed to sort table: %m");
135
136 table_set_header(table, arg_legend);
137
138 r = table_print(table, NULL);
139 if (r < 0)
140 return log_error_errno(r, "Failed to show table: %m");
141 }
142
143 if (arg_legend) {
144 if (table_get_rows(table) > 1)
145 printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
146 else
147 printf("No inhibitors.\n");
148 }
149
150 return 0;
151 }
152
153 static int help(void) {
154 _cleanup_free_ char *link = NULL;
155 int r;
156
157 r = terminal_urlify_man("systemd-inhibit", "1", &link);
158 if (r < 0)
159 return log_oom();
160
161 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
162 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
163 " -h --help Show this help\n"
164 " --version Show package version\n"
165 " --no-pager Do not pipe output into a pager\n"
166 " --no-legend Do not show the headers and footers\n"
167 " --what=WHAT Operations to inhibit, colon separated list of:\n"
168 " shutdown, sleep, idle, handle-power-key,\n"
169 " handle-suspend-key, handle-hibernate-key,\n"
170 " handle-lid-switch\n"
171 " --who=STRING A descriptive string who is inhibiting\n"
172 " --why=STRING A descriptive string why is being inhibited\n"
173 " --mode=MODE One of block or delay\n"
174 " --list List active inhibitors\n"
175 "\nSee the %s for details.\n"
176 , program_invocation_short_name
177 , link
178 );
179
180 return 0;
181 }
182
183 static int parse_argv(int argc, char *argv[]) {
184
185 enum {
186 ARG_VERSION = 0x100,
187 ARG_WHAT,
188 ARG_WHO,
189 ARG_WHY,
190 ARG_MODE,
191 ARG_LIST,
192 ARG_NO_PAGER,
193 ARG_NO_LEGEND,
194 };
195
196 static const struct option options[] = {
197 { "help", no_argument, NULL, 'h' },
198 { "version", no_argument, NULL, ARG_VERSION },
199 { "what", required_argument, NULL, ARG_WHAT },
200 { "who", required_argument, NULL, ARG_WHO },
201 { "why", required_argument, NULL, ARG_WHY },
202 { "mode", required_argument, NULL, ARG_MODE },
203 { "list", no_argument, NULL, ARG_LIST },
204 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
205 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
206 {}
207 };
208
209 int c;
210
211 assert(argc >= 0);
212 assert(argv);
213
214 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
215
216 switch (c) {
217
218 case 'h':
219 return help();
220
221 case ARG_VERSION:
222 return version();
223
224 case ARG_WHAT:
225 arg_what = optarg;
226 break;
227
228 case ARG_WHO:
229 arg_who = optarg;
230 break;
231
232 case ARG_WHY:
233 arg_why = optarg;
234 break;
235
236 case ARG_MODE:
237 arg_mode = optarg;
238 break;
239
240 case ARG_LIST:
241 arg_action = ACTION_LIST;
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("Unhandled option");
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_parse_environment();
274 log_open();
275
276 r = parse_argv(argc, argv);
277 if (r <= 0)
278 return r;
279
280 r = sd_bus_default_system(&bus);
281 if (r < 0)
282 return log_error_errno(r, "Failed to connect to bus: %m");
283
284 if (arg_action == ACTION_LIST)
285 return print_inhibitors(bus);
286
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 = -1;
292 pid_t pid;
293
294 /* Ignore SIGINT and allow the forked process to receive it */
295 (void) ignore_signals(SIGINT, -1);
296
297 if (!arg_who)
298 arg_who = w = strv_join(argv + optind, " ");
299
300 if (!arg_mode)
301 arg_mode = "block";
302
303 fd = inhibit(bus, &error);
304 if (fd < 0)
305 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
306
307 arguments = strv_copy(argv + optind);
308 if (!arguments)
309 return log_oom();
310
311 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
312 if (r < 0)
313 return r;
314 if (r == 0) {
315 /* Child */
316 execvp(arguments[0], arguments);
317 log_open();
318 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
319 _exit(EXIT_FAILURE);
320 }
321
322 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
323 }
324 }
325
326 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);