logd: transport log data via the new ubus fd api
[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
18 #include <linux/types.h>
19
20 #include <libubox/uloop.h>
21 #include <libubox/blobmsg.h>
22 #include <libubox/list.h>
23 #include <libubox/ustream.h>
24 #include <libubus.h>
25
26 #include "syslog.h"
27
28 int debug = 0;
29 static struct blob_buf b;
30 static struct ubus_auto_conn conn;
31 static LIST_HEAD(clients);
32
33 static const struct blobmsg_policy read_policy =
34         { .name = "lines", .type = BLOBMSG_TYPE_INT32 };
35
36 static const struct blobmsg_policy write_policy =
37         { .name = "event", .type = BLOBMSG_TYPE_STRING };
38
39 struct client {
40         struct list_head list;
41
42         struct ustream_fd s;
43         int fd;
44 };
45
46 static void
47 client_close(struct ustream *s)
48 {
49         struct client *cl = container_of(s, struct client, s.stream);
50
51         list_del(&cl->list);
52         ustream_free(s);
53         close(cl->fd);
54         free(cl);
55 }
56
57 static void
58 client_notify_write(struct ustream *s, int bytes)
59 {
60         if (s->w.data_bytes < 128 && ustream_read_blocked(s))
61                 ustream_set_read_blocked(s, false);
62 }
63
64 static void client_notify_state(struct ustream *s)
65 {
66         if (!s->eof)
67                 return;
68
69         if (!s->w.data_bytes)
70                 return client_close(s);
71 }
72
73 static int
74 read_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 client *cl;
79         struct blob_attr *tb;
80         struct log_head *l;
81         int count = 0;
82         int fds[2];
83
84         if (msg) {
85                 blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
86                 if (tb)
87                         count = blobmsg_get_u32(tb);
88         }
89
90         pipe(fds);
91         ubus_request_set_fd(ctx, req, fds[0]);
92
93         cl = calloc(1, sizeof(*cl));
94         cl->s.stream.notify_write = client_notify_write;
95         cl->s.stream.notify_state = client_notify_state;
96         cl->fd = fds[1];
97         ustream_fd_init(&cl->s, cl->fd);
98         list_add(&cl->list, &clients);
99         l = log_list(count, NULL);
100         while ((!tb || count) && l) {
101                 blob_buf_init(&b, 0);
102                 blobmsg_add_string(&b, "msg", l->data);
103                 blobmsg_add_u32(&b, "id", l->id);
104                 blobmsg_add_u32(&b, "priority", l->priority);
105                 blobmsg_add_u32(&b, "source", l->source);
106                 blobmsg_add_u64(&b, "time", l->ts.tv_sec);
107                 l = log_list(count, l);
108                 if (ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false) <= 0)
109                         break;
110         }
111         return 0;
112 }
113
114 static int
115 write_log(struct ubus_context *ctx, struct ubus_object *obj,
116                 struct ubus_request_data *req, const char *method,
117                 struct blob_attr *msg)
118 {
119         struct blob_attr *tb;
120         char *event;
121
122         if (msg) {
123                 blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
124                 if (tb) {
125                         event = blobmsg_get_string(tb);
126                         log_add(event, strlen(event) + 1, SOURCE_SYSLOG);
127                 }
128         }
129
130         return 0;
131 }
132
133 static const struct ubus_method log_methods[] = {
134         { .name = "read", .handler = read_log, .policy = &read_policy, .n_policy = 1 },
135         { .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
136 };
137
138 static struct ubus_object_type log_object_type =
139         UBUS_OBJECT_TYPE("log", log_methods);
140
141 static struct ubus_object log_object = {
142         .name = "log",
143         .type = &log_object_type,
144         .methods = log_methods,
145         .n_methods = ARRAY_SIZE(log_methods),
146 };
147
148 void
149 ubus_notify_log(struct log_head *l)
150 {
151         struct client *c;
152
153         if (list_empty(&clients))
154                 return;
155
156         list_for_each_entry(c, &clients, list) {
157                 blob_buf_init(&b, 0);
158                 blobmsg_add_string(&b, "msg", l->data);
159                 blobmsg_add_u32(&b, "id", l->id);
160                 blobmsg_add_u32(&b, "priority", l->priority);
161                 blobmsg_add_u32(&b, "source", l->source);
162                 blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
163                 ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
164         }
165 }
166
167 static void
168 ubus_connect_handler(struct ubus_context *ctx)
169 {
170         int ret;
171
172         ret = ubus_add_object(ctx, &log_object);
173         if (ret)
174                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
175         fprintf(stderr, "log: connected to ubus\n");
176 }
177
178 int
179 main(int argc, char **argv)
180 {
181         signal(SIGPIPE, SIG_IGN);
182
183         uloop_init();
184         log_init();
185         conn.cb = ubus_connect_handler;
186         ubus_auto_connect(&conn);
187         uloop_run();
188         log_shutdown();
189         uloop_done();
190
191         return 0;
192 }