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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt