]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/inhibit.c
process-util: add new FORK_DEATHSIG_SIGKILL flag, rename FORK_DEATHSIG → FORK_DEATHSI...
[thirdparty/systemd.git] / src / login / inhibit.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
d6b4d1c7 12#include "build.h"
0fb0c56f 13#include "bus-error.h"
d962e737 14#include "bus-locator.h"
3f6fd1ba 15#include "bus-util.h"
3ffd4af2 16#include "fd-util.h"
a9426617 17#include "format-table.h"
f97b34a6 18#include "format-util.h"
5e332028 19#include "main-func.h"
f3c9133c 20#include "pager.h"
294bf0c3 21#include "pretty-print.h"
0b452006 22#include "process-util.h"
ce30c8dc 23#include "signal-util.h"
3f6fd1ba 24#include "strv.h"
353b2baa 25#include "terminal-util.h"
b1d4f8e1 26#include "user-util.h"
eecd1362 27
4943c1c9 28static const char* arg_what = "idle:sleep:shutdown";
eecd1362
LP
29static const char* arg_who = NULL;
30static const char* arg_why = "Unknown reason";
ca5447c0 31static const char* arg_mode = NULL;
0221d68a 32static PagerFlags arg_pager_flags = 0;
a9426617 33static bool arg_legend = true;
eecd1362
LP
34
35static enum {
36 ACTION_INHIBIT,
37 ACTION_LIST
38} arg_action = ACTION_INHIBIT;
39
0fb0c56f 40static int inhibit(sd_bus *bus, sd_bus_error *error) {
4afd3348 41 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3137e0bd 42 int r;
0fb0c56f 43 int fd;
eecd1362 44
d962e737 45 r = bus_call_method(bus, bus_login_mgr, "Inhibit", error, &reply, "ssss", arg_what, arg_who, arg_why, arg_mode);
3137e0bd
LP
46 if (r < 0)
47 return r;
eecd1362 48
0fb0c56f
TG
49 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
50 if (r < 0)
5b30bef8 51 return r;
eecd1362 52
7c248223 53 return RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
eecd1362
LP
54}
55
0e6872cd
LP
56static int print_inhibitors(sd_bus *bus) {
57 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4afd3348 58 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
a9426617 59 _cleanup_(table_unrefp) Table *table = NULL;
eecd1362
LP
60 int r;
61
384c2c32 62 pager_open(arg_pager_flags);
f3c9133c 63
6ce68c7c 64 r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", &error, &reply, NULL);
3137e0bd 65 if (r < 0)
0e6872cd 66 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
eecd1362 67
9969b542 68 table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
a9426617
LP
69 if (!table)
70 return log_oom();
71
72 /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
73 (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
619b0c07 74 (void) table_set_maximum_width(table, TABLE_HEADER_CELL(0), columns()/2);
a9426617 75
0fb0c56f
TG
76 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
77 if (r < 0)
40be0704 78 return bus_log_parse_error(r);
eecd1362 79
a9426617 80 for (;;) {
391a4f72 81 _cleanup_free_ char *comm = NULL, *u = NULL;
a9426617
LP
82 const char *what, *who, *why, *mode;
83 uint32_t uid, pid;
84
85 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
86 if (r < 0)
87 return bus_log_parse_error(r);
88 if (r == 0)
89 break;
eecd1362 90
ca5447c0
MM
91 if (arg_mode && !streq(mode, arg_mode))
92 continue;
93
d7d74854 94 (void) pid_get_comm(pid, &comm);
391a4f72
LP
95 u = uid_to_name(uid);
96
a9426617
LP
97 r = table_add_many(table,
98 TABLE_STRING, who,
805f2df1 99 TABLE_UID, (uid_t) uid,
a9426617 100 TABLE_STRING, strna(u),
805f2df1 101 TABLE_PID, (pid_t) pid,
a9426617
LP
102 TABLE_STRING, strna(comm),
103 TABLE_STRING, what,
104 TABLE_STRING, why,
105 TABLE_STRING, mode);
106 if (r < 0)
bd17fa8c 107 return table_log_add_error(r);
eecd1362 108 }
0fb0c56f
TG
109
110 r = sd_bus_message_exit_container(reply);
111 if (r < 0)
40be0704 112 return bus_log_parse_error(r);
eecd1362 113
a9426617 114 if (table_get_rows(table) > 1) {
ef1e0b9a 115 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
a9426617 116 if (r < 0)
df83eb54 117 return table_log_sort_error(r);
a9426617
LP
118
119 table_set_header(table, arg_legend);
120
121 r = table_print(table, NULL);
122 if (r < 0)
4b6607d9 123 return table_log_print_error(r);
a9426617
LP
124 }
125
126 if (arg_legend) {
127 if (table_get_rows(table) > 1)
128 printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
129 else
130 printf("No inhibitors.\n");
131 }
132
4654e558 133 return 0;
eecd1362
LP
134}
135
37ec0fdd
LP
136static int help(void) {
137 _cleanup_free_ char *link = NULL;
138 int r;
139
140 r = terminal_urlify_man("systemd-inhibit", "1", &link);
141 if (r < 0)
142 return log_oom();
143
353b2baa
LP
144 printf("%s [OPTIONS...] COMMAND ...\n"
145 "\n%sExecute a process while inhibiting shutdown/sleep/idle.%s\n\n"
eecd1362
LP
146 " -h --help Show this help\n"
147 " --version Show package version\n"
f3c9133c 148 " --no-pager Do not pipe output into a pager\n"
a9426617 149 " --no-legend Do not show the headers and footers\n"
12a1309e
LP
150 " --what=WHAT Operations to inhibit, colon separated list of:\n"
151 " shutdown, sleep, idle, handle-power-key,\n"
2f1bb513
СНА
152 " handle-suspend-key, handle-hibernate-key,\n"
153 " handle-lid-switch\n"
eecd1362
LP
154 " --who=STRING A descriptive string who is inhibiting\n"
155 " --why=STRING A descriptive string why is being inhibited\n"
156 " --mode=MODE One of block or delay\n"
601185b4 157 " --list List active inhibitors\n"
bc556335
DDM
158 "\nSee the %s for details.\n",
159 program_invocation_short_name,
160 ansi_highlight(),
161 ansi_normal(),
162 link);
37ec0fdd
LP
163
164 return 0;
eecd1362
LP
165}
166
167static int parse_argv(int argc, char *argv[]) {
168
169 enum {
170 ARG_VERSION = 0x100,
171 ARG_WHAT,
172 ARG_WHO,
173 ARG_WHY,
174 ARG_MODE,
175 ARG_LIST,
f3c9133c 176 ARG_NO_PAGER,
a9426617 177 ARG_NO_LEGEND,
eecd1362
LP
178 };
179
180 static const struct option options[] = {
181 { "help", no_argument, NULL, 'h' },
182 { "version", no_argument, NULL, ARG_VERSION },
183 { "what", required_argument, NULL, ARG_WHAT },
184 { "who", required_argument, NULL, ARG_WHO },
185 { "why", required_argument, NULL, ARG_WHY },
186 { "mode", required_argument, NULL, ARG_MODE },
187 { "list", no_argument, NULL, ARG_LIST },
f3c9133c 188 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a9426617 189 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
eb9da376 190 {}
eecd1362
LP
191 };
192
193 int c;
194
195 assert(argc >= 0);
196 assert(argv);
197
ef9c12b1
YW
198 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
199 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
200 optind = 0;
601185b4 201 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
eecd1362
LP
202
203 switch (c) {
204
205 case 'h':
37ec0fdd 206 return help();
eecd1362
LP
207
208 case ARG_VERSION:
3f6fd1ba 209 return version();
eecd1362
LP
210
211 case ARG_WHAT:
212 arg_what = optarg;
213 break;
214
215 case ARG_WHO:
216 arg_who = optarg;
217 break;
218
219 case ARG_WHY:
220 arg_why = optarg;
221 break;
222
223 case ARG_MODE:
224 arg_mode = optarg;
225 break;
226
227 case ARG_LIST:
228 arg_action = ACTION_LIST;
229 break;
230
f3c9133c 231 case ARG_NO_PAGER:
0221d68a 232 arg_pager_flags |= PAGER_DISABLE;
f3c9133c
DT
233 break;
234
a9426617
LP
235 case ARG_NO_LEGEND:
236 arg_legend = false;
237 break;
238
eb9da376 239 case '?':
eecd1362 240 return -EINVAL;
eb9da376
LP
241
242 default:
04499a70 243 assert_not_reached();
eecd1362 244 }
eecd1362 245
d9130355 246 if (arg_action == ACTION_INHIBIT && optind == argc)
2f2343c6
KS
247 arg_action = ACTION_LIST;
248
baaa35ad
ZJS
249 else if (arg_action == ACTION_INHIBIT && optind >= argc)
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
251 "Missing command line to execute.");
eecd1362
LP
252
253 return 1;
254}
255
b453c447 256static int run(int argc, char *argv[]) {
4afd3348 257 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5220a6f3 258 int r;
eecd1362 259
1a043959 260 log_show_color(true);
eecd1362
LP
261 log_parse_environment();
262 log_open();
263
264 r = parse_argv(argc, argv);
b453c447
ZJS
265 if (r <= 0)
266 return r;
eecd1362 267
76b54375 268 r = sd_bus_default_system(&bus);
b453c447 269 if (r < 0)
10a7340a 270 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL);
eecd1362 271
a34c79d0
ZJS
272 if (arg_action == ACTION_LIST)
273 return print_inhibitors(bus);
a34c79d0 274 else {
0e6872cd 275 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b473691d 276 _cleanup_strv_free_ char **arguments = NULL;
5220a6f3 277 _cleanup_free_ char *w = NULL;
254d1313 278 _cleanup_close_ int fd = -EBADF;
eecd1362
LP
279 pid_t pid;
280
106f12a0 281 /* Ignore SIGINT and allow the forked process to receive it */
9c274488 282 (void) ignore_signals(SIGINT);
106f12a0 283
1b5e34fe
LP
284 if (!arg_who) {
285 w = strv_join(argv + optind, " ");
286 if (!w)
287 return log_oom();
288
289 arg_who = w;
290 }
eecd1362 291
ca5447c0
MM
292 if (!arg_mode)
293 arg_mode = "block";
294
eecd1362 295 fd = inhibit(bus, &error);
b453c447 296 if (fd < 0)
6bc7a6ac 297 return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
eecd1362 298
b473691d
LP
299 arguments = strv_copy(argv + optind);
300 if (!arguments)
301 return log_oom();
302
e9ccae31 303 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
b6e1fff1 304 if (r < 0)
b453c447 305 return r;
4c253ed1 306 if (r == 0) {
eecd1362 307 /* Child */
b473691d 308 execvp(arguments[0], arguments);
0b1f3c76 309 log_open();
56f64d95 310 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
eecd1362
LP
311 _exit(EXIT_FAILURE);
312 }
313
b453c447 314 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
eecd1362 315 }
eecd1362 316}
b453c447
ZJS
317
318DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);