/***
This file is part of systemd.
Copyright 2010 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 <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "def.h"
#include "fd-util.h"
#include "formats-util.h"
#include "initreq.h"
#include "list.h"
#include "log.h"
#include "special.h"
#include "util.h"
typedef struct Server {
int epoll_fd;
unsigned n_fifos;
bool quit;
} Server;
struct Fifo {
int fd;
};
static const struct {
const int runlevel;
const char *special;
bool isolate;
} table[] = {
{ '0', SPECIAL_POWEROFF_TARGET, false },
{ '1', SPECIAL_RESCUE_TARGET, true },
{ 's', SPECIAL_RESCUE_TARGET, true },
{ 'S', SPECIAL_RESCUE_TARGET, true },
{ '2', SPECIAL_MULTI_USER_TARGET, true },
{ '3', SPECIAL_MULTI_USER_TARGET, true },
{ '4', SPECIAL_MULTI_USER_TARGET, true },
{ '5', SPECIAL_GRAPHICAL_TARGET, true },
{ '6', SPECIAL_REBOOT_TARGET, false },
};
unsigned i;
for (i = 0; i < ELEMENTSOF(table); i++)
return SPECIAL_KEXEC_TARGET;
}
return NULL;
}
const char *target;
const char *mode;
bool isolate = false;
int r;
assert(s);
if (!target) {
return;
}
if (isolate)
mode = "isolate";
else
mode = "replace-irreversibly";
r = sd_bus_call_method(
s->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnit",
&error,
NULL,
if (r < 0) {
return;
}
}
assert(s);
log_error("Got initctl request with invalid magic. Ignoring.");
return;
}
case INIT_CMD_RUNLVL:
log_error("Got invalid runlevel. Ignoring.");
else
case 'u':
case 'U':
/* The bus connection will be
* terminated if PID 1 is reexecuted,
* hence let's just exit here, and
* rely on that we'll be restarted on
* the next request */
s->quit = true;
break;
case 'q':
case 'Q':
break;
default:
}
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;
}
}
ssize_t l;
assert(f);
sizeof(f->buffer) - f->bytes_read);
if (l <= 0) {
return 0;
}
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)
safe_close(f->fd);
}
free(f);
}
assert(s);
while (s->fifos)
safe_close(s->epoll_fd);
if (s->bus) {
sd_bus_flush(s->bus);
sd_bus_unref(s->bus);
}
}
int r;
unsigned i;
assert(s);
zero(*s);
if (s->epoll_fd < 0) {
r = log_error_errno(errno,
"Failed to create epoll object: %m");
goto fail;
}
for (i = 0; i < n_sockets; i++) {
Fifo *f;
int fd;
fd = SD_LISTEN_FDS_START+i;
if (r < 0) {
log_error_errno(r, "Failed to determine file descriptor type: %m");
goto fail;
}
if (!r) {
log_error("Wrong file descriptor type.");
r = -EINVAL;
goto fail;
}
if (!f) {
r = -ENOMEM;
goto fail;
}
f->fd = -1;
r = -errno;
fifo_free(f);
goto fail;
}
f->server = s;
s->n_fifos ++;
}
r = bus_connect_system_systemd(&s->bus);
if (r < 0) {
log_error_errno(r, "Failed to get D-Bus connection: %m");
r = -EIO;
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;
}
r = fifo_process(f);
if (r < 0) {
log_info_errno(r, "Got error on fifo: %m");
fifo_free(f);
return r;
}
return 0;
}
int r = EXIT_FAILURE, n;
if (getppid() != 1) {
log_error("This program should be invoked by init only.");
return EXIT_FAILURE;
}
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
n = sd_listen_fds(true);
if (n < 0) {
log_error_errno(r, "Failed to read listening file descriptors from environment: %m");
return EXIT_FAILURE;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
return EXIT_FAILURE;
}
if (server_init(&server, (unsigned) n) < 0)
return EXIT_FAILURE;
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
int k;
if (k < 0) {
continue;
goto fail;
}
if (k <= 0)
break;
goto fail;
}
r = EXIT_SUCCESS;
fail:
sd_notify(false,
"STOPPING=1\n"
"STATUS=Shutting down...");
return r;
}