logread: cleanup pid file handling
[project/ubox.git] / log / logread.c
index edac1d9..c4ab429 100644 (file)
@@ -78,7 +78,7 @@ static void log_handle_reconnect(struct uloop_timeout *timeout)
 {
        sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
        if (sender.fd < 0) {
-               fprintf(stderr, "failed to connect: %s\n", strerror(errno));
+               fprintf(stderr, "failed to connect: %m\n");
                uloop_timeout_set(&retry, 1000);
        } else {
                uloop_fd_add(&sender, ULOOP_READ);
@@ -100,10 +100,9 @@ static int log_notify(struct blob_attr *msg)
 {
        struct blob_attr *tb[__LOG_MAX];
        struct stat s;
-       char buf[512];
+       char buf[LOG_LINE_SIZE + 128];
        char buf_ts[32];
        uint32_t p;
-       char *str;
        time_t t;
        uint32_t t_ms = 0;
        char *c, *m;
@@ -127,7 +126,7 @@ static int log_notify(struct blob_attr *msg)
                }
                sender.fd = open(log_file, O_CREAT | O_WRONLY | O_APPEND, 0600);
                if (sender.fd < 0) {
-                       fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
+                       fprintf(stderr, "failed to open %s: %m\n", log_file);
                        exit(-1);
                }
        }
@@ -145,7 +144,7 @@ static int log_notify(struct blob_attr *msg)
        c = ctime(&t);
        p = blobmsg_get_u32(tb[LOG_PRIO]);
        c[strlen(c) - 1] = '\0';
-       str = blobmsg_format_json(msg, true);
+
        if (log_type == LOG_NET) {
                int err;
 
@@ -191,7 +190,6 @@ static int log_notify(struct blob_attr *msg)
                ret = write(sender.fd, buf, strlen(buf));
        }
 
-       free(str);
        if (log_type == LOG_FILE)
                fsync(sender.fd);
 
@@ -236,8 +234,11 @@ static void logread_fd_data_cb(struct ustream *s, int bytes)
                log_notify(a);
                ustream_consume(s, cur_len);
        }
-       if (!log_follow)
-               uloop_end();
+}
+
+static void logread_fd_state_cb(struct ustream *s)
+{
+       uloop_end();
 }
 
 static void logread_fd_cb(struct ubus_request *req, int fd)
@@ -245,6 +246,7 @@ static void logread_fd_cb(struct ubus_request *req, int fd)
        static struct ustream_fd test_fd;
 
        test_fd.stream.notify_read = logread_fd_data_cb;
+       test_fd.stream.notify_state = logread_fd_state_cb;
        ustream_fd_init(&test_fd, fd);
 }
 
@@ -320,6 +322,14 @@ int main(int argc, char **argv)
        }
        ubus_add_uloop(ctx);
 
+       if (log_follow && pid_file) {
+               FILE *fp = fopen(pid_file, "w+");
+               if (fp) {
+                       fprintf(fp, "%d", getpid());
+                       fclose(fp);
+               }
+       }
+
        /* ugly ugly ugly ... we need a real reconnect logic */
        do {
                ret = ubus_lookup_id(ctx, "log", &id);
@@ -331,19 +341,11 @@ int main(int argc, char **argv)
 
                blob_buf_init(&b, 0);
                blobmsg_add_u8(&b, "stream", 1);
+               blobmsg_add_u8(&b, "oneshot", !log_follow);
                if (lines)
                        blobmsg_add_u32(&b, "lines", lines);
                else if (log_follow)
                        blobmsg_add_u32(&b, "lines", 0);
-               if (log_follow) {
-                       if (pid_file) {
-                               FILE *fp = fopen(pid_file, "w+");
-                               if (fp) {
-                                       fprintf(fp, "%d", getpid());
-                                       fclose(fp);
-                               }
-                       }
-               }
 
                if (log_ip && log_port) {
                        openlog("logread", LOG_PID, LOG_DAEMON);
@@ -355,7 +357,7 @@ int main(int argc, char **argv)
                        log_type = LOG_FILE;
                        sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600);
                        if (sender.fd < 0) {
-                               fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
+                               fprintf(stderr, "failed to open %s: %m\n", log_file);
                                exit(-1);
                        }
                } else {
@@ -372,5 +374,8 @@ int main(int argc, char **argv)
 
        } while (ret && tries--);
 
+       if (log_follow && pid_file)
+               unlink(pid_file);
+
        return ret;
 }