log: make valgrind less complain by freeing allocated memory
[project/ubox.git] / log / 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 <sys/types.h>
16 #include <sys/stat.h>
17
18 #include <fcntl.h>
19 #include <time.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24
25 #define SYSLOG_NAMES
26 #include <syslog.h>
27
28 #include <libubox/ustream.h>
29 #include <libubox/blobmsg_json.h>
30 #include <libubox/usock.h>
31 #include <libubox/uloop.h>
32 #include "libubus.h"
33 #include "syslog.h"
34
35 enum {
36         LOG_STDOUT,
37         LOG_FILE,
38         LOG_NET,
39 };
40
41 enum {
42         LOG_MSG,
43         LOG_ID,
44         LOG_PRIO,
45         LOG_SOURCE,
46         LOG_TIME,
47         __LOG_MAX
48 };
49
50 static const struct blobmsg_policy log_policy[] = {
51         [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
52         [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
53         [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
54         [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
55         [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
56 };
57
58 static struct uloop_timeout retry;
59 static struct uloop_fd sender;
60 static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
61 static int log_type = LOG_STDOUT;
62 static int log_size, log_udp, log_follow, log_trailer_null = 0;
63
64 static const char* getcodetext(int value, CODE *codetable) {
65         CODE *i;
66
67         if (value >= 0)
68                 for (i = codetable; i->c_val != -1; i++)
69                         if (i->c_val == value)
70                                 return (i->c_name);
71         return "<unknown>";
72 };
73
74 static void log_handle_reconnect(struct uloop_timeout *timeout)
75 {
76         sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
77         if (sender.fd < 0) {
78                 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
79                 uloop_timeout_set(&retry, 1000);
80         } else {
81                 uloop_fd_add(&sender, ULOOP_READ);
82                 syslog(LOG_INFO, "Logread connected to %s:%s\n", log_ip, log_port);
83         }
84 }
85
86 static void log_handle_fd(struct uloop_fd *u, unsigned int events)
87 {
88         if (u->eof) {
89                 uloop_fd_delete(u);
90                 close(sender.fd);
91                 sender.fd = -1;
92                 uloop_timeout_set(&retry, 1000);
93         }
94 }
95
96 static int log_notify(struct blob_attr *msg)
97 {
98         struct blob_attr *tb[__LOG_MAX];
99         struct stat s;
100         char buf[512];
101         uint32_t p;
102         char *str;
103         time_t t;
104         char *c, *m;
105         int ret = 0;
106
107         if (sender.fd < 0)
108                 return 0;
109
110         blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
111         if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
112                 return 1;
113
114         if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size)) {
115                 char *old = malloc(strlen(log_file) + 5);
116
117                 close(sender.fd);
118                 if (old) {
119                         sprintf(old, "%s.old", log_file);
120                         rename(log_file, old);
121                         free(old);
122                 }
123                 sender.fd = open(log_file, O_CREAT | O_WRONLY | O_APPEND, 0600);
124                 if (sender.fd < 0) {
125                         fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
126                         exit(-1);
127                 }
128         }
129
130         m = blobmsg_get_string(tb[LOG_MSG]);
131         t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
132         c = ctime(&t);
133         p = blobmsg_get_u32(tb[LOG_PRIO]);
134         c[strlen(c) - 1] = '\0';
135         str = blobmsg_format_json(msg, true);
136         if (log_type == LOG_NET) {
137                 int err;
138
139                 snprintf(buf, sizeof(buf), "<%u>", p);
140                 strncat(buf, c + 4, 16);
141                 if (hostname) {
142                         strncat(buf, hostname, sizeof(buf) - strlen(buf) - 1);
143                         strncat(buf, " ", sizeof(buf) - strlen(buf) - 1);
144                 }
145                 if (log_prefix) {
146                         strncat(buf, log_prefix, sizeof(buf) - strlen(buf) - 1);
147                         strncat(buf, ": ", sizeof(buf) - strlen(buf) - 1);
148                 }
149                 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
150                         strncat(buf, "kernel: ", sizeof(buf) - strlen(buf) - 1);
151                 strncat(buf, m, sizeof(buf) - strlen(buf) - 1);
152                 if (log_udp)
153                         err = write(sender.fd, buf, strlen(buf));
154                 else {
155                         size_t buflen = strlen(buf);
156                         if (!log_trailer_null)
157                                 buf[buflen] = '\n';
158                         err = send(sender.fd, buf, buflen + 1, 0);
159                 }
160
161                 if (err < 0) {
162                         syslog(LOG_INFO, "failed to send log data to %s:%s via %s\n",
163                                 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
164                         uloop_fd_delete(&sender);
165                         close(sender.fd);
166                         sender.fd = -1;
167                         uloop_timeout_set(&retry, 1000);
168                 }
169         } else {
170                 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
171                         c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
172                         (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
173                 ret = write(sender.fd, buf, strlen(buf));
174         }
175
176         free(str);
177         if (log_type == LOG_FILE)
178                 fsync(sender.fd);
179
180         return ret;
181 }
182
183 static int usage(const char *prog)
184 {
185         fprintf(stderr, "Usage: %s [options]\n"
186                 "Options:\n"
187                 "    -s <path>          Path to ubus socket\n"
188                 "    -l <count>         Got only the last 'count' messages\n"
189                 "    -r <server> <port> Stream message to a server\n"
190                 "    -F <file>          Log file\n"
191                 "    -S <bytes>         Log size\n"
192                 "    -p <file>          PID file\n"
193                 "    -h <hostname>      Add hostname to the message\n"
194                 "    -P <prefix>        Prefix custom text to streamed messages\n"
195                 "    -f                 Follow log messages\n"
196                 "    -u                 Use UDP as the protocol\n"
197                 "    -0                 Use \\0 instead of \\n as trailer when using TCP\n"
198                 "\n", prog);
199         return 1;
200 }
201
202 static void logread_fd_data_cb(struct ustream *s, int bytes)
203 {
204         while (true) {
205                 int len;
206                 struct blob_attr *a;
207
208                 a = (void*) ustream_get_read_buf(s, &len);
209                 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
210                         break;
211                 log_notify(a);
212                 ustream_consume(s, blob_len(a) + sizeof(*a));
213         }
214         if (!log_follow)
215                 uloop_end();
216 }
217
218 static void logread_fd_cb(struct ubus_request *req, int fd)
219 {
220         static struct ustream_fd test_fd;
221
222         test_fd.stream.notify_read = logread_fd_data_cb;
223         ustream_fd_init(&test_fd, fd);
224 }
225
226 int main(int argc, char **argv)
227 {
228         static struct ubus_request req;
229         struct ubus_context *ctx;
230         uint32_t id;
231         const char *ubus_socket = NULL;
232         int ch, ret, lines = 0;
233         static struct blob_buf b;
234         int tries = 5;
235
236         signal(SIGPIPE, SIG_IGN);
237
238         while ((ch = getopt(argc, argv, "u0fcs:l:r:F:p:S:P:h:")) != -1) {
239                 switch (ch) {
240                 case 'u':
241                         log_udp = 1;
242                         break;
243                 case '0':
244                         log_trailer_null = 1;
245                         break;
246                 case 's':
247                         ubus_socket = optarg;
248                         break;
249                 case 'r':
250                         log_ip = optarg++;
251                         log_port = argv[optind++];
252                         break;
253                 case 'F':
254                         log_file = optarg;
255                         break;
256                 case 'p':
257                         pid_file = optarg;
258                         break;
259                 case 'P':
260                         log_prefix = optarg;
261                         break;
262                 case 'f':
263                         log_follow = 1;
264                         break;
265                 case 'l':
266                         lines = atoi(optarg);
267                         break;
268                 case 'S':
269                         log_size = atoi(optarg);
270                         if (log_size < 1)
271                                 log_size = 1;
272                         log_size *= 1024;
273                         break;
274                 case 'h':
275                         hostname = optarg;
276                         break;
277                 default:
278                         return usage(*argv);
279                 }
280         }
281         uloop_init();
282
283         ctx = ubus_connect(ubus_socket);
284         if (!ctx) {
285                 fprintf(stderr, "Failed to connect to ubus\n");
286                 return -1;
287         }
288         ubus_add_uloop(ctx);
289
290         /* ugly ugly ugly ... we need a real reconnect logic */
291         do {
292                 ret = ubus_lookup_id(ctx, "log", &id);
293                 if (ret) {
294                         fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
295                         sleep(1);
296                         continue;
297                 }
298
299                 blob_buf_init(&b, 0);
300                 if (lines)
301                         blobmsg_add_u32(&b, "lines", lines);
302                 else if (log_follow)
303                         blobmsg_add_u32(&b, "lines", 0);
304                 if (log_follow) {
305                         if (pid_file) {
306                                 FILE *fp = fopen(pid_file, "w+");
307                                 if (fp) {
308                                         fprintf(fp, "%d", getpid());
309                                         fclose(fp);
310                                 }
311                         }
312                 }
313
314                 if (log_ip && log_port) {
315                         openlog("logread", LOG_PID, LOG_DAEMON);
316                         log_type = LOG_NET;
317                         sender.cb = log_handle_fd;
318                         retry.cb = log_handle_reconnect;
319                         uloop_timeout_set(&retry, 1000);
320                 } else if (log_file) {
321                         log_type = LOG_FILE;
322                         sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600);
323                         if (sender.fd < 0) {
324                                 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
325                                 exit(-1);
326                         }
327                 } else {
328                         sender.fd = STDOUT_FILENO;
329                 }
330
331                 ubus_invoke_async(ctx, id, "read", b.head, &req);
332                 req.fd_cb = logread_fd_cb;
333                 ubus_complete_request_async(ctx, &req);
334
335                 uloop_run();
336                 ubus_free(ctx);
337                 uloop_done();
338
339         } while (ret && tries--);
340
341         return ret;
342 }