Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/sysklogd/klogd.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/sysklogd/klogd.c
r821 r1770 18 18 */ 19 19 20 #include "busybox.h" 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <signal.h> /* for our signal() handlers */ 24 #include <string.h> /* strncpy() */ 25 #include <errno.h> /* errno and friends */ 26 #include <unistd.h> 27 #include <ctype.h> 20 #include "libbb.h" 28 21 #include <sys/syslog.h> 29 22 #include <sys/klog.h> … … 33 26 klogctl(7, NULL, 0); 34 27 klogctl(0, 0, 0); 35 /* logMessage(0, "Kernel log daemon exiting."); */ 36 syslog(LOG_NOTICE, "Kernel log daemon exiting."); 28 syslog(LOG_NOTICE, "Kernel log daemon exiting"); 37 29 exit(EXIT_SUCCESS); 38 30 } … … 41 33 #define OPT_FOREGROUND 2 42 34 43 #define KLOGD_LOGBUF_SIZE 4096 35 #define KLOGD_LOGBUF_SIZE BUFSIZ 36 #define log_buffer bb_common_bufsiz1 44 37 38 int klogd_main(int argc, char **argv); 45 39 int klogd_main(int argc, char **argv) 46 40 { 47 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE); 48 int console_log_level = -1; 49 int priority = LOG_INFO; 50 int i, n, lastc; 41 int i = i; /* silence gcc */ 51 42 char *start; 52 43 44 /* do normal option parsing */ 45 getopt32(argv, "c:n", &start); 53 46 54 { 55 unsigned long opt; 47 if (option_mask32 & OPT_LEVEL) { 48 /* Valid levels are between 1 and 8 */ 49 i = xatoul_range(start, 1, 8); 50 } 56 51 57 /* do normal option parsing */ 58 opt = bb_getopt_ulflags(argc, argv, "c:n", &start); 59 60 if (opt & OPT_LEVEL) { 61 /* Valid levels are between 1 and 8 */ 62 console_log_level = bb_xgetlarg(start, 10, 1, 8); 63 } 64 65 if (!(opt & OPT_FOREGROUND)) { 66 #ifdef BB_NOMMU 67 vfork_daemon_rexec(0, 1, argc, argv, "-n"); 68 #else 69 bb_xdaemon(0, 1); 70 #endif 71 } 52 if (!(option_mask32 & OPT_FOREGROUND)) { 53 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); 72 54 } 73 55 … … 83 65 klogctl(1, NULL, 0); 84 66 85 /* Set level of kernel console messaging. .*/86 if ( console_log_level != -1)87 klogctl(8, NULL, console_log_level);67 /* Set level of kernel console messaging. */ 68 if (option_mask32 & OPT_LEVEL) 69 klogctl(8, NULL, i); 88 70 89 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);71 syslog(LOG_NOTICE, "klogd started: %s", bb_banner); 90 72 73 /* Note: this code does not detect incomplete messages 74 * (messages not ending with '\n' or just when kernel 75 * generates too many messages for us to keep up) 76 * and will split them in two separate lines */ 91 77 while (1) { 92 /* Use kernel syscalls */ 93 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE); 94 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE); 78 int n; 79 int priority; 80 81 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1); 95 82 if (n < 0) { 96 83 if (errno == EINTR) 97 84 continue; 98 syslog(LOG_ERR, "klogd: Error from sys_sycall: %d - %m.\n",99 100 exit(EXIT_FAILURE);85 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m", 86 errno); 87 break; 101 88 } 102 103 /* klogctl buffer parsing modelled after code in dmesg.c */ 104 start = &log_buffer[0]; 105 lastc = '\0'; 106 for (i = 0; i < n; i++) { 107 if (lastc == '\0' && log_buffer[i] == '<') { 108 priority = 0; 89 log_buffer[n] = '\n'; 90 i = 0; 91 while (i < n) { 92 priority = LOG_INFO; 93 start = &log_buffer[i]; 94 if (log_buffer[i] == '<') { 109 95 i++; 110 while (log_buffer[i] >= '0' && log_buffer[i] <= '9') { 111 priority = priority * 10 + (log_buffer[i] - '0'); 96 // kernel never ganerates multi-digit prios 97 //priority = 0; 98 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') { 99 // priority = priority * 10 + (log_buffer[i] - '0'); 100 // i++; 101 //} 102 if (isdigit(log_buffer[i])) { 103 priority = (log_buffer[i] - '0'); 112 104 i++; 113 105 } … … 116 108 start = &log_buffer[i]; 117 109 } 118 if (log_buffer[i] == '\n') { 119 log_buffer[i] = '\0'; /* zero terminate this message */ 120 syslog(priority, "%s", start); 121 start = &log_buffer[i + 1]; 122 priority = LOG_INFO; 123 } 124 lastc = log_buffer[i]; 110 while (log_buffer[i] != '\n') 111 i++; 112 log_buffer[i] = '\0'; 113 syslog(priority, "%s", start); 114 i++; 125 115 } 126 116 } 127 if (ENABLE_FEATURE_CLEAN_UP)128 RELEASE_CONFIG_BUFFER(log_buffer);129 117 130 return EXIT_ SUCCESS;118 return EXIT_FAILURE; 131 119 }
Note:
See TracChangeset
for help on using the changeset viewer.