2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include <linux/types.h>
20 #include <libubox/uloop.h>
21 #include <libubox/blobmsg.h>
28 static struct blob_buf b;
29 static struct ubus_context *_ctx;
30 static struct uloop_timeout ubus_timer;
32 static const struct blobmsg_policy read_policy =
33 { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
35 static const struct blobmsg_policy write_policy =
36 { .name = "event", .type = BLOBMSG_TYPE_STRING };
39 read_log(struct ubus_context *ctx, struct ubus_object *obj,
40 struct ubus_request_data *req, const char *method,
41 struct blob_attr *msg)
49 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
51 count = blobmsg_get_u32(tb);
55 lines = blobmsg_open_array(&b, "lines");
57 l = log_list(count, NULL);
60 entry = blobmsg_open_table(&b, NULL);
61 blobmsg_add_string(&b, "msg", l->data);
62 blobmsg_add_u32(&b, "id", l->id);
63 blobmsg_add_u32(&b, "priority", l->priority);
64 blobmsg_add_u32(&b, "source", l->source);
65 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
66 blobmsg_close_table(&b, entry);
67 l = log_list(count, l);
69 blobmsg_close_table(&b, lines);
70 ubus_send_reply(ctx, req, b.head);
76 write_log(struct ubus_context *ctx, struct ubus_object *obj,
77 struct ubus_request_data *req, const char *method,
78 struct blob_attr *msg)
84 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
86 event = blobmsg_get_string(tb);
87 log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
95 log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
97 notify = obj->has_subscribers;
100 static const struct ubus_method log_methods[] = {
101 { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
102 { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
105 static struct ubus_object_type log_object_type =
106 UBUS_OBJECT_TYPE("log", log_methods);
108 static struct ubus_object log_object = {
110 .type = &log_object_type,
111 .methods = log_methods,
112 .n_methods = ARRAY_SIZE(log_methods),
113 .subscribe_cb = log_subscribe_cb,
117 ubus_notify_log(struct log_head *l)
124 blob_buf_init(&b, 0);
125 blobmsg_add_u32(&b, "id", l->id);
126 blobmsg_add_u32(&b, "priority", l->priority);
127 blobmsg_add_u32(&b, "source", l->source);
128 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
130 ret = ubus_notify(_ctx, &log_object, l->data, b.head, -1);
132 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
136 ubus_reconnect_cb(struct uloop_timeout *timeout)
138 if (!ubus_reconnect(_ctx, NULL))
139 ubus_add_uloop(_ctx);
141 uloop_timeout_set(timeout, 1000);
145 ubus_disconnect_cb(struct ubus_context *ctx)
147 ubus_timer.cb = ubus_reconnect_cb;
148 uloop_timeout_set(&ubus_timer, 1000);
152 ubus_connect_cb(struct uloop_timeout *timeout)
156 _ctx = ubus_connect(NULL);
158 uloop_timeout_set(timeout, 1000);
159 fprintf(stderr, "failed to connect to ubus\n");
162 _ctx->connection_lost = ubus_disconnect_cb;
163 ret = ubus_add_object(_ctx, &log_object);
165 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
166 fprintf(stderr, "log: connected to ubus\n");
167 ubus_add_uloop(_ctx);
171 main(int argc, char **argv)
173 int ch, log_size = 0;
175 signal(SIGPIPE, SIG_IGN);
177 while ((ch = getopt(argc, argv, "S:")) != -1) {
180 log_size = atoi(optarg);
188 ubus_timer.cb = ubus_connect_cb;
189 uloop_timeout_set(&ubus_timer, 1000);