123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include <syslog.h>
- #if ENABLE_FEATURE_KLOGD_KLOGCTL
- # include <sys/klog.h>
- static void klogd_open(void)
- {
-
- klogctl(1, NULL, 0);
- }
- static void klogd_setloglevel(int lvl)
- {
-
- klogctl(8, NULL, lvl);
- }
- static int klogd_read(char *bufp, int len)
- {
- return klogctl(2, bufp, len);
- }
- # define READ_ERROR "klogctl(2) error"
- static void klogd_close(void)
- {
-
- klogctl(7, NULL, 0);
- klogctl(0, NULL, 0);
- }
- #else
- # ifndef _PATH_KLOG
- # ifdef __GNU__
- # define _PATH_KLOG "/dev/klog"
- # else
- # error "your system's _PATH_KLOG is unknown"
- # endif
- # endif
- # define PATH_PRINTK "/proc/sys/kernel/printk"
- enum { klogfd = 3 };
- static void klogd_open(void)
- {
- int fd = xopen(_PATH_KLOG, O_RDONLY);
- xmove_fd(fd, klogfd);
- }
- static void klogd_setloglevel(int lvl)
- {
- FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
- if (fp) {
-
- fprintf(fp, "%u\n", lvl);
- fclose(fp);
- }
- }
- static int klogd_read(char *bufp, int len)
- {
- return read(klogfd, bufp, len);
- }
- # define READ_ERROR "read error"
- static void klogd_close(void)
- {
- klogd_setloglevel(7);
- if (ENABLE_FEATURE_CLEAN_UP)
- close(klogfd);
- }
- #endif
- #define log_buffer bb_common_bufsiz1
- enum {
- KLOGD_LOGBUF_SIZE = COMMON_BUFSIZE,
- OPT_LEVEL = (1 << 0),
- OPT_FOREGROUND = (1 << 1),
- };
- int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int klogd_main(int argc UNUSED_PARAM, char **argv)
- {
- int i = 0;
- char *opt_c;
- int opt;
- int used;
- setup_common_bufsiz();
- opt = getopt32(argv, "c:n", &opt_c);
- if (opt & OPT_LEVEL) {
-
- i = xatou_range(opt_c, 1, 8);
- }
- if (!(opt & OPT_FOREGROUND)) {
- bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
- }
- logmode = LOGMODE_SYSLOG;
-
- klogd_open();
- openlog("kernel", 0, LOG_KERN);
-
- if (i)
- klogd_setloglevel(i);
- signal(SIGHUP, SIG_IGN);
-
- bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
- syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
- write_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
- used = 0;
- while (!bb_got_signal) {
- int n;
- int priority;
- char *start;
-
- start = log_buffer + used;
- n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
- if (n < 0) {
- if (errno == EINTR)
- continue;
- bb_perror_msg(READ_ERROR);
- break;
- }
- start[n] = '\0';
-
- start = log_buffer;
- while (1) {
- char *newline = strchrnul(start, '\n');
- if (*newline == '\0') {
-
-
- overlapping_strcpy(log_buffer, start);
- used = newline - start;
- if (used < KLOGD_LOGBUF_SIZE-1) {
-
- break;
- }
-
- used = 0;
- newline = NULL;
- } else {
- *newline++ = '\0';
- }
-
- priority = LOG_INFO;
- if (*start == '<') {
- start++;
- if (*start)
- priority = strtoul(start, &start, 10);
- if (*start == '>')
- start++;
- }
-
- if (*start)
- syslog(priority, "%s", start);
- if (!newline)
- break;
- start = newline;
- }
- }
- klogd_close();
- syslog(LOG_NOTICE, "klogd: exiting");
- remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
- if (bb_got_signal)
- kill_myself_with_sig(bb_got_signal);
- return EXIT_FAILURE;
- }
|