f1c7606d95fb37eca27792c912b9f511c69354c7
[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 MAXLINE 1024
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(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, "%s", 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 syslog_handle_fd(struct uloop_fd *fd, unsigned int events)
135 {
136 static char buf[MAXLINE];
137 int len;
138
139 while (1) {
140 char *c;
141
142 len = recv(fd->fd, buf, MAXLINE - 1, 0);
143 if (len < 0) {
144 if (errno == EINTR)
145 continue;
146
147 break;
148 }
149 if (!len)
150 break;
151
152 buf[len] = 0;
153 for (c = buf; *c; c++) {
154 if (*c == '\n')
155 *c = ' ';
156 }
157
158 log_add(buf, c - buf + 1, SOURCE_SYSLOG);
159 }
160 }
161
162 static void
163 klog_cb(struct ustream *s, int bytes)
164 {
165 struct ustream_buf *buf = s->r.head;
166 char *newline, *str;
167 int len;
168
169 do {
170 str = ustream_get_read_buf(s, NULL);
171 if (!str)
172 break;
173 newline = strchr(buf->data, '\n');
174 if (!newline)
175 break;
176 *newline = 0;
177 len = newline + 1 - str;
178 log_add(buf->data, len, SOURCE_KLOG);
179 ustream_consume(s, len);
180 } while (1);
181 }
182
183 static struct uloop_fd syslog_fd = {
184 .cb = syslog_handle_fd
185 };
186
187 static struct ustream_fd klog = {
188 .stream.string_data = true,
189 .stream.notify_read = klog_cb,
190 };
191
192 static int
193 klog_open(void)
194 {
195 int fd;
196
197 fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
198 if (fd < 0) {
199 fprintf(stderr, "Failed to open %s\n", KLOG_DEFAULT_PROC);
200 return -1;
201 }
202 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
203 ustream_fd_init(&klog, fd);
204 return 0;
205 }
206
207 static int
208 syslog_open(void)
209 {
210 unlink(log_dev);
211 syslog_fd.fd = usock(USOCK_UNIX | USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
212 if (syslog_fd.fd < 0) {
213 fprintf(stderr,"Failed to open %s\n", log_dev);
214 return -1;
215 }
216 chmod(log_dev, 0666);
217 uloop_fd_add(&syslog_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
218
219 return 0;
220 }
221
222 struct log_head*
223 log_list(int count, struct log_head *h)
224 {
225 unsigned int min = count;
226
227 if (count)
228 min = (count < current_id) ? (current_id - count) : (0);
229 if (!h && oldest->id >= min)
230 return oldest;
231 if (!h)
232 h = oldest;
233
234 while (h != newest) {
235 h = log_next(h, h->size);
236 if (!h->size && (h > newest))
237 h = log;
238 if (h->id >= min && (h != newest))
239 return h;
240 }
241
242 return NULL;
243 }
244
245 int
246 log_buffer_init(int size)
247 {
248 struct log_head *_log = malloc(size);
249
250 if (!_log) {
251 fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
252 return -1;
253 }
254
255 memset(_log, 0, size);
256
257 if (log && ((log_size + sizeof(struct log_head)) < size)) {
258 struct log_head *start = _log;
259 struct log_head *end = ((void*) _log) + size;
260 struct log_head *l;
261
262 l = log_list(0, NULL);
263 while ((start < end) && l && l->size) {
264 memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
265 start = (struct log_head *) &l->data[PAD(l->size)];
266 l = log_list(0, l);
267 }
268 free(log);
269 newest = start;
270 newest->size = 0;
271 oldest = log = _log;
272 log_end = ((void*) log) + size;
273 } else {
274 oldest = newest = log = _log;
275 log_end = ((void*) log) + size;
276 }
277 log_size = size;
278
279 return 0;
280 }
281
282 void
283 log_init(int _log_size)
284 {
285 if (_log_size > 0)
286 log_size = _log_size;
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 if (syslog_fd.registered) {
305 uloop_fd_delete(&syslog_fd);
306 close(syslog_fd.fd);
307 }
308
309 ustream_free(&klog.stream);
310 close(klog.fd.fd);
311 free(log);
312 regfree(&pat_prio);
313 regfree(&pat_tstamp);
314 }