From 12728c38621c6271dd2102d3e11cf95ff42eeff8 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Wed, 26 Feb 2014 02:57:30 +0000 Subject: [PATCH] logd: transport log data via the new ubus fd api this fixes the size limitation patch. Signed-off-by: John Crispin --- log/logd.c | 92 +++++++++++++++------- log/logread.c | 214 ++++++++++++++++++++------------------------------ 2 files changed, 150 insertions(+), 156 deletions(-) diff --git a/log/logd.c b/log/logd.c index eda3a7d..0407f4c 100644 --- a/log/logd.c +++ b/log/logd.c @@ -12,20 +12,23 @@ */ #include +#include #include #include #include #include +#include +#include #include #include "syslog.h" int debug = 0; -static int notify; static struct blob_buf b; static struct ubus_auto_conn conn; +static LIST_HEAD(clients); static const struct blobmsg_policy read_policy = { .name = "lines", .type = BLOBMSG_TYPE_INT32 }; @@ -33,15 +36,50 @@ static const struct blobmsg_policy read_policy = static const struct blobmsg_policy write_policy = { .name = "event", .type = BLOBMSG_TYPE_STRING }; +struct client { + struct list_head list; + + struct ustream_fd s; + int fd; +}; + +static void +client_close(struct ustream *s) +{ + struct client *cl = container_of(s, struct client, s.stream); + + list_del(&cl->list); + ustream_free(s); + close(cl->fd); + free(cl); +} + +static void +client_notify_write(struct ustream *s, int bytes) +{ + if (s->w.data_bytes < 128 && ustream_read_blocked(s)) + ustream_set_read_blocked(s, false); +} + +static void client_notify_state(struct ustream *s) +{ + if (!s->eof) + return; + + if (!s->w.data_bytes) + return client_close(s); +} + static int read_log(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { + struct client *cl; struct blob_attr *tb; struct log_head *l; - void *lines, *entry; int count = 0; + int fds[2]; if (msg) { blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg)); @@ -49,24 +87,27 @@ read_log(struct ubus_context *ctx, struct ubus_object *obj, count = blobmsg_get_u32(tb); } - blob_buf_init(&b, 0); - lines = blobmsg_open_array(&b, "lines"); + pipe(fds); + ubus_request_set_fd(ctx, req, fds[0]); + cl = calloc(1, sizeof(*cl)); + cl->s.stream.notify_write = client_notify_write; + cl->s.stream.notify_state = client_notify_state; + cl->fd = fds[1]; + ustream_fd_init(&cl->s, cl->fd); + list_add(&cl->list, &clients); l = log_list(count, NULL); - - while (l) { - entry = blobmsg_open_table(&b, NULL); + while ((!tb || count) && l) { + blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg", l->data); blobmsg_add_u32(&b, "id", l->id); blobmsg_add_u32(&b, "priority", l->priority); blobmsg_add_u32(&b, "source", l->source); blobmsg_add_u64(&b, "time", l->ts.tv_sec); - blobmsg_close_table(&b, entry); l = log_list(count, l); + if (ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false) <= 0) + break; } - blobmsg_close_table(&b, lines); - ubus_send_reply(ctx, req, b.head); - return 0; } @@ -89,12 +130,6 @@ write_log(struct ubus_context *ctx, struct ubus_object *obj, return 0; } -static void -log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj) -{ - notify = obj->has_subscribers; -} - static const struct ubus_method log_methods[] = { { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 }, { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 }, @@ -108,26 +143,25 @@ static struct ubus_object log_object = { .type = &log_object_type, .methods = log_methods, .n_methods = ARRAY_SIZE(log_methods), - .subscribe_cb = log_subscribe_cb, }; void ubus_notify_log(struct log_head *l) { - int ret; + struct client *c; - if (!notify) + if (list_empty(&clients)) return; - blob_buf_init(&b, 0); - blobmsg_add_u32(&b, "id", l->id); - blobmsg_add_u32(&b, "priority", l->priority); - blobmsg_add_u32(&b, "source", l->source); - blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000)); - - ret = ubus_notify(&conn.ctx, &log_object, l->data, b.head, -1); - if (ret) - fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret)); + list_for_each_entry(c, &clients, list) { + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "msg", l->data); + blobmsg_add_u32(&b, "id", l->id); + blobmsg_add_u32(&b, "priority", l->priority); + blobmsg_add_u32(&b, "source", l->source); + blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000)); + ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false); + } } static void diff --git a/log/logread.c b/log/logread.c index 8c9fda8..28c9462 100644 --- a/log/logread.c +++ b/log/logread.c @@ -25,6 +25,7 @@ #define SYSLOG_NAMES #include +#include #include #include #include @@ -54,12 +55,11 @@ static const struct blobmsg_policy log_policy[] = { [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 }, }; -static struct ubus_subscriber log_event; static struct uloop_timeout retry; static struct uloop_fd sender; static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname; static int log_type = LOG_STDOUT; -static int log_size, log_udp; +static int log_size, log_udp, log_follow = 0; static const char* getcodetext(int value, CODE *codetable) { CODE *i; @@ -83,12 +83,6 @@ static void log_handle_reconnect(struct uloop_timeout *timeout) } } -static void log_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, - uint32_t id) -{ - fprintf(stderr, "Object %08x went away\n", id); -} - static void log_handle_fd(struct uloop_fd *u, unsigned int events) { if (u->eof) { @@ -99,9 +93,7 @@ static void log_handle_fd(struct uloop_fd *u, unsigned int events) } } -static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, - struct ubus_request_data *req, const char *method, - struct blob_attr *msg) +static int log_notify(struct blob_attr *msg) { struct blob_attr *tb[__LOG_MAX]; struct stat s; @@ -109,13 +101,13 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, uint32_t p; char *str; time_t t; - char *c; + char *c, *m; if (sender.fd < 0) return 0; blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg)); - if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME]) + if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG]) return 1; if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size)) { @@ -129,11 +121,12 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, } 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: %s\n", log_file, strerror(errno)); exit(-1); } } + m = blobmsg_get_string(tb[LOG_MSG]); t = blobmsg_get_u64(tb[LOG_TIME]) / 1000; c = ctime(&t); p = blobmsg_get_u32(tb[LOG_PRIO]); @@ -151,7 +144,7 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, } if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG) strncat(buf, "kernel: ", sizeof(buf)); - strncat(buf, method, sizeof(buf)); + strncat(buf, m, sizeof(buf)); if (log_udp) err = write(sender.fd, buf, strlen(buf)); else @@ -168,8 +161,7 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, } 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); + (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m); write(sender.fd, buf, strlen(buf)); } @@ -180,104 +172,6 @@ static int log_notify(struct ubus_context *ctx, struct ubus_object *obj, return 0; } -static void follow_log(struct ubus_context *ctx, int id) -{ - FILE *fp; - int ret; - - signal(SIGPIPE, SIG_IGN); - - if (pid_file) { - fp = fopen(pid_file, "w+"); - if (fp) { - fprintf(fp, "%d", getpid()); - fclose(fp); - } - } - - uloop_init(); - ubus_add_uloop(ctx); - - log_event.remove_cb = log_handle_remove; - log_event.cb = log_notify; - ret = ubus_register_subscriber(ctx, &log_event); - if (ret) - fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); - - ret = ubus_subscribe(ctx, &log_event, id); - if (ret) - fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); - - if (log_ip && log_port) { - openlog("logread", LOG_PID, LOG_DAEMON); - log_type = LOG_NET; - sender.cb = log_handle_fd; - retry.cb = log_handle_reconnect; - uloop_timeout_set(&retry, 1000); - } else if (log_file) { - 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)); - exit(-1); - } - } else { - sender.fd = STDOUT_FILENO; - } - - uloop_run(); - ubus_free(ctx); - uloop_done(); -} - -enum { - READ_LINE, - __READ_MAX -}; - - - -static const struct blobmsg_policy read_policy[] = { - [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY }, -}; - -static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg) -{ - struct blob_attr *cur; - struct blob_attr *_tb[__READ_MAX]; - time_t t; - int rem; - - if (!msg) - return; - - blobmsg_parse(read_policy, ARRAY_SIZE(read_policy), _tb, blob_data(msg), blob_len(msg)); - if (!_tb[READ_LINE]) - return; - blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) { - struct blob_attr *tb[__LOG_MAX]; - uint32_t p; - char *c; - - if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) - continue; - - blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur)); - if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME]) - continue; - - t = blobmsg_get_u64(tb[LOG_TIME]); - p = blobmsg_get_u32(tb[LOG_PRIO]); - c = ctime(&t); - c[strlen(c) - 1] = '\0'; - - printf("%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:"), - blobmsg_get_string(tb[LOG_MSG])); - } -} - static int usage(const char *prog) { fprintf(stderr, "Usage: %s [options]\n" @@ -296,14 +190,45 @@ static int usage(const char *prog) return 1; } +static void logread_fd_data_cb(struct ustream *s, int bytes) +{ + while (true) { + int len; + struct blob_attr *a; + + a = (void*) ustream_get_read_buf(s, &len); + if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a)) + break; + log_notify(a); + ustream_consume(s, blob_len(a) + sizeof(*a)); + } + if (!log_follow) + uloop_end(); +} + +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; + ustream_fd_init(&test_fd, fd); +} + +static void logread_complete_cb(struct ubus_request *req, int ret) +{ +} + int main(int argc, char **argv) { + static struct ubus_request req; struct ubus_context *ctx; uint32_t id; const char *ubus_socket = NULL; - int ch, ret, subscribe = 0, lines = 0; + int ch, ret, lines = 0; static struct blob_buf b; - int retry = 5; + int tries = 5; + + signal(SIGPIPE, SIG_IGN); while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:P:h:")) != -1) { switch (ch) { @@ -327,7 +252,7 @@ int main(int argc, char **argv) log_prefix = optarg; break; case 'f': - subscribe = 1; + log_follow = 1; break; case 'l': lines = atoi(optarg); @@ -345,12 +270,14 @@ int main(int argc, char **argv) return usage(*argv); } } + uloop_init(); ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } + ubus_add_uloop(ctx); /* ugly ugly ugly ... we need a real reconnect logic */ do { @@ -360,16 +287,49 @@ int main(int argc, char **argv) sleep(1); continue; } - if (!subscribe || lines) { - blob_buf_init(&b, 0); - if (lines) - blobmsg_add_u32(&b, "lines", lines); - ubus_invoke(ctx, id, "read", b.head, read_cb, 0, 3000); + + blob_buf_init(&b, 0); + 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); + log_type = LOG_NET; + sender.cb = log_handle_fd; + retry.cb = log_handle_reconnect; + uloop_timeout_set(&retry, 1000); + } else if (log_file) { + 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)); + exit(-1); + } + } else { + sender.fd = STDOUT_FILENO; } - if (subscribe) - follow_log(ctx, id); - } while (ret && retry--); + ubus_invoke_async(ctx, id, "read", b.head, &req); + req.fd_cb = logread_fd_cb; + req.complete_cb = logread_complete_cb; + ubus_complete_request_async(ctx, &req); - return 0; + uloop_run(); + ubus_free(ctx); + uloop_done(); + + } while (ret && tries--); + + return ret; } -- 2.30.2