ubox/logd: free regex's on log_shutdown() call
[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 "syslog.h"
34
35 #define LOG_DEFAULT_SIZE        (16 * 1024)
36 #define LOG_DEFAULT_SOCKET      "/dev/log"
37 #define LOG_LINE_LEN            256
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(sizeof(struct log_head) + 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 slog_cb(struct ustream *s, int bytes)
133 {
134         struct ustream_buf *buf = s->r.head;
135         char *str;
136         int len;
137
138         do {
139                 str = ustream_get_read_buf(s, NULL);
140                 if (!str)
141                         break;
142                 len = strlen(buf->data);
143                 if (!len) {
144                         bytes -= 1;
145                         ustream_consume(s, 1);
146                         continue;
147                 }
148                 log_add(buf->data, len + 1, SOURCE_SYSLOG);
149                 ustream_consume(s, len);
150                 bytes -= len;
151         } while (bytes > 0);
152 }
153
154 static void
155 klog_cb(struct ustream *s, int bytes)
156 {
157         struct ustream_buf *buf = s->r.head;
158         char *newline, *str;
159         int len;
160
161         do {
162                 str = ustream_get_read_buf(s, NULL);
163                 if (!str)
164                         break;
165                 newline = strchr(buf->data, '\n');
166                 if (!newline)
167                         break;
168                 *newline = 0;
169                 len = newline + 1 - str;
170                 log_add(buf->data, len, SOURCE_KLOG);
171                 ustream_consume(s, len);
172         } while (1);
173 }
174
175 struct ustream_fd slog = {
176         .stream.string_data = true,
177         .stream.notify_read = slog_cb,
178 };
179
180 struct ustream_fd klog = {
181         .stream.string_data = true,
182         .stream.notify_read = klog_cb,
183 };
184
185 static int
186 klog_open(void)
187 {
188         int fd;
189
190         fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
191         if (fd < 0) {
192                 fprintf(stderr, "Failed to open %s\n", KLOG_DEFAULT_PROC);
193                 return -1;
194         }
195         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
196         ustream_fd_init(&klog, fd);
197         return 0;
198 }
199
200 static int
201 syslog_open(void)
202 {
203         int fd;
204
205         unlink(log_dev);
206         fd = usock(USOCK_UNIX | USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
207         if (fd < 0) {
208                 fprintf(stderr,"Failed to open %s\n", log_dev);
209                 return -1;
210         }
211         chmod(log_dev, 0666);
212         ustream_fd_init(&slog, fd);
213         return 0;
214 }
215
216 struct log_head*
217 log_list(int count, struct log_head *h)
218 {
219         unsigned int min = count;
220
221         if (count)
222                 min = (count < current_id) ? (current_id - count) : (0);
223         if (!h && oldest->id >= min)
224                 return oldest;
225         if (!h)
226                 h = oldest;
227
228         while (h != newest) {
229                 h = log_next(h, h->size);
230                 if (!h->size && (h > newest))
231                         h = log;
232                 if (h->id >= min && (h != newest))
233                         return h;
234         }
235
236         return NULL;
237 }
238
239 int
240 log_buffer_init(int size)
241 {
242         struct log_head *_log = malloc(size);
243
244         if (!_log) {
245                 fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
246                 return -1;
247         }
248
249         memset(_log, 0, size);
250
251         if (log && ((log_size + sizeof(struct log_head)) < size)) {
252                 struct log_head *start = _log;
253                 struct log_head *end = ((void*) _log) + size;
254                 struct log_head *l;
255
256                 l = log_list(0, NULL);
257                 while ((start < end) && l && l->size) {
258                         memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
259                         start = (struct log_head *) &l->data[PAD(l->size)];
260                         l = log_list(0, l);
261                 }
262                 free(log);
263                 newest = start;
264                 newest->size = 0;
265                 oldest = log = _log;
266                 log_end = ((void*) log) + size;
267         } else {
268                 oldest = newest = log = _log;
269                 log_end = ((void*) log) + size;
270         }
271         log_size = size;
272
273         return 0;
274 }
275
276 void
277 log_init(int _log_size)
278 {
279         if (_log_size > 0)
280                 log_size = _log_size;
281
282         regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
283         regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
284
285         if (log_buffer_init(log_size)) {
286                 fprintf(stderr, "Failed to allocate log memory\n");
287                 exit(-1);
288         }
289
290         syslog_open();
291         klog_open();
292         openlog("sysinit", LOG_CONS, LOG_DAEMON);
293 }
294
295 void
296 log_shutdown(void)
297 {
298         ustream_free(&slog.stream);
299         ustream_free(&klog.stream);
300         close(slog.fd.fd);
301         close(klog.fd.fd);
302         free(log);
303         regfree(&pat_prio);
304         regfree(&pat_tstamp);
305 }