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