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/>.
29 #include "dbus-common.h"
34 static const char* arg_what = "idle:sleep:shutdown";
35 static const char* arg_who = NULL;
36 static const char* arg_why = "Unknown reason";
37 static const char* arg_mode = "block";
42 } arg_action = ACTION_INHIBIT;
44 static int inhibit(DBusConnection *bus, DBusError *error) {
45 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
48 r = bus_method_call_with_reply(
50 "org.freedesktop.login1",
51 "/org/freedesktop/login1",
52 "org.freedesktop.login1.Manager",
56 DBUS_TYPE_STRING, &arg_what,
57 DBUS_TYPE_STRING, &arg_who,
58 DBUS_TYPE_STRING, &arg_why,
59 DBUS_TYPE_STRING, &arg_mode,
64 if (!dbus_message_get_args(reply, error,
65 DBUS_TYPE_UNIX_FD, &r,
72 static int print_inhibitors(DBusConnection *bus, DBusError *error) {
73 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
75 DBusMessageIter iter, sub, sub2;
78 r = bus_method_call_with_reply(
80 "org.freedesktop.login1",
81 "/org/freedesktop/login1",
82 "org.freedesktop.login1.Manager",
90 if (!dbus_message_iter_init(reply, &iter))
93 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
96 dbus_message_iter_recurse(&iter, &sub);
97 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
98 const char *what, *who, *why, *mode;
99 dbus_uint32_t uid, pid;
101 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT)
104 dbus_message_iter_recurse(&sub, &sub2);
106 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) < 0 ||
107 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) < 0 ||
108 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) < 0 ||
109 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) < 0 ||
110 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
111 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0)
114 printf(" Who: %s (UID %lu, PID %lu)\n"
118 who, (unsigned long) uid, (unsigned long) pid,
123 dbus_message_iter_next(&sub);
128 printf("%u inhibitors listed.\n", n);
132 static int help(void) {
134 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
135 "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
136 " -h --help Show this help\n"
137 " --version Show package version\n"
138 " --what=WHAT Operations to inhibit, colon separated list of:\n"
139 " shutdown, sleep, idle, handle-power-key,\n"
140 " handle-suspend-key, handle-hibernate-key,\n"
141 " handle-lid-switch\n"
142 " --who=STRING A descriptive string who is inhibiting\n"
143 " --why=STRING A descriptive string why is being inhibited\n"
144 " --mode=MODE One of block or delay\n"
145 " --list List active inhibitors\n",
146 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) {
187 puts(PACKAGE_STRING);
188 puts(SYSTEMD_FEATURES);
208 arg_action = ACTION_LIST;
212 log_error("Unknown option code %c", c);
217 if (arg_action == ACTION_INHIBIT && argc == 1)
218 arg_action = ACTION_LIST;
220 else if (arg_action == ACTION_INHIBIT && optind >= argc) {
221 log_error("Missing command line to execute.");
228 int main(int argc, char *argv[]) {
229 int r, exit_code = 0;
230 DBusConnection *bus = NULL;
232 int _cleanup_close_ fd = -1;
234 dbus_error_init(&error);
236 log_parse_environment();
239 r = parse_argv(argc, argv);
243 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
245 log_error("Failed to connect to bus: %s", bus_error_message(&error));
250 if (arg_action == ACTION_LIST) {
252 r = print_inhibitors(bus, &error);
254 log_error("Failed to list inhibitors: %s", bus_error_message_or_strerror(&error, -r));
263 arg_who = w = strv_join(argv + optind, " ");
265 fd = inhibit(bus, &error);
269 log_error("Failed to inhibit: %s", bus_error_message_or_strerror(&error, -r));
276 log_error("Failed to fork: %m");
284 close_nointr_nofail(fd);
285 close_all_fds(NULL, 0);
287 execvp(argv[optind], argv + optind);
288 log_error("Failed to execute %s: %m", argv[optind]);
292 r = wait_for_terminate_and_warn(argv[optind], pid);
299 dbus_connection_close(bus);
300 dbus_connection_unref(bus);
303 dbus_error_free(&error);
305 return r < 0 ? EXIT_FAILURE : exit_code;