ubox: make logd use new ubus autoconnect
[project/ubox.git] / log / logd.c
1 /*
2  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
3  *
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
7  *
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.
12  */
13
14 #include <stdio.h>
15 #include <syslog.h>
16
17 #include <linux/types.h>
18
19 #include <libubox/uloop.h>
20 #include <libubox/blobmsg.h>
21 #include <libubus.h>
22
23 #include "syslog.h"
24
25 int debug = 0;
26 static int notify;
27 static struct blob_buf b;
28 static struct ubus_auto_conn conn;
29
30 static const struct blobmsg_policy read_policy =
31         { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
32
33 static const struct blobmsg_policy write_policy =
34         { .name = "event", .type = BLOBMSG_TYPE_STRING };
35
36 static int
37 read_log(struct ubus_context *ctx, struct ubus_object *obj,
38                 struct ubus_request_data *req, const char *method,
39                 struct blob_attr *msg)
40 {
41         struct blob_attr *tb;
42         struct log_head *l;
43         void *lines, *entry;
44         int count = 0;
45
46         if (msg) {
47                 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
48                 if (tb)
49                         count = blobmsg_get_u32(tb);
50         }
51
52         blob_buf_init(&b, 0);
53         lines = blobmsg_open_array(&b, "lines");
54
55         l = log_list(count, NULL);
56
57         while (l) {
58                 entry = blobmsg_open_table(&b, NULL);
59                 blobmsg_add_string(&b, "msg", l->data);
60                 blobmsg_add_u32(&b, "id", l->id);
61                 blobmsg_add_u32(&b, "priority", l->priority);
62                 blobmsg_add_u32(&b, "source", l->source);
63                 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
64                 blobmsg_close_table(&b, entry);
65                 l = log_list(count, l);
66         }
67         blobmsg_close_table(&b, lines);
68         ubus_send_reply(ctx, req, b.head);
69
70         return 0;
71 }
72
73 static int
74 write_log(struct ubus_context *ctx, struct ubus_object *obj,
75                 struct ubus_request_data *req, const char *method,
76                 struct blob_attr *msg)
77 {
78         struct blob_attr *tb;
79         char *event;
80
81         if (msg) {
82                 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
83                 if (tb) {
84                         event = blobmsg_get_string(tb);
85                         log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
86                 }
87         }
88
89         return 0;
90 }
91
92 static void
93 log_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
94 {
95         notify = obj->has_subscribers;
96 }
97
98 static const struct ubus_method log_methods[] = {
99         { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
100         { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
101 };
102
103 static struct ubus_object_type log_object_type =
104         UBUS_OBJECT_TYPE("log", log_methods);
105
106 static struct ubus_object log_object = {
107         .name = "log",
108         .type = &log_object_type,
109         .methods = log_methods,
110         .n_methods = ARRAY_SIZE(log_methods),
111         .subscribe_cb = log_subscribe_cb,
112 };
113
114 void
115 ubus_notify_log(struct log_head *l)
116 {
117         int ret;
118
119         if (!notify)
120                 return;
121
122         blob_buf_init(&b, 0);
123         blobmsg_add_u32(&b, "id", l->id);
124         blobmsg_add_u32(&b, "priority", l->priority);
125         blobmsg_add_u32(&b, "source", l->source);
126         blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
127
128         ret = ubus_notify(&conn.ctx, &log_object, l->data, b.head, -1);
129         if (ret)
130                 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
131 }
132
133 static void
134 ubus_connect_handler(struct ubus_context *ctx)
135 {
136         int ret;
137
138         ret = ubus_add_object(ctx, &log_object);
139         if (ret)
140                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
141         fprintf(stderr, "log: connected to ubus\n");
142 }
143
144 int
145 main(int argc, char **argv)
146 {
147         signal(SIGPIPE, SIG_IGN);
148
149         uloop_init();
150         log_init();
151         conn.cb = ubus_connect_handler;
152         ubus_auto_connect(&conn);
153         uloop_run();
154         log_shutdown();
155         uloop_done();
156
157         return 0;
158 }