inhibit.c revision dcee01125dde502bd8108c36ddf2026c1348865f
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "sd-bus.h"
#include "bus-util.h"
#include "bus-error.h"
#include "util.h"
#include "build.h"
#include "strv.h"
static const char* arg_what = "idle:sleep:shutdown";
static const char* arg_why = "Unknown reason";
static enum {
} arg_action = ACTION_INHIBIT;
int r;
int fd;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
&reply,
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return -errno;
return r;
}
unsigned n = 0;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListInhibitors",
&reply,
"");
if (r < 0)
return r;
if (r < 0)
return bus_log_parse_error(r);
continue;
u = uid_to_name(uid);
" What: %s\n"
" Why: %s\n"
" Mode: %s\n\n",
what,
why,
mode);
n++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
printf("%u inhibitors listed.\n", n);
return 0;
}
static void help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --what=WHAT Operations to inhibit, colon separated list of:\n"
" shutdown, sleep, idle, handle-power-key,\n"
" handle-suspend-key, handle-hibernate-key,\n"
" handle-lid-switch\n"
" --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"
" --list List active inhibitors\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_WHAT:
break;
case ARG_WHO:
break;
case ARG_WHY:
break;
case ARG_MODE:
break;
case ARG_LIST:
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Missing command line to execute.");
return -EINVAL;
}
return 1;
}
int r;
log_open();
if (r < 0)
return EXIT_FAILURE;
if (r == 0)
return EXIT_SUCCESS;
r = sd_bus_default_system(&bus);
if (r < 0) {
log_error_errno(r, "Failed to connect to bus: %m");
return EXIT_FAILURE;
}
if (arg_action == ACTION_LIST) {
if (r < 0) {
return EXIT_FAILURE;
}
} else {
_cleanup_free_ char *w = NULL;
if (!arg_who)
if (!arg_mode)
arg_mode = "block";
if (fd < 0) {
return EXIT_FAILURE;
}
if (pid < 0) {
return EXIT_FAILURE;
}
if (pid == 0) {
/* Child */
close_all_fds(NULL, 0);
}
return r < 0 ? EXIT_FAILURE : r;
}
return 0;
}