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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt