]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/inhibit.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / login / inhibit.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
eecd1362 2
3f6fd1ba 3#include <fcntl.h>
eecd1362 4#include <getopt.h>
eecd1362 5#include <stdio.h>
3f6fd1ba 6#include <stdlib.h>
eecd1362
LP
7#include <unistd.h>
8
0fb0c56f 9#include "sd-bus.h"
3f6fd1ba 10
b5efdb8a 11#include "alloc-util.h"
0fb0c56f 12#include "bus-error.h"
3f6fd1ba 13#include "bus-util.h"
3ffd4af2 14#include "fd-util.h"
a9426617 15#include "format-table.h"
f97b34a6 16#include "format-util.h"
5e332028 17#include "main-func.h"
f3c9133c 18#include "pager.h"
294bf0c3 19#include "pretty-print.h"
0b452006 20#include "process-util.h"
ce30c8dc 21#include "signal-util.h"
3f6fd1ba 22#include "strv.h"
b1d4f8e1 23#include "user-util.h"
3f6fd1ba 24#include "util.h"
eecd1362 25
4943c1c9 26static const char* arg_what = "idle:sleep:shutdown";
eecd1362
LP
27static const char* arg_who = NULL;
28static const char* arg_why = "Unknown reason";
ca5447c0 29static const char* arg_mode = NULL;
0221d68a 30static PagerFlags arg_pager_flags = 0;
a9426617 31static bool arg_legend = true;
eecd1362
LP
32
33static enum {
34 ACTION_INHIBIT,
35 ACTION_LIST
36} arg_action = ACTION_INHIBIT;
37
0fb0c56f 38static int inhibit(sd_bus *bus, sd_bus_error *error) {
4afd3348 39 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3137e0bd 40 int r;
0fb0c56f 41 int fd;
eecd1362 42
0fb0c56f 43 r = sd_bus_call_method(
b9c26b41 44 bus,
eecd1362
LP
45 "org.freedesktop.login1",
46 "/org/freedesktop/login1",
47 "org.freedesktop.login1.Manager",
b9c26b41 48 "Inhibit",
0fb0c56f 49 error,
b9c26b41 50 &reply,
0fb0c56f 51 "ssss", arg_what, arg_who, arg_why, arg_mode);
3137e0bd
LP
52 if (r < 0)
53 return r;
eecd1362 54
0fb0c56f
TG
55 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
56 if (r < 0)
5b30bef8 57 return r;
eecd1362 58
ead34950 59 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
0fb0c56f
TG
60 if (r < 0)
61 return -errno;
62
3137e0bd 63 return r;
eecd1362
LP
64}
65
0e6872cd
LP
66static int print_inhibitors(sd_bus *bus) {
67 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348 68 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
a9426617 69 _cleanup_(table_unrefp) Table *table = NULL;
eecd1362
LP
70 int r;
71
0221d68a 72 (void) pager_open(arg_pager_flags);
f3c9133c 73
0fb0c56f 74 r = sd_bus_call_method(
b9c26b41 75 bus,
eecd1362
LP
76 "org.freedesktop.login1",
77 "/org/freedesktop/login1",
78 "org.freedesktop.login1.Manager",
b9c26b41 79 "ListInhibitors",
0e6872cd 80 &error,
b9c26b41 81 &reply,
0fb0c56f 82 "");
3137e0bd 83 if (r < 0)
0e6872cd 84 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
eecd1362 85
9969b542 86 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
a9426617
LP
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
0fb0c56f
TG
93 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
94 if (r < 0)
40be0704 95 return bus_log_parse_error(r);
eecd1362 96
a9426617 97 for (;;) {
391a4f72 98 _cleanup_free_ char *comm = NULL, *u = NULL;
a9426617
LP
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;
eecd1362 107
ca5447c0
MM
108 if (arg_mode && !streq(mode, arg_mode))
109 continue;
110
a9426617 111 (void) get_process_comm(pid, &comm);
391a4f72
LP
112 u = uid_to_name(uid);
113
a9426617
LP
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");
eecd1362 125 }
0fb0c56f
TG
126
127 r = sd_bus_message_exit_container(reply);
128 if (r < 0)
40be0704 129 return bus_log_parse_error(r);
eecd1362 130
a9426617
LP
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
4654e558 150 return 0;
eecd1362
LP
151}
152
37ec0fdd
LP
153static 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
eecd1362 161 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4943c1c9 162 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
eecd1362
LP
163 " -h --help Show this help\n"
164 " --version Show package version\n"
f3c9133c 165 " --no-pager Do not pipe output into a pager\n"
a9426617 166 " --no-legend Do not show the headers and footers\n"
12a1309e
LP
167 " --what=WHAT Operations to inhibit, colon separated list of:\n"
168 " shutdown, sleep, idle, handle-power-key,\n"
2f1bb513
СНА
169 " handle-suspend-key, handle-hibernate-key,\n"
170 " handle-lid-switch\n"
eecd1362
LP
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"
601185b4 174 " --list List active inhibitors\n"
37ec0fdd
LP
175 "\nSee the %s for details.\n"
176 , program_invocation_short_name
177 , link
178 );
179
180 return 0;
eecd1362
LP
181}
182
183static 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,
f3c9133c 192 ARG_NO_PAGER,
a9426617 193 ARG_NO_LEGEND,
eecd1362
LP
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 },
f3c9133c 204 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a9426617 205 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
eb9da376 206 {}
eecd1362
LP
207 };
208
209 int c;
210
211 assert(argc >= 0);
212 assert(argv);
213
601185b4 214 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
eecd1362
LP
215
216 switch (c) {
217
218 case 'h':
37ec0fdd 219 return help();
eecd1362
LP
220
221 case ARG_VERSION:
3f6fd1ba 222 return version();
eecd1362
LP
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
f3c9133c 244 case ARG_NO_PAGER:
0221d68a 245 arg_pager_flags |= PAGER_DISABLE;
f3c9133c
DT
246 break;
247
a9426617
LP
248 case ARG_NO_LEGEND:
249 arg_legend = false;
250 break;
251
eb9da376 252 case '?':
eecd1362 253 return -EINVAL;
eb9da376
LP
254
255 default:
256 assert_not_reached("Unhandled option");
eecd1362 257 }
eecd1362 258
d9130355 259 if (arg_action == ACTION_INHIBIT && optind == argc)
2f2343c6
KS
260 arg_action = ACTION_LIST;
261
baaa35ad
ZJS
262 else if (arg_action == ACTION_INHIBIT && optind >= argc)
263 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
264 "Missing command line to execute.");
eecd1362
LP
265
266 return 1;
267}
268
b453c447 269static int run(int argc, char *argv[]) {
4afd3348 270 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5220a6f3 271 int r;
eecd1362
LP
272
273 log_parse_environment();
274 log_open();
275
276 r = parse_argv(argc, argv);
b453c447
ZJS
277 if (r <= 0)
278 return r;
eecd1362 279
76b54375 280 r = sd_bus_default_system(&bus);
b453c447
ZJS
281 if (r < 0)
282 return log_error_errno(r, "Failed to connect to bus: %m");
eecd1362 283
a34c79d0
ZJS
284 if (arg_action == ACTION_LIST)
285 return print_inhibitors(bus);
eecd1362 286
a34c79d0 287 else {
0e6872cd 288 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
5220a6f3
LP
289 _cleanup_close_ int fd = -1;
290 _cleanup_free_ char *w = NULL;
eecd1362
LP
291 pid_t pid;
292
106f12a0
CH
293 /* Ignore SIGINT and allow the forked process to receive it */
294 (void) ignore_signals(SIGINT, -1);
295
eecd1362
LP
296 if (!arg_who)
297 arg_who = w = strv_join(argv + optind, " ");
298
ca5447c0
MM
299 if (!arg_mode)
300 arg_mode = "block";
301
eecd1362 302 fd = inhibit(bus, &error);
b453c447 303 if (fd < 0)
6bc7a6ac 304 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
eecd1362 305
0672e2c6 306 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
b6e1fff1 307 if (r < 0)
b453c447 308 return r;
4c253ed1 309 if (r == 0) {
eecd1362 310 /* Child */
eecd1362 311 execvp(argv[optind], argv + optind);
0b1f3c76 312 log_open();
56f64d95 313 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
eecd1362
LP
314 _exit(EXIT_FAILURE);
315 }
316
b453c447 317 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
eecd1362 318 }
eecd1362 319}
b453c447
ZJS
320
321DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);