procd: Exit askfirst on read error
[project/procd.git] / logread.c
index fc1d0ce..bd8c697 100644 (file)
--- a/logread.c
+++ b/logread.c
@@ -58,11 +58,21 @@ static struct uloop_timeout retry;
 static struct uloop_fd sender;
 static const char *log_file, *log_ip, *log_port, *pid_file;
 static int log_type = LOG_STDOUT;
-static int log_size;
+static int log_size, log_udp;
+
+static const char* getcodetext(int value, CODE *codetable) {
+       CODE *i;
+
+       if (value >= 0)
+               for (i = codetable; i->c_val != -1; i++)
+                       if (i->c_val == value)
+                               return (i->c_name);
+       return "<unknown>";
+};
 
 static void log_handle_reconnect(struct uloop_timeout *timeout)
 {
-       sender.fd = usock(USOCK_TCP | USOCK_NUMERIC, log_ip, log_port);
+       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));
                uloop_timeout_set(&retry, 1000);
@@ -116,7 +126,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);
@@ -128,14 +138,32 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
        p = blobmsg_get_u32(tb[LOG_PRIO]);
        c[strlen(c) - 1] = '\0';
        str = blobmsg_format_json(msg, true);
-       snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
-               c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
-               (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
-               method);
-       if (log_type == LOG_NET)
-               send(sender.fd, buf, strlen(buf), 0);
-       else
+       if (log_type == LOG_NET) {
+               int err;
+
+               snprintf(buf, sizeof(buf), "%s%s\n",
+                       (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : ("kernel: "),
+                       method);
+               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, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
+                       (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
+                       method);
                write(sender.fd, buf, strlen(buf));
+       }
 
        free(str);
        if (log_type == LOG_FILE)
@@ -180,7 +208,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);
@@ -199,6 +227,8 @@ enum {
        __READ_MAX
 };
 
+
+
 static const struct blobmsg_policy read_policy[] = {
        [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
 };
@@ -234,7 +264,7 @@ static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg)
                c[strlen(c) - 1] = '\0';
 
                printf("%s %s.%s%s %s\n",
-                       c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
+                       c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
                        (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
                        blobmsg_get_string(tb[LOG_MSG]));
        }
@@ -251,6 +281,7 @@ static int usage(const char *prog)
                "    -S <bytes>         Log size\n"
                "    -p <file>          PID file\n"
                "    -f                 Follow log messages\n"
+               "    -u                 Use UDP as the protocol\n"
                "\n", prog);
        return 1;
 }
@@ -263,8 +294,11 @@ int main(int argc, char **argv)
        int ch, ret, subscribe = 0, lines = 0;
        static struct blob_buf b;
 
-       while ((ch = getopt(argc, argv, "fcs:l:r:F:p:S:")) != -1) {
+       while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:")) != -1) {
                switch (ch) {
+               case 'u':
+                       log_udp = 1;
+                       break;
                case 's':
                        ubus_socket = optarg;
                        break;