1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
16 #include "alloc-util.h"
17 #include "bus-error.h"
20 #include "format-util.h"
21 #include "process-util.h"
22 #include "signal-util.h"
24 #include "user-util.h"
27 /// Additional includes needed by elogind
28 #include "musl_missing.h"
30 static const char* arg_what = "idle:sleep:shutdown";
31 static const char* arg_who = NULL;
32 static const char* arg_why = "Unknown reason";
33 static const char* arg_mode = NULL;
38 } arg_action = ACTION_INHIBIT;
40 static int inhibit(sd_bus *bus, sd_bus_error *error) {
41 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
45 r = sd_bus_call_method(
47 "org.freedesktop.login1",
48 "/org/freedesktop/login1",
49 "org.freedesktop.login1.Manager",
53 "ssss", arg_what, arg_who, arg_why, arg_mode);
57 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
61 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
68 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
69 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
70 const char *what, *who, *why, *mode;
71 unsigned int uid, pid;
75 r = sd_bus_call_method(
77 "org.freedesktop.login1",
78 "/org/freedesktop/login1",
79 "org.freedesktop.login1.Manager",
87 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
89 return bus_log_parse_error(r);
91 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
92 _cleanup_free_ char *comm = NULL, *u = NULL;
94 if (arg_mode && !streq(mode, arg_mode))
97 get_process_comm(pid, &comm);
100 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
104 who, uid, strna(u), pid, strna(comm),
112 return bus_log_parse_error(r);
114 r = sd_bus_message_exit_container(reply);
116 return bus_log_parse_error(r);
118 printf("%u inhibitors listed.\n", n);
122 static void help(void) {
123 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
124 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
125 " -h --help Show this help\n"
126 " --version Show package version\n"
127 " --what=WHAT Operations to inhibit, colon separated list of:\n"
128 " shutdown, sleep, idle, handle-power-key,\n"
129 " handle-suspend-key, handle-hibernate-key,\n"
130 " handle-lid-switch\n"
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"
134 " --list List active inhibitors\n"
135 , program_invocation_short_name);
138 static int parse_argv(int argc, char *argv[]) {
149 static const struct option options[] = {
150 { "help", no_argument, NULL, 'h' },
151 { "version", no_argument, NULL, ARG_VERSION },
152 { "what", required_argument, NULL, ARG_WHAT },
153 { "who", required_argument, NULL, ARG_WHO },
154 { "why", required_argument, NULL, ARG_WHY },
155 { "mode", required_argument, NULL, ARG_MODE },
156 { "list", no_argument, NULL, ARG_LIST },
165 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
193 arg_action = ACTION_LIST;
200 assert_not_reached("Unhandled option");
203 if (arg_action == ACTION_INHIBIT && optind == argc)
204 arg_action = ACTION_LIST;
206 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
207 log_error("Missing command line to execute.");
214 int main(int argc, char *argv[]) {
215 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
216 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
219 elogind_set_program_name(argv[0]);
220 log_parse_environment();
223 r = parse_argv(argc, argv);
229 r = sd_bus_default_system(&bus);
231 log_error_errno(r, "Failed to connect to bus: %m");
235 if (arg_action == ACTION_LIST) {
237 r = print_inhibitors(bus, &error);
239 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
244 _cleanup_close_ int fd = -1;
245 _cleanup_free_ char *w = NULL;
248 /* Ignore SIGINT and allow the forked process to receive it */
249 (void) ignore_signals(SIGINT, -1);
252 arg_who = w = strv_join(argv + optind, " ");
257 fd = inhibit(bus, &error);
259 log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
263 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
268 execvp(argv[optind], argv + optind);
270 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
274 r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
275 return r < 0 ? EXIT_FAILURE : r;