the cloexec logic of the watchdog was broken
[project/procd.git] / logread.c
index ba12f44..1bf3ed8 100644 (file)
--- a/logread.c
+++ b/logread.c
@@ -116,7 +116,7 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
                        rename(log_file, old);
                        free(old);
                }
-               sender.fd = open(log_file, O_CREAT | O_WRONLY | O_TRUNC);
+               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));
                        exit(-1);
@@ -129,10 +129,24 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
        c[strlen(c) - 1] = '\0';
        str = blobmsg_format_json(msg, true);
        if (log_type == LOG_NET) {
+               int err;
+
                snprintf(buf, sizeof(buf), "%s%s\n",
                        (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : ("kernel: "),
                        method);
-               send(sender.fd, buf, strlen(buf), 0);
+               if (log_udp)
+                       err = write(sender.fd, buf, strlen(buf));
+               else
+                       err = send(sender.fd, buf, strlen(buf), 0);
+
+               if (err < 0) {
+                       syslog(0, "failed to send log data to %s:%s via %s\n",
+                               log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
+                       uloop_fd_delete(&sender);
+                       close(sender.fd);
+                       sender.fd = -1;
+                       uloop_timeout_set(&retry, 1000);
+               }
        } else {
                snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
                        c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
@@ -184,7 +198,7 @@ static void follow_log(struct ubus_context *ctx, int id)
                uloop_timeout_set(&retry, 1000);
        } else if (log_file) {
                log_type = LOG_FILE;
-               sender.fd = open(log_file, O_CREAT | O_WRONLY);
+               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));
                        exit(-1);