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