initctl.c revision 0b7964b804e093d31c9adc34ba1917017c7f4d60
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "util.h"
#include "log.h"
#include "list.h"
#include "initreq.h"
#define SERVER_FD_START 3
#define SERVER_FD_MAX 16
typedef struct Server {
int epoll_fd;
unsigned n_fifos;
} Server;
struct Fifo {
int fd;
struct init_request buffer;
};
static const char *translate_runlevel(int runlevel) {
switch (runlevel) {
case '0':
return "halt.target";
case '1':
case 's':
case 'S':
return "rescue.target";
case '2':
return "runlevel2.target";
case '3':
return "runlevel3.target";
case '4':
return "runlevel4.target";
case '5':
return "runlevel5.target";
case '6':
return "reboot.target";
default:
return NULL;
}
}
const char *target;
assert(s);
goto finish;
}
if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1", "GetUnit"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not attach group information to signal message.");
goto finish;
}
goto finish;
}
goto finish;
}
if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", path, "org.freedesktop.systemd1.Unit", "Start"))) {
log_error("Could not allocate message.");
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not attach group information to signal message.");
goto finish;
}
goto finish;
}
if (m)
if (reply)
}
assert(s);
log_error("Got initctl request with invalid magic. Ignoring.");
return;
}
case INIT_CMD_RUNLVL:
log_error("Got invalid runlevel. Ignoring.");
else
return;
case INIT_CMD_POWERFAIL:
case INIT_CMD_POWERFAILNOW:
case INIT_CMD_POWEROK:
log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!");
return;
case INIT_CMD_CHANGECONS:
log_warning("Received console change initctl request. This is not implemented in systemd.");
return;
case INIT_CMD_SETENV:
case INIT_CMD_UNSETENV:
log_warning("Received environment initctl request. This is not implemented in systemd.");
return;
default:
log_warning("Received unknown initctl request. Ignoring.");
return;
}
}
static int fifo_process(Fifo *f) {
ssize_t l;
assert(f);
if ((l = read(f->fd, ((uint8_t*) &f->buffer) + f->bytes_read, sizeof(f->buffer) - f->bytes_read)) <= 0) {
return 0;
return -1;
}
f->bytes_read += l;
if (f->bytes_read == sizeof(f->buffer)) {
f->bytes_read = 0;
}
return 0;
}
assert(f);
if (f->server) {
}
if (f->fd >= 0) {
if (f->server)
}
free(f);
}
static int verify_environment(unsigned *n_sockets) {
unsigned long long pid;
const char *e;
int r;
unsigned ns;
if (!(e = getenv("LISTEN_PID"))) {
log_error("Missing $LISTEN_PID environment variable.");
return -ENOENT;
}
if ((r = safe_atollu(e, &pid)) < 0) {
return r;
}
log_error("Socket nor for me.");
return -ENOENT;
}
if (!(e = getenv("LISTEN_FDS"))) {
log_error("Missing $LISTEN_FDS environment variable.");
return -ENOENT;
}
return -E2BIG;
}
log_error("Wrong number of file descriptors passed: %s", e);
return -E2BIG;
}
return 0;
}
static void server_done(Server *s) {
assert(s);
while (s->fifos)
if (s->epoll_fd >= 0)
if (s->bus)
dbus_connection_unref(s->bus);
}
int r;
unsigned i;
assert(s);
zero(*s);
r = -errno;
goto fail;
}
for (i = 0; i < n_sockets; i++) {
struct epoll_event ev;
Fifo *f;
r = -ENOMEM;
goto fail;
}
f->fd = -1;
r = -errno;
fifo_free(f);
goto fail;
}
f->fd = SERVER_FD_START+i;
f->server = s;
s->n_fifos ++;
}
goto fail;
}
return 0;
fail:
server_done(s);
return r;
}
int r;
Fifo *f;
assert(s);
log_info("Got invalid event from epoll. (3)");
return -EIO;
}
if ((r = fifo_process(f)) < 0) {
fifo_free(f);
return r;
}
return 0;
}
int r = 3;
unsigned n;
if (verify_environment(&n) < 0)
return 1;
if (server_init(&server, n) < 0)
return 2;
for (;;) {
struct epoll_event event;
int k;
&event, 1,
TIMEOUT)) < 0) {
continue;
goto fail;
}
if (k <= 0)
break;
goto fail;
}
r = 0;
fail:
return r;
}