logind-inhibit.c revision 527b7a421ff3927d4f3f170b1b143452e88ae1dc
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/***
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering This file is part of systemd.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Copyright 2012 Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is free software; you can redistribute it and/or modify it
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering under the terms of the GNU Lesser General Public License as published by
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering (at your option) any later version.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is distributed in the hope that it will be useful, but
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Lesser General Public License for more details.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering You should have received a copy of the GNU Lesser General Public License
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering***/
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <errno.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <fcntl.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <string.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include <unistd.h>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "util.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "mkdir.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering#include "logind-inhibit.h"
a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7Harald Hoyer#include "fileio.h"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart PoetteringInhibitor* inhibitor_new(Manager *m, const char* id) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Inhibitor *i;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(m);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i = new0(Inhibitor, 1);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!i)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->state_file = strappend("/run/systemd/inhibit/", id);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!i->state_file) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
2b6bf07dd23bb467099d213c97b3875c5e453491Zbigniew Jędrzejewski-Szmek i->id = basename(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (hashmap_put(m->inhibitors, i->id, i) < 0) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->manager = m;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->fifo_fd = -1;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return i;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringvoid inhibitor_free(Inhibitor *i) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering hashmap_remove(i->manager->inhibitors, i->id);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering inhibitor_remove_fifo(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(i->who);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering free(i->why);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->state_file) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unlink(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint inhibitor_save(Inhibitor *i) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_free_ char *temp_path = NULL;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_fclose_ FILE *f = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0aKay Sievers r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering goto finish;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = fopen_temporary(i->state_file, &f, &temp_path);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering goto finish;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fchmod(fileno(f), 0644);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fprintf(f,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "# This is private data. Do not parse.\n"
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "WHAT=%s\n"
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering "MODE=%s\n"
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek "UID="UID_FMT"\n"
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek "PID="PID_FMT"\n",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering inhibit_what_to_string(i->what),
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering inhibit_mode_to_string(i->mode),
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek i->uid,
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek i->pid);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->who) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_free_ char *cc = NULL;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering cc = cescape(i->who);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!cc)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = -ENOMEM;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering else
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fprintf(f, "WHO=%s\n", cc);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->why) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_free_ char *cc = NULL;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering cc = cescape(i->why);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!cc)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = -ENOMEM;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering else
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fprintf(f, "WHY=%s\n", cc);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->fifo_path)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fprintf(f, "FIFO=%s\n", i->fifo_path);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fflush(f);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (ferror(f) || rename(temp_path, i->state_file) < 0) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = -errno;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unlink(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unlink(temp_path);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringfinish:
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to save inhibit data %s: %m", i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint inhibitor_start(Inhibitor *i) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->started)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering dual_timestamp_get(&i->since);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s started.",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering strna(i->who), strna(i->why),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek i->pid, i->uid,
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering inhibit_mode_to_string(i->mode));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering inhibitor_save(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->started = true;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint inhibitor_stop(Inhibitor *i) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->started)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("Inhibitor %s (%s) pid="PID_FMT" uid="UID_FMT" mode=%s stopped.",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering strna(i->who), strna(i->why),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek i->pid, i->uid,
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering inhibit_mode_to_string(i->mode));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->state_file)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unlink(i->state_file);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->started = false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint inhibitor_load(Inhibitor *i) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_free_ char
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *what = NULL,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *uid = NULL,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *pid = NULL,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering *who = NULL,
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering *why = NULL,
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering *mode = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering InhibitWhat w;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering InhibitMode mm;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering char *cc;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering int r;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = parse_env_file(i->state_file, NEWLINE,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "WHAT", &what,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "UID", &uid,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "PID", &pid,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "WHO", &who,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "WHY", &why,
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering "MODE", &mode,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "FIFO", &i->fifo_path,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering NULL);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering w = what ? inhibit_what_from_string(what) : 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (w >= 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->what = w;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering mm = mode ? inhibit_mode_from_string(mode) : INHIBIT_BLOCK;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (mm >= 0)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering i->mode = mm;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn if (uid) {
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn r = parse_uid(uid, &i->uid);
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn }
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn if (pid) {
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn r = parse_pid(pid, &i->pid);
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (who) {
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering r = cunescape(who, 0, &cc);
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering if (r < 0)
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i->who);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->who = cc;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (why) {
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering r = cunescape(why, 0, &cc);
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering if (r < 0)
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i->why);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->why = cc;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->fifo_path) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int fd;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fd = inhibitor_create_fifo(i);
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering safe_close(fd);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return 0;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int inhibitor_dispatch_fifo(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering Inhibitor *i = userdata;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(s);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(fd == i->fifo_fd);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(i);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering inhibitor_stop(i);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering inhibitor_free(i);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringint inhibitor_create_fifo(Inhibitor *i) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering int r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* Create FIFO */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (!i->fifo_path) {
d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0aKay Sievers r = mkdir_safe_label("/run/systemd/inhibit", 0755, 0, 0);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering i->fifo_path = strjoin("/run/systemd/inhibit/", i->id, ".ref", NULL);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (!i->fifo_path)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -ENOMEM;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (mkfifo(i->fifo_path, 0600) < 0 && errno != EEXIST)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -errno;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* Open reading side */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->fifo_fd < 0) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->fifo_fd = open(i->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->fifo_fd < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -errno;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (!i->event_source) {
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering r = sd_event_add_io(i->manager->event, &i->event_source, i->fifo_fd, 0, inhibitor_dispatch_fifo, i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
718db96199eb307751264e4163555662c9a389faLennart Poettering r = sd_event_source_set_priority(i->event_source, SD_EVENT_PRIORITY_IDLE);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (r < 0)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* Open writing side */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = open(i->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (r < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return -errno;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return r;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringvoid inhibitor_remove_fifo(Inhibitor *i) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(i);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering i->event_source = sd_event_source_unref(i->event_source);
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering i->fifo_fd = safe_close(i->fifo_fd);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (i->fifo_path) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering unlink(i->fifo_path);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering free(i->fifo_path);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering i->fifo_path = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart PoetteringInhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Inhibitor *i;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Iterator j;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering InhibitWhat what = 0;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert(m);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering HASHMAP_FOREACH(i, m->inhibitors, j)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (i->mode == mm)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering what |= i->what;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return what;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poetteringstatic int pid_is_active(Manager *m, pid_t pid) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering Session *s;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering int r;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering r = manager_get_session_by_pid(m, pid, &s);
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering if (r < 0)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering return r;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering /* If there's no session assigned to it, then it's globally
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering * active on all ttys */
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering if (r == 0)
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering return 1;
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering return session_is_active(s);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering}
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poetteringbool manager_is_inhibited(
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering Manager *m,
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering InhibitWhat w,
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering InhibitMode mm,
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering dual_timestamp *since,
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering bool ignore_inactive,
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering bool ignore_uid,
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering uid_t uid,
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering Inhibitor **offending) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering Inhibitor *i;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering Iterator j;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering struct dual_timestamp ts = { 0, 0 };
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering bool inhibited = false;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering assert(m);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering assert(w > 0 && w < _INHIBIT_WHAT_MAX);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering HASHMAP_FOREACH(i, m->inhibitors, j) {
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering if (!(i->what & w))
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering continue;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (i->mode != mm)
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering continue;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering if (ignore_inactive && pid_is_active(m, i->pid) <= 0)
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering continue;
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering if (ignore_uid && i->uid == uid)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering continue;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering if (!inhibited ||
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering i->since.monotonic < ts.monotonic)
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering ts = i->since;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering inhibited = true;
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering if (offending)
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering *offending = i;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering }
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering if (since)
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering *since = ts;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering return inhibited;
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering}
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringconst char *inhibit_what_to_string(InhibitWhat w) {
ec202eae8e84a4c99f054f771cb832046cb8769fShawn Landden static thread_local char buffer[97];
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering char *p;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (w < 0 || w >= _INHIBIT_WHAT_MAX)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = buffer;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (w & INHIBIT_SHUTDOWN)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = stpcpy(p, "shutdown:");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (w & INHIBIT_SLEEP)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = stpcpy(p, "sleep:");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (w & INHIBIT_IDLE)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = stpcpy(p, "idle:");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (w & INHIBIT_HANDLE_POWER_KEY)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = stpcpy(p, "handle-power-key:");
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering if (w & INHIBIT_HANDLE_SUSPEND_KEY)
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering p = stpcpy(p, "handle-suspend-key:");
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering p = stpcpy(p, "handle-hibernate-key:");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (w & INHIBIT_HANDLE_LID_SWITCH)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering p = stpcpy(p, "handle-lid-switch:");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (p > buffer)
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering *(p-1) = 0;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering else
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering *p = 0;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering return buffer;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart PoetteringInhibitWhat inhibit_what_from_string(const char *s) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering InhibitWhat what = 0;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek const char *word, *state;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering size_t l;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek FOREACH_WORD_SEPARATOR(word, l, s, ":", state) {
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek if (l == 8 && strneq(word, "shutdown", l))
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering what |= INHIBIT_SHUTDOWN;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 5 && strneq(word, "sleep", l))
4943c1c94ba751c98763f4232b4350481b22c90aLennart Poettering what |= INHIBIT_SLEEP;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 4 && strneq(word, "idle", l))
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering what |= INHIBIT_IDLE;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 16 && strneq(word, "handle-power-key", l))
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering what |= INHIBIT_HANDLE_POWER_KEY;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 18 && strneq(word, "handle-suspend-key", l))
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering what |= INHIBIT_HANDLE_SUSPEND_KEY;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 20 && strneq(word, "handle-hibernate-key", l))
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering what |= INHIBIT_HANDLE_HIBERNATE_KEY;
a2a5291b3f5ab6ed4c92f51d0fd10a03047380d8Zbigniew Jędrzejewski-Szmek else if (l == 17 && strneq(word, "handle-lid-switch", l))
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering what |= INHIBIT_HANDLE_LID_SWITCH;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering else
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return _INHIBIT_WHAT_INVALID;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering }
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return what;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering}
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poetteringstatic const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering [INHIBIT_BLOCK] = "block",
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering [INHIBIT_DELAY] = "delay"
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering};
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering
eecd1362f7f4de432483b5d77c56726c3621a83aLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(inhibit_mode, InhibitMode);