2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
14 #include <sys/types.h>
21 #include <linux/types.h>
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>
33 static struct blob_buf b
;
34 static struct ubus_auto_conn conn
;
35 static struct udebug_ubus udebug
;
36 static LIST_HEAD(clients
);
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
},
51 static const struct blobmsg_policy write_policy
=
52 { .name
= "event", .type
= BLOBMSG_TYPE_STRING
};
55 struct list_head list
;
62 client_close(struct ustream
*s
)
64 struct client
*cl
= container_of(s
, struct client
, s
.stream
);
72 static void client_notify_state(struct ustream
*s
)
77 static void client_notify_write(struct ustream
*s
, int bytes
)
79 if (ustream_pending_data(s
, true))
86 log_fill_msg(struct blob_buf
*b
, struct log_head
*l
)
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));
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
)
101 struct blob_attr
*tb
[__READ_MAX
] = {};
107 bool oneshot
= false;
114 blobmsg_parse(read_policy
, __READ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
116 count
= blobmsg_get_u32(tb
[READ_LINES
]);
118 stream
= blobmsg_get_bool(tb
[READ_STREAM
]);
119 if (tb
[READ_ONESHOT
])
120 oneshot
= blobmsg_get_bool(tb
[READ_ONESHOT
]);
123 l
= log_list(count
, NULL
);
125 if (pipe(fds
) == -1) {
126 fprintf(stderr
, "logd: failed to create pipe: %m\n");
130 ubus_request_set_fd(ctx
, req
, fds
[0]);
131 cl
= calloc(1, sizeof(*cl
));
132 cl
->s
.stream
.notify_state
= client_notify_state
;
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);
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);
146 cl
->s
.stream
.notify_write
= client_notify_write
;
147 client_notify_write(&cl
->s
.stream
, 0);
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
);
155 blobmsg_close_table(&b
, e
);
156 l
= log_list(count
, l
);
158 blobmsg_close_array(&b
, c
);
159 ubus_send_reply(ctx
, req
, b
.head
);
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
)
170 struct blob_attr
*tb
;
176 blobmsg_parse(&write_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
178 event
= blobmsg_get_string(tb
);
179 len
= strlen(event
) + 1;
180 if (len
> LOG_LINE_SIZE
) {
185 log_add(event
, len
, SOURCE_SYSLOG
);
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 },
197 static struct ubus_object_type log_object_type
=
198 UBUS_OBJECT_TYPE("log", log_methods
);
200 static struct ubus_object log_object
= {
202 .type
= &log_object_type
,
203 .methods
= log_methods
,
204 .n_methods
= ARRAY_SIZE(log_methods
),
208 ubus_notify_log(struct log_head
*l
)
212 if (list_empty(&clients
) && !log_object
.has_subscribers
)
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));
222 if (log_object
.has_subscribers
)
223 ubus_notify(&conn
.ctx
, &log_object
, "message", b
.head
, -1);
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);
232 ubus_connect_handler(struct ubus_context
*ctx
)
236 ret
= ubus_add_object(ctx
, &log_object
);
238 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
241 fprintf(stderr
, "log: connected to ubus\n");
245 main(int argc
, char **argv
)
247 int ch
, log_size
= 16;
248 struct passwd
*p
= NULL
;
250 signal(SIGPIPE
, SIG_IGN
);
251 while ((ch
= getopt(argc
, argv
, "S:")) != -1) {
254 log_size
= atoi(optarg
);
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");
269 if (setgid(p
->pw_gid
) < 0) {
270 fprintf(stderr
, "setgid() failed: %s\n", strerror(errno
));
274 if (setuid(p
->pw_uid
) < 0) {
275 fprintf(stderr
, "setuid() failed: %s\n", strerror(errno
));
280 udebug_ubus_free(&udebug
);
283 ubus_auto_shutdown(&conn
);