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>
32 static struct blob_buf b
;
33 static struct ubus_auto_conn conn
;
34 static LIST_HEAD(clients
);
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
},
49 static const struct blobmsg_policy write_policy
=
50 { .name
= "event", .type
= BLOBMSG_TYPE_STRING
};
53 struct list_head list
;
60 client_close(struct ustream
*s
)
62 struct client
*cl
= container_of(s
, struct client
, s
.stream
);
70 static void client_notify_state(struct ustream
*s
)
75 static void client_notify_write(struct ustream
*s
, int bytes
)
77 if (ustream_pending_data(s
, true))
84 log_fill_msg(struct blob_buf
*b
, struct log_head
*l
)
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));
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
)
99 struct blob_attr
*tb
[__READ_MAX
] = {};
105 bool oneshot
= false;
112 blobmsg_parse(read_policy
, __READ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
114 count
= blobmsg_get_u32(tb
[READ_LINES
]);
116 stream
= blobmsg_get_bool(tb
[READ_STREAM
]);
117 if (tb
[READ_ONESHOT
])
118 oneshot
= blobmsg_get_bool(tb
[READ_ONESHOT
]);
121 l
= log_list(count
, NULL
);
123 if (pipe(fds
) == -1) {
124 fprintf(stderr
, "logd: failed to create pipe: %m\n");
128 ubus_request_set_fd(ctx
, req
, fds
[0]);
129 cl
= calloc(1, sizeof(*cl
));
130 cl
->s
.stream
.notify_state
= client_notify_state
;
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);
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);
144 cl
->s
.stream
.notify_write
= client_notify_write
;
145 client_notify_write(&cl
->s
.stream
, 0);
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
);
153 blobmsg_close_table(&b
, e
);
154 l
= log_list(count
, l
);
156 blobmsg_close_array(&b
, c
);
157 ubus_send_reply(ctx
, req
, b
.head
);
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
)
168 struct blob_attr
*tb
;
174 blobmsg_parse(&write_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
176 event
= blobmsg_get_string(tb
);
177 len
= strlen(event
) + 1;
178 if (len
> LOG_LINE_SIZE
) {
183 log_add(event
, len
, SOURCE_SYSLOG
);
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 },
195 static struct ubus_object_type log_object_type
=
196 UBUS_OBJECT_TYPE("log", log_methods
);
198 static struct ubus_object log_object
= {
200 .type
= &log_object_type
,
201 .methods
= log_methods
,
202 .n_methods
= ARRAY_SIZE(log_methods
),
206 ubus_notify_log(struct log_head
*l
)
210 if (list_empty(&clients
))
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));
220 list_for_each_entry(c
, &clients
, list
)
221 ustream_write(&c
->s
.stream
, (void *) b
.head
, blob_len(b
.head
) + sizeof(struct blob_attr
), false);
227 ubus_connect_handler(struct ubus_context
*ctx
)
231 ret
= ubus_add_object(ctx
, &log_object
);
233 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
236 fprintf(stderr
, "log: connected to ubus\n");
240 main(int argc
, char **argv
)
242 int ch
, log_size
= 16;
243 struct passwd
*p
= NULL
;
245 signal(SIGPIPE
, SIG_IGN
);
246 while ((ch
= getopt(argc
, argv
, "S:")) != -1) {
249 log_size
= atoi(optarg
);
259 conn
.cb
= ubus_connect_handler
;
260 ubus_auto_connect(&conn
);
261 p
= getpwnam("logd");
263 if (setgid(p
->pw_gid
) < 0) {
264 fprintf(stderr
, "setgid() failed: %s\n", strerror(errno
));
268 if (setuid(p
->pw_uid
) < 0) {
269 fprintf(stderr
, "setuid() failed: %s\n", strerror(errno
));
276 ubus_auto_shutdown(&conn
);