log: allow filtering messages with a regexp pattern
[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 static const struct blobmsg_policy read_policy =
35         { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
36
37 static const struct blobmsg_policy write_policy =
38         { .name = "event", .type = BLOBMSG_TYPE_STRING };
39
40 struct client {
41         struct list_head list;
42
43         struct ustream_fd s;
44         int fd;
45 };
46
47 static void
48 client_close(struct ustream *s)
49 {
50         struct client *cl = container_of(s, struct client, s.stream);
51
52         list_del(&cl->list);
53         ustream_free(s);
54         close(cl->fd);
55         free(cl);
56 }
57
58 static void client_notify_state(struct ustream *s)
59 {
60         client_close(s);
61 }
62
63 static int
64 read_log(struct ubus_context *ctx, struct ubus_object *obj,
65                 struct ubus_request_data *req, const char *method,
66                 struct blob_attr *msg)
67 {
68         struct client *cl;
69         struct blob_attr *tb;
70         struct log_head *l;
71         int count = 0;
72         int fds[2];
73         int ret;
74
75         if (msg) {
76                 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
77                 if (tb)
78                         count = blobmsg_get_u32(tb);
79         }
80
81         if (pipe(fds) == -1) {
82                 fprintf(stderr, "logd: failed to create pipe: %s\n", strerror(errno));
83                 return -1;
84         }
85         ubus_request_set_fd(ctx, req, fds[0]);
86         cl = calloc(1, sizeof(*cl));
87         cl->s.stream.notify_state = client_notify_state;
88         cl->fd = fds[1];
89         ustream_fd_init(&cl->s, cl->fd);
90         list_add(&cl->list, &clients);
91         l = log_list(count, NULL);
92         while ((!tb || count) && l) {
93                 blob_buf_init(&b, 0);
94                 blobmsg_add_string(&b, "msg", l->data);
95                 blobmsg_add_u32(&b, "id", l->id);
96                 blobmsg_add_u32(&b, "priority", l->priority);
97                 blobmsg_add_u32(&b, "source", l->source);
98                 blobmsg_add_u64(&b, "time", l->ts.tv_sec * 1000LL);
99                 l = log_list(count, l);
100                 ret = ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
101                 blob_buf_free(&b);
102                 if (ret < 0)
103                         break;
104         }
105         return 0;
106 }
107
108 static int
109 write_log(struct ubus_context *ctx, struct ubus_object *obj,
110                 struct ubus_request_data *req, const char *method,
111                 struct blob_attr *msg)
112 {
113         struct blob_attr *tb;
114         char *event;
115
116         if (msg) {
117                 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
118                 if (tb) {
119                         event = blobmsg_get_string(tb);
120                         log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
121                 }
122         }
123
124         return 0;
125 }
126
127 static const struct ubus_method log_methods[] = {
128         { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
129         { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
130 };
131
132 static struct ubus_object_type log_object_type =
133         UBUS_OBJECT_TYPE("log", log_methods);
134
135 static struct ubus_object log_object = {
136         .name = "log",
137         .type = &log_object_type,
138         .methods = log_methods,
139         .n_methods = ARRAY_SIZE(log_methods),
140 };
141
142 void
143 ubus_notify_log(struct log_head *l)
144 {
145         struct client *c;
146
147         if (list_empty(&clients))
148                 return;
149
150         blob_buf_init(&b, 0);
151         blobmsg_add_string(&b, "msg", l->data);
152         blobmsg_add_u32(&b, "id", l->id);
153         blobmsg_add_u32(&b, "priority", l->priority);
154         blobmsg_add_u32(&b, "source", l->source);
155         blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
156
157         list_for_each_entry(c, &clients, list)
158                 ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
159
160         blob_buf_free(&b);
161 }
162
163 static void
164 ubus_connect_handler(struct ubus_context *ctx)
165 {
166         int ret;
167
168         ret = ubus_add_object(ctx, &log_object);
169         if (ret) {
170                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
171                 exit(1);
172         }
173         fprintf(stderr, "log: connected to ubus\n");
174 }
175
176 int
177 main(int argc, char **argv)
178 {
179         int ch, log_size = 16;
180
181         signal(SIGPIPE, SIG_IGN);
182         while ((ch = getopt(argc, argv, "S:")) != -1) {
183                 switch (ch) {
184                 case 'S':
185                         log_size = atoi(optarg);
186                         if (log_size < 1)
187                                 log_size = 16;
188                         break;
189                 }
190         }
191         log_size *= 1024;
192
193         uloop_init();
194         log_init(log_size);
195         conn.cb = ubus_connect_handler;
196         ubus_auto_connect(&conn);
197         uloop_run();
198         log_shutdown();
199         uloop_done();
200
201         return 0;
202 }