initctl.c revision e99e38bbdcca3fe5956823bdb3d38544ccf93221
/*-*- 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"
#include "manager.h"
#include "sd-daemon.h"
#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) {
static const struct {
const int runlevel;
const char *special;
} table[] = {
{ '0', SPECIAL_RUNLEVEL0_TARGET },
{ '1', SPECIAL_RUNLEVEL1_TARGET },
{ 's', SPECIAL_RUNLEVEL1_TARGET },
{ 'S', SPECIAL_RUNLEVEL1_TARGET },
{ '2', SPECIAL_RUNLEVEL2_TARGET },
{ '3', SPECIAL_RUNLEVEL3_TARGET },
{ '4', SPECIAL_RUNLEVEL4_TARGET },
{ '5', SPECIAL_RUNLEVEL5_TARGET },
{ '6', SPECIAL_RUNLEVEL6_TARGET },
};
unsigned i;
for (i = 0; i < ELEMENTSOF(table); i++)
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.Manager", "LoadUnit"))) {
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)
close_nointr_nofail(f->fd);
}
free(f);
}
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 = SD_LISTEN_FDS_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, n;
if ((n = sd_listen_fds(true)) < 0) {
return 1;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
return 2;
}
if (server_init(&server, (unsigned) 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;
}