2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/types.h>
17 #include <sys/socket.h>
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/ustream.h>
36 #define LOG_DEFAULT_SIZE (16 * 1024)
37 #define LOG_DEFAULT_SOCKET "/dev/log"
38 #define SYSLOG_PADDING 16
40 #define KLOG_DEFAULT_PROC "/proc/kmsg"
42 #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
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;
51 static struct log_head*
52 log_next(struct log_head *h, int size)
54 struct log_head *n = (struct log_head *) &h->data[PAD(size)];
56 return (n >= log_end) ? (log) : (n);
60 log_add(char *buf, int size, int source)
62 regmatch_t matches[4];
63 struct log_head *next;
67 /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
69 fprintf(stderr, "%s", buf);
73 /* strip trailing newline */
74 if (buf[size - 2] == '\n') {
79 /* strip the priority */
80 ret = regexec(&pat_prio, buf, 3, matches, 0);
82 priority = atoi(&buf[matches[1].rm_so]);
83 size -= matches[2].rm_so;
84 buf += matches[2].rm_so;
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;
96 /* strip syslog timestamp */
97 if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
98 size -= SYSLOG_PADDING;
99 buf += SYSLOG_PADDING;
102 //fprintf(stderr, "-> %d - %s\n", priority, buf);
104 /* find new oldest entry */
105 next = log_next(newest, size);
107 while ((oldest > newest) && (oldest <= next) && (oldest != log))
108 oldest = log_next(oldest, oldest->size);
110 //fprintf(stderr, "Log wrap\n");
112 next = log_next(log, size);
113 for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
118 /* add the log message */
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);
126 ubus_notify_log(newest);
132 syslog_handle_fd(struct uloop_fd *fd, unsigned int events)
134 static char buf[LOG_LINE_SIZE];
140 len = recv(fd->fd, buf, LOG_LINE_SIZE - 1, 0);
151 for (c = buf; *c; c++) {
156 log_add(buf, c - buf + 1, SOURCE_SYSLOG);
161 klog_cb(struct ustream *s, int bytes)
163 struct ustream_buf *buf = s->r.head;
168 str = ustream_get_read_buf(s, NULL);
171 newline = strchr(buf->data, '\n');
175 len = newline + 1 - str;
176 log_add(buf->data, len, SOURCE_KLOG);
177 ustream_consume(s, len);
181 static struct uloop_fd syslog_fd = {
182 .cb = syslog_handle_fd
185 static struct ustream_fd klog = {
186 .stream.string_data = true,
187 .stream.notify_read = klog_cb,
195 fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
197 fprintf(stderr, "Failed to open %s\n", KLOG_DEFAULT_PROC);
200 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
201 ustream_fd_init(&klog, fd);
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);
214 chmod(log_dev, 0666);
215 uloop_fd_add(&syslog_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
221 log_list(int count, struct log_head *h)
223 unsigned int min = count;
226 min = (count < current_id) ? (current_id - count) : (0);
227 if (!h && oldest->id >= min)
232 while (h != newest) {
233 h = log_next(h, h->size);
234 if (!h->size && (h > newest))
236 if (h->id >= min && (h != newest))
244 log_buffer_init(int size)
246 struct log_head *_log = malloc(size);
249 fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
253 memset(_log, 0, size);
255 if (log && ((log_size + sizeof(struct log_head)) < size)) {
256 struct log_head *start = _log;
257 struct log_head *end = ((void*) _log) + size;
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)];
270 log_end = ((void*) log) + size;
272 oldest = newest = log = _log;
273 log_end = ((void*) log) + size;
281 log_init(int _log_size)
284 log_size = _log_size;
286 regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
287 regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
289 if (log_buffer_init(log_size)) {
290 fprintf(stderr, "Failed to allocate log memory\n");
296 openlog("sysinit", LOG_CONS, LOG_DAEMON);
302 if (syslog_fd.registered) {
303 uloop_fd_delete(&syslog_fd);
307 ustream_free(&klog.stream);
311 regfree(&pat_tstamp);