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