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>
29 #include <libubox/uloop.h>
30 #include <libubox/usock.h>
31 #include <libubox/ustream.h>
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
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(sizeof(struct log_head
) + 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 slog_cb(struct ustream
*s
, int bytes
)
134 struct ustream_buf
*buf
= s
->r
.head
;
139 str
= ustream_get_read_buf(s
, NULL
);
142 len
= strlen(buf
->data
);
145 ustream_consume(s
, 1);
148 log_add(buf
->data
, len
+ 1, SOURCE_SYSLOG
);
149 ustream_consume(s
, len
);
155 klog_cb(struct ustream
*s
, int bytes
)
157 struct ustream_buf
*buf
= s
->r
.head
;
162 str
= ustream_get_read_buf(s
, NULL
);
165 newline
= strchr(buf
->data
, '\n');
169 len
= newline
+ 1 - str
;
170 log_add(buf
->data
, len
, SOURCE_KLOG
);
171 ustream_consume(s
, len
);
175 struct ustream_fd slog
= {
176 .stream
.string_data
= true,
177 .stream
.notify_read
= slog_cb
,
180 struct ustream_fd klog
= {
181 .stream
.string_data
= true,
182 .stream
.notify_read
= klog_cb
,
190 fd
= open(KLOG_DEFAULT_PROC
, O_RDONLY
| O_NONBLOCK
);
192 fprintf(stderr
, "Failed to open %s\n", KLOG_DEFAULT_PROC
);
195 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
196 ustream_fd_init(&klog
, fd
);
206 fd
= usock(USOCK_UNIX
| USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
, log_dev
, NULL
);
208 fprintf(stderr
,"Failed to open %s\n", log_dev
);
211 chmod(log_dev
, 0666);
212 ustream_fd_init(&slog
, fd
);
217 log_list(int count
, struct log_head
*h
)
219 unsigned int min
= count
;
222 min
= (count
< current_id
) ? (current_id
- count
) : (0);
223 if (!h
&& oldest
->id
>= min
)
228 while (h
!= newest
) {
229 h
= log_next(h
, h
->size
);
230 if (!h
->size
&& (h
> newest
))
232 if (h
->id
>= min
&& (h
!= newest
))
240 log_buffer_init(int size
)
242 struct log_head
*_log
= malloc(size
);
245 fprintf(stderr
, "Failed to initialize log buffer with size %d\n", log_size
);
249 memset(_log
, 0, size
);
251 if (log
&& ((log_size
+ sizeof(struct log_head
)) < size
)) {
252 struct log_head
*start
= _log
;
253 struct log_head
*end
= ((void*) _log
) + size
;
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
)];
266 log_end
= ((void*) log
) + size
;
268 oldest
= newest
= log
= _log
;
269 log_end
= ((void*) log
) + size
;
277 log_init(int _log_size
)
280 log_size
= _log_size
;
282 regcomp(&pat_prio
, "^<([0-9]*)>(.*)", REG_EXTENDED
);
283 regcomp(&pat_tstamp
, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED
);
285 if (log_buffer_init(log_size
)) {
286 fprintf(stderr
, "Failed to allocate log memory\n");
292 openlog("sysinit", LOG_CONS
, LOG_DAEMON
);
298 ustream_free(&slog
.stream
);
299 ustream_free(&klog
.stream
);
304 regfree(&pat_tstamp
);