logind-inhibit.c revision ec202eae8e84a4c99f054f771cb832046cb8769f
/*-*- 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 <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include "mkdir.h"
#include "path-util.h"
#include "logind-inhibit.h"
#include "fileio.h"
Inhibitor *i;
assert(m);
if (!i)
return NULL;
if (!i->state_file) {
free(i);
return NULL;
}
free(i->state_file);
free(i);
return NULL;
}
i->manager = m;
i->fifo_fd = -1;
return i;
}
void inhibitor_free(Inhibitor *i) {
assert(i);
if (i->state_file) {
unlink(i->state_file);
free(i->state_file);
}
free(i);
}
int inhibitor_save(Inhibitor *i) {
int r;
assert(i);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
fprintf(f,
"# This is private data. Do not parse.\n"
"WHAT=%s\n"
"MODE=%s\n"
"UID=%lu\n"
"PID=%lu\n",
(unsigned long) i->uid,
(unsigned long) i->pid);
if (i->who) {
if (!cc)
r = -ENOMEM;
else
}
if (i->why) {
if (!cc)
r = -ENOMEM;
else
}
if (i->fifo_path)
fflush(f);
r = -errno;
unlink(i->state_file);
}
if (r < 0)
return r;
}
int inhibitor_start(Inhibitor *i) {
assert(i);
if (i->started)
return 0;
dual_timestamp_get(&i->since);
log_debug("Inhibitor %s (%s) pid=%lu uid=%lu mode=%s started.",
inhibit_mode_to_string(i->mode));
inhibitor_save(i);
i->started = true;
manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
return 0;
}
int inhibitor_stop(Inhibitor *i) {
assert(i);
if (i->started)
log_debug("Inhibitor %s (%s) pid=%lu uid=%lu mode=%s stopped.",
inhibit_mode_to_string(i->mode));
if (i->state_file)
unlink(i->state_file);
i->started = false;
manager_send_changed(i->manager, i->mode == INHIBIT_BLOCK ? "BlockInhibited" : "DelayInhibited", NULL);
return 0;
}
int inhibitor_load(Inhibitor *i) {
_cleanup_free_ char
InhibitWhat w;
char *cc;
int r;
"WHAT", &what,
"UID", &uid,
"PID", &pid,
"WHO", &who,
"WHY", &why,
"MODE", &mode,
"FIFO", &i->fifo_path,
NULL);
if (r < 0)
return r;
if (w >= 0)
i->what = w;
if (mm >= 0)
if (uid) {
if (r < 0)
return r;
}
if (pid) {
if (r < 0)
return r;
}
if (who) {
if (!cc)
return -ENOMEM;
}
if (why) {
if (!cc)
return -ENOMEM;
}
if (i->fifo_path) {
int fd;
fd = inhibitor_create_fifo(i);
if (fd >= 0)
}
return 0;
}
assert(s);
assert(i);
inhibitor_stop(i);
inhibitor_free(i);
return 0;
}
int inhibitor_create_fifo(Inhibitor *i) {
int r;
assert(i);
/* Create FIFO */
if (!i->fifo_path) {
if (r < 0)
return r;
if (!i->fifo_path)
return -ENOMEM;
return -errno;
}
/* Open reading side */
if (i->fifo_fd < 0) {
if (i->fifo_fd < 0)
return -errno;
}
if (!i->event_source) {
r = sd_event_add_io(i->manager->event, i->fifo_fd, 0, inhibitor_dispatch_fifo, i, &i->event_source);
if (r < 0)
return r;
if (r < 0)
return r;
}
/* Open writing side */
if (r < 0)
return -errno;
return r;
}
void inhibitor_remove_fifo(Inhibitor *i) {
assert(i);
if (i->event_source)
if (i->fifo_fd >= 0) {
i->fifo_fd = -1;
}
if (i->fifo_path) {
}
}
Inhibitor *i;
Iterator j;
InhibitWhat what = 0;
assert(m);
HASHMAP_FOREACH(i, m->inhibitors, j)
return what;
}
Session *s;
int r;
r = manager_get_session_by_pid(m, pid, &s);
if (r < 0)
return r;
/* If there's no session assigned to it, then it's globally
* active on all ttys */
if (r == 0)
return 1;
return session_is_active(s);
}
bool manager_is_inhibited(
Manager *m,
InhibitWhat w,
bool ignore_inactive,
bool ignore_uid,
Inhibitor *i;
Iterator j;
struct dual_timestamp ts = { 0, 0 };
bool inhibited = false;
assert(m);
assert(w > 0 && w < _INHIBIT_WHAT_MAX);
HASHMAP_FOREACH(i, m->inhibitors, j) {
if (!(i->what & w))
continue;
continue;
continue;
continue;
if (!inhibited ||
inhibited = true;
if (offending)
*offending = i;
}
if (since)
return inhibited;
}
const char *inhibit_what_to_string(InhibitWhat w) {
char *p;
if (w < 0 || w >= _INHIBIT_WHAT_MAX)
return NULL;
p = buffer;
if (w & INHIBIT_SHUTDOWN)
p = stpcpy(p, "shutdown:");
if (w & INHIBIT_SLEEP)
p = stpcpy(p, "sleep:");
if (w & INHIBIT_IDLE)
p = stpcpy(p, "idle:");
if (w & INHIBIT_HANDLE_POWER_KEY)
p = stpcpy(p, "handle-power-key:");
if (w & INHIBIT_HANDLE_SUSPEND_KEY)
p = stpcpy(p, "handle-suspend-key:");
if (w & INHIBIT_HANDLE_HIBERNATE_KEY)
p = stpcpy(p, "handle-hibernate-key:");
if (w & INHIBIT_HANDLE_LID_SWITCH)
p = stpcpy(p, "handle-lid-switch:");
if (p > buffer)
*(p-1) = 0;
else
*p = 0;
return buffer;
}
InhibitWhat inhibit_what_from_string(const char *s) {
InhibitWhat what = 0;
char *w, *state;
size_t l;
what |= INHIBIT_SHUTDOWN;
what |= INHIBIT_SLEEP;
what |= INHIBIT_IDLE;
else
return _INHIBIT_WHAT_INVALID;
}
return what;
}
static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
[INHIBIT_BLOCK] = "block",
[INHIBIT_DELAY] = "delay"
};