2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <fcntl.h>
|
2012-05-05 00:34:48 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <stdlib.h>
|
2012-05-05 00:34:48 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
#include "sd-bus.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-08-14 02:54:25 +02:00
|
|
|
#include "bus-error.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "bus-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2018-05-17 15:45:37 +02:00
|
|
|
#include "pager.h"
|
2015-04-10 19:10:00 +02:00
|
|
|
#include "process-util.h"
|
2015-05-31 23:55:55 +02:00
|
|
|
#include "signal-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strv.h"
|
2018-08-09 10:32:31 +02:00
|
|
|
#include "terminal-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "util.h"
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2012-05-05 00:46:31 +02:00
|
|
|
static const char* arg_what = "idle:sleep:shutdown";
|
2012-05-05 00:34:48 +02:00
|
|
|
static const char* arg_who = NULL;
|
|
|
|
static const char* arg_why = "Unknown reason";
|
2014-11-07 15:45:53 +01:00
|
|
|
static const char* arg_mode = NULL;
|
2018-05-17 15:45:37 +02:00
|
|
|
static bool arg_no_pager = false;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
static enum {
|
|
|
|
ACTION_INHIBIT,
|
|
|
|
ACTION_LIST
|
|
|
|
} arg_action = ACTION_INHIBIT;
|
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
static int inhibit(sd_bus *bus, sd_bus_error *error) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
2012-09-10 09:38:49 +02:00
|
|
|
int r;
|
2013-08-14 02:54:25 +02:00
|
|
|
int fd;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
r = sd_bus_call_method(
|
2012-08-08 17:20:04 +02:00
|
|
|
bus,
|
2012-05-05 00:34:48 +02:00
|
|
|
"org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager",
|
2012-08-08 17:20:04 +02:00
|
|
|
"Inhibit",
|
2013-08-14 02:54:25 +02:00
|
|
|
error,
|
2012-08-08 17:20:04 +02:00
|
|
|
&reply,
|
2013-08-14 02:54:25 +02:00
|
|
|
"ssss", arg_what, arg_who, arg_why, arg_mode);
|
2012-09-10 09:38:49 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
|
|
|
|
if (r < 0)
|
2013-11-07 21:26:31 +01:00
|
|
|
return r;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2014-05-13 16:40:53 +02:00
|
|
|
r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
|
2013-08-14 02:54:25 +02:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2012-09-10 09:38:49 +02:00
|
|
|
return r;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
static int print_inhibitors(sd_bus *bus, sd_bus_error *error) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
2013-08-14 02:54:25 +02:00
|
|
|
const char *what, *who, *why, *mode;
|
|
|
|
unsigned int uid, pid;
|
2012-05-05 00:34:48 +02:00
|
|
|
unsigned n = 0;
|
|
|
|
int r;
|
|
|
|
|
2018-05-17 15:45:37 +02:00
|
|
|
(void) pager_open(arg_no_pager, false);
|
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
r = sd_bus_call_method(
|
2012-08-08 17:20:04 +02:00
|
|
|
bus,
|
2012-05-05 00:34:48 +02:00
|
|
|
"org.freedesktop.login1",
|
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager",
|
2012-08-08 17:20:04 +02:00
|
|
|
"ListInhibitors",
|
2013-08-14 02:54:25 +02:00
|
|
|
error,
|
2012-08-08 17:20:04 +02:00
|
|
|
&reply,
|
2013-08-14 02:54:25 +02:00
|
|
|
"");
|
2012-09-10 09:38:49 +02:00
|
|
|
if (r < 0)
|
2013-03-19 01:31:21 +01:00
|
|
|
return r;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
|
|
|
|
if (r < 0)
|
2013-11-07 22:08:41 +01:00
|
|
|
return bus_log_parse_error(r);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-08-14 02:54:25 +02:00
|
|
|
while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
|
2013-04-24 17:56:47 +02:00
|
|
|
_cleanup_free_ char *comm = NULL, *u = NULL;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2014-11-07 15:45:53 +01:00
|
|
|
if (arg_mode && !streq(mode, arg_mode))
|
|
|
|
continue;
|
|
|
|
|
2013-04-24 17:56:47 +02:00
|
|
|
get_process_comm(pid, &comm);
|
|
|
|
u = uid_to_name(uid);
|
|
|
|
|
2014-04-25 13:45:15 +02:00
|
|
|
printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n"
|
2013-02-17 20:00:34 +01:00
|
|
|
" What: %s\n"
|
|
|
|
" Why: %s\n"
|
|
|
|
" Mode: %s\n\n",
|
2014-04-25 13:45:15 +02:00
|
|
|
who, uid, strna(u), pid, strna(comm),
|
2013-02-17 20:00:34 +01:00
|
|
|
what,
|
|
|
|
why,
|
|
|
|
mode);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
2013-08-14 02:54:25 +02:00
|
|
|
if (r < 0)
|
2013-11-07 22:08:41 +01:00
|
|
|
return bus_log_parse_error(r);
|
2013-08-14 02:54:25 +02:00
|
|
|
|
|
|
|
r = sd_bus_message_exit_container(reply);
|
|
|
|
if (r < 0)
|
2013-11-07 22:08:41 +01:00
|
|
|
return bus_log_parse_error(r);
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-02-17 20:00:34 +01:00
|
|
|
printf("%u inhibitors listed.\n", n);
|
2013-03-19 01:31:21 +01:00
|
|
|
return 0;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-inhibit", "1", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2012-05-05 00:34:48 +02:00
|
|
|
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
|
2012-05-05 00:46:31 +02:00
|
|
|
"Execute a process while inhibiting shutdown/sleep/idle.\n\n"
|
2012-05-05 00:34:48 +02:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
2018-05-17 15:45:37 +02:00
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2012-09-19 15:28:55 +02:00
|
|
|
" --what=WHAT Operations to inhibit, colon separated list of:\n"
|
|
|
|
" shutdown, sleep, idle, handle-power-key,\n"
|
2012-10-03 03:48:19 +02:00
|
|
|
" handle-suspend-key, handle-hibernate-key,\n"
|
|
|
|
" handle-lid-switch\n"
|
2012-05-05 00:34:48 +02:00
|
|
|
" --who=STRING A descriptive string who is inhibiting\n"
|
|
|
|
" --why=STRING A descriptive string why is being inhibited\n"
|
|
|
|
" --mode=MODE One of block or delay\n"
|
2014-08-02 17:12:21 +02:00
|
|
|
" --list List active inhibitors\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_WHAT,
|
|
|
|
ARG_WHO,
|
|
|
|
ARG_WHY,
|
|
|
|
ARG_MODE,
|
|
|
|
ARG_LIST,
|
2018-05-17 15:45:37 +02:00
|
|
|
ARG_NO_PAGER,
|
2012-05-05 00:34:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "what", required_argument, NULL, ARG_WHAT },
|
|
|
|
{ "who", required_argument, NULL, ARG_WHO },
|
|
|
|
{ "why", required_argument, NULL, ARG_WHY },
|
|
|
|
{ "mode", required_argument, NULL, ARG_MODE },
|
|
|
|
{ "list", no_argument, NULL, ARG_LIST },
|
2018-05-17 15:45:37 +02:00
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2012-05-05 00:34:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
case ARG_WHAT:
|
|
|
|
arg_what = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_WHO:
|
|
|
|
arg_who = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_WHY:
|
|
|
|
arg_why = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_MODE:
|
|
|
|
arg_mode = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_LIST:
|
|
|
|
arg_action = ACTION_LIST;
|
|
|
|
break;
|
|
|
|
|
2018-05-17 15:45:37 +02:00
|
|
|
case ARG_NO_PAGER:
|
|
|
|
arg_no_pager = true;
|
|
|
|
break;
|
|
|
|
|
2013-11-06 18:28:39 +01:00
|
|
|
case '?':
|
2012-05-05 00:34:48 +02:00
|
|
|
return -EINVAL;
|
2013-11-06 18:28:39 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-10 19:24:48 +01:00
|
|
|
if (arg_action == ACTION_INHIBIT && optind == argc)
|
2013-03-05 19:32:36 +01:00
|
|
|
arg_action = ACTION_LIST;
|
|
|
|
|
|
|
|
else if (arg_action == ACTION_INHIBIT && optind >= argc) {
|
2012-05-05 00:34:48 +02:00
|
|
|
log_error("Missing command line to execute.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2013-10-30 16:13:21 +01:00
|
|
|
int r;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
2013-11-06 17:33:44 +01:00
|
|
|
if (r < 0)
|
2013-08-14 02:54:25 +02:00
|
|
|
return EXIT_FAILURE;
|
2013-11-06 17:33:44 +01:00
|
|
|
if (r == 0)
|
|
|
|
return EXIT_SUCCESS;
|
2012-05-05 00:34:48 +02:00
|
|
|
|
2013-11-11 22:00:48 +01:00
|
|
|
r = sd_bus_default_system(&bus);
|
2013-08-14 02:54:25 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to connect to bus: %m");
|
2013-08-14 02:54:25 +02:00
|
|
|
return EXIT_FAILURE;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_action == ACTION_LIST) {
|
|
|
|
|
|
|
|
r = print_inhibitors(bus, &error);
|
2018-05-17 15:45:37 +02:00
|
|
|
pager_close();
|
2012-05-05 00:34:48 +02:00
|
|
|
if (r < 0) {
|
2013-08-14 02:54:25 +02:00
|
|
|
log_error("Failed to list inhibitors: %s", bus_error_message(&error, -r));
|
|
|
|
return EXIT_FAILURE;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-10-30 16:13:21 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
_cleanup_free_ char *w = NULL;
|
2012-05-05 00:34:48 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
2018-03-28 21:58:10 +02:00
|
|
|
/* Ignore SIGINT and allow the forked process to receive it */
|
|
|
|
(void) ignore_signals(SIGINT, -1);
|
|
|
|
|
2012-05-05 00:34:48 +02:00
|
|
|
if (!arg_who)
|
|
|
|
arg_who = w = strv_join(argv + optind, " ");
|
|
|
|
|
2014-11-07 15:45:53 +01:00
|
|
|
if (!arg_mode)
|
|
|
|
arg_mode = "block";
|
|
|
|
|
2012-05-05 00:34:48 +02:00
|
|
|
fd = inhibit(bus, &error);
|
|
|
|
if (fd < 0) {
|
2015-03-07 20:23:38 +01:00
|
|
|
log_error("Failed to inhibit: %s", bus_error_message(&error, fd));
|
2013-08-14 02:54:25 +02:00
|
|
|
return EXIT_FAILURE;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 21:49:19 +01:00
|
|
|
r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
|
|
|
|
if (r < 0)
|
2013-08-14 02:54:25 +02:00
|
|
|
return EXIT_FAILURE;
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r == 0) {
|
2012-05-05 00:34:48 +02:00
|
|
|
/* Child */
|
|
|
|
execvp(argv[optind], argv + optind);
|
2018-02-22 00:35:00 +01:00
|
|
|
log_open();
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
|
2012-05-05 00:34:48 +02:00
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-12-28 00:51:19 +01:00
|
|
|
r = wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
|
2013-10-30 16:13:21 +01:00
|
|
|
return r < 0 ? EXIT_FAILURE : r;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 14:58:21 +02:00
|
|
|
return EXIT_SUCCESS;
|
2012-05-05 00:34:48 +02:00
|
|
|
}
|