logd: add support for sending log messages as replies
[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         blob_buf_init(b, 0);
75         blobmsg_add_string(b, "msg", l->data);
76         blobmsg_add_u32(b, "id", l->id);
77         blobmsg_add_u32(b, "priority", l->priority);
78         blobmsg_add_u32(b, "source", l->source);
79         blobmsg_add_u64(b, "time", l->ts.tv_sec * 1000LL);
80 }
81
82 static int
83 read_log(struct ubus_context *ctx, struct ubus_object *obj,
84                 struct ubus_request_data *req, const char *method,
85                 struct blob_attr *msg)
86 {
87         struct client *cl;
88         struct blob_attr *tb[__READ_MAX];
89         struct log_head *l;
90         int count = 0;
91         int fds[2];
92         int ret;
93         bool stream = true;
94
95         if (msg) {
96                 blobmsg_parse(read_policy, __READ_MAX, tb, blob_data(msg), blob_len(msg));
97                 if (tb[READ_LINES])
98                         count = blobmsg_get_u32(tb[READ_LINES]);
99                 if (tb[READ_STREAM])
100                         stream = blobmsg_get_bool(tb[READ_STREAM]);
101         }
102         if (!stream)
103                 count = 100;
104
105         if (pipe(fds) == -1) {
106                 fprintf(stderr, "logd: failed to create pipe: %s\n", strerror(errno));
107                 return -1;
108         }
109
110         l = log_list(count, NULL);
111         if (stream) {
112                 ubus_request_set_fd(ctx, req, fds[0]);
113                 cl = calloc(1, sizeof(*cl));
114                 cl->s.stream.notify_state = client_notify_state;
115                 cl->fd = fds[1];
116                 ustream_fd_init(&cl->s, cl->fd);
117                 list_add(&cl->list, &clients);
118                 while ((!tb[READ_LINES] || count) && l) {
119                         log_fill_msg(&b, l);
120                         l = log_list(count, l);
121                         ret = ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
122                         if (ret < 0)
123                                 break;
124                 }
125         } else {
126                 while ((!tb[READ_LINES] || count) && l) {
127                         log_fill_msg(&b, l);
128                         ubus_send_reply(ctx, req, b.head);
129                         l = log_list(count, l);
130                 }
131         }
132         blob_buf_free(&b);
133         return 0;
134 }
135
136 static int
137 write_log(struct ubus_context *ctx, struct ubus_object *obj,
138                 struct ubus_request_data *req, const char *method,
139                 struct blob_attr *msg)
140 {
141         struct blob_attr *tb;
142         char *event;
143
144         if (msg) {
145                 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
146                 if (tb) {
147                         event = blobmsg_get_string(tb);
148                         log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
149                 }
150         }
151
152         return 0;
153 }
154
155 static const struct ubus_method log_methods[] = {
156         UBUS_METHOD("read", read_log, read_policy),
157         { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
158 };
159
160 static struct ubus_object_type log_object_type =
161         UBUS_OBJECT_TYPE("log", log_methods);
162
163 static struct ubus_object log_object = {
164         .name = "log",
165         .type = &log_object_type,
166         .methods = log_methods,
167         .n_methods = ARRAY_SIZE(log_methods),
168 };
169
170 void
171 ubus_notify_log(struct log_head *l)
172 {
173         struct client *c;
174
175         if (list_empty(&clients))
176                 return;
177
178         blob_buf_init(&b, 0);
179         blobmsg_add_string(&b, "msg", l->data);
180         blobmsg_add_u32(&b, "id", l->id);
181         blobmsg_add_u32(&b, "priority", l->priority);
182         blobmsg_add_u32(&b, "source", l->source);
183         blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
184
185         list_for_each_entry(c, &clients, list)
186                 ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
187
188         blob_buf_free(&b);
189 }
190
191 static void
192 ubus_connect_handler(struct ubus_context *ctx)
193 {
194         int ret;
195
196         ret = ubus_add_object(ctx, &log_object);
197         if (ret) {
198                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
199                 exit(1);
200         }
201         fprintf(stderr, "log: connected to ubus\n");
202 }
203
204 int
205 main(int argc, char **argv)
206 {
207         int ch, log_size = 16;
208
209         signal(SIGPIPE, SIG_IGN);
210         while ((ch = getopt(argc, argv, "S:")) != -1) {
211                 switch (ch) {
212                 case 'S':
213                         log_size = atoi(optarg);
214                         if (log_size < 1)
215                                 log_size = 16;
216                         break;
217                 }
218         }
219         log_size *= 1024;
220
221         uloop_init();
222         log_init(log_size);
223         conn.cb = ubus_connect_handler;
224         ubus_auto_connect(&conn);
225         uloop_run();
226         log_shutdown();
227         uloop_done();
228
229         return 0;
230 }