2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/types.h>
17 #include <libubox/uloop.h>
18 #include <libubox/blobmsg_json.h>
23 static struct ubus_subscriber log_event;
26 struct ubus_context *_ctx;
27 static struct blob_buf b;
29 static const struct blobmsg_policy read_policy =
30 { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
32 static const struct blobmsg_policy write_policy =
33 { .name = "event", .type = BLOBMSG_TYPE_STRING };
35 static int read_log(struct ubus_context *ctx, struct ubus_object *obj,
36 struct ubus_request_data *req, const char *method,
37 struct blob_attr *msg)
45 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
47 count = blobmsg_get_u32(tb);
51 lines = blobmsg_open_array(&b, "lines");
53 l = log_list(count, NULL);
56 entry = blobmsg_open_table(&b, NULL);
57 blobmsg_add_string(&b, "msg", l->data);
58 blobmsg_add_u32(&b, "id", l->id);
59 blobmsg_add_u32(&b, "priority", l->priority);
60 blobmsg_add_u32(&b, "source", l->source);
61 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
62 blobmsg_close_table(&b, entry);
63 l = log_list(count, l);
65 blobmsg_close_table(&b, lines);
66 ubus_send_reply(ctx, req, b.head);
71 static int write_log(struct ubus_context *ctx, struct ubus_object *obj,
72 struct ubus_request_data *req, const char *method,
73 struct blob_attr *msg)
79 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
81 event = blobmsg_get_string(tb);
82 log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
89 static void log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
91 notify = obj->has_subscribers;
94 static const struct ubus_method log_methods[] = {
95 { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
96 { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
99 static struct ubus_object_type log_object_type =
100 UBUS_OBJECT_TYPE("log", log_methods);
102 static struct ubus_object log_object = {
104 .type = &log_object_type,
105 .methods = log_methods,
106 .n_methods = ARRAY_SIZE(log_methods),
107 .subscribe_cb = log_subscribe_cb,
110 void ubus_notify_log(struct log_head *l)
117 blob_buf_init(&b, 0);
118 blobmsg_add_u32(&b, "id", l->id);
119 blobmsg_add_u32(&b, "priority", l->priority);
120 blobmsg_add_u32(&b, "source", l->source);
121 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
123 ret = ubus_notify(_ctx, &log_object, l->data, b.head, -1);
125 ERROR("Failed to notify log: %s\n", ubus_strerror(ret));
128 void ubus_init_log(struct ubus_context *ctx)
134 ret = ubus_add_object(ctx, &log_object);
136 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
138 ret = ubus_register_subscriber(ctx, &log_event);
140 ERROR("Failed to add watch handler: %s\n", ubus_strerror(ret));