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