ubox: Replace { 0 } with {}.
[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 <unistd.h>
16 #include <syslog.h>
17 #include <unistd.h>
18
19 #include <linux/types.h>
20
21 #include <libubox/uloop.h>
22 #include <libubox/blobmsg.h>
23 #include <libubox/list.h>
24 #include <libubox/ustream.h>
25 #include <libubus.h>
26
27 #include "syslog.h"
28
29 int debug = 0;
30 static struct blob_buf b;
31 static struct ubus_auto_conn conn;
32 static LIST_HEAD(clients);
33
34 enum {
35 READ_LINES,
36 READ_STREAM,
37 __READ_MAX
38 };
39
40 static const struct blobmsg_policy read_policy[__READ_MAX] = {
41 [READ_LINES] = { .name = "lines", .type = BLOBMSG_TYPE_INT32 },
42 [READ_STREAM] = { .name = "stream", .type = BLOBMSG_TYPE_BOOL },
43 };
44
45 static const struct blobmsg_policy write_policy =
46 { .name = "event", .type = BLOBMSG_TYPE_STRING };
47
48 struct client {
49 struct list_head list;
50
51 struct ustream_fd s;
52 int fd;
53 };
54
55 static void
56 client_close(struct ustream *s)
57 {
58 struct client *cl = container_of(s, struct client, s.stream);
59
60 list_del(&cl->list);
61 ustream_free(s);
62 close(cl->fd);
63 free(cl);
64 }
65
66 static void client_notify_state(struct ustream *s)
67 {
68 client_close(s);
69 }
70
71 static void
72 log_fill_msg(struct blob_buf *b, struct log_head *l)
73 {
74 blobmsg_add_string(b, "msg", l->data);
75 blobmsg_add_u32(b, "id", l->id);
76 blobmsg_add_u32(b, "priority", l->priority);
77 blobmsg_add_u32(b, "source", l->source);
78 blobmsg_add_u64(b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
79 }
80
81 static int
82 read_log(struct ubus_context *ctx, struct ubus_object *obj,
83 struct ubus_request_data *req, const char *method,
84 struct blob_attr *msg)
85 {
86 struct client *cl;
87 struct blob_attr *tb[__READ_MAX] = {};
88 struct log_head *l;
89 int count = 0;
90 int fds[2];
91 int ret;
92 bool stream = true;
93 void *c, *e;
94
95 if (!stream)
96 count = 100;
97
98 if (msg) {
99 blobmsg_parse(read_policy, __READ_MAX, tb, blob_data(msg), blob_len(msg));
100 if (tb[READ_LINES])
101 count = blobmsg_get_u32(tb[READ_LINES]);
102 if (tb[READ_STREAM])
103 stream = blobmsg_get_bool(tb[READ_STREAM]);
104 }
105
106 if (pipe(fds) == -1) {
107 fprintf(stderr, "logd: failed to create pipe: %s\n", strerror(errno));
108 return -1;
109 }
110
111 l = log_list(count, NULL);
112 if (stream) {
113 ubus_request_set_fd(ctx, req, fds[0]);
114 cl = calloc(1, sizeof(*cl));
115 cl->s.stream.notify_state = client_notify_state;
116 cl->fd = fds[1];
117 ustream_fd_init(&cl->s, cl->fd);
118 list_add(&cl->list, &clients);
119 while ((!tb[READ_LINES] || count) && l) {
120 blob_buf_init(&b, 0);
121 log_fill_msg(&b, l);
122 l = log_list(count, l);
123 ret = ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
124 if (ret < 0)
125 break;
126 }
127 } else {
128 blob_buf_init(&b, 0);
129 c = blobmsg_open_array(&b, "log");
130 while ((!tb[READ_LINES] || count) && l) {
131 e = blobmsg_open_table(&b, NULL);
132 log_fill_msg(&b, l);
133 blobmsg_close_table(&b, e);
134 l = log_list(count, l);
135 }
136 blobmsg_close_array(&b, c);
137 ubus_send_reply(ctx, req, b.head);
138 }
139 blob_buf_free(&b);
140 return 0;
141 }
142
143 static int
144 write_log(struct ubus_context *ctx, struct ubus_object *obj,
145 struct ubus_request_data *req, const char *method,
146 struct blob_attr *msg)
147 {
148 struct blob_attr *tb;
149 char *event;
150
151 if (msg) {
152 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
153 if (tb) {
154 event = blobmsg_get_string(tb);
155 log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
156 }
157 }
158
159 return 0;
160 }
161
162 static const struct ubus_method log_methods[] = {
163 UBUS_METHOD("read", read_log, read_policy),
164 { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
165 };
166
167 static struct ubus_object_type log_object_type =
168 UBUS_OBJECT_TYPE("log", log_methods);
169
170 static struct ubus_object log_object = {
171 .name = "log",
172 .type = &log_object_type,
173 .methods = log_methods,
174 .n_methods = ARRAY_SIZE(log_methods),
175 };
176
177 void
178 ubus_notify_log(struct log_head *l)
179 {
180 struct client *c;
181
182 if (list_empty(&clients))
183 return;
184
185 blob_buf_init(&b, 0);
186 blobmsg_add_string(&b, "msg", l->data);
187 blobmsg_add_u32(&b, "id", l->id);
188 blobmsg_add_u32(&b, "priority", l->priority);
189 blobmsg_add_u32(&b, "source", l->source);
190 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
191
192 list_for_each_entry(c, &clients, list)
193 ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
194
195 blob_buf_free(&b);
196 }
197
198 static void
199 ubus_connect_handler(struct ubus_context *ctx)
200 {
201 int ret;
202
203 ret = ubus_add_object(ctx, &log_object);
204 if (ret) {
205 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
206 exit(1);
207 }
208 fprintf(stderr, "log: connected to ubus\n");
209 }
210
211 int
212 main(int argc, char **argv)
213 {
214 int ch, log_size = 16;
215
216 signal(SIGPIPE, SIG_IGN);
217 while ((ch = getopt(argc, argv, "S:")) != -1) {
218 switch (ch) {
219 case 'S':
220 log_size = atoi(optarg);
221 if (log_size < 1)
222 log_size = 16;
223 break;
224 }
225 }
226 log_size *= 1024;
227
228 uloop_init();
229 log_init(log_size);
230 conn.cb = ubus_connect_handler;
231 ubus_auto_connect(&conn);
232 uloop_run();
233 log_shutdown();
234 uloop_done();
235 ubus_auto_shutdown(&conn);
236
237 return 0;
238 }