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"
22 #include "process-util.h"
23 #include "signal-util.h"
25 #include "user-util.h"
28 /// Additional includes needed by elogind
29 #include "musl_missing.h"
31 static const char* arg_what = "idle:sleep:shutdown";
32 static const char* arg_who = NULL;
33 static const char* arg_why = "Unknown reason";
34 static const char* arg_mode = NULL;
35 static bool arg_no_pager = false;
40 } arg_action = ACTION_INHIBIT;
42 static int inhibit(sd_bus *bus, sd_bus_error *error) {
43 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
47 r = sd_bus_call_method(
49 "org.freedesktop.login1",
50 "/org/freedesktop/login1",
51 "org.freedesktop.login1.Manager",
55 "ssss", arg_what, arg_who, arg_why, arg_mode);
59 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
63 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
70 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
71 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
72 const char *what, *who, *why, *mode;
73 unsigned int uid, pid;
77 (void) pager_open(arg_no_pager, false);
79 r = sd_bus_call_method(
81 "org.freedesktop.login1",
82 "/org/freedesktop/login1",
83 "org.freedesktop.login1.Manager",
91 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
93 return bus_log_parse_error(r);
95 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
96 _cleanup_free_ char *comm = NULL, *u = NULL;
98 if (arg_mode && !streq(mode, arg_mode))
101 get_process_comm(pid, &comm);
102 u = uid_to_name(uid);
104 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
108 who, uid, strna(u), pid, strna(comm),
116 return bus_log_parse_error(r);
118 r = sd_bus_message_exit_container(reply);
120 return bus_log_parse_error(r);
122 printf("%u inhibitors listed.\n", n);
126 static void help(void) {
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 , program_invocation_short_name);
143 static int parse_argv(int argc, char *argv[]) {
155 static const struct option options[] = {
156 { "help", no_argument, NULL, 'h' },
157 { "version", no_argument, NULL, ARG_VERSION },
158 { "what", required_argument, NULL, ARG_WHAT },
159 { "who", required_argument, NULL, ARG_WHO },
160 { "why", required_argument, NULL, ARG_WHY },
161 { "mode", required_argument, NULL, ARG_MODE },
162 { "list", no_argument, NULL, ARG_LIST },
163 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
172 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
200 arg_action = ACTION_LIST;
211 assert_not_reached("Unhandled option");
214 if (arg_action == ACTION_INHIBIT && optind == argc)
215 arg_action = ACTION_LIST;
217 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
218 log_error("Missing command line to execute.");
225 int main(int argc, char *argv[]) {
226 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
227 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
230 elogind_set_program_name(argv[0]);
231 log_parse_environment();
234 r = parse_argv(argc, argv);
240 r = sd_bus_default_system(&bus);
242 log_error_errno(r, "Failed to connect to bus: %m");
246 if (arg_action == ACTION_LIST) {
248 r = print_inhibitors(bus, &error);
251 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
256 _cleanup_close_ int fd = -1;
257 _cleanup_free_ char *w = NULL;
260 /* Ignore SIGINT and allow the forked process to receive it */
261 (void) ignore_signals(SIGINT, -1);
264 arg_who = w = strv_join(argv + optind, " ");
269 fd = inhibit(bus, &error);
271 log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
275 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
280 execvp(argv[optind], argv + optind);
282 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
286 r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
287 return r < 0 ? EXIT_FAILURE : r;