inhibit.c revision 820d3acfe924e58965d14b4711d5df31c5db199a
df8bdeb362277e8d95a74d6c097341fe97409948johnz/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
df8bdeb362277e8d95a74d6c097341fe97409948johnz This file is part of systemd.
df8bdeb362277e8d95a74d6c097341fe97409948johnz Copyright 2012 Lennart Poettering
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 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 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/>.
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic enum {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1",
735564919188238196dbd0d320770dda59b38369Anthony Scarpino "org.freedesktop.login1.Manager",
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
df8bdeb362277e8d95a74d6c097341fe97409948johnz unsigned n = 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "org.freedesktop.login1.Manager",
df8bdeb362277e8d95a74d6c097341fe97409948johnz "ListInhibitors",
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
df8bdeb362277e8d95a74d6c097341fe97409948johnz while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
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",
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM r = sd_bus_message_exit_container(reply);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM printf("%u inhibitors listed.\n", n);
df8bdeb362277e8d95a74d6c097341fe97409948johnzstatic void help(void) {
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.COMstatic int parse_argv(int argc, char *argv[]) {
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 },
735564919188238196dbd0d320770dda59b38369Anthony Scarpino while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
df8bdeb362277e8d95a74d6c097341fe97409948johnz else if (arg_action == ACTION_INHIBIT && optind >= argc) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM arg_who = w = strv_join(argv + optind, " ");
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (fd < 0) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error("Failed to inhibit: %s", bus_error_message(&error, -r));
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM log_error_errno(errno, "Failed to fork: %m");
df8bdeb362277e8d95a74d6c097341fe97409948johnz log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
df8bdeb362277e8d95a74d6c097341fe97409948johnz r = wait_for_terminate_and_warn(argv[optind], pid, true);
df8bdeb362277e8d95a74d6c097341fe97409948johnz return r < 0 ? EXIT_FAILURE : r;