5d1be68cef0c34fdd30b83290d8520dc4e1798ab
[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 #define SYSLOG_NAMES
20 #include <syslog.h>
21
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/usock.h>
24 #include <libubox/uloop.h>
25 #include "libubus.h"
26
27 enum {
28         LOG_MSG,
29         LOG_ID,
30         LOG_PRIO,
31         LOG_SOURCE,
32         LOG_TIME,
33         __LOG_MAX
34 };
35
36 static const struct blobmsg_policy log_policy[] = {
37         [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
38         [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
39         [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
40         [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
41         [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
42 };
43
44 enum {
45         WATCH_ID,
46         WATCH_COUNTER,
47         __WATCH_MAX
48 };
49
50 static struct ubus_subscriber log_event;
51 static struct uloop_fd sender;
52
53 static void log_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
54                         uint32_t id)
55 {
56         fprintf(stderr, "Object %08x went away\n", id);
57 }
58
59 static int log_notify(struct ubus_context *ctx, struct ubus_object *obj,
60                         struct ubus_request_data *req, const char *method,
61                         struct blob_attr *msg)
62 {
63         struct blob_attr *tb[__LOG_MAX];
64         char buf[256];
65         uint32_t p;
66         char *str;
67         time_t t;
68         char *c;
69
70         blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
71         if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
72                 return 1;
73
74         t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
75         c = ctime(&t);
76         p = blobmsg_get_u32(tb[LOG_PRIO]);
77         c[strlen(c) - 1] = '\0';
78         str = blobmsg_format_json(msg, true);
79         snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
80                 c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
81                 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
82                 method);
83         write(sender.fd, buf, strlen(buf));
84
85         free(str);
86
87         return 0;
88 }
89
90 static void follow_log(struct ubus_context *ctx, int id, const char *url, const char *port)
91 {
92         int ret;
93
94         uloop_init();
95         ubus_add_uloop(ctx);
96
97         log_event.remove_cb = log_handle_remove;
98         log_event.cb = log_notify;
99         ret = ubus_register_subscriber(ctx, &log_event);
100         if (ret)
101                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
102
103         ret = ubus_subscribe(ctx, &log_event, id);
104         if (ret)
105                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
106
107         if (url && port) {
108                 sender.fd = usock(USOCK_TCP | USOCK_NUMERIC, url, port);
109                 if (sender.fd < 0) {
110                         fprintf(stderr, "failed to connect: %s\n", strerror(errno));
111                         exit(-1);
112                 } else {
113                         uloop_fd_add(&sender, ULOOP_READ);
114                 }
115         } else {
116                 sender.fd = STDOUT_FILENO;
117         }
118
119         uloop_run();
120         ubus_free(ctx);
121         uloop_done();
122 }
123
124 enum {
125         READ_LINE,
126         __READ_MAX
127 };
128
129 static const struct blobmsg_policy read_policy[] = {
130         [READ_LINE] = { .name = "lines", .type = BLOBMSG_TYPE_ARRAY },
131 };
132
133 static void read_cb(struct ubus_request *req, int type, struct blob_attr *msg)
134 {
135         struct blob_attr *cur;
136         struct blob_attr *_tb[__READ_MAX];
137         time_t t;
138         int rem;
139
140         if (!msg)
141                 return;
142
143         blobmsg_parse(read_policy, ARRAY_SIZE(read_policy), _tb, blob_data(msg), blob_len(msg));
144         if (!_tb[READ_LINE])
145                 return;
146         blobmsg_for_each_attr(cur, _tb[READ_LINE], rem) {
147                 struct blob_attr *tb[__LOG_MAX];
148                 uint32_t p;
149                 char *c;
150
151                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
152                         continue;
153
154                 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
155                 if (!tb[LOG_MSG] || !tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME])
156                         continue;
157
158                 t = blobmsg_get_u64(tb[LOG_TIME]);
159                 p = blobmsg_get_u32(tb[LOG_PRIO]);
160                 c = ctime(&t);
161                 c[strlen(c) - 1] = '\0';
162
163                 printf("%s %s.%s%s %s\n",
164                         c, facilitynames[LOG_FAC(p)].c_name, prioritynames[LOG_PRI(p)].c_name,
165                         (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"),
166                         blobmsg_get_string(tb[LOG_MSG]));
167         }
168 }
169
170 static int usage(const char *prog)
171 {
172         fprintf(stderr, "Usage: %s [options]\n"
173                 "Options:\n"
174                 "    -s <path>          Path to ubus socket\n"
175                 "    -l <count>         Got only the last 'count' messages\n"
176                 "    -r <server> <port> Stream message to a server\n"
177                 "    -f                 Follow log messages\n"
178                 "\n", prog);
179         return 1;
180 }
181
182 int main(int argc, char **argv)
183 {
184         struct ubus_context *ctx;
185         uint32_t id;
186         const char *ubus_socket = NULL, *url = NULL, *port = NULL;
187         int ch, ret, subscribe = 0, lines = 0;
188         static struct blob_buf b;
189
190         while ((ch = getopt(argc, argv, "fs:l:r:")) != -1) {
191                 switch (ch) {
192                 case 's':
193                         ubus_socket = optarg;
194                         break;
195                 case 'r':
196                         url = optarg++;
197                         port = argv[optind++];
198                         break;
199                 case 'f':
200                         subscribe = 1;
201                         break;
202                 case 'l':
203                         lines = atoi(optarg);
204                         break;
205                 default:
206                         return usage(*argv);
207                 }
208         }
209
210         ctx = ubus_connect(ubus_socket);
211         if (!ctx) {
212                 fprintf(stderr, "Failed to connect to ubus\n");
213                 return -1;
214         }
215
216         ret = ubus_lookup_id(ctx, "log", &id);
217         if (ret)
218                 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
219
220         if (!subscribe || lines) {
221                 blob_buf_init(&b, 0);
222                 if (lines)
223                         blobmsg_add_u32(&b, "lines", lines);
224                 ubus_invoke(ctx, id, "read", b.head, read_cb, 0, 3000);
225         }
226
227         if (subscribe)
228                 follow_log(ctx, id, url, port);
229
230         return 0;
231 }