add debug handler
[project/procd.git] / syslog.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
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
8  *
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.
13  */
14
15 #include <linux/un.h>
16
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20
21 #include <fcntl.h>
22 #include <regex.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <libubox/uloop.h>
30 #include <libubox/usock.h>
31 #include <libubox/ustream.h>
32
33 #include "procd.h"
34 #include "syslog.h"
35
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
40
41 #define KLOG_DEFAULT_PROC       "/proc/kmsg"
42
43 #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
44
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 regex_t pat_prio;
50 regex_t pat_tstamp;
51
52 static struct log_head *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 log_add(char *buf, int size, int source)
60 {
61         regmatch_t matches[4];
62         struct log_head *next;
63         int priority = 0;
64         int ret;
65
66         /* strip trailing newline */
67         if (buf[size - 2] == '\n') {
68                 buf[size - 2] = '\0';
69                 size -= 1;
70         }
71
72         /* strip the priority */
73         ret = regexec(&pat_prio, buf, 3, matches, 0);
74         if (!ret) {
75                 priority = atoi(&buf[matches[1].rm_so]);
76                 size -= matches[2].rm_so;
77                 buf += matches[2].rm_so;
78         }
79
80 #if 0
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;
86         }
87 #endif
88
89         /* strip syslog timestamp */
90         if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
91                 size -= SYSLOG_PADDING;
92                 buf += SYSLOG_PADDING;
93         }
94
95         DEBUG(2, "-> %d - %s\n", priority, buf);
96
97         /* find new oldest entry */
98         next = log_next(newest, size);
99         if (next > newest) {
100                 while ((oldest > newest) && (oldest <= next) && (oldest != log))
101                         oldest = log_next(oldest, oldest->size);
102         } else {
103                 DEBUG(2, "Log wrap\n");
104                 newest->size = 0;
105                 next = log_next(log, size);
106                 for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
107                         ;
108                 newest = log;
109         }
110
111         /* add the log message */
112         newest->size = size;
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);
118
119         ubus_notify_log(newest);
120
121         newest = next;
122 }
123
124 static void slog_cb(struct ustream *s, int bytes)
125 {
126         struct ustream_buf *buf = s->r.head;
127         char *str;
128         int len;
129
130         do {
131                 str = ustream_get_read_buf(s, NULL);
132                 if (!str)
133                         break;
134                 len = strlen(buf->data);
135                 if (!len) {
136                         bytes -= 1;
137                         ustream_consume(s, 1);
138                         continue;
139                 }
140                 log_add(buf->data, len + 1, SOURCE_SYSLOG);
141                 ustream_consume(s, len);
142                 bytes -= len;
143         } while (bytes > 0);
144 }
145
146 static void klog_cb(struct ustream *s, int bytes)
147 {
148         struct ustream_buf *buf = s->r.head;
149         char *newline, *str;
150         int len;
151
152         do {
153                 str = ustream_get_read_buf(s, NULL);
154                 if (!str)
155                         break;
156                 newline = strchr(buf->data, '\n');
157                 if (!newline)
158                         break;
159                 *newline = 0;
160                 len = newline + 1 - str;
161                 log_add(buf->data, len, SOURCE_KLOG);
162                 ustream_consume(s, len);
163         } while (1);
164 }
165
166 struct ustream_fd slog = {
167         .stream.string_data = true,
168         .stream.notify_read = slog_cb,
169 };
170
171 struct ustream_fd klog = {
172         .stream.string_data = true,
173         .stream.notify_read = klog_cb,
174 };
175
176 static int klog_open(void)
177 {
178         int fd;
179
180         DEBUG(1, "Opening %s\n", KLOG_DEFAULT_PROC);
181         fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
182         if (fd < 0) {
183                 ERROR("Failed to open %s\n", KLOG_DEFAULT_PROC);
184                 return -1;
185         }
186         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
187         ustream_fd_init(&klog, fd);
188         return 0;
189 }
190
191 static int syslog_open(void)
192 {
193         int fd;
194
195         DEBUG(1, "Opening %s\n", log_dev);
196         unlink(log_dev);
197         fd = usock(USOCK_UNIX | USOCK_UDP |  USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
198         if (fd < 0) {
199                 ERROR("Failed to open %s\n", log_dev);
200                 return -1;
201         }
202         chmod(log_dev, 0666);
203         ustream_fd_init(&slog, fd);
204         return 0;
205 }
206
207 struct log_head* log_list(int count, struct log_head *h)
208 {
209         unsigned int min = count;
210
211         if (count)
212                 min = (count < current_id) ? (current_id - count) : (0);
213         if (!h && oldest->id >= min)
214                 return oldest;
215         if (!h)
216                 h = oldest;
217
218         while (h != newest) {
219                 h = log_next(h, h->size);
220                 if (!h->size && (h > newest))
221                         h = log;
222                 if (h->id >= min && (h != newest))
223                         return h;
224         }
225
226         return NULL;
227 }
228
229 int log_buffer_init(int size)
230 {
231         struct log_head *_log = malloc(size);
232
233         if (!_log) {
234                 ERROR("Failed to initialize log buffer with size %d\n", log_size);
235                 return -1;
236         }
237
238         memset(_log, 0, size);
239
240         if (log && ((log_size + sizeof(struct log_head)) < size)) {
241                 struct log_head *start = _log;
242                 struct log_head *end = ((void*) _log) + size;
243                 struct log_head *l;
244
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)];
249                         l = log_list(0, l);
250                 }
251                 free(log);
252                 newest = start;
253                 newest->size = 0;
254                 oldest = log = _log;
255                 log_end = ((void*) log) + size;
256         } else {
257                 oldest = newest = log = _log;
258                 log_end = ((void*) log) + size;
259         }
260         log_size = size;
261
262         return 0;
263 }
264
265 int log_buffer_size(void)
266 {
267         return log_size;
268 }
269
270 void log_init(void)
271 {
272         regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
273         regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
274
275         if (log_buffer_init(log_size)) {
276                 ERROR("Failed to allocate log memory\n");
277                 exit(-1);
278         }
279
280         syslog_open();
281         klog_open();
282         openlog("procd", LOG_PID, LOG_DAEMON);
283 }