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