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.
17 #include <linux/types.h>
19 #include <libubox/uloop.h>
20 #include <libubox/blobmsg.h>
27 static struct blob_buf b
;
28 static struct ubus_context
*_ctx
;
29 static struct uloop_timeout ubus_timer
;
31 static const struct blobmsg_policy read_policy
=
32 { .name
= "lines", .type
= BLOBMSG_TYPE_INT32
};
34 static const struct blobmsg_policy write_policy
=
35 { .name
= "event", .type
= BLOBMSG_TYPE_STRING
};
38 read_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
39 struct ubus_request_data
*req
, const char *method
,
40 struct blob_attr
*msg
)
48 blobmsg_parse(&read_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
50 count
= blobmsg_get_u32(tb
);
54 lines
= blobmsg_open_array(&b
, "lines");
56 l
= log_list(count
, NULL
);
59 entry
= blobmsg_open_table(&b
, NULL
);
60 blobmsg_add_string(&b
, "msg", l
->data
);
61 blobmsg_add_u32(&b
, "id", l
->id
);
62 blobmsg_add_u32(&b
, "priority", l
->priority
);
63 blobmsg_add_u32(&b
, "source", l
->source
);
64 blobmsg_add_u64(&b
, "time", l
->ts
.tv_sec
);
65 blobmsg_close_table(&b
, entry
);
66 l
= log_list(count
, l
);
68 blobmsg_close_table(&b
, lines
);
69 ubus_send_reply(ctx
, req
, b
.head
);
75 write_log(struct ubus_context
*ctx
, struct ubus_object
*obj
,
76 struct ubus_request_data
*req
, const char *method
,
77 struct blob_attr
*msg
)
83 blobmsg_parse(&write_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
85 event
= blobmsg_get_string(tb
);
86 log_add(event
, strlen(event
) + 1, SOURCE_SYSLOG
);
94 log_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
96 notify
= obj
->has_subscribers
;
99 static const struct ubus_method log_methods
[] = {
100 { .name
= "read", .handler
= read_log
, .policy
= &read_policy
, .n_policy
= 1 },
101 { .name
= "write", .handler
= write_log
, .policy
= &write_policy
, .n_policy
= 1 },
104 static struct ubus_object_type log_object_type
=
105 UBUS_OBJECT_TYPE("log", log_methods
);
107 static struct ubus_object log_object
= {
109 .type
= &log_object_type
,
110 .methods
= log_methods
,
111 .n_methods
= ARRAY_SIZE(log_methods
),
112 .subscribe_cb
= log_subscribe_cb
,
116 ubus_notify_log(struct log_head
*l
)
123 blob_buf_init(&b
, 0);
124 blobmsg_add_u32(&b
, "id", l
->id
);
125 blobmsg_add_u32(&b
, "priority", l
->priority
);
126 blobmsg_add_u32(&b
, "source", l
->source
);
127 blobmsg_add_u64(&b
, "time", (((__u64
) l
->ts
.tv_sec
) * 1000) + (l
->ts
.tv_nsec
/ 1000000));
129 ret
= ubus_notify(_ctx
, &log_object
, l
->data
, b
.head
, -1);
131 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
135 ubus_reconnect_cb(struct uloop_timeout
*timeout
)
137 if (!ubus_reconnect(_ctx
, NULL
))
138 ubus_add_uloop(_ctx
);
140 uloop_timeout_set(timeout
, 1000);
144 ubus_disconnect_cb(struct ubus_context
*ctx
)
146 ubus_timer
.cb
= ubus_reconnect_cb
;
147 uloop_timeout_set(&ubus_timer
, 1000);
151 ubus_connect_cb(struct uloop_timeout
*timeout
)
155 _ctx
= ubus_connect(NULL
);
157 uloop_timeout_set(timeout
, 1000);
158 fprintf(stderr
, "failed to connect to ubus\n");
161 _ctx
->connection_lost
= ubus_disconnect_cb
;
162 ret
= ubus_add_object(_ctx
, &log_object
);
164 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
165 fprintf(stderr
, "log: connected to ubus\n");
166 ubus_add_uloop(_ctx
);
170 main(int argc
, char **argv
)
172 signal(SIGPIPE
, SIG_IGN
);
175 ubus_timer
.cb
= ubus_connect_cb
;
176 uloop_timeout_set(&ubus_timer
, 1000);