chiark / gitweb /
minor fixes to help texts
[elogind.git] / src / logger.c
index 2e036dd77bc14153d9c3e90a5d6cbcd2b28d569e..cc394df55c37e342c67f07082f6b0523371e184d 100644 (file)
@@ -36,6 +36,7 @@
 #include "log.h"
 #include "list.h"
 #include "sd-daemon.h"
+#include "tcpwrap.h"
 
 #define STREAM_BUFFER 2048
 #define STREAMS_MAX 256
@@ -89,7 +90,7 @@ struct Stream {
         LIST_FIELDS(Stream, stream);
 };
 
-static int stream_log(Stream *s, char *p, usec_t timestamp) {
+static int stream_log(Stream *s, char *p, usec_t ts) {
 
         char header_priority[16], header_time[64], header_pid[16];
         struct iovec iovec[5];
@@ -134,7 +135,7 @@ static int stream_log(Stream *s, char *p, usec_t timestamp) {
                 time_t t;
                 struct tm *tm;
 
-                t = (time_t) (timestamp / USEC_PER_SEC);
+                t = (time_t) (ts / USEC_PER_SEC);
                 if (!(tm = localtime(&t)))
                         return -EINVAL;
 
@@ -142,7 +143,7 @@ static int stream_log(Stream *s, char *p, usec_t timestamp) {
                         return -EINVAL;
         }
 
-        snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) s->pid);
+        snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) s->pid);
         char_array_0(header_pid);
 
         zero(iovec);
@@ -177,7 +178,7 @@ static int stream_log(Stream *s, char *p, usec_t timestamp) {
         return 0;
 }
 
-static int stream_line(Stream *s, char *p, usec_t timestamp) {
+static int stream_line(Stream *s, char *p, usec_t ts) {
         int r;
 
         assert(s);
@@ -236,13 +237,13 @@ static int stream_line(Stream *s, char *p, usec_t timestamp) {
                 return 0;
 
         case STREAM_RUNNING:
-                return stream_log(s, p, timestamp);
+                return stream_log(s, p, ts);
         }
 
         assert_not_reached("Unknown stream state");
 }
 
-static int stream_scan(Stream *s, usec_t timestamp) {
+static int stream_scan(Stream *s, usec_t ts) {
         char *p;
         size_t remaining;
         int r = 0;
@@ -259,7 +260,7 @@ static int stream_scan(Stream *s, usec_t timestamp) {
 
                 *newline = 0;
 
-                if ((r = stream_line(s, p, timestamp)) >= 0) {
+                if ((r = stream_line(s, p, ts)) >= 0) {
                         remaining -= newline-p+1;
                         p = newline+1;
                 }
@@ -273,7 +274,7 @@ static int stream_scan(Stream *s, usec_t timestamp) {
         return r;
 }
 
-static int stream_process(Stream *s, usec_t timestamp) {
+static int stream_process(Stream *s, usec_t ts) {
         ssize_t l;
         int r;
         assert(s);
@@ -292,7 +293,7 @@ static int stream_process(Stream *s, usec_t timestamp) {
                 return 0;
 
         s->length += l;
-        r = stream_scan(s, timestamp);
+        r = stream_scan(s, ts);
 
         if (r < 0)
                 return r;
@@ -340,6 +341,11 @@ static int stream_new(Server *s, int server_fd) {
                 return 0;
         }
 
+        if (!socket_tcpwrap(fd, "systemd-logger")) {
+                close_nointr_nofail(fd);
+                return 0;
+        }
+
         if (!(stream = new0(Stream, 1))) {
                 close_nointr_nofail(fd);
                 return -ENOMEM;
@@ -501,10 +507,10 @@ static int process_event(Server *s, struct epoll_event *ev) {
                 }
 
         } else {
-                usec_t timestamp;
+                usec_t ts;
                 Stream *stream = ev->data.ptr;
 
-                timestamp = now(CLOCK_REALTIME);
+                ts = now(CLOCK_REALTIME);
 
                 if (!(ev->events & EPOLLIN)) {
                         log_info("Got invalid event from epoll. (2)");
@@ -512,7 +518,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
                         return 0;
                 }
 
-                if ((r = stream_process(stream, timestamp)) <= 0) {
+                if ((r = stream_process(stream, ts)) <= 0) {
 
                         if (r < 0)
                                 log_info("Got error on stream: %s", strerror(-r));
@@ -529,10 +535,20 @@ int main(int argc, char *argv[]) {
         Server server;
         int r = 3, n;
 
+        if (getppid() != 1) {
+                log_error("This program should be invoked by init only.");
+                return 1;
+        }
+
+        if (argc > 1) {
+                log_error("This program does not take arguments.");
+                return 1;
+        }
+
         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
         log_parse_environment();
 
-        log_info("systemd-logger running as pid %llu", (unsigned long long) getpid());
+        log_info("systemd-logger running as pid %lu", (unsigned long) getpid());
 
         if ((n = sd_listen_fds(true)) < 0) {
                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
@@ -547,6 +563,10 @@ int main(int argc, char *argv[]) {
         if (server_init(&server, (unsigned) n) < 0)
                 return 3;
 
+        sd_notify(false,
+                  "READY=1\n"
+                  "STATUS=Processing requests...");
+
         for (;;) {
                 struct epoll_event event;
                 int k;
@@ -571,9 +591,12 @@ int main(int argc, char *argv[]) {
         r = 0;
 
 fail:
+        sd_notify(false,
+                  "STATUS=Shutting down...");
+
         server_done(&server);
 
-        log_info("systemd-logger stopped as pid %llu", (unsigned long long) getpid());
+        log_info("systemd-logger stopped as pid %lu", (unsigned long) getpid());
 
         return r;
 }