[Toybox] [cleanup] klogd
Felix Janda
felix.janda at posteo.de
Sat Aug 10 11:28:23 PDT 2013
Hello,
below a patch doing the inlining as suggested by Isaac and
another patch which moves the common functionality for
daemonizing in klogd and syslogd to lib/pending.c.
Cheers,
Felix
# HG changeset patch
# User Felix Janda <felix.janda at posteo.de>
# Date 1376156111 -7200
# Node ID 4a16dbca8a3b45a5fec45ee0462c263bd6d35cc5
# Parent 4837af10cc3690c7c56d9ab7c02db1c01f44bd31
Cleanup klogd
Remove #if and inline most functions
diff -r 4837af10cc36 -r 4a16dbca8a3b toys/pending/klogd.c
--- a/toys/pending/klogd.c Fri Aug 09 20:46:02 2013 +0200
+++ b/toys/pending/klogd.c Sat Aug 10 19:35:11 2013 +0200
@@ -24,87 +24,38 @@
#define FOR_klogd
#include "toys.h"
#include <signal.h>
+#include <sys/klog.h>
GLOBALS(
long level;
+
int fd;
)
-#if CFG_KLOGD_SOURCE_RING_BUFFER
-#include <sys/klog.h>
-static void open_klogd(void)
-{
- syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
- klogctl(1, NULL, 0);
-}
-
-static int read_klogd(char *bufptr, int len)
-{
- return klogctl(2, bufptr, len);
-}
-
static void set_log_level(int level)
{
- klogctl(8, NULL, level);
-}
-
-static void close_klogd(void)
-{
- klogctl(7, NULL, 0);
- klogctl(0, NULL, 0);
-}
-#else
-static void open_klogd(void)
-{
- TT.fd = xopen("/proc/kmsg", O_RDONLY); //_PATH_KLOG in paths.h
- syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
-}
-
-static int read_klogd(char *bufptr, int len)
-{
- return xread(TT.fd, bufptr, len);
-}
-
-static void set_log_level(int level)
-{
+ if (CFG_KLOGD_SOURCE_RING_BUFFER)
+ klogctl(8, NULL, level);
+ else {
FILE *fptr = xfopen("/proc/sys/kernel/printk", "w");
fprintf(fptr, "%u\n", level);
fclose(fptr);
fptr = NULL;
+ }
}
-static void close_klogd(void)
-{
- set_log_level(7);
- xclose(TT.fd);
-}
-#endif
-
static void handle_signal(int sig)
{
- close_klogd();
+ if (CFG_KLOGD_SOURCE_RING_BUFFER) {
+ klogctl(7, NULL, 0);
+ klogctl(0, NULL, 0);
+ } else {
+ set_log_level(7);
+ xclose(TT.fd);
+ }
syslog(LOG_NOTICE,"KLOGD: Daemon exiting......");
exit(1);
}
-static int daemonize(void)
-{
- pid_t pid;
- int fd = open("/dev/null", O_RDWR);
- if (fd < 0) fd = open("/", O_RDONLY, 0666);
- if((pid = fork()) < 0) {
- perror_msg("DAEMON: fail to fork");
- return -1;
- }
- if (pid) exit(EXIT_SUCCESS);
-
- setsid();
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2) close(fd);
- return 0;
-}
-
/*
* Read kernel ring buffer in local buff and keep track of
* "used" amount to track next read to start.
@@ -116,13 +67,36 @@
sigatexit(handle_signal);
if (toys.optflags & FLAG_c) set_log_level(TT.level); //set log level
- if (!(toys.optflags & FLAG_n)) daemonize(); //Make it daemon
- open_klogd();
+ if (!(toys.optflags & FLAG_n)) { //Make it daemon
+ pid_t pid;
+ int fd = open("/dev/null", O_RDWR);
+ if (fd < 0) fd = open("/", O_RDONLY, 0666);
+ if((pid = fork()) < 0) perror_exit("DAEMON: fail to fork");
+ if (pid) exit(EXIT_SUCCESS);
+
+ setsid();
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2) close(fd);
+ }
+
+ if (CFG_KLOGD_SOURCE_RING_BUFFER) {
+ syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
+ klogctl(1, NULL, 0);
+ } else {
+ TT.fd = xopen("/proc/kmsg", O_RDONLY); //_PATH_KLOG in paths.h
+ syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
+ }
openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
while(1) {
start = msg_buffer + used; //start updated for re-read.
- size = read_klogd(start, sizeof(msg_buffer) - used - 1);
+ if (CFG_KLOGD_SOURCE_RING_BUFFER) {
+ size = klogctl(2, start, sizeof(msg_buffer) - used - 1);
+ } else {
+ size = xread(TT.fd, start, sizeof(msg_buffer) - used - 1);
+ }
if (size < 0) perror_exit("error reading file:");
start[size] = '\0'; //Ensure last line to be NUL terminated.
if (used) start = msg_buffer;
# HG changeset patch
# User Felix Janda <felix.janda at posteo.de>
# Date 1376158698 -7200
# Node ID 90f4e672b18e8b0e8b0d8b304eeabd70ab5a3fb9
# Parent 4a16dbca8a3b45a5fec45ee0462c263bd6d35cc5
Add daemonize function to lib for klogd and syslogd
diff -r 4a16dbca8a3b -r 90f4e672b18e lib/lib.h
--- a/lib/lib.h Sat Aug 10 19:35:11 2013 +0200
+++ b/lib/lib.h Sat Aug 10 20:18:18 2013 +0200
@@ -202,3 +202,5 @@
// grep helper functions
char *astrcat (char *, char *);
char *xastrcat (char *, char *);
+
+void daemonize(void);
diff -r 4a16dbca8a3b -r 90f4e672b18e lib/pending.c
--- a/lib/pending.c Sat Aug 10 19:35:11 2013 +0200
+++ b/lib/pending.c Sat Aug 10 20:18:18 2013 +0200
@@ -102,3 +102,19 @@
if (!x) error_exit ("xastrcat");
return x;
}
+
+void daemonize(void)
+{
+ int fd = open("/dev/null", O_RDWR);
+ if (fd < 0) fd = xcreate("/", O_RDONLY, 0666);
+
+ pid_t pid = fork();
+ if (pid < 0) perror_exit("DAEMON: failed to fork");
+ if (pid) exit(EXIT_SUCCESS);
+
+ setsid();
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2) close(fd);
+}
diff -r 4a16dbca8a3b -r 90f4e672b18e toys/pending/klogd.c
--- a/toys/pending/klogd.c Sat Aug 10 19:35:11 2013 +0200
+++ b/toys/pending/klogd.c Sat Aug 10 20:18:18 2013 +0200
@@ -67,19 +67,7 @@
sigatexit(handle_signal);
if (toys.optflags & FLAG_c) set_log_level(TT.level); //set log level
- if (!(toys.optflags & FLAG_n)) { //Make it daemon
- pid_t pid;
- int fd = open("/dev/null", O_RDWR);
- if (fd < 0) fd = open("/", O_RDONLY, 0666);
- if((pid = fork()) < 0) perror_exit("DAEMON: fail to fork");
- if (pid) exit(EXIT_SUCCESS);
-
- setsid();
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2) close(fd);
- }
+ if (!(toys.optflags & FLAG_n)) daemonize(); //Make it daemon
if (CFG_KLOGD_SOURCE_RING_BUFFER) {
syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
diff -r 4a16dbca8a3b -r 90f4e672b18e toys/pending/syslogd.c
--- a/toys/pending/syslogd.c Sat Aug 10 19:35:11 2013 +0200
+++ b/toys/pending/syslogd.c Sat Aug 10 20:18:18 2013 +0200
@@ -632,25 +632,6 @@
signal(SIGQUIT, signal_handler);
}
-static void syslog_daemon(void)
-{
- int fd = open("/dev/null", O_RDWR);
- if (fd < 0) fd = xcreate("/", O_RDONLY, 0666);
-
- pid_t pid = fork();
- if (pid < 0) perror_exit("DAEMON: failed to fork");
- if (pid) exit(EXIT_SUCCESS);
-
- setsid();
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2) close(fd);
-
- //don't daemonize again if SIGHUP received.
- toys.optflags |= FLAG_n;
-}
-
void syslogd_main(void)
{
unsocks_t *tsd;
@@ -688,7 +669,10 @@
setup_signal();
if (parse_config_file() == -1) goto clean_and_exit;
open_logfiles();
- if (!flag_chk(FLAG_n)) syslog_daemon();
+ if (!flag_chk(FLAG_n)) {
+ //don't daemonize again if SIGHUP received.
+ toys.optflags |= FLAG_n;
+ }
{
int pid_fd = open("/var/run/syslogd.pid", O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (pid_fd > 0) {
1376159303.0
More information about the Toybox
mailing list