logind-inhibit.c revision f981b9c5be32a199bce6335196d986f5b7e45ba6
/*-*- 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 <errno.h>
#include <unistd.h>
#include "util.h"
#include "mkdir.h"
#include "path-util.h"
#include "logind-inhibit.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;
FILE *f;
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);
}
fclose(f);
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\0" : "DelayInhibited\0");
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\0" : "DelayInhibited\0");
return 0;
}
int inhibitor_load(Inhibitor *i) {
InhibitWhat w;
int r;
char *cc,
"WHAT", &what,
"UID", &uid,
"PID", &pid,
"WHO", &who,
"WHY", &why,
"MODE", &mode,
"FIFO", &i->fifo_path,
NULL);
if (r < 0)
goto finish;
if (w >= 0)
i->what = w;
if (mm >= 0)
if (uid) {
if (r < 0)
goto finish;
}
if (pid) {
if (r < 0)
goto finish;
}
if (who) {
if (!cc) {
r = -ENOMEM;
goto finish;
}
}
if (why) {
if (!cc) {
r = -ENOMEM;
goto finish;
}
}
if (i->fifo_path) {
int fd;
fd = inhibitor_create_fifo(i);
if (fd >= 0)
}
return r;
}
int inhibitor_create_fifo(Inhibitor *i) {
int r;
assert(i);
/* Create FIFO */
if (!i->fifo_path) {
if (r < 0)
return r;
return -ENOMEM;
return -errno;
}
/* Open reading side */
if (i->fifo_fd < 0) {
struct epoll_event ev;
if (i->fifo_fd < 0)
return -errno;
if (r < 0)
return r;
return -errno;
}
/* Open writing side */
if (r < 0)
return -errno;
return r;
}
void inhibitor_remove_fifo(Inhibitor *i) {
assert(i);
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->inhibitor_fds, j)
return what;
}
Session *s;
int r;
r = manager_get_session_by_pid(m, pid, &s);
if (r <= 0)
return r;
return session_is_active(s);
}
bool manager_is_inhibited(
Manager *m,
InhibitWhat w,
bool only_active) {
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->inhibitor_fds, j) {
if (!(i->what & w))
continue;
continue;
continue;
if (!inhibited ||
inhibited = true;
}
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_SLEEP_KEY)
p = stpcpy(p, "handle-sleep-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"
};