• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt