#include "list.h"
#include "initreq.h"
#include "manager.h"
+#include "sd-daemon.h"
-#define SERVER_FD_START 3
#define SERVER_FD_MAX 16
#define TIMEOUT ((int) (10*MSEC_PER_SEC))
free(f);
}
-static int verify_environment(unsigned *n_sockets) {
- unsigned long long pid;
- const char *e;
- int r;
- unsigned ns;
-
- assert_se(n_sockets);
-
- if (!(e = getenv("LISTEN_PID"))) {
- log_error("Missing $LISTEN_PID environment variable.");
- return -ENOENT;
- }
-
- if ((r = safe_atollu(e, &pid)) < 0) {
- log_error("Failed to parse $LISTEN_PID: %s", strerror(-r));
- return r;
- }
-
- if (pid != (unsigned long long) getpid()) {
- log_error("Socket nor for me.");
- return -ENOENT;
- }
-
- if (!(e = getenv("LISTEN_FDS"))) {
- log_error("Missing $LISTEN_FDS environment variable.");
- return -ENOENT;
- }
-
- if ((r = safe_atou(e, &ns)) < 0) {
- log_error("Failed to parse $LISTEN_FDS: %s", strerror(-r));
- return -E2BIG;
- }
-
- if (ns <= 0 || ns > SERVER_FD_MAX) {
- log_error("Wrong number of file descriptors passed: %s", e);
- return -E2BIG;
- }
-
- *n_sockets = ns;
-
- return 0;
-}
-
static void server_done(Server *s) {
assert(s);
zero(ev);
ev.events = EPOLLIN;
ev.data.ptr = f;
- if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SERVER_FD_START+i, &ev) < 0) {
+ if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SD_LISTEN_FDS_START+i, &ev) < 0) {
r = -errno;
fifo_free(f);
log_error("Failed to add fifo fd to epoll object: %s", strerror(errno));
goto fail;
}
- f->fd = SERVER_FD_START+i;
+ f->fd = SD_LISTEN_FDS_START+i;
LIST_PREPEND(Fifo, fifo, s->fifos, f);
f->server = s;
s->n_fifos ++;
int main(int argc, char *argv[]) {
Server server;
- int r = 3;
- unsigned n;
+ int r = 3, n;
+
+ log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+ log_parse_environment();
log_info("systemd-initctl running as pid %llu", (unsigned long long) getpid());
- if (verify_environment(&n) < 0)
+ if ((n = sd_listen_fds(true)) < 0) {
+ log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
return 1;
+ }
+
+ if (n <= 0 || n > SERVER_FD_MAX) {
+ log_error("No or too many file descriptors passed.");
+ return 2;
+ }
- if (server_init(&server, n) < 0)
+ if (server_init(&server, (unsigned) n) < 0)
return 2;
for (;;) {
#include "util.h"
#include "log.h"
#include "list.h"
+#include "sd-daemon.h"
#define STREAM_BUFFER 2048
#define STREAMS_MAX 256
-#define SERVER_FD_START 3
#define SERVER_FD_MAX 16
#define TIMEOUT ((int) (10*MSEC_PER_SEC))
return r;
}
-static int verify_environment(unsigned *n_sockets) {
- unsigned long long pid;
- const char *e;
- int r;
- unsigned ns;
-
- assert_se(n_sockets);
-
- if (!(e = getenv("LISTEN_PID"))) {
- log_error("Missing $LISTEN_PID environment variable.");
- return -ENOENT;
- }
-
- if ((r = safe_atollu(e, &pid)) < 0) {
- log_error("Failed to parse $LISTEN_PID: %s", strerror(-r));
- return r;
- }
-
- if (pid != (unsigned long long) getpid()) {
- log_error("Socket nor for me.");
- return -ENOENT;
- }
-
- if (!(e = getenv("LISTEN_FDS"))) {
- log_error("Missing $LISTEN_FDS environment variable.");
- return -ENOENT;
- }
-
- if ((r = safe_atou(e, &ns)) < 0) {
- log_error("Failed to parse $LISTEN_FDS: %s", strerror(-r));
- return -E2BIG;
- }
-
- if (ns <= 0 || ns > SERVER_FD_MAX) {
- log_error("Wrong number of file descriptors passed: %s", e);
- return -E2BIG;
- }
-
- *n_sockets = ns;
-
- return 0;
-}
-
static void server_done(Server *s) {
unsigned i;
assert(s);
stream_free(s->streams);
for (i = 0; i < s->n_server_fd; i++)
- close_nointr_nofail(SERVER_FD_START+i);
+ close_nointr_nofail(SD_LISTEN_FDS_START+i);
if (s->syslog_fd >= 0)
close_nointr_nofail(s->syslog_fd);
zero(ev);
ev.events = EPOLLIN;
- ev.data.ptr = UINT_TO_PTR(SERVER_FD_START+i);
- if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SERVER_FD_START+i, &ev) < 0) {
+ ev.data.ptr = UINT_TO_PTR(SD_LISTEN_FDS_START+i);
+ if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SD_LISTEN_FDS_START+i, &ev) < 0) {
r = -errno;
log_error("Failed to add server fd to epoll object: %s", strerror(errno));
goto fail;
assert(s);
/* Yes, this is a bit ugly, we assume that that valid pointers
- * are > SERVER_FD_START+SERVER_FD_MAX. Which is certainly
+ * are > SD_LISTEN_FDS_START+SERVER_FD_MAX. Which is certainly
* true on Linux (and probably most other OSes, too, since the
* first 4k usually are part of a seperate null pointer
* dereference page. */
- if (PTR_TO_UINT(ev->data.ptr) >= SERVER_FD_START &&
- PTR_TO_UINT(ev->data.ptr) < SERVER_FD_START+s->n_server_fd) {
+ if (PTR_TO_UINT(ev->data.ptr) >= SD_LISTEN_FDS_START &&
+ PTR_TO_UINT(ev->data.ptr) < SD_LISTEN_FDS_START+s->n_server_fd) {
if (ev->events != EPOLLIN) {
log_info("Got invalid event from epoll. (1)");
timestamp = now(CLOCK_REALTIME);
if (!(ev->events & EPOLLIN)) {
- log_info("Got invalid event from epoll. (3)");
+ log_info("Got invalid event from epoll. (2)");
stream_free(stream);
return 0;
}
int main(int argc, char *argv[]) {
Server server;
- int r = 3;
- unsigned n;
+ int r = 3, n;
+
+ log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+ log_parse_environment();
log_info("systemd-logger running as pid %llu", (unsigned long long) getpid());
- if (verify_environment(&n) < 0)
+ if ((n = sd_listen_fds(true)) < 0) {
+ log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
return 1;
+ }
- if (server_init(&server, n) < 0)
+ 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 3;
for (;;) {
struct epoll_event event;