1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
30 #include "bus-error.h"
33 #include "format-util.h"
34 #include "process-util.h"
35 #include "signal-util.h"
37 #include "user-util.h"
40 /// Additional includes needed by elogind
41 #include "musl_missing.h"
43 static const char* arg_what = "idle:sleep:shutdown";
44 static const char* arg_who = NULL;
45 static const char* arg_why = "Unknown reason";
46 static const char* arg_mode = NULL;
51 } arg_action = ACTION_INHIBIT;
53 static int inhibit(sd_bus *bus, sd_bus_error *error) {
54 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
58 r = sd_bus_call_method(
60 "org.freedesktop.login1",
61 "/org/freedesktop/login1",
62 "org.freedesktop.login1.Manager",
66 "ssss", arg_what, arg_who, arg_why, arg_mode);
70 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
74 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
81 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
82 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
83 const char *what, *who, *why, *mode;
84 unsigned int uid, pid;
88 r = sd_bus_call_method(
90 "org.freedesktop.login1",
91 "/org/freedesktop/login1",
92 "org.freedesktop.login1.Manager",
100 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
102 return bus_log_parse_error(r);
104 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
105 _cleanup_free_ char *comm = NULL, *u = NULL;
107 if (arg_mode && !streq(mode, arg_mode))
110 get_process_comm(pid, &comm);
111 u = uid_to_name(uid);
113 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
117 who, uid, strna(u), pid, strna(comm),
125 return bus_log_parse_error(r);
127 r = sd_bus_message_exit_container(reply);
129 return bus_log_parse_error(r);
131 printf("%u inhibitors listed.\n", n);
135 static void help(void) {
136 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
137 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
138 " -h --help Show this help\n"
139 " --version Show package version\n"
140 " --what=WHAT Operations to inhibit, colon separated list of:\n"
141 " shutdown, sleep, idle, handle-power-key,\n"
142 " handle-suspend-key, handle-hibernate-key,\n"
143 " handle-lid-switch\n"
144 " --who=STRING A descriptive string who is inhibiting\n"
145 " --why=STRING A descriptive string why is being inhibited\n"
146 " --mode=MODE One of block or delay\n"
147 " --list List active inhibitors\n"
148 , program_invocation_short_name);
151 static int parse_argv(int argc, char *argv[]) {
162 static const struct option options[] = {
163 { "help", no_argument, NULL, 'h' },
164 { "version", no_argument, NULL, ARG_VERSION },
165 { "what", required_argument, NULL, ARG_WHAT },
166 { "who", required_argument, NULL, ARG_WHO },
167 { "why", required_argument, NULL, ARG_WHY },
168 { "mode", required_argument, NULL, ARG_MODE },
169 { "list", no_argument, NULL, ARG_LIST },
178 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
206 arg_action = ACTION_LIST;
213 assert_not_reached("Unhandled option");
216 if (arg_action == ACTION_INHIBIT && optind == argc)
217 arg_action = ACTION_LIST;
219 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
220 log_error("Missing command line to execute.");
227 int main(int argc, char *argv[]) {
228 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
229 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
232 elogind_set_program_name(argv[0]);
233 log_parse_environment();
236 r = parse_argv(argc, argv);
242 r = sd_bus_default_system(&bus);
244 log_error_errno(r, "Failed to connect to bus: %m");
248 if (arg_action == ACTION_LIST) {
250 r = print_inhibitors(bus, &error);
252 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
257 _cleanup_close_ int fd = -1;
258 _cleanup_free_ char *w = NULL;
262 arg_who = w = strv_join(argv + optind, " ");
267 fd = inhibit(bus, &error);
269 log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
273 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
278 execvp(argv[optind], argv + optind);
279 log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
283 r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
284 return r < 0 ? EXIT_FAILURE : r;