1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "bus-error.h"
36 static const char* arg_what = "idle:sleep:shutdown";
37 static const char* arg_who = NULL;
38 static const char* arg_why = "Unknown reason";
39 static const char* arg_mode = "block";
44 } arg_action = ACTION_INHIBIT;
46 static int inhibit(sd_bus *bus, sd_bus_error *error) {
47 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
51 r = sd_bus_call_method(
53 "org.freedesktop.login1",
54 "/org/freedesktop/login1",
55 "org.freedesktop.login1.Manager",
59 "ssss", arg_what, arg_who, arg_why, arg_mode);
63 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
67 r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
74 static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
75 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
76 const char *what, *who, *why, *mode;
77 unsigned int uid, pid;
81 r = sd_bus_call_method(
83 "org.freedesktop.login1",
84 "/org/freedesktop/login1",
85 "org.freedesktop.login1.Manager",
93 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
95 return bus_log_parse_error(r);
97 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
98 _cleanup_free_ char *comm = NULL, *u = NULL;
100 get_process_comm(pid, &comm);
101 u = uid_to_name(uid);
103 printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
107 who, uid, strna(u), pid, strna(comm),
115 return bus_log_parse_error(r);
117 r = sd_bus_message_exit_container(reply);
119 return bus_log_parse_error(r);
121 printf("%u inhibitors listed.\n", n);
125 static void help(void) {
126 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
127 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
128 " -h --help Show this help\n"
129 " --version Show package version\n"
130 " --what=WHAT Operations to inhibit, colon separated list of:\n"
131 " shutdown, sleep, idle, handle-power-key,\n"
132 " handle-suspend-key, handle-hibernate-key,\n"
133 " handle-lid-switch\n"
134 " --who=STRING A descriptive string who is inhibiting\n"
135 " --why=STRING A descriptive string why is being inhibited\n"
136 " --mode=MODE One of block or delay\n"
137 " --list List active inhibitors\n"
138 , program_invocation_short_name);
141 static int parse_argv(int argc, char *argv[]) {
152 static const struct option options[] = {
153 { "help", no_argument, NULL, 'h' },
154 { "version", no_argument, NULL, ARG_VERSION },
155 { "what", required_argument, NULL, ARG_WHAT },
156 { "who", required_argument, NULL, ARG_WHO },
157 { "why", required_argument, NULL, ARG_WHY },
158 { "mode", required_argument, NULL, ARG_MODE },
159 { "list", no_argument, NULL, ARG_LIST },
168 while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
177 puts(PACKAGE_STRING);
178 puts(SYSTEMD_FEATURES);
198 arg_action = ACTION_LIST;
205 assert_not_reached("Unhandled option");
208 if (arg_action == ACTION_INHIBIT && argc == 1)
209 arg_action = ACTION_LIST;
211 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
212 log_error("Missing command line to execute.");
219 int main(int argc, char *argv[]) {
220 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
221 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
224 log_parse_environment();
227 r = parse_argv(argc, argv);
233 r = sd_bus_default_system(&bus);
235 log_error("Failed to connect to bus: %s", strerror(-r));
239 if (arg_action == ACTION_LIST) {
241 r = print_inhibitors(bus, &error);
243 log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
248 _cleanup_close_ int fd = -1;
249 _cleanup_free_ char *w = NULL;
253 arg_who = w = strv_join(argv + optind, " ");
255 fd = inhibit(bus, &error);
257 log_error("Failed to inhibit: %s", bus_error_message(&error, -r));
263 log_error("Failed to fork: %m");
270 close_all_fds(NULL, 0);
272 execvp(argv[optind], argv + optind);
273 log_error("Failed to execute %s: %m", argv[optind]);
277 r = wait_for_terminate_and_warn(argv[optind], pid);
278 return r < 0 ? EXIT_FAILURE : r;