syslog.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  3. *
  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
  7. *
  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.
  12. */
  13. #include <linux/un.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <regex.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <syslog.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <libubox/uloop.h>
  28. #include <libubox/usock.h>
  29. #include <libubox/ustream.h>
  30. #include "syslog.h"
  31. #define LOG_DEFAULT_SIZE (16 * 1024)
  32. #define LOG_DEFAULT_SOCKET "/dev/log"
  33. #define SYSLOG_PADDING 16
  34. #define KLOG_DEFAULT_PROC "/proc/kmsg"
  35. #define PAD(x) (x % 4) ? (((x) - (x % 4)) + 4) : (x)
  36. static char *log_dev = LOG_DEFAULT_SOCKET;
  37. static int log_size = LOG_DEFAULT_SIZE;
  38. static struct log_head *log, *log_end, *oldest, *newest;
  39. static int current_id = 0;
  40. static regex_t pat_prio;
  41. static regex_t pat_tstamp;
  42. static struct log_head*
  43. log_next(struct log_head *h, int size)
  44. {
  45. struct log_head *n = (struct log_head *) &h->data[PAD(size)];
  46. return (n >= log_end) ? (log) : (n);
  47. }
  48. void
  49. log_add(char *buf, int size, int source)
  50. {
  51. regmatch_t matches[4];
  52. struct log_head *next;
  53. int priority = 0;
  54. int ret;
  55. char *c;
  56. /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
  57. if (!log) {
  58. fprintf(stderr, "%s", buf);
  59. return;
  60. }
  61. for (c = buf; *c; c++) {
  62. if (*c == '\n')
  63. *c = ' ';
  64. }
  65. c = buf + size - 2;
  66. while (isspace(*c)) {
  67. size--;
  68. c--;
  69. }
  70. buf[size - 1] = 0;
  71. /* strip the priority */
  72. ret = regexec(&pat_prio, buf, 3, matches, 0);
  73. if (!ret) {
  74. priority = atoi(&buf[matches[1].rm_so]);
  75. size -= matches[2].rm_so;
  76. buf += matches[2].rm_so;
  77. }
  78. #if 0
  79. /* strip kernel timestamp */
  80. ret = regexec(&pat_tstamp,buf, 4, matches, 0);
  81. if ((source == SOURCE_KLOG) && !ret) {
  82. size -= matches[3].rm_so;
  83. buf += matches[3].rm_so;
  84. }
  85. #endif
  86. /* strip syslog timestamp */
  87. if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) {
  88. size -= SYSLOG_PADDING;
  89. buf += SYSLOG_PADDING;
  90. }
  91. //fprintf(stderr, "-> %d - %s\n", priority, buf);
  92. /* find new oldest entry */
  93. next = log_next(newest, size);
  94. if (next > newest) {
  95. while ((oldest > newest) && (oldest <= next) && (oldest != log))
  96. oldest = log_next(oldest, oldest->size);
  97. } else {
  98. //fprintf(stderr, "Log wrap\n");
  99. newest->size = 0;
  100. next = log_next(log, size);
  101. for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size))
  102. ;
  103. newest = log;
  104. }
  105. /* add the log message */
  106. newest->size = size;
  107. newest->id = current_id++;
  108. newest->priority = priority;
  109. newest->source = source;
  110. clock_gettime(CLOCK_REALTIME, &newest->ts);
  111. strcpy(newest->data, buf);
  112. ubus_notify_log(newest);
  113. newest = next;
  114. }
  115. static void
  116. syslog_handle_fd(struct uloop_fd *fd, unsigned int events)
  117. {
  118. static char buf[LOG_LINE_SIZE];
  119. int len;
  120. while (1) {
  121. len = recv(fd->fd, buf, LOG_LINE_SIZE - 1, 0);
  122. if (len < 0) {
  123. if (errno == EINTR)
  124. continue;
  125. break;
  126. }
  127. if (!len)
  128. break;
  129. buf[len] = 0;
  130. log_add(buf, strlen(buf) + 1, SOURCE_SYSLOG);
  131. }
  132. }
  133. static void
  134. klog_cb(struct ustream *s, int bytes)
  135. {
  136. struct ustream_buf *buf = s->r.head;
  137. char *newline, *str;
  138. int len;
  139. do {
  140. str = ustream_get_read_buf(s, NULL);
  141. if (!str)
  142. break;
  143. newline = strchr(buf->data, '\n');
  144. if (!newline)
  145. break;
  146. *newline = 0;
  147. len = newline + 1 - str;
  148. log_add(buf->data, len, SOURCE_KLOG);
  149. ustream_consume(s, len);
  150. } while (1);
  151. }
  152. static struct uloop_fd syslog_fd = {
  153. .cb = syslog_handle_fd
  154. };
  155. static struct ustream_fd klog = {
  156. .stream.string_data = true,
  157. .stream.notify_read = klog_cb,
  158. };
  159. static int
  160. klog_open(void)
  161. {
  162. int fd;
  163. fd = open(KLOG_DEFAULT_PROC, O_RDONLY | O_NONBLOCK);
  164. if (fd < 0) {
  165. fprintf(stderr, "Failed to open %s\n", KLOG_DEFAULT_PROC);
  166. return -1;
  167. }
  168. fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  169. ustream_fd_init(&klog, fd);
  170. return 0;
  171. }
  172. static int
  173. syslog_open(void)
  174. {
  175. unlink(log_dev);
  176. syslog_fd.fd = usock(USOCK_UNIX | USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, log_dev, NULL);
  177. if (syslog_fd.fd < 0) {
  178. fprintf(stderr,"Failed to open %s\n", log_dev);
  179. return -1;
  180. }
  181. chmod(log_dev, 0666);
  182. uloop_fd_add(&syslog_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  183. return 0;
  184. }
  185. struct log_head*
  186. log_list(int count, struct log_head *h)
  187. {
  188. unsigned int min = count;
  189. if (count)
  190. min = (count < current_id) ? (current_id - count) : (0);
  191. if (!h && oldest->id >= min)
  192. return oldest;
  193. if (!h)
  194. h = oldest;
  195. while (h != newest) {
  196. h = log_next(h, h->size);
  197. if (!h->size && (h > newest))
  198. h = log;
  199. if (h->id >= min && (h != newest))
  200. return h;
  201. }
  202. return NULL;
  203. }
  204. int
  205. log_buffer_init(int size)
  206. {
  207. struct log_head *_log = calloc(1, size);
  208. if (!_log) {
  209. fprintf(stderr, "Failed to initialize log buffer with size %d\n", log_size);
  210. return -1;
  211. }
  212. if (log && ((log_size + sizeof(struct log_head)) < size)) {
  213. struct log_head *start = _log;
  214. struct log_head *end = ((void*) _log) + size;
  215. struct log_head *l;
  216. l = log_list(0, NULL);
  217. while ((start < end) && l && l->size) {
  218. memcpy(start, l, PAD(sizeof(struct log_head) + l->size));
  219. start = (struct log_head *) &l->data[PAD(l->size)];
  220. l = log_list(0, l);
  221. }
  222. free(log);
  223. newest = start;
  224. newest->size = 0;
  225. oldest = log = _log;
  226. log_end = ((void*) log) + size;
  227. } else {
  228. oldest = newest = log = _log;
  229. log_end = ((void*) log) + size;
  230. }
  231. log_size = size;
  232. return 0;
  233. }
  234. void
  235. log_init(int _log_size)
  236. {
  237. if (_log_size > 0)
  238. log_size = _log_size;
  239. regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED);
  240. regcomp(&pat_tstamp, "^\[[ 0]*([0-9]*).([0-9]*)] (.*)", REG_EXTENDED);
  241. if (log_buffer_init(log_size)) {
  242. fprintf(stderr, "Failed to allocate log memory\n");
  243. exit(-1);
  244. }
  245. syslog_open();
  246. klog_open();
  247. openlog("sysinit", LOG_CONS, LOG_DAEMON);
  248. }
  249. void
  250. log_shutdown(void)
  251. {
  252. if (syslog_fd.registered) {
  253. uloop_fd_delete(&syslog_fd);
  254. close(syslog_fd.fd);
  255. }
  256. ustream_free(&klog.stream);
  257. close(klog.fd.fd);
  258. free(log);
  259. regfree(&pat_prio);
  260. regfree(&pat_tstamp);
  261. }