logd: enforce line length limit for ubus based log messages as well
[project/ubox.git] / log / syslog.c
1 /*
2  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
3  *
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
7  *
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.
12  */
13
14 #include <linux/un.h>
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/stat.h>
19
20 #include <fcntl.h>
21 #include <regex.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <syslog.h>
28 #include <errno.h>
29
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/ustream.h>
33
34 #include "syslog.h"
35
36 #define LOG_DEFAULT_SIZE        (16 * 1024)
37 #define LOG_DEFAULT_SOCKET      "/dev/log"
38 #define SYSLOG_PADDING          16
39
40 #define KLOG_DEFAULT_PROC       "/proc/kmsg"
41
42 #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
43
44 static char *log_dev = LOG_DEFAULT_SOCKET;
45 static int log_size = LOG_DEFAULT_SIZE;
46 static struct log_head *log, *log_end, *oldest, *newest;
47 static int current_id = 0;
48 static regex_t pat_prio;
49 static regex_t pat_tstamp;
50
51 static struct log_head*
52 log_next(struct log_head *h, int size)
53 {
54         struct log_head *n = (struct log_head *) &h->data[PAD(size)];
55
56         return (n >= log_end) ? (log) : (n);
57 }
58
59 void
60 log_add(char *buf, int size, int source)
61 {
62         regmatch_t matches[4];
63         struct log_head *next;
64         int priority = 0;
65         int ret;
66
67         /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
68         if (!log) {
69                 fprintf(stderr, "%s", buf);
70                 return;
71         }
72
73         /* strip trailing newline */
74         if (buf[size - 2] == '\n') {
75                 buf[size - 2] = '\0';
76                 size -= 1;
77         }
78
79         /* strip the priority */
80         ret = regexec(&pat_prio, buf, 3, matches, 0);
81         if (!ret) {
82                 priority = atoi(&buf[matches[1].rm_so]);
83                 size -= matches[2].rm_so;
84                 buf += matches[2].rm_so;
85         }
86
87 #if 0
88         /* strip kernel timestamp */
89         ret = regexec(&pat_tstamp,buf, 4, matches, 0);
90         if ((source == SOURCE_KLOG) && !ret) {
91                 size -= matches[3].rm_so;
92                 buf += matches[3].rm_so;
93         }
94 #endif
95
96         /* strip syslog timestamp */
97         if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
98                 size -= SYSLOG_PADDING;
99                 buf += SYSLOG_PADDING;
100         }
101
102         //fprintf(stderr, "-> %d - %s\n", priority, buf);
103
104         /* find new oldest entry */
105         next = log_next(newest, size);
106         if (next > newest) {
107                 while ((oldest > newest) && (oldest <= next) && (oldest != log))
108                         oldest = log_next(oldest, oldest->size);
109         } else {
110                 //fprintf(stderr, "Log wrap\n");
111                 newest->size = 0;
112                 next = log_next(log, size);
113                 for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
114                         ;
115                 newest = log;
116         }
117
118         /* add the log message */
119         newest->size = size;
120         newest->id = current_id++;
121         newest->priority = priority;
122         newest->source = source;
123         clock_gettime(CLOCK_REALTIME, &newest->ts);
124         strcpy(newest->data, buf);
125
126         ubus_notify_log(newest);
127
128         newest = next;
129 }
130
131 static void
132 syslog_handle_fd(struct uloop_fd *fd, unsigned int events)
133 {
134         static char buf[LOG_LINE_SIZE];
135         int len;
136
137         while (1) {
138                 char *c;
139
140                 len = recv(fd->fd, buf, LOG_LINE_SIZE - 1, 0);
141                 if (len < 0) {
142                         if (errno == EINTR)
143                                 continue;
144
145                         break;
146                 }
147                 if (!len)
148                         break;
149
150                 buf[len] = 0;
151                 for (c = buf; *c; c++) {
152                     if (*c == '\n')
153                         *c = ' ';
154                 }
155
156                 log_add(buf, c - buf + 1, SOURCE_SYSLOG);
157         }
158 }
159
160 static void
161 klog_cb(struct ustream *s, int bytes)
162 {
163         struct ustream_buf *buf = s->r.head;
164         char *newline, *str;
165         int len;
166
167         do {
168                 str = ustream_get_read_buf(s, NULL);
169                 if (!str)
170                         break;
171                 newline = strchr(buf->data, '\n');
172                 if (!newline)
173                         break;
174                 *newline = 0;
175                 len = newline + 1 - str;
176                 log_add(buf->data, len, SOURCE_KLOG);
177                 ustream_consume(s, len);
178         } while (1);
179 }
180
181 static struct uloop_fd syslog_fd = {
182         .cb = syslog_handle_fd
183 };
184
185 static struct ustream_fd klog = {
186         .stream.string_data = true,
187         .stream.notify_read = klog_cb,
188 };
189
190 static int
191 klog_open(void)
192 {
193         int fd;
194
195         fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
196         if (fd < 0) {
197                 fprintf(stderr, "Failed to open %s\n", KLOG_DEFAULT_PROC);
198                 return -1;
199         }
200         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
201         ustream_fd_init(&klog, fd);
202         return 0;
203 }
204
205 static int
206 syslog_open(void)
207 {
208         unlink(log_dev);
209         syslog_fd.fd = usock(USOCK_UNIX | USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
210         if (syslog_fd.fd < 0) {
211                 fprintf(stderr,"Failed to open %s\n", log_dev);
212                 return -1;
213         }
214         chmod(log_dev, 0666);
215         uloop_fd_add(&syslog_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
216
217         return 0;
218 }
219
220 struct log_head*
221 log_list(int count, struct log_head *h)
222 {
223         unsigned int min = count;
224
225         if (count)
226                 min = (count < current_id) ? (current_id - count) : (0);
227         if (!h && oldest->id >= min)
228                 return oldest;
229         if (!h)
230                 h = oldest;
231
232         while (h != newest) {
233                 h = log_next(h, h->size);
234                 if (!h->size && (h > newest))
235                         h = log;
236                 if (h->id >= min && (h != newest))
237                         return h;
238         }
239
240         return NULL;
241 }
242
243 int
244 log_buffer_init(int size)
245 {
246         struct log_head *_log = malloc(size);
247
248         if (!_log) {
249                 fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
250                 return -1;
251         }
252
253         memset(_log, 0, size);
254
255         if (log && ((log_size + sizeof(struct log_head)) < size)) {
256                 struct log_head *start = _log;
257                 struct log_head *end = ((void*) _log) + size;
258                 struct log_head *l;
259
260                 l = log_list(0, NULL);
261                 while ((start < end) && l && l->size) {
262                         memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
263                         start = (struct log_head *) &l->data[PAD(l->size)];
264                         l = log_list(0, l);
265                 }
266                 free(log);
267                 newest = start;
268                 newest->size = 0;
269                 oldest = log = _log;
270                 log_end = ((void*) log) + size;
271         } else {
272                 oldest = newest = log = _log;
273                 log_end = ((void*) log) + size;
274         }
275         log_size = size;
276
277         return 0;
278 }
279
280 void
281 log_init(int _log_size)
282 {
283         if (_log_size > 0)
284                 log_size = _log_size;
285
286         regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
287         regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
288
289         if (log_buffer_init(log_size)) {
290                 fprintf(stderr, "Failed to allocate log memory\n");
291                 exit(-1);
292         }
293
294         syslog_open();
295         klog_open();
296         openlog("sysinit", LOG_CONS, LOG_DAEMON);
297 }
298
299 void
300 log_shutdown(void)
301 {
302         if (syslog_fd.registered) {
303                 uloop_fd_delete(&syslog_fd);
304                 close(syslog_fd.fd);
305         }
306
307         ustream_free(&klog.stream);
308         close(klog.fd.fd);
309         free(log);
310         regfree(&pat_prio);
311         regfree(&pat_tstamp);
312 }