]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/inhibit.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / login / inhibit.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
eecd1362 2/***
eecd1362 3 Copyright 2012 Lennart Poettering
eecd1362
LP
4***/
5
3f6fd1ba 6#include <fcntl.h>
eecd1362 7#include <getopt.h>
eecd1362 8#include <stdio.h>
3f6fd1ba 9#include <stdlib.h>
eecd1362
LP
10#include <unistd.h>
11
0fb0c56f 12#include "sd-bus.h"
3f6fd1ba 13
b5efdb8a 14#include "alloc-util.h"
0fb0c56f 15#include "bus-error.h"
3f6fd1ba 16#include "bus-util.h"
3ffd4af2 17#include "fd-util.h"
f97b34a6 18#include "format-util.h"
f3c9133c 19#include "pager.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;
f3c9133c 30static bool arg_no_pager = false;
eecd1362
LP
31
32static enum {
33 ACTION_INHIBIT,
34 ACTION_LIST
35} arg_action = ACTION_INHIBIT;
36
0fb0c56f 37static int inhibit(sd_bus *bus, sd_bus_error *error) {
4afd3348 38 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3137e0bd 39 int r;
0fb0c56f 40 int fd;
eecd1362 41
0fb0c56f 42 r = sd_bus_call_method(
b9c26b41 43 bus,
eecd1362
LP
44 "org.freedesktop.login1",
45 "/org/freedesktop/login1",
46 "org.freedesktop.login1.Manager",
b9c26b41 47 "Inhibit",
0fb0c56f 48 error,
b9c26b41 49 &reply,
0fb0c56f 50 "ssss", arg_what, arg_who, arg_why, arg_mode);
3137e0bd
LP
51 if (r < 0)
52 return r;
eecd1362 53
0fb0c56f
TG
54 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
55 if (r < 0)
5b30bef8 56 return r;
eecd1362 57
ead34950 58 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
0fb0c56f
TG
59 if (r < 0)
60 return -errno;
61
3137e0bd 62 return r;
eecd1362
LP
63}
64
0fb0c56f 65static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
4afd3348 66 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
0fb0c56f
TG
67 const char *what, *who, *why, *mode;
68 unsigned int uid, pid;
eecd1362 69 unsigned n = 0;
eecd1362
LP
70 int r;
71
f3c9133c
DT
72 (void) pager_open(arg_no_pager, false);
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",
0fb0c56f 80 error,
b9c26b41 81 &reply,
0fb0c56f 82 "");
3137e0bd 83 if (r < 0)
4654e558 84 return r;
eecd1362 85
0fb0c56f
TG
86 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
87 if (r < 0)
40be0704 88 return bus_log_parse_error(r);
eecd1362 89
0fb0c56f 90 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
391a4f72 91 _cleanup_free_ char *comm = NULL, *u = NULL;
eecd1362 92
ca5447c0
MM
93 if (arg_mode && !streq(mode, arg_mode))
94 continue;
95
391a4f72
LP
96 get_process_comm(pid, &comm);
97 u = uid_to_name(uid);
98
de0671ee 99 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
41330ddb
MM
100 " What: %s\n"
101 " Why: %s\n"
102 " Mode: %s\n\n",
de0671ee 103 who, uid, strna(u), pid, strna(comm),
41330ddb
MM
104 what,
105 why,
106 mode);
eecd1362 107
eecd1362
LP
108 n++;
109 }
0fb0c56f 110 if (r < 0)
40be0704 111 return bus_log_parse_error(r);
0fb0c56f
TG
112
113 r = sd_bus_message_exit_container(reply);
114 if (r < 0)
40be0704 115 return bus_log_parse_error(r);
eecd1362 116
41330ddb 117 printf("%u inhibitors listed.\n", n);
4654e558 118 return 0;
eecd1362
LP
119}
120
601185b4 121static void help(void) {
eecd1362 122 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4943c1c9 123 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
eecd1362
LP
124 " -h --help Show this help\n"
125 " --version Show package version\n"
f3c9133c 126 " --no-pager Do not pipe output into a pager\n"
12a1309e
LP
127 " --what=WHAT Operations to inhibit, colon separated list of:\n"
128 " shutdown, sleep, idle, handle-power-key,\n"
2f1bb513
СНА
129 " handle-suspend-key, handle-hibernate-key,\n"
130 " handle-lid-switch\n"
eecd1362
LP
131 " --who=STRING A descriptive string who is inhibiting\n"
132 " --why=STRING A descriptive string why is being inhibited\n"
133 " --mode=MODE One of block or delay\n"
601185b4
ZJS
134 " --list List active inhibitors\n"
135 , program_invocation_short_name);
eecd1362
LP
136}
137
138static int parse_argv(int argc, char *argv[]) {
139
140 enum {
141 ARG_VERSION = 0x100,
142 ARG_WHAT,
143 ARG_WHO,
144 ARG_WHY,
145 ARG_MODE,
146 ARG_LIST,
f3c9133c 147 ARG_NO_PAGER,
eecd1362
LP
148 };
149
150 static const struct option options[] = {
151 { "help", no_argument, NULL, 'h' },
152 { "version", no_argument, NULL, ARG_VERSION },
153 { "what", required_argument, NULL, ARG_WHAT },
154 { "who", required_argument, NULL, ARG_WHO },
155 { "why", required_argument, NULL, ARG_WHY },
156 { "mode", required_argument, NULL, ARG_MODE },
157 { "list", no_argument, NULL, ARG_LIST },
f3c9133c 158 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
eb9da376 159 {}
eecd1362
LP
160 };
161
162 int c;
163
164 assert(argc >= 0);
165 assert(argv);
166
601185b4 167 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
eecd1362
LP
168
169 switch (c) {
170
171 case 'h':
601185b4
ZJS
172 help();
173 return 0;
eecd1362
LP
174
175 case ARG_VERSION:
3f6fd1ba 176 return version();
eecd1362
LP
177
178 case ARG_WHAT:
179 arg_what = optarg;
180 break;
181
182 case ARG_WHO:
183 arg_who = optarg;
184 break;
185
186 case ARG_WHY:
187 arg_why = optarg;
188 break;
189
190 case ARG_MODE:
191 arg_mode = optarg;
192 break;
193
194 case ARG_LIST:
195 arg_action = ACTION_LIST;
196 break;
197
f3c9133c
DT
198 case ARG_NO_PAGER:
199 arg_no_pager = true;
200 break;
201
eb9da376 202 case '?':
eecd1362 203 return -EINVAL;
eb9da376
LP
204
205 default:
206 assert_not_reached("Unhandled option");
eecd1362 207 }
eecd1362 208
d9130355 209 if (arg_action == ACTION_INHIBIT && optind == argc)
2f2343c6
KS
210 arg_action = ACTION_LIST;
211
212 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
eecd1362
LP
213 log_error("Missing command line to execute.");
214 return -EINVAL;
215 }
216
217 return 1;
218}
219
220int main(int argc, char *argv[]) {
4afd3348
LP
221 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
222 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5220a6f3 223 int r;
eecd1362
LP
224
225 log_parse_environment();
226 log_open();
227
228 r = parse_argv(argc, argv);
45a7c6b5 229 if (r < 0)
0fb0c56f 230 return EXIT_FAILURE;
45a7c6b5
LP
231 if (r == 0)
232 return EXIT_SUCCESS;
eecd1362 233
76b54375 234 r = sd_bus_default_system(&bus);
0fb0c56f 235 if (r < 0) {
da927ba9 236 log_error_errno(r, "Failed to connect to bus: %m");
0fb0c56f 237 return EXIT_FAILURE;
eecd1362
LP
238 }
239
240 if (arg_action == ACTION_LIST) {
241
242 r = print_inhibitors(bus, &error);
f3c9133c 243 pager_close();
eecd1362 244 if (r < 0) {
0fb0c56f
TG
245 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
246 return EXIT_FAILURE;
eecd1362
LP
247 }
248
249 } else {
5220a6f3
LP
250 _cleanup_close_ int fd = -1;
251 _cleanup_free_ char *w = NULL;
eecd1362
LP
252 pid_t pid;
253
106f12a0
CH
254 /* Ignore SIGINT and allow the forked process to receive it */
255 (void) ignore_signals(SIGINT, -1);
256
eecd1362
LP
257 if (!arg_who)
258 arg_who = w = strv_join(argv + optind, " ");
259
ca5447c0
MM
260 if (!arg_mode)
261 arg_mode = "block";
262
eecd1362 263 fd = inhibit(bus, &error);
eecd1362 264 if (fd < 0) {
dcee0112 265 log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
0fb0c56f 266 return EXIT_FAILURE;
eecd1362
LP
267 }
268
b6e1fff1
LP
269 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
270 if (r < 0)
0fb0c56f 271 return EXIT_FAILURE;
4c253ed1 272 if (r == 0) {
eecd1362 273 /* Child */
eecd1362 274 execvp(argv[optind], argv + optind);
0b1f3c76 275 log_open();
56f64d95 276 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
eecd1362
LP
277 _exit(EXIT_FAILURE);
278 }
279
7d4904fe 280 r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
5220a6f3 281 return r < 0 ? EXIT_FAILURE : r;
eecd1362
LP
282 }
283
4c0e48f9 284 return EXIT_SUCCESS;
eecd1362 285}