logind-inhibit.c revision f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6
/*-*- 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 "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"
"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;
log_debug("Inhibitor %s (%s) pid=%lu uid=%lu started.",
inhibitor_save(i);
i->started = true;
return 0;
}
int inhibitor_stop(Inhibitor *i) {
assert(i);
if (i->started)
log_debug("Inhibitor %s (%s) pid=%lu uid=%lu stopped.",
if (i->state_file)
unlink(i->state_file);
i->started = false;
return 0;
}
int inhibitor_load(Inhibitor *i) {
InhibitWhat w;
int r;
char *cc,
"WHAT", &what,
"UID", &uid,
"PID", &pid,
"WHO", &who,
"WHY", &why,
"FIFO", &i->fifo_path,
NULL);
if (r < 0)
goto finish;
w = inhibit_what_from_string(what);
if (w >= 0)
i->what = w;
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;
}
const char *inhibit_what_to_string(InhibitWhat w) {
static const char* const table[_INHIBIT_WHAT_MAX] = {
[0] = "",
[INHIBIT_SHUTDOWN] = "shutdown",
[INHIBIT_SUSPEND] = "suspend",
[INHIBIT_IDLE] = "idle",
};
if (w < 0 || w >= _INHIBIT_WHAT_MAX)
return NULL;
return table[w];
}
InhibitWhat inhibit_what_from_string(const char *s) {
InhibitWhat what = 0;
char *w, *state;
size_t l;
what |= INHIBIT_SHUTDOWN;
what |= INHIBIT_SUSPEND;
what |= INHIBIT_IDLE;
else
return _INHIBIT_WHAT_INVALID;
}
return what;
}