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>
31 #include <libubox/uloop.h>
32 #include <libubox/usock.h>
33 #include <libubox/ustream.h>
37 #define LOG_DEFAULT_SIZE (16 * 1024)
38 #define LOG_DEFAULT_SOCKET "/dev/log"
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
*
53 log_next(struct log_head
*h
, int size
)
55 struct log_head
*n
= (struct log_head
*) &h
->data
[PAD(size
)];
57 return (n
>= log_end
) ? (log
) : (n
);
61 log_add(char *buf
, int size
, int source
)
63 regmatch_t matches
[4];
64 struct log_head
*next
;
69 /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
71 fprintf(stderr
, "%s", buf
);
75 for (c
= buf
; *c
; c
++) {
88 /* strip the priority */
89 ret
= regexec(&pat_prio
, buf
, 3, matches
, 0);
91 priority
= atoi(&buf
[matches
[1].rm_so
]);
92 size
-= matches
[2].rm_so
;
93 buf
+= matches
[2].rm_so
;
97 /* strip kernel timestamp */
98 ret
= regexec(&pat_tstamp
,buf
, 4, matches
, 0);
99 if ((source
== SOURCE_KLOG
) && !ret
) {
100 size
-= matches
[3].rm_so
;
101 buf
+= matches
[3].rm_so
;
105 /* strip syslog timestamp */
106 if ((source
== SOURCE_SYSLOG
) && (size
> SYSLOG_PADDING
) && (buf
[SYSLOG_PADDING
- 1] == ' ')) {
107 size
-= SYSLOG_PADDING
;
108 buf
+= SYSLOG_PADDING
;
111 //fprintf(stderr, "-> %d - %s\n", priority, buf);
113 /* find new oldest entry */
114 next
= log_next(newest
, size
);
116 while ((oldest
> newest
) && (oldest
<= next
) && (oldest
!= log
))
117 oldest
= log_next(oldest
, oldest
->size
);
119 //fprintf(stderr, "Log wrap\n");
121 next
= log_next(log
, size
);
122 for (oldest
= log
; oldest
<= next
; oldest
= log_next(oldest
, oldest
->size
))
127 /* add the log message */
129 newest
->id
= current_id
++;
130 newest
->priority
= priority
;
131 newest
->source
= source
;
132 clock_gettime(CLOCK_REALTIME
, &newest
->ts
);
133 strcpy(newest
->data
, buf
);
135 ubus_notify_log(newest
);
141 syslog_handle_fd(struct uloop_fd
*fd
, unsigned int events
)
143 static char buf
[LOG_LINE_SIZE
];
147 len
= recv(fd
->fd
, buf
, LOG_LINE_SIZE
- 1, 0);
159 log_add(buf
, strlen(buf
) + 1, SOURCE_SYSLOG
);
164 klog_cb(struct ustream
*s
, int bytes
)
166 struct ustream_buf
*buf
= s
->r
.head
;
171 str
= ustream_get_read_buf(s
, NULL
);
174 newline
= strchr(buf
->data
, '\n');
178 len
= newline
+ 1 - str
;
179 log_add(buf
->data
, len
, SOURCE_KLOG
);
180 ustream_consume(s
, len
);
184 static struct uloop_fd syslog_fd
= {
185 .cb
= syslog_handle_fd
188 static struct ustream_fd klog
= {
189 .stream
.string_data
= true,
190 .stream
.notify_read
= klog_cb
,
198 fd
= open(KLOG_DEFAULT_PROC
, O_RDONLY
| O_NONBLOCK
);
200 fprintf(stderr
, "Failed to open %s\n", KLOG_DEFAULT_PROC
);
203 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
204 ustream_fd_init(&klog
, fd
);
212 syslog_fd
.fd
= usock(USOCK_UNIX
| USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
, log_dev
, NULL
);
213 if (syslog_fd
.fd
< 0) {
214 fprintf(stderr
,"Failed to open %s\n", log_dev
);
217 chmod(log_dev
, 0666);
218 uloop_fd_add(&syslog_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
224 log_list(int count
, struct log_head
*h
)
226 unsigned int min
= count
;
229 min
= (count
< current_id
) ? (current_id
- count
) : (0);
230 if (!h
&& oldest
->id
>= min
)
235 while (h
!= newest
) {
236 h
= log_next(h
, h
->size
);
237 if (!h
->size
&& (h
> newest
))
239 if (h
->id
>= min
&& (h
!= newest
))
247 log_buffer_init(int size
)
249 struct log_head
*_log
= calloc(1, size
);
252 fprintf(stderr
, "Failed to initialize log buffer with size %d\n", log_size
);
256 if (log
&& ((log_size
+ sizeof(struct log_head
)) < size
)) {
257 struct log_head
*start
= _log
;
258 struct log_head
*end
= ((void*) _log
) + size
;
261 l
= log_list(0, NULL
);
262 while ((start
< end
) && l
&& l
->size
) {
263 memcpy(start
, l
, PAD(sizeof(struct log_head
) + l
->size
));
264 start
= (struct log_head
*) &l
->data
[PAD(l
->size
)];
271 log_end
= ((void*) log
) + size
;
273 oldest
= newest
= log
= _log
;
274 log_end
= ((void*) log
) + size
;
282 log_init(int _log_size
)
285 log_size
= _log_size
;
287 regcomp(&pat_prio
, "^<([0-9]*)>(.*)", REG_EXTENDED
);
288 regcomp(&pat_tstamp
, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED
);
290 if (log_buffer_init(log_size
)) {
291 fprintf(stderr
, "Failed to allocate log memory\n");
297 openlog("sysinit", LOG_CONS
, LOG_DAEMON
);
303 if (syslog_fd
.registered
) {
304 uloop_fd_delete(&syslog_fd
);
308 ustream_free(&klog
.stream
);
312 regfree(&pat_tstamp
);