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.
19 #include <linux/types.h>
21 #include <libubox/uloop.h>
22 #include <libubox/blobmsg.h>
23 #include <libubox/list.h>
24 #include <libubox/ustream.h>
30 static struct blob_buf b
;
31 static struct ubus_auto_conn conn
;
32 static LIST_HEAD(clients
);
34 static const struct blobmsg_policy read_policy
=
35 { .name
= "lines", .type
= BLOBMSG_TYPE_INT32
};
37 static const struct blobmsg_policy write_policy
=
38 { .name
= "event", .type
= BLOBMSG_TYPE_STRING
};
41 struct list_head list
;
48 client_close(struct ustream
*s
)
50 struct client
*cl
= container_of(s
, struct client
, s
.stream
);
58 static void client_notify_state(struct ustream
*s
)
64 read_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
65 struct ubus_request_data
*req
, const char *method
,
66 struct blob_attr
*msg
)
75 blobmsg_parse(&read_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
77 count
= blobmsg_get_u32(tb
);
80 if (pipe(fds
) == -1) {
81 fprintf(stderr
, "logd: failed to create pipe: %s\n", strerror(errno
));
84 ubus_request_set_fd(ctx
, req
, fds
[0]);
85 cl
= calloc(1, sizeof(*cl
));
86 cl
->s
.stream
.notify_state
= client_notify_state
;
88 ustream_fd_init(&cl
->s
, cl
->fd
);
89 list_add(&cl
->list
, &clients
);
90 l
= log_list(count
, NULL
);
91 while ((!tb
|| count
) && l
) {
93 blobmsg_add_string(&b
, "msg", l
->data
);
94 blobmsg_add_u32(&b
, "id", l
->id
);
95 blobmsg_add_u32(&b
, "priority", l
->priority
);
96 blobmsg_add_u32(&b
, "source", l
->source
);
97 blobmsg_add_u64(&b
, "time", l
->ts
.tv_sec
* 1000LL);
98 l
= log_list(count
, l
);
99 if (ustream_write(&cl
->s
.stream
, (void *) b
.head
, blob_len(b
.head
) + sizeof(struct blob_attr
), false) <= 0)
106 write_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
107 struct ubus_request_data
*req
, const char *method
,
108 struct blob_attr
*msg
)
110 struct blob_attr
*tb
;
114 blobmsg_parse(&write_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
116 event
= blobmsg_get_string(tb
);
117 log_add(event
, strlen(event
) + 1, SOURCE_SYSLOG
);
124 static const struct ubus_method log_methods
[] = {
125 { .name
= "read", .handler
= read_log
, .policy
= &read_policy
, .n_policy
= 1 },
126 { .name
= "write", .handler
= write_log
, .policy
= &write_policy
, .n_policy
= 1 },
129 static struct ubus_object_type log_object_type
=
130 UBUS_OBJECT_TYPE("log", log_methods
);
132 static struct ubus_object log_object
= {
134 .type
= &log_object_type
,
135 .methods
= log_methods
,
136 .n_methods
= ARRAY_SIZE(log_methods
),
140 ubus_notify_log(struct log_head
*l
)
144 if (list_empty(&clients
))
147 list_for_each_entry(c
, &clients
, list
) {
148 blob_buf_init(&b
, 0);
149 blobmsg_add_string(&b
, "msg", l
->data
);
150 blobmsg_add_u32(&b
, "id", l
->id
);
151 blobmsg_add_u32(&b
, "priority", l
->priority
);
152 blobmsg_add_u32(&b
, "source", l
->source
);
153 blobmsg_add_u64(&b
, "time", (((__u64
) l
->ts
.tv_sec
) * 1000) + (l
->ts
.tv_nsec
/ 1000000));
154 ustream_write(&c
->s
.stream
, (void *) b
.head
, blob_len(b
.head
) + sizeof(struct blob_attr
), false);
159 ubus_connect_handler(struct ubus_context
*ctx
)
163 ret
= ubus_add_object(ctx
, &log_object
);
165 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
168 fprintf(stderr
, "log: connected to ubus\n");
172 main(int argc
, char **argv
)
174 int ch
, log_size
= 16;
176 signal(SIGPIPE
, SIG_IGN
);
177 while ((ch
= getopt(argc
, argv
, "S:")) != -1) {
180 log_size
= atoi(optarg
);
190 conn
.cb
= ubus_connect_handler
;
191 ubus_auto_connect(&conn
);