fix hotplug
[project/procd.git] / logread.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <time.h>
16 #include <unistd.h>
17 #include <stdio.h>
18
19 #include <libubox/blobmsg_json.h>
20 #include <libubox/uloop.h>
21 #include "libubus.h"
22
23 enum {
24         LOG_MSG,
25         LOG_ID,
26         LOG_PRIO,
27         LOG_SOURCE,
28         LOG_TIME,
29         __LOG_MAX
30 };
31
32 static const struct blobmsg_policy log_policy[] = {
33         [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
34         [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
35         [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
36         [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
37         [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
38 };
39
40 enum {
41         WATCH_ID,
42         WATCH_COUNTER,
43         __WATCH_MAX
44 };
45
46 static struct ubus_subscriber log_event;
47
48 static void log_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
49                         uint32_t id)
50 {
51         fprintf(stderr, "Object %08x went away\n", id);
52 }
53
54 static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
55                         struct ubus_request_data *req, const char *method,
56                         struct blob_attr *msg)
57 {
58         struct blob_attr *tb[__LOG_MAX];
59         char *str;
60         time_t t;
61         char *c;
62
63         blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
64         if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
65                 return 1;
66
67         t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
68         c = ctime(&t);
69         c[strlen(c) - 1] = '\0';
70         str = blobmsg_format_json(msg, true);
71         printf("%s - %s: %s\n",
72                 c, (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("syslog") : ("kernel"), method);
73         free(str);
74
75         return 0;
76 }
77
78 static void follow_log(struct ubus_context *ctx, int id)
79 {
80         int ret;
81
82         uloop_init();
83         ubus_add_uloop(ctx);
84
85         log_event.remove_cb = log_handle_remove;
86         log_event.cb = log_notify;
87         ret = ubus_register_subscriber(ctx, &log_event);
88         if (ret)
89                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
90
91         ret = ubus_subscribe(ctx, &log_event, id);
92         if (ret)
93                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
94
95         uloop_run();
96         ubus_free(ctx);
97         uloop_done();
98 }
99
100 enum {
101         READ_LINE,
102         __READ_MAX
103 };
104
105 static const struct blobmsg_policy read_policy[] = {
106         [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
107 };
108
109 static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg)
110 {
111         struct blob_attr *cur;
112         struct blob_attr *_tb[__READ_MAX];
113         time_t t;
114         int rem;
115
116         if (!msg)
117                 return;
118
119         blobmsg_parse(read_policy, ARRAY_SIZE(read_policy), _tb, blob_data(msg), blob_len(msg));
120         if (!_tb[READ_LINE])
121                 return;
122         blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) {
123                 struct blob_attr *tb[__LOG_MAX];
124                 char *c;
125
126                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
127                         continue;
128
129                 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
130                 if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
131                         continue;
132
133                 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
134                 c = ctime(&t);
135                 c[strlen(c) - 1] = '\0';
136                 printf("%s - %s: %s\n",
137                         c, (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("syslog") : ("kernel"),
138                         blobmsg_get_string(tb[LOG_MSG]));
139         }
140 }
141
142 static int usage(const char *prog)
143 {
144         fprintf(stderr, "Usage: %s [options]\n"
145                 "Options:\n"
146                 "    -s <path>          Path to ubus socket\n"
147                 "    -l <count>         Got only the last 'count' messages\n"
148                 "    -f                 Follow log messages\n"
149                 "\n", prog);
150         return 1;
151 }
152
153 int main(int argc, char **argv)
154 {
155         struct ubus_context *ctx;
156         uint32_t id;
157         const char *ubus_socket = NULL;
158         int ch, ret, subscribe = 0, lines = 0;
159         static struct blob_buf b;
160
161         while ((ch = getopt(argc, argv, "fs:l:")) != -1) {
162                 switch (ch) {
163                 case 's':
164                         ubus_socket = optarg;
165                         break;
166                 case 'f':
167                         subscribe = 1;
168                         break;
169                 case 'l':
170                         lines = atoi(optarg);
171                         break;
172                 default:
173                         usage(*argv);
174                         break;
175                 }
176         }
177
178         ctx = ubus_connect(ubus_socket);
179         if (!ctx) {
180                 fprintf(stderr, "Failed to connect to ubus\n");
181                 return -1;
182         }
183
184         ret = ubus_lookup_id(ctx, "log", &id);
185         if (ret)
186                 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
187
188         if (!subscribe || lines) {
189                 blob_buf_init(&b, 0);
190                 if (lines)
191                         blobmsg_add_u32(&b, "lines", lines);
192                 ubus_invoke(ctx, id, "read", b.head, read_cb, 0, 3000);
193         }
194
195         if (subscribe)
196                 follow_log(ctx, id);
197
198         return 0;
199 }