d0b528d6c7b4bf6f42aeffb08e6a2f8b774a7271
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>
37 #define LOG_DEFAULT_SIZE (16 * 1024)
38 #define LOG_DEFAULT_SOCKET "/dev/log"
39 #define LOG_LINE_LEN 256
40 #define SYSLOG_PADDING 16
42 #define KLOG_DEFAULT_PROC "/proc/kmsg"
44 #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
46 static char *log_dev
= LOG_DEFAULT_SOCKET
;
47 static int log_size
= LOG_DEFAULT_SIZE
;
48 static struct log_head
*log
, *log_end
, *oldest
, *newest
;
49 static int current_id
= 0;
50 static regex_t pat_prio
;
51 static regex_t pat_tstamp
;
53 static struct log_head
*
54 log_next(struct log_head
*h
, int size
)
56 struct log_head
*n
= (struct log_head
*) &h
->data
[PAD(sizeof(struct log_head
) + size
)];
58 return (n
>= log_end
) ? (log
) : (n
);
62 log_add(char *buf
, int size
, int source
)
64 regmatch_t matches
[4];
65 struct log_head
*next
;
69 /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
75 /* strip trailing newline */
76 if (buf
[size
- 2] == '\n') {
81 /* strip the priority */
82 ret
= regexec(&pat_prio
, buf
, 3, matches
, 0);
84 priority
= atoi(&buf
[matches
[1].rm_so
]);
85 size
-= matches
[2].rm_so
;
86 buf
+= matches
[2].rm_so
;
90 /* strip kernel timestamp */
91 ret
= regexec(&pat_tstamp
,buf
, 4, matches
, 0);
92 if ((source
== SOURCE_KLOG
) && !ret
) {
93 size
-= matches
[3].rm_so
;
94 buf
+= matches
[3].rm_so
;
98 /* strip syslog timestamp */
99 if ((source
== SOURCE_SYSLOG
) && (size
> SYSLOG_PADDING
) && (buf
[SYSLOG_PADDING
- 1] == ' ')) {
100 size
-= SYSLOG_PADDING
;
101 buf
+= SYSLOG_PADDING
;
104 //fprintf(stderr, "-> %d - %s\n", priority, buf);
106 /* find new oldest entry */
107 next
= log_next(newest
, size
);
109 while ((oldest
> newest
) && (oldest
<= next
) && (oldest
!= log
))
110 oldest
= log_next(oldest
, oldest
->size
);
112 //fprintf(stderr, "Log wrap\n");
114 next
= log_next(log
, size
);
115 for (oldest
= log
; oldest
<= next
; oldest
= log_next(oldest
, oldest
->size
))
120 /* add the log message */
122 newest
->id
= current_id
++;
123 newest
->priority
= priority
;
124 newest
->source
= source
;
125 clock_gettime(CLOCK_REALTIME
, &newest
->ts
);
126 strcpy(newest
->data
, buf
);
128 ubus_notify_log(newest
);
134 slog_cb(struct ustream
*s
, int bytes
)
136 struct ustream_buf
*buf
= s
->r
.head
;
141 str
= ustream_get_read_buf(s
, NULL
);
144 len
= strlen(buf
->data
);
147 ustream_consume(s
, 1);
150 log_add(buf
->data
, len
+ 1, SOURCE_SYSLOG
);
151 ustream_consume(s
, len
);
157 klog_cb(struct ustream
*s
, int bytes
)
159 struct ustream_buf
*buf
= s
->r
.head
;
164 str
= ustream_get_read_buf(s
, NULL
);
167 newline
= strchr(buf
->data
, '\n');
171 len
= newline
+ 1 - str
;
172 log_add(buf
->data
, len
, SOURCE_KLOG
);
173 ustream_consume(s
, len
);
177 struct ustream_fd slog
= {
178 .stream
.string_data
= true,
179 .stream
.notify_read
= slog_cb
,
182 struct ustream_fd klog
= {
183 .stream
.string_data
= true,
184 .stream
.notify_read
= klog_cb
,
192 fd
= open(KLOG_DEFAULT_PROC
, O_RDONLY
| O_NONBLOCK
);
194 fprintf(stderr
, "Failed to open %s\n", KLOG_DEFAULT_PROC
);
197 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
198 ustream_fd_init(&klog
, fd
);
208 fd
= usock(USOCK_UNIX
| USOCK_UDP
| USOCK_SERVER
| USOCK_NONBLOCK
, log_dev
, NULL
);
210 fprintf(stderr
,"Failed to open %s\n", log_dev
);
213 chmod(log_dev
, 0666);
214 ustream_fd_init(&slog
, fd
);
219 log_list(int count
, struct log_head
*h
)
221 unsigned int min
= count
;
224 min
= (count
< current_id
) ? (current_id
- count
) : (0);
225 if (!h
&& oldest
->id
>= min
)
230 while (h
!= newest
) {
231 h
= log_next(h
, h
->size
);
232 if (!h
->size
&& (h
> newest
))
234 if (h
->id
>= min
&& (h
!= newest
))
242 log_buffer_init(int size
)
244 struct log_head
*_log
= malloc(size
);
247 fprintf(stderr
, "Failed to initialize log buffer with size %d\n", log_size
);
251 memset(_log
, 0, size
);
253 if (log
&& ((log_size
+ sizeof(struct log_head
)) < size
)) {
254 struct log_head
*start
= _log
;
255 struct log_head
*end
= ((void*) _log
) + size
;
258 l
= log_list(0, NULL
);
259 while ((start
< end
) && l
&& l
->size
) {
260 memcpy(start
, l
, PAD(sizeof(struct log_head
) + l
->size
));
261 start
= (struct log_head
*) &l
->data
[PAD(l
->size
)];
268 log_end
= ((void*) log
) + size
;
270 oldest
= newest
= log
= _log
;
271 log_end
= ((void*) log
) + size
;
279 log_init(int _log_size
)
282 log_size
= _log_size
;
284 /* reserve 512 bytes for protocol overhead */
285 if (log_size
> (UBUS_MAX_MSGLEN
- 512))
286 log_size
= UBUS_MAX_MSGLEN
- 512;
288 regcomp(&pat_prio
, "^<([0-9]*)>(.*)", REG_EXTENDED
);
289 regcomp(&pat_tstamp
, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED
);
291 if (log_buffer_init(log_size
)) {
292 fprintf(stderr
, "Failed to allocate log memory\n");
298 openlog("sysinit", LOG_CONS
, LOG_DAEMON
);
304 ustream_free(&slog
.stream
);
305 ustream_free(&klog
.stream
);