diff options
| author | Ales Kozumplik <akozumpl@redhat.com> | 2009-12-18 15:56:41 +0100 |
|---|---|---|
| committer | Ales Kozumplik <akozumpl@redhat.com> | 2010-01-20 14:00:35 +0100 |
| commit | 0fde437ad02a7cfb170472bad0ddf3b3faf4e7b0 (patch) | |
| tree | d357a80ab1d86a1d984af1f0c5585e25e9a59a5c /loader | |
| parent | 7f86c220caccf1b9f6c1457572332013f4e887fc (diff) | |
| download | anaconda-0fde437ad02a7cfb170472bad0ddf3b3faf4e7b0.tar.gz anaconda-0fde437ad02a7cfb170472bad0ddf3b3faf4e7b0.tar.xz anaconda-0fde437ad02a7cfb170472bad0ddf3b3faf4e7b0.zip | |
Introduces rsylogd to anaconda (part of #524980)
A proper syslog daemon allows for remote logging that includes installed
system's syslog.
Removes the init.c code that simulated syslog activity until now.
Diffstat (limited to 'loader')
| -rw-r--r-- | loader/Makefile.am | 3 | ||||
| -rw-r--r-- | loader/init.c | 237 |
2 files changed, 83 insertions, 157 deletions
diff --git a/loader/Makefile.am b/loader/Makefile.am index b3bb3e301..b37057c7c 100644 --- a/loader/Makefile.am +++ b/loader/Makefile.am @@ -54,7 +54,8 @@ loader_SOURCES = loader.c copy.c log.c moduleinfo.c loadermisc.c \ urlinstall.c net.c urls.c telnet.c telnetd.c \ rpmextract.c -init_CFLAGS = $(COMMON_CFLAGS) +init_CFLAGS = $(COMMON_CFLAGS) $(GLIB_CFLAGS) +init_LDADD = $(GLIB_LIBS) init_SOURCES = init.c undomounts.c shutdown.c copy.c shutdown_CFLAGS = $(COMMON_CFLAGS) -DAS_SHUTDOWN=1 diff --git a/loader/init.c b/loader/init.c index 6cb013dec..443b53fe2 100644 --- a/loader/init.c +++ b/loader/init.c @@ -26,6 +26,7 @@ #ifndef SOCK_STREAM # define SOCK_STREAM 1 #endif +#define klogctl syslog #else #include <ctype.h> #include <dirent.h> @@ -53,13 +54,13 @@ #include <linux/vt.h> #include <termios.h> #include <libgen.h> +#include <glib.h> #include "init.h" #include "copy.h" #include "devt.h" #include "devices.h" -#define syslog klogctl #endif #include <asm/types.h> @@ -113,6 +114,7 @@ char * env[] = { void shutDown(int doKill, reboot_action rebootAction); static int getKillPolicy(void); +static void getSyslog(char*); struct termios ts; static int expected_exit = 0; @@ -138,164 +140,40 @@ static void fatal_error(int usePerror) { #endif } -static int logChunk(int len, char *inbuf, char *outbuf) { - int inctr, outctr; - - for (inctr = 0, outctr = 0; inctr < len; inctr++) { - /* If the character is a NULL that's immediately followed by a open - * bracket, we've found the beginning of a new kernel message. Put in - * a line separator. - */ - if (inbuf[inctr] == '\0' && inctr+1 < len && inbuf[inctr+1] == '<') { - outbuf[outctr] = '\n'; - outctr++; - } - - /* Or, if we see a NULL right before the end of the chunk, that's also - * a good place to add a separator. - */ - else if (inbuf[inctr] == '\0' && inctr+1 == len) { - outbuf[outctr] = '\n'; - outctr++; - } - - /* Otherwise, simply output the character as long as it's not NULL. */ - else if (inbuf[inctr] != '\0') { - outbuf[outctr] = inbuf[inctr]; - outctr++; - } - } - - return outctr; -} - -static void doklog(char * fn) { - fd_set readset, unixs; - int in, out, i; - int log; - socklen_t s; - int sock = -1; - struct sockaddr_un sockaddr; - char inbuf[1024], outbuf[1024]; - int readfd; +/* sets up and launches syslog */ +static void startSyslog(void) { + int conf_fd; int ret; - - in = open("/proc/kmsg", O_RDONLY,0); - if (in < 0) { - /* FIXME: was perror */ - printstr("open /proc/kmsg"); - return; - } - - out = open(fn, O_WRONLY, 0); - if (out < 0) - printf("couldn't open %s for syslog -- still using /tmp/syslog\n", fn); - - log = open("/tmp/syslog", O_WRONLY | O_CREAT, 0644); - if (log < 0) { - /* FIXME: was perror */ - printstr("error opening /tmp/syslog"); - sleep(5); - - close(in); - return; + char addr[128]; + char forwardtcp[] = "*.* @@"; + + /* update the config file with command line arguments first */ + getSyslog(addr); + if (strlen(addr) > 0) { + conf_fd = open("/etc/rsyslog.conf", O_WRONLY|O_APPEND); + if (conf_fd < 0) { + printf("error opening /etc/rsyslog.conf: %d\n", errno); + printf("syslog forwarding will not be enabled\n"); + sleep(5); + } else { + ret = write(conf_fd, forwardtcp, strlen(forwardtcp)); + ret = write(conf_fd, addr, strlen(addr)); + ret = write(conf_fd, "\n", 1); + close(conf_fd); + } } - /* if we get this far, we should be in good shape */ - - if (fork()) { - /* parent */ - close(in); - close(out); - close(log); - return; - } - close(0); - close(1); - close(2); - - dup2(1, log); - -#if defined(USE_LOGDEV) - /* now open the syslog socket */ - sockaddr.sun_family = AF_UNIX; - strcpy(sockaddr.sun_path, "/dev/log"); - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { - printf("error creating socket: %d\n", errno); - sleep(5); - } - printstr("got socket\n"); - if (bind(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr.sun_family) + - strlen(sockaddr.sun_path))) { - printf("bind error: %d\n", errno); - sleep(5); - } - printstr("bound socket\n"); - chmod("/dev/log", 0666); - if (listen(sock, 5)) { - printf("listen error: %d\n", errno); - sleep(5); + /* rsyslog is going to take care of things, so disable console logging */ + klogctl(8, NULL, 1); + /* now we really start the daemon. */ + int status; + status = system("/sbin/rsyslogd -c 4"); + if (status < 0 || + !WIFEXITED(status) || + WEXITSTATUS(status) != 0) { + printf("Unable to start syslog daemon.\n"); + fatal_error(1); } -#endif - - syslog(8, NULL, 1); - - FD_ZERO(&unixs); - while (1) { - memcpy(&readset, &unixs, sizeof(unixs)); - - if (sock >= 0) - FD_SET(sock, &readset); - - FD_SET(in, &readset); - - i = select(20, &readset, NULL, NULL, NULL); - if (i <= 0) continue; - - if (FD_ISSET(in, &readset)) { - i = read(in, inbuf, sizeof(inbuf)); - if (i > 0) { - int loggedLen = logChunk(i, inbuf, outbuf); - - if (out >= 0) - ret = write(out, outbuf, loggedLen); - ret = write(log, outbuf, loggedLen); - } - } - - for (readfd = 0; readfd < 20; ++readfd) { - if (FD_ISSET(readfd, &readset) && FD_ISSET(readfd, &unixs)) { - i = read(readfd, inbuf, sizeof(inbuf)); - if (i > 0) { - int loggedLen = logChunk(i, inbuf, outbuf); - - if (out >= 0) - ret = write(out, outbuf, loggedLen); - - ret = write(log, outbuf, loggedLen); - } else if (i == 0) { - /* socket closed */ - close(readfd); - FD_CLR(readfd, &unixs); - } - } - } - - if (sock >= 0 && FD_ISSET(sock, &readset)) { - s = sizeof(sockaddr); - readfd = accept(sock, (struct sockaddr *) &sockaddr, &s); - if (readfd < 0) { - if (out >= 0) - ret = write(out, "error in accept\n", 16); - ret = write(log, "error in accept\n", 16); - close(sock); - sock = -1; - } else { - FD_SET(readfd, &unixs); - } - } - } } static int setupTerminal(int fd) { @@ -439,6 +317,53 @@ static int getKillPolicy(void) { return 1; } +/* Looks through /proc/cmdline for remote syslog paramters. */ +static void getSyslog(char *addr) { + int fd; + int len; + char buf[1024]; + + /* assume nothing gets found */ + addr[0] = '\0'; + if ((fd = open("/proc/cmdline", O_RDONLY,0)) <= 0) { + return; + } + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + buf[len] = '\0'; + + /* Parse the command line into argument vector using glib */ + int i; + int argc; + char** argv; + GError* err; + if (!g_shell_parse_argv(buf, &argc, &argv, &err )) { + g_error_free(err); + return; + } + for (i = 0; i < argc; ++i) { + /* find what we are looking for */ + if (!strncmp(argv[i], "syslog=", 7)) { + strncpy(addr, argv[i] + 7, 127); + addr[127] = '\0'; + break; + } + } + g_strfreev(argv); + + /* address can be either a hostname or IPv4 or IPv6, with or without port; + thus we only allow the following characters in the address: letters and + digits, dots, colons, slashes, dashes and square brackets */ + if (!g_regex_match_simple("^[\\w.:/\\-\\[\\]]*$", addr, 0, 0)) { + /* the parameter is malformed, disable its use */ + addr[0] = '\0'; + printf("The syslog= command line parameter is malformed and will be\n"); + printf("ignored by the installer.\n"); + sleep(5); + } + +} + static int getInitPid(void) { int fd = 0, pid = -1, ret; char * buf = calloc(1, 10); @@ -761,7 +686,7 @@ int main(int argc, char **argv) { /* Now we have some /tmp space set up, and /etc and /dev point to it. We should be in pretty good shape. */ - doklog("/dev/tty4"); + startSyslog(); /* write out a pid file */ if ((fd = open("/var/run/init.pid", O_WRONLY|O_CREAT, 0644)) > 0) { |
