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