inhibit.c revision 820d3acfe924e58965d14b4711d5df31c5db199a
df8bdeb362277e8d95a74d6c097341fe97409948johnz/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz/***
df8bdeb362277e8d95a74d6c097341fe97409948johnz This file is part of systemd.
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz Copyright 2012 Lennart Poettering
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz systemd is free software; you can redistribute it and/or modify it
df8bdeb362277e8d95a74d6c097341fe97409948johnz under the terms of the GNU Lesser General Public License as published by
df8bdeb362277e8d95a74d6c097341fe97409948johnz the Free Software Foundation; either version 2.1 of the License, or
df8bdeb362277e8d95a74d6c097341fe97409948johnz (at your option) any later version.
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz systemd is distributed in the hope that it will be useful, but
df8bdeb362277e8d95a74d6c097341fe97409948johnz WITHOUT ANY WARRANTY; without even the implied warranty of
df8bdeb362277e8d95a74d6c097341fe97409948johnz MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
df8bdeb362277e8d95a74d6c097341fe97409948johnz Lesser General Public License for more details.
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz You should have received a copy of the GNU Lesser General Public License
df8bdeb362277e8d95a74d6c097341fe97409948johnz along with systemd; If not, see <http://www.gnu.org/licenses/>.
df8bdeb362277e8d95a74d6c097341fe97409948johnz***/
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <getopt.h>
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll#include <assert.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <stdlib.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <stdio.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <unistd.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include <fcntl.h>
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "sd-bus.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "bus-util.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "bus-error.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "util.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "build.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz#include "strv.h"
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic const char* arg_what = "idle:sleep:shutdown";
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic const char* arg_who = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic const char* arg_why = "Unknown reason";
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic const char* arg_mode = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic enum {
df8bdeb362277e8d95a74d6c097341fe97409948johnz ACTION_INHIBIT,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ACTION_LIST
df8bdeb362277e8d95a74d6c097341fe97409948johnz} arg_action = ACTION_INHIBIT;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic int inhibit(sd_bus *bus, sd_bus_error *error) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz int r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz int fd;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = sd_bus_call_method(
df8bdeb362277e8d95a74d6c097341fe97409948johnz bus,
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "/org/freedesktop/login1",
735564919188238196dbd0d320770dda59b38369Anthony Scarpino "org.freedesktop.login1.Manager",
735564919188238196dbd0d320770dda59b38369Anthony Scarpino "Inhibit",
735564919188238196dbd0d320770dda59b38369Anthony Scarpino error,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino &reply,
df8bdeb362277e8d95a74d6c097341fe97409948johnz "ssss", arg_what, arg_who, arg_why, arg_mode);
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
df8bdeb362277e8d95a74d6c097341fe97409948johnz return r;
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
735564919188238196dbd0d320770dda59b38369Anthony Scarpino return r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
df8bdeb362277e8d95a74d6c097341fe97409948johnz return -errno;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz return r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz}
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz const char *what, *who, *why, *mode;
df8bdeb362277e8d95a74d6c097341fe97409948johnz unsigned int uid, pid;
df8bdeb362277e8d95a74d6c097341fe97409948johnz unsigned n = 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz int r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = sd_bus_call_method(
df8bdeb362277e8d95a74d6c097341fe97409948johnz bus,
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "/org/freedesktop/login1",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1.Manager",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "ListInhibitors",
df8bdeb362277e8d95a74d6c097341fe97409948johnz error,
df8bdeb362277e8d95a74d6c097341fe97409948johnz &reply,
df8bdeb362277e8d95a74d6c097341fe97409948johnz "");
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return r;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
df8bdeb362277e8d95a74d6c097341fe97409948johnz return bus_log_parse_error(r);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_free_ char *comm = NULL, *u = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (arg_mode && !streq(mode, arg_mode))
df8bdeb362277e8d95a74d6c097341fe97409948johnz continue;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz get_process_comm(pid, &comm);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino u = uid_to_name(uid);
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " What: %s\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " Why: %s\n"
735564919188238196dbd0d320770dda59b38369Anthony Scarpino " Mode: %s\n\n",
735564919188238196dbd0d320770dda59b38369Anthony Scarpino who, uid, strna(u), pid, strna(comm),
735564919188238196dbd0d320770dda59b38369Anthony Scarpino what,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino why,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino mode);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
735564919188238196dbd0d320770dda59b38369Anthony Scarpino n++;
df8bdeb362277e8d95a74d6c097341fe97409948johnz }
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (r < 0)
df8bdeb362277e8d95a74d6c097341fe97409948johnz return bus_log_parse_error(r);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = sd_bus_message_exit_container(reply);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (r < 0)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return bus_log_parse_error(r);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM printf("%u inhibitors listed.\n", n);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return 0;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM}
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic void help(void) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz printf("%s [OPTIONS...] {COMMAND} ...\n\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " -h --help Show this help\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " --version Show package version\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " --what=WHAT Operations to inhibit, colon separated list of:\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " shutdown, sleep, idle, handle-power-key,\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " handle-suspend-key, handle-hibernate-key,\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " handle-lid-switch\n"
df8bdeb362277e8d95a74d6c097341fe97409948johnz " --who=STRING A descriptive string who is inhibiting\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM " --why=STRING A descriptive string why is being inhibited\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM " --mode=MODE One of block or delay\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM " --list List active inhibitors\n"
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM , program_invocation_short_name);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM}
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMstatic int parse_argv(int argc, char *argv[]) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM enum {
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_VERSION = 0x100,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_WHAT,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_WHO,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_WHY,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_MODE,
df8bdeb362277e8d95a74d6c097341fe97409948johnz ARG_LIST,
df8bdeb362277e8d95a74d6c097341fe97409948johnz };
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz static const struct option options[] = {
735564919188238196dbd0d320770dda59b38369Anthony Scarpino { "help", no_argument, NULL, 'h' },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "version", no_argument, NULL, ARG_VERSION },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "what", required_argument, NULL, ARG_WHAT },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "who", required_argument, NULL, ARG_WHO },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "why", required_argument, NULL, ARG_WHY },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "mode", required_argument, NULL, ARG_MODE },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll { "list", no_argument, NULL, ARG_LIST },
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll {}
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll };
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll
735564919188238196dbd0d320770dda59b38369Anthony Scarpino int c;
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
735564919188238196dbd0d320770dda59b38369Anthony Scarpino assert(argc >= 0);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino assert(argv);
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
735564919188238196dbd0d320770dda59b38369Anthony Scarpino while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
735564919188238196dbd0d320770dda59b38369Anthony Scarpino switch (c) {
735564919188238196dbd0d320770dda59b38369Anthony Scarpino
df8bdeb362277e8d95a74d6c097341fe97409948johnz case 'h':
df8bdeb362277e8d95a74d6c097341fe97409948johnz help();
df8bdeb362277e8d95a74d6c097341fe97409948johnz return 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_VERSION:
df8bdeb362277e8d95a74d6c097341fe97409948johnz puts(PACKAGE_STRING);
df8bdeb362277e8d95a74d6c097341fe97409948johnz puts(SYSTEMD_FEATURES);
df8bdeb362277e8d95a74d6c097341fe97409948johnz return 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_WHAT:
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_what = optarg;
df8bdeb362277e8d95a74d6c097341fe97409948johnz break;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_WHO:
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_who = optarg;
df8bdeb362277e8d95a74d6c097341fe97409948johnz break;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_WHY:
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_why = optarg;
df8bdeb362277e8d95a74d6c097341fe97409948johnz break;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_MODE:
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_mode = optarg;
df8bdeb362277e8d95a74d6c097341fe97409948johnz break;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case ARG_LIST:
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_action = ACTION_LIST;
df8bdeb362277e8d95a74d6c097341fe97409948johnz break;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz case '?':
df8bdeb362277e8d95a74d6c097341fe97409948johnz return -EINVAL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz default:
df8bdeb362277e8d95a74d6c097341fe97409948johnz assert_not_reached("Unhandled option");
df8bdeb362277e8d95a74d6c097341fe97409948johnz }
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (arg_action == ACTION_INHIBIT && optind == argc)
df8bdeb362277e8d95a74d6c097341fe97409948johnz arg_action = ACTION_LIST;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz else if (arg_action == ACTION_INHIBIT && optind >= argc) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_error("Missing command line to execute.");
df8bdeb362277e8d95a74d6c097341fe97409948johnz return -EINVAL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz }
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz return 1;
df8bdeb362277e8d95a74d6c097341fe97409948johnz}
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnzint main(int argc, char *argv[]) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_close_unref_ sd_bus *bus = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz int r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_parse_environment();
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_open();
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = parse_argv(argc, argv);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (r < 0)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return EXIT_FAILURE;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (r == 0)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return EXIT_SUCCESS;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = sd_bus_default_system(&bus);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (r < 0) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_error_errno(r, "Failed to connect to bus: %m");
df8bdeb362277e8d95a74d6c097341fe97409948johnz return EXIT_FAILURE;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM }
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (arg_action == ACTION_LIST) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = print_inhibitors(bus, &error);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (r < 0) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return EXIT_FAILURE;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM }
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM } else {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM _cleanup_close_ int fd = -1;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM _cleanup_free_ char *w = NULL;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM pid_t pid;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (!arg_who)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM arg_who = w = strv_join(argv + optind, " ");
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (!arg_mode)
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM arg_mode = "block";
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
df8bdeb362277e8d95a74d6c097341fe97409948johnz fd = inhibit(bus, &error);
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (fd < 0) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error("Failed to inhibit: %s", bus_error_message(&error, -r));
df8bdeb362277e8d95a74d6c097341fe97409948johnz return EXIT_FAILURE;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM }
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM pid = fork();
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (pid < 0) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error_errno(errno, "Failed to fork: %m");
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM return EXIT_FAILURE;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM }
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM if (pid == 0) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM /* Child */
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM close_all_fds(NULL, 0);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
df8bdeb362277e8d95a74d6c097341fe97409948johnz execvp(argv[optind], argv + optind);
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
df8bdeb362277e8d95a74d6c097341fe97409948johnz _exit(EXIT_FAILURE);
df8bdeb362277e8d95a74d6c097341fe97409948johnz }
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = wait_for_terminate_and_warn(argv[optind], pid, true);
df8bdeb362277e8d95a74d6c097341fe97409948johnz return r < 0 ? EXIT_FAILURE : r;
df8bdeb362277e8d95a74d6c097341fe97409948johnz }
df8bdeb362277e8d95a74d6c097341fe97409948johnz
df8bdeb362277e8d95a74d6c097341fe97409948johnz return 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz}
df8bdeb362277e8d95a74d6c097341fe97409948johnz