2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
29 #include <libubox/uloop.h>
30 #include <libubox/usock.h>
31 #include <libubox/ustream.h>
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
41 #define KLOG_DEFAULT_PROC "/proc/kmsg"
43 #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
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;
52 static struct log_head *log_next(struct log_head *h, int size)
54 struct log_head *n = (struct log_head *) &h->data[PAD(sizeof(struct log_head) + size)];
56 return (n >= log_end) ? (log) : (n);
59 void log_add(char *buf, int size, int source)
61 regmatch_t matches[4];
62 struct log_head *next;
66 /* strip trailing newline */
67 if (buf[size - 2] == '\n') {
72 /* strip the priority */
73 ret = regexec(&pat_prio, buf, 3, matches, 0);
75 priority = atoi(&buf[matches[1].rm_so]);
76 size -= matches[2].rm_so;
77 buf += matches[2].rm_so;
81 /* strip kernel timestamp */
82 ret = regexec(&pat_tstamp,buf, 4, matches, 0);
83 if ((source == SOURCE_KLOG) && !ret) {
84 size -= matches[3].rm_so;
85 buf += matches[3].rm_so;
89 /* strip syslog timestamp */
90 if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
91 size -= SYSLOG_PADDING;
92 buf += SYSLOG_PADDING;
95 DEBUG(2, "-> %d - %s\n", priority, buf);
97 /* find new oldest entry */
98 next = log_next(newest, size);
100 while ((oldest > newest) && (oldest <= next) && (oldest != log))
101 oldest = log_next(oldest, oldest->size);
103 DEBUG(2, "Log wrap\n");
105 next = log_next(log, size);
106 for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
111 /* add the log message */
113 newest->id = current_id++;
114 newest->priority = priority;
115 newest->source = source;
116 clock_gettime(CLOCK_REALTIME, &newest->ts);
117 strcpy(newest->data, buf);
119 ubus_notify_log(newest);
124 static void slog_cb(struct ustream *s, int bytes)
126 struct ustream_buf *buf = s->r.head;
131 str = ustream_get_read_buf(s, NULL);
134 len = strlen(buf->data);
137 ustream_consume(s, 1);
140 log_add(buf->data, len + 1, SOURCE_SYSLOG);
141 ustream_consume(s, len);
146 static void klog_cb(struct ustream *s, int bytes)
148 struct ustream_buf *buf = s->r.head;
153 str = ustream_get_read_buf(s, NULL);
156 newline = strchr(buf->data, '\n');
160 len = newline + 1 - str;
161 log_add(buf->data, len, SOURCE_KLOG);
162 ustream_consume(s, len);
166 struct ustream_fd slog = {
167 .stream.string_data = true,
168 .stream.notify_read = slog_cb,
171 struct ustream_fd klog = {
172 .stream.string_data = true,
173 .stream.notify_read = klog_cb,
176 static int klog_open(void)
180 DEBUG(1, "Opening %s\n", KLOG_DEFAULT_PROC);
181 fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
183 ERROR("Failed to open %s\n", KLOG_DEFAULT_PROC);
186 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
187 ustream_fd_init(&klog, fd);
191 static int syslog_open(void)
195 DEBUG(1, "Opening %s\n", log_dev);
197 fd = usock(USOCK_UNIX | USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
199 ERROR("Failed to open %s\n", log_dev);
202 chmod(log_dev, 0666);
203 ustream_fd_init(&slog, fd);
207 struct log_head* log_list(int count, struct log_head *h)
209 unsigned int min = count;
212 min = (count < current_id) ? (current_id - count) : (0);
213 if (!h && oldest->id >= min)
218 while (h != newest) {
219 h = log_next(h, h->size);
220 if (!h->size && (h > newest))
222 if (h->id >= min && (h != newest))
229 int log_buffer_init(int size)
231 struct log_head *_log = malloc(size);
234 ERROR("Failed to initialize log buffer with size %d\n", log_size);
238 memset(_log, 0, size);
240 if (log && ((log_size + sizeof(struct log_head)) < size)) {
241 struct log_head *start = _log;
242 struct log_head *end = ((void*) _log) + size;
245 l = log_list(0, NULL);
246 while ((start < end) && l && l->size) {
247 memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
248 start = (struct log_head *) &l->data[PAD(l->size)];
255 log_end = ((void*) log) + size;
257 oldest = newest = log = _log;
258 log_end = ((void*) log) + size;
267 regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
268 regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
270 if (log_buffer_init(log_size)) {
271 ERROR("Failed to allocate log memory\n");
277 openlog("procd", LOG_PID, LOG_DAEMON);