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