Hi, I am currently working on converting syslogd to libevent. Theo recommended to do that before adding tcp and tls support.
With this diff all my regression tests for syslogd pass. I will try to pull parts of the diff into separate changes to make review easier. I have not tested the syslogc feature yet. So I will write more tests before committing this. bluhm Index: usr.sbin/syslogd/Makefile =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/Makefile,v retrieving revision 1.5 diff -u -p -r1.5 Makefile --- usr.sbin/syslogd/Makefile 4 Jan 2004 08:28:49 -0000 1.5 +++ usr.sbin/syslogd/Makefile 29 Aug 2014 15:09:10 -0000 @@ -1,7 +1,8 @@ # $OpenBSD: Makefile,v 1.5 2004/01/04 08:28:49 djm Exp $ -PROG= syslogd -SRCS= syslogd.c ttymsg.c privsep.c privsep_fdpass.c ringbuf.c -MAN= syslogd.8 syslog.conf.5 +PROG = syslogd +SRCS = syslogd.c ttymsg.c privsep.c privsep_fdpass.c ringbuf.c +MAN = syslogd.8 syslog.conf.5 +LDFLAGS = -levent .include <bsd.prog.mk> Index: usr.sbin/syslogd/privsep.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/privsep.c,v retrieving revision 1.43 diff -u -p -r1.43 privsep.c --- usr.sbin/syslogd/privsep.c 25 Aug 2014 20:19:14 -0000 1.43 +++ usr.sbin/syslogd/privsep.c 29 Aug 2014 15:09:10 -0000 @@ -153,7 +153,6 @@ priv_init(char *conf, int numeric, int l dup2(nullfd, STDOUT_FILENO); dup2(nullfd, STDERR_FILENO); } - if (nullfd > 2) close(nullfd); @@ -172,19 +171,21 @@ priv_init(char *conf, int numeric, int l close(socks[1]); /* Close descriptors that only the unpriv child needs */ - for (i = 0; i < nfunix; i++) - if (pfd[PFD_UNIX_0 + i].fd != -1) - close(pfd[PFD_UNIX_0 + i].fd); - if (pfd[PFD_INET].fd != -1) - close(pfd[PFD_INET].fd); - if (pfd[PFD_INET6].fd != -1) - close(pfd[PFD_INET6].fd); - if (pfd[PFD_CTLSOCK].fd != -1) - close(pfd[PFD_CTLSOCK].fd); - if (pfd[PFD_CTLCONN].fd != -1) - close(pfd[PFD_CTLCONN].fd); - if (pfd[PFD_KLOG].fd) - close(pfd[PFD_KLOG].fd); + if (fd_ctlconn != -1) + close(fd_ctlconn); + if (fd_ctlsock != -1) + close(fd_ctlsock); + for (i = 0; i < MAXFUNIX; i++) + if (fd_funix[i] != -1) + close(fd_funix[i]); + if (fd_klog != -1) + close(fd_klog); + if (fd_pair != -1) + close(fd_pair); + if (fd_udp != -1) + close(fd_udp); + if (fd_udp6 != -1) + close(fd_udp6); /* Save the config file specified by the child process */ if (strlcpy(config_file, conf, sizeof config_file) >= sizeof(config_file)) @@ -369,11 +370,11 @@ priv_init(char *conf, int numeric, int l close(socks[0]); /* Unlink any domain sockets that have been opened */ - for (i = 0; i < nfunix; i++) - if (funixn[i] != NULL && pfd[PFD_UNIX_0 + i].fd != -1) - (void)unlink(funixn[i]); - if (ctlsock_path != NULL && pfd[PFD_CTLSOCK].fd != -1) - (void)unlink(ctlsock_path); + for (i = 0; i < MAXFUNIX; i++) + if (path_funix[i] != NULL && fd_funix[i] != -1) + unlink(path_funix[i]); + if (path_ctlsock != NULL && fd_ctlsock != -1) + unlink(path_ctlsock); if (restart) { int r; Index: usr.sbin/syslogd/syslogd.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v retrieving revision 1.119 diff -u -p -r1.119 syslogd.c --- usr.sbin/syslogd/syslogd.c 25 Aug 2014 18:19:18 -0000 1.119 +++ usr.sbin/syslogd/syslogd.c 29 Aug 2014 17:34:52 -0000 @@ -65,7 +65,6 @@ #include <sys/param.h> #include <sys/ioctl.h> #include <sys/stat.h> -#include <sys/wait.h> #include <sys/socket.h> #include <sys/msgbuf.h> #include <sys/uio.h> @@ -81,6 +80,7 @@ #include <ctype.h> #include <errno.h> #include <err.h> +#include <event.h> #include <fcntl.h> #include <paths.h> #include <poll.h> @@ -183,8 +183,7 @@ char *TypeNames[9] = { struct filed *Files; struct filed consfile; -int nfunix = 1; /* Number of Unix domain sockets requested */ -char *funixn[MAXFUNIX] = { _PATH_LOG }; /* Paths to Unix domain sockets */ +char *path_funix[MAXFUNIX] = { _PATH_LOG }; /* Path to Unix domain sockets */ int Debug; /* debug flag */ int Startup = 1; /* startup flag */ char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ @@ -199,7 +198,7 @@ int IPv4Only = 0; /* when true, disable int IPv6Only = 0; /* when true, disable IPv4 */ int IncludeHostname = 0; /* include RFC 3164 style hostnames when forwarding */ -char *ctlsock_path = NULL; /* Path to control socket */ +char *path_ctlsock = NULL; /* Path to control socket */ #define CTL_READING_CMD 1 #define CTL_WRITING_REPLY 2 @@ -245,19 +244,31 @@ char *reply_text; /* Start of reply tex size_t ctl_reply_size = 0; /* Number of bytes used in reply */ size_t ctl_reply_offset = 0; /* Number of bytes of reply written so far */ -struct pollfd pfd[N_PFD]; +char *linebuf; +int linesize; -volatile sig_atomic_t MarkSet; -volatile sig_atomic_t WantDie; -volatile sig_atomic_t DoInit; +int fd_ctlsock = -1, fd_ctlconn = -1, fd_funix[MAXFUNIX], + fd_klog = -1, fd_pair = -1, fd_udp = -1, fd_udp6 = -1; +struct event ev_ctlaccept, ev_ctlread, ev_ctlwrite, ev_funix[MAXFUNIX], + ev_klog, ev_pair, ev_udp, ev_udp6, + ev_hup, ev_int, ev_quit, ev_term, ev_mark; + +void klog_readcb(int, short, void *); +void udp_readcb(int, short, void *); +void unix_readcb(int, short, void *); +void die_signalcb(int, short, void *); +void mark_timercb(int, short, void *); +void init_signalcb(int, short, void *); +void ctlsock_acceptcb(int, short, void *); +void ctlconn_readcb(int, short, void *); +void ctlconn_writecb(int, short, void *); +void ctlconn_logto(char *); +void ctlconn_cleanup(void); struct filed *cfline(char *, char *); void cvthname(struct sockaddr *, char *, size_t); int decode(const char *, const CODE *); -void dodie(int); -void doinit(int); void die(int); -void domark(int); void markit(void); void fprintlog(struct filed *, int, char *); void init(void); @@ -266,7 +277,6 @@ void logmsg(int, char *, char *, int); struct filed *find_dup(struct filed *); void printline(char *, char *); void printsys(char *); -void reapchild(int); char *ttymsg(struct iovec *, int, char *, int); void usage(void); void wallmsg(struct filed *, struct iovec *); @@ -274,24 +284,19 @@ int loghost(char *, char **, char **, ch int getmsgbufsize(void); int unix_socket(char *, int, mode_t); void double_rbuf(int); -void ctlsock_accept_handler(void); -void ctlconn_read_handler(void); -void ctlconn_write_handler(void); void tailify_replytext(char *, int); -void logto_ctlconn(char *); int main(int argc, char *argv[]) { - int ch, i, linesize, fd; - struct sockaddr_un fromunix; - struct sockaddr_storage from; - socklen_t len; - char *p, *line; - char resolve[MAXHOSTNAMELEN]; - int lockpipe[2] = { -1, -1}, pair[2], nullfd; - struct addrinfo hints, *res, *res0; - FILE *fp; + struct addrinfo hints, *res, *res0; + struct timeval to; + char *p; + int ch, i, nfunix = 1; + int lockpipe[2] = { -1, -1}, pair[2], nullfd, fd; + + for (i = 0; i < MAXFUNIX; i++) + fd_funix[i] = -1; while ((ch = getopt(argc, argv, "46dhnuf:m:p:a:s:")) != -1) switch (ch) { @@ -319,7 +324,7 @@ main(int argc, char *argv[]) NoDNS = 1; break; case 'p': /* path */ - funixn[0] = optarg; + path_funix[0] = optarg; break; case 'u': /* allow udp input port */ SecureMode = 0; @@ -330,10 +335,10 @@ main(int argc, char *argv[]) "out of descriptors, ignoring %s\n", optarg); else - funixn[nfunix++] = optarg; + path_funix[nfunix++] = optarg; break; case 's': - ctlsock_path = optarg; + path_ctlsock = optarg; break; default: usage(); @@ -368,17 +373,11 @@ main(int argc, char *argv[]) if (linesize < MAXLINE) linesize = MAXLINE; linesize++; - if ((line = malloc(linesize)) == NULL) { + if ((linebuf = malloc(linesize)) == NULL) { logerror("Couldn't allocate line buffer"); die(0); } - /* Clear poll array, set all fds to ignore */ - for (i = 0; i < N_PFD; i++) { - pfd[i].fd = -1; - pfd[i].events = 0; - } - memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; @@ -393,94 +392,87 @@ main(int argc, char *argv[]) } for (res = res0; res; res = res->ai_next) { - struct pollfd *pfdp; + int *fdp; switch (res->ai_family) { case AF_INET: if (IPv6Only) continue; - pfdp = &pfd[PFD_INET]; + fdp = &fd_udp; break; case AF_INET6: if (IPv4Only) continue; - pfdp = &pfd[PFD_INET6]; + fdp = &fd_udp6; break; default: continue; } - if (pfdp->fd >= 0) + if (*fdp >= 0) continue; - fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (fd < 0) + *fdp = socket(res->ai_family, res->ai_socktype, + res->ai_protocol); + if (*fdp == -1) continue; - if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) { + if (bind(*fdp, res->ai_addr, res->ai_addrlen) < 0) { logerror("bind"); - close(fd); + close(*fdp); + *fdp = -1; if (!Debug) die(0); - fd = -1; continue; } - pfdp->fd = fd; if (SecureMode) - shutdown(pfdp->fd, SHUT_RD); + shutdown(*fdp, SHUT_RD); else { - double_rbuf(pfdp->fd); - pfdp->events = POLLIN; + double_rbuf(*fdp); } } - freeaddrinfo(res0); #ifndef SUN_LEN #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) #endif for (i = 0; i < nfunix; i++) { - if ((fd = unix_socket(funixn[i], SOCK_DGRAM, 0666)) == -1) { + fd_funix[i] = unix_socket(path_funix[i], SOCK_DGRAM, 0666); + if (fd_funix[i] == -1) { if (i == 0 && !Debug) die(0); continue; } - double_rbuf(fd); - pfd[PFD_UNIX_0 + i].fd = fd; - pfd[PFD_UNIX_0 + i].events = POLLIN; + double_rbuf(fd_funix[i]); } - nfunix++; if (socketpair(AF_UNIX, SOCK_DGRAM, PF_UNSPEC, pair) == -1) die(0); - fd = pair[0]; - double_rbuf(fd); - pfd[PFD_UNIX_0 + i].fd = fd; - pfd[PFD_UNIX_0 + i].events = POLLIN; - - if (ctlsock_path != NULL) { - fd = unix_socket(ctlsock_path, SOCK_STREAM, 0600); - if (fd != -1) { - if (listen(fd, 16) == -1) { + fd_pair = pair[0]; + double_rbuf(fd_pair); + + if (path_ctlsock != NULL) { + fd_ctlsock = unix_socket(path_ctlsock, SOCK_STREAM, 0600); + if (fd_ctlsock == -1) { + dprintf("can't open %s (%d)\n", path_ctlsock, errno); + if (!Debug) + die(0); + } else { + if (listen(fd_ctlsock, 16) == -1) { logerror("ctlsock listen"); die(0); } - pfd[PFD_CTLSOCK].fd = fd; - pfd[PFD_CTLSOCK].events = POLLIN; - } else if (!Debug) - die(0); + } } - if ((fd = open(_PATH_KLOG, O_RDONLY, 0)) == -1) { + fd_klog = open(_PATH_KLOG, O_RDONLY, 0); + if (fd_klog == -1) { dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); } else { - pfd[PFD_KLOG].fd = fd; - pfd[PFD_KLOG].events = POLLIN; + if (ioctl(fd_klog, LIOCSFD, &pair[1]) == -1) + dprintf("LIOCSFD errno %d\n", errno); } - - if (ioctl(fd, LIOCSFD, &pair[1]) == -1) - dprintf("LIOCSFD errno %d\n", errno); close(pair[1]); dprintf("off & running....\n"); @@ -510,6 +502,8 @@ main(int argc, char *argv[]) /* tuck my process id away */ if (!Debug) { + FILE *fp; + fp = fopen(_PATH_LOGPID, "w"); if (fp != NULL) { fprintf(fp, "%ld\n", (long)getpid()); @@ -522,12 +516,35 @@ main(int argc, char *argv[]) errx(1, "unable to privsep"); /* Process is now unprivileged and inside a chroot */ + event_init(); + + event_set(&ev_ctlaccept, fd_ctlsock, EV_READ|EV_PERSIST, + ctlsock_acceptcb, &ev_ctlaccept); + event_set(&ev_ctlread, fd_ctlconn, EV_READ|EV_PERSIST, + ctlconn_readcb, &ev_ctlread); + event_set(&ev_ctlwrite, fd_ctlconn, EV_WRITE|EV_PERSIST, + ctlconn_writecb, &ev_ctlwrite); + for (i = 0; i < MAXFUNIX; i++) + event_set(&ev_funix[i], fd_funix[i], EV_READ|EV_PERSIST, + unix_readcb, &ev_funix[i]); + event_set(&ev_klog, fd_klog, EV_READ|EV_PERSIST, klog_readcb, &ev_klog); + event_set(&ev_pair, fd_pair, EV_READ|EV_PERSIST, unix_readcb, &ev_pair); + event_set(&ev_udp, fd_udp, EV_READ|EV_PERSIST, udp_readcb, &ev_udp); + event_set(&ev_udp6, fd_udp6, EV_READ|EV_PERSIST, udp_readcb, &ev_udp6); + + signal_set(&ev_hup, SIGHUP, init_signalcb, &ev_hup); + signal_set(&ev_int, SIGINT, die_signalcb, &ev_int); + signal_set(&ev_quit, SIGQUIT, die_signalcb, &ev_quit); + signal_set(&ev_term, SIGTERM, die_signalcb, &ev_term); + + evtimer_set(&ev_mark, mark_timercb, &ev_mark); + init(); Startup = 0; /* Allocate ctl socket reply buffer if we have a ctl socket */ - if (pfd[PFD_CTLSOCK].fd != -1 && + if (fd_ctlsock != -1 && (ctl_reply = malloc(CTL_REPLY_MAXSIZE)) == NULL) { logerror("Couldn't allocate ctlsock reply buffer"); die(0); @@ -538,10 +555,10 @@ main(int argc, char *argv[]) dup2(nullfd, STDIN_FILENO); dup2(nullfd, STDOUT_FILENO); dup2(nullfd, STDERR_FILENO); - if (nullfd > 2) - close(nullfd); close(lockpipe[1]); } + if (nullfd > 2) + close(nullfd); /* * Signal to the priv process that the initial config parsing is done @@ -549,105 +566,96 @@ main(int argc, char *argv[]) */ priv_config_parse_done(); - (void)signal(SIGHUP, doinit); - (void)signal(SIGTERM, dodie); - (void)signal(SIGINT, Debug ? dodie : SIG_IGN); - (void)signal(SIGQUIT, Debug ? dodie : SIG_IGN); - (void)signal(SIGCHLD, reapchild); - (void)signal(SIGALRM, domark); - (void)signal(SIGPIPE, SIG_IGN); - (void)alarm(TIMERINTVL); + if (fd_ctlsock != -1) + event_add(&ev_ctlaccept, NULL); + for (i = 0; i < MAXFUNIX; i++) + if (fd_funix[i] != -1) + event_add(&ev_funix[i], NULL); + if (fd_klog != -1) + event_add(&ev_klog, NULL); + if (fd_pair != -1) + event_add(&ev_pair, NULL); + if (!SecureMode) { + if (fd_udp != -1) + event_add(&ev_udp, NULL); + if (fd_udp6 != -1) + event_add(&ev_udp6, NULL); + } + + signal_add(&ev_hup, NULL); + signal_add(&ev_term, NULL); + if (Debug) { + signal_add(&ev_int, NULL); + signal_add(&ev_quit, NULL); + } else { + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + } + signal(SIGCHLD, SIG_IGN); + signal(SIGPIPE, SIG_IGN); + + to.tv_sec = TIMERINTVL; + to.tv_usec = 0; + evtimer_add(&ev_mark, &to); logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: start", LocalHostName, ADDDATE); dprintf("syslogd: started\n"); - for (;;) { - if (MarkSet) - markit(); - if (WantDie) - die(WantDie); - - if (DoInit) { - init(); - DoInit = 0; - - logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", - LocalHostName, ADDDATE); - dprintf("syslogd: restarted\n"); - } + event_dispatch(); + /* NOTREACHED */ + return (0); +} - switch (poll(pfd, PFD_UNIX_0 + nfunix, -1)) { - case 0: - continue; - case -1: - if (errno != EINTR) - logerror("poll"); - continue; - } +void +klog_readcb(int fd, short event, void *arg) +{ + struct event *ev = arg; + ssize_t n; - if ((pfd[PFD_KLOG].revents & POLLIN) != 0) { - i = read(pfd[PFD_KLOG].fd, line, linesize - 1); - if (i > 0) { - line[i] = '\0'; - printsys(line); - } else if (i < 0 && errno != EINTR) { - logerror("klog"); - pfd[PFD_KLOG].fd = -1; - pfd[PFD_KLOG].events = 0; - } - } - if ((pfd[PFD_INET].revents & POLLIN) != 0) { - len = sizeof(from); - i = recvfrom(pfd[PFD_INET].fd, line, MAXLINE, 0, - (struct sockaddr *)&from, &len); - if (i > 0) { - line[i] = '\0'; - cvthname((struct sockaddr *)&from, resolve, - sizeof(resolve)); - dprintf("cvthname res: %s\n", resolve); - printline(resolve, line); - } else if (i < 0 && errno != EINTR) - logerror("recvfrom inet"); - } - if ((pfd[PFD_INET6].revents & POLLIN) != 0) { - len = sizeof(from); - i = recvfrom(pfd[PFD_INET6].fd, line, MAXLINE, 0, - (struct sockaddr *)&from, &len); - if (i > 0) { - line[i] = '\0'; - cvthname((struct sockaddr *)&from, resolve, - sizeof(resolve)); - dprintf("cvthname res: %s\n", resolve); - printline(resolve, line); - } else if (i < 0 && errno != EINTR) - logerror("recvfrom inet6"); - } - if ((pfd[PFD_CTLSOCK].revents & POLLIN) != 0) - ctlsock_accept_handler(); - if ((pfd[PFD_CTLCONN].revents & POLLIN) != 0) - ctlconn_read_handler(); - if ((pfd[PFD_CTLCONN].revents & POLLOUT) != 0) - ctlconn_write_handler(); - - for (i = 0; i < nfunix; i++) { - if ((pfd[PFD_UNIX_0 + i].revents & POLLIN) != 0) { - ssize_t rlen; - - len = sizeof(fromunix); - rlen = recvfrom(pfd[PFD_UNIX_0 + i].fd, line, - MAXLINE, 0, (struct sockaddr *)&fromunix, - &len); - if (rlen > 0) { - line[rlen] = '\0'; - printline(LocalHostName, line); - } else if (rlen == -1 && errno != EINTR) - logerror("recvfrom unix"); - } - } + n = read(fd, linebuf, linesize - 1); + if (n > 0) { + linebuf[n] = '\0'; + printsys(linebuf); + } else if (n < 0) { + logerror("klog"); + event_del(ev); } - /* NOTREACHED */ - free(pfd); - return (0); +} + +void +udp_readcb(int fd, short event, void *arg) +{ + struct sockaddr_storage sa; + socklen_t salen; + ssize_t n; + + salen = sizeof(sa); + n = recvfrom(fd, linebuf, MAXLINE, 0, (struct sockaddr *)&sa, &salen); + if (n > 0) { + char resolve[MAXHOSTNAMELEN]; + + linebuf[n] = '\0'; + cvthname((struct sockaddr *)&sa, resolve, sizeof(resolve)); + dprintf("cvthname res: %s\n", resolve); + printline(resolve, linebuf); + } else if (n < 0) + logerror("recvfrom udp"); +} + +void +unix_readcb(int fd, short event, void *arg) +{ + struct sockaddr_un sa; + socklen_t salen; + ssize_t n; + + salen = sizeof(sa); + n = recvfrom(fd, linebuf, MAXLINE, 0, (struct sockaddr *)&sa, &salen); + if (n > 0) { + linebuf[n] = '\0'; + printline(LocalHostName, linebuf); + } else if (n < 0) + logerror("recvfrom unix"); } void @@ -748,18 +756,12 @@ logmsg(int pri, char *msg, char *from, i { struct filed *f; int fac, msglen, prilev, i; - sigset_t mask, omask; char *timestamp; char prog[NAME_MAX+1]; dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n", pri, flags, from, msg); - sigemptyset(&mask); - sigaddset(&mask, SIGALRM); - sigaddset(&mask, SIGHUP); - sigprocmask(SIG_BLOCK, &mask, &omask); - /* * Check to see if msg looks non-standard. */ @@ -804,7 +806,6 @@ logmsg(int pri, char *msg, char *from, i (void)close(f->f_file); f->f_file = -1; } - (void)sigprocmask(SIG_SETMASK, &omask, NULL); return; } for (f = Files; f; f = f->f_next) { @@ -869,7 +870,6 @@ logmsg(int pri, char *msg, char *from, i if (f->f_quick) break; } - (void)sigprocmask(SIG_SETMASK, &omask, NULL); } void @@ -936,10 +936,10 @@ fprintlog(struct filed *f, int flags, ch dprintf(" %s\n", f->f_un.f_forw.f_loghost); switch (f->f_un.f_forw.f_addr.ss_family) { case AF_INET: - fd = pfd[PFD_INET].fd; + fd = fd_udp; break; case AF_INET6: - fd = pfd[PFD_INET6].fd; + fd = fd_udp6; break; default: fd = -1; @@ -1059,7 +1059,7 @@ fprintlog(struct filed *f, int flags, ch if (ringbuf_append_line(f->f_un.f_mb.f_rb, line) == 1) f->f_un.f_mb.f_overflow = 1; if (f->f_un.f_mb.f_attached) - logto_ctlconn(line); + ctlconn_logto(line); break; } f->f_prevcount = 0; @@ -1120,18 +1120,6 @@ wallmsg(struct filed *f, struct iovec *i reenter = 0; } -/* ARGSUSED */ -void -reapchild(int signo) -{ - int save_errno = errno; - int status; - - while (waitpid(-1, &status, WNOHANG) > 0) - ; - errno = save_errno; -} - /* * Return a printable representation of a host address. */ @@ -1153,23 +1141,25 @@ cvthname(struct sockaddr *f, char *resul } void -dodie(int signo) +die_signalcb(int signum, short event, void *arg) { - WantDie = signo; + die(signum); } -/* ARGSUSED */ void -domark(int signo) +mark_timercb(int unused, short event, void *arg) { - MarkSet = 1; + markit(); } -/* ARGSUSED */ void -doinit(int signo) +init_signalcb(int signum, short event, void *arg) { - DoInit = 1; + init(); + + logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", + LocalHostName, ADDDATE); + dprintf("syslogd: restarted\n"); } /* @@ -1201,7 +1191,6 @@ die(int signo) char buf[100]; Initialized = 0; /* Don't log SIGCHLDs */ - alarm(0); for (f = Files; f != NULL; f = f->f_next) { /* flush any pending output */ if (f->f_prevcount) @@ -1565,19 +1554,19 @@ cfline(char *line, char *prog) if (proto == NULL) proto = "udp"; if (strcmp(proto, "udp") == 0) { - if (pfd[PFD_INET].fd == -1) + if (fd_udp == -1) proto = "udp6"; - if (pfd[PFD_INET6].fd == -1) + if (fd_udp6 == -1) proto = "udp4"; } else if (strcmp(proto, "udp4") == 0) { - if (pfd[PFD_INET].fd == -1) { + if (fd_udp == -1) { snprintf(ebuf, sizeof(ebuf), "no udp4 \"%s\"", f->f_un.f_forw.f_loghost); logerror(ebuf); break; } } else if (strcmp(proto, "udp6") == 0) { - if (pfd[PFD_INET6].fd == -1) { + if (fd_udp6 == -1) { snprintf(ebuf, sizeof(ebuf), "no udp6 \"%s\"", f->f_un.f_forw.f_loghost); logerror(ebuf); @@ -1811,8 +1800,6 @@ markit(void) BACKOFF(f); } } - MarkSet = 0; - (void)alarm(TIMERINTVL); } int @@ -1884,18 +1871,17 @@ double_rbuf(int fd) } } -static void +void ctlconn_cleanup(void) { - struct filed *f; - - if (pfd[PFD_CTLCONN].fd != -1) - close(pfd[PFD_CTLCONN].fd); - - pfd[PFD_CTLCONN].fd = -1; - pfd[PFD_CTLCONN].events = pfd[PFD_CTLCONN].revents = 0; + struct filed *f; - pfd[PFD_CTLSOCK].events = POLLIN; + if (close(fd_ctlconn) == -1) + logerror("close ctlconn"); + fd_ctlconn = -1; + event_del(&ev_ctlread); + event_del(&ev_ctlwrite); + event_add(&ev_ctlaccept, NULL); if (ctl_state == CTL_WRITING_CONT_REPLY) for (f = Files; f != NULL; f = f->f_next) @@ -1906,23 +1892,24 @@ ctlconn_cleanup(void) } void -ctlsock_accept_handler(void) +ctlsock_acceptcb(int fd, short event, void *arg) { - int fd, flags; + struct event *ev = arg; + int flags; dprintf("Accepting control connection\n"); - fd = accept(pfd[PFD_CTLSOCK].fd, NULL, NULL); + fd = accept(fd, NULL, NULL); if (fd == -1) { - if (errno != EINTR && errno != EWOULDBLOCK && - errno != ECONNABORTED) + if (errno != EWOULDBLOCK && errno != ECONNABORTED) logerror("accept ctlsock"); return; } - ctlconn_cleanup(); + if (fd_ctlconn != -1) + ctlconn_cleanup(); /* Only one connection at a time */ - pfd[PFD_CTLSOCK].events = pfd[PFD_CTLSOCK].revents = 0; + event_del(ev); if ((flags = fcntl(fd, F_GETFL)) == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { @@ -1931,8 +1918,13 @@ ctlsock_accept_handler(void) return; } - pfd[PFD_CTLCONN].fd = fd; - pfd[PFD_CTLCONN].events = POLLIN; + fd_ctlconn = fd; + /* file descriptor has changed, reset event */ + event_set(&ev_ctlread, fd_ctlconn, EV_READ|EV_PERSIST, + ctlconn_readcb, &ev_ctlread); + event_set(&ev_ctlwrite, fd_ctlconn, EV_WRITE|EV_PERSIST, + ctlconn_writecb, &ev_ctlwrite); + event_add(&ev_ctlread, NULL); ctl_state = CTL_READING_CMD; ctl_cmd_bytes = 0; } @@ -1951,12 +1943,12 @@ static struct filed } void -ctlconn_read_handler(void) +ctlconn_readcb(int fd, short event, void *arg) { - ssize_t n; - struct filed *f; - u_int32_t flags = 0; - struct ctl_reply_hdr *reply_hdr = (struct ctl_reply_hdr *)ctl_reply; + struct filed *f; + struct ctl_reply_hdr *reply_hdr = (struct ctl_reply_hdr *)ctl_reply; + ssize_t n; + u_int32_t flags = 0; if (ctl_state == CTL_WRITING_REPLY || ctl_state == CTL_WRITING_CONT_REPLY) { @@ -1965,13 +1957,10 @@ ctlconn_read_handler(void) return; } - retry: - n = read(pfd[PFD_CTLCONN].fd, (char*)&ctl_cmd + ctl_cmd_bytes, + n = read(fd, (char*)&ctl_cmd + ctl_cmd_bytes, sizeof(ctl_cmd) - ctl_cmd_bytes); switch (n) { case -1: - if (errno == EINTR) - goto retry; logerror("ctlconn read"); /* FALLTHROUGH */ case 0: @@ -1980,7 +1969,6 @@ ctlconn_read_handler(void) default: ctl_cmd_bytes += n; } - if (ctl_cmd_bytes < sizeof(ctl_cmd)) return; @@ -2074,21 +2062,18 @@ ctlconn_read_handler(void) ctl_state = (ctl_cmd.cmd == CMD_READ_CONT) ? CTL_WRITING_CONT_REPLY : CTL_WRITING_REPLY; - pfd[PFD_CTLCONN].events = POLLOUT; - - /* monitor terminating syslogc */ - pfd[PFD_CTLCONN].events |= POLLIN; + event_add(&ev_ctlwrite, NULL); /* another syslogc can kick us out */ if (ctl_state == CTL_WRITING_CONT_REPLY) - pfd[PFD_CTLSOCK].events = POLLIN; - + event_add(&ev_ctlaccept, NULL); } void -ctlconn_write_handler(void) +ctlconn_writecb(int fd, short event, void *arg) { - ssize_t n; + struct event *ev = arg; + ssize_t n; if (!(ctl_state == CTL_WRITING_REPLY || ctl_state == CTL_WRITING_CONT_REPLY)) { @@ -2097,13 +2082,11 @@ ctlconn_write_handler(void) ctlconn_cleanup(); return; } - retry: - n = write(pfd[PFD_CTLCONN].fd, ctl_reply + ctl_reply_offset, + + n = write(fd, ctl_reply + ctl_reply_offset, ctl_reply_size - ctl_reply_offset); switch (n) { case -1: - if (errno == EINTR) - goto retry; if (errno != EPIPE) logerror("ctlconn write"); /* FALLTHROUGH */ @@ -2113,26 +2096,29 @@ ctlconn_write_handler(void) default: ctl_reply_offset += n; } - if (ctl_reply_offset >= ctl_reply_size) { - /* - * Make space in the buffer for continous writes. - * Set offset behind reply header to skip it - */ - if (ctl_state == CTL_WRITING_CONT_REPLY) { - *reply_text = '\0'; - ctl_reply_offset = ctl_reply_size = CTL_REPLY_SIZE; - - /* Now is a good time to report dropped lines */ - if (membuf_drop) { - strlcat(reply_text, "<ENOBUFS>\n", MAX_MEMBUF); - ctl_reply_size = CTL_REPLY_SIZE; - membuf_drop = 0; - } else { - /* Nothing left to write */ - pfd[PFD_CTLCONN].events = POLLIN; - } - } else - ctlconn_cleanup(); + if (ctl_reply_offset < ctl_reply_size) + return; + + if (ctl_state != CTL_WRITING_CONT_REPLY) { + ctlconn_cleanup(); + return; + } + + /* + * Make space in the buffer for continous writes. + * Set offset behind reply header to skip it + */ + *reply_text = '\0'; + ctl_reply_offset = ctl_reply_size = CTL_REPLY_SIZE; + + /* Now is a good time to report dropped lines */ + if (membuf_drop) { + strlcat(reply_text, "<ENOBUFS>\n", MAX_MEMBUF); + ctl_reply_size = CTL_REPLY_SIZE; + membuf_drop = 0; + } else { + /* Nothing left to write */ + event_del(ev); } } @@ -2159,7 +2145,7 @@ tailify_replytext(char *replytext, int l } void -logto_ctlconn(char *line) +ctlconn_logto(char *line) { size_t l; @@ -2175,5 +2161,5 @@ logto_ctlconn(char *line) memcpy(ctl_reply + ctl_reply_size, line, l); memcpy(ctl_reply + ctl_reply_size + l, "\n", 2); ctl_reply_size += l + 1; - pfd[PFD_CTLCONN].events |= POLLOUT; + event_add(&ev_ctlwrite, NULL); } Index: usr.sbin/syslogd/syslogd.h =================================================================== RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.h,v retrieving revision 1.12 diff -u -p -r1.12 syslogd.h --- usr.sbin/syslogd/syslogd.h 25 Aug 2014 18:19:18 -0000 1.12 +++ usr.sbin/syslogd/syslogd.h 29 Aug 2014 15:09:10 -0000 @@ -40,23 +40,14 @@ int receive_fd(int); /* The list of domain sockets */ #define MAXFUNIX 21 -extern int nfunix; -extern char *funixn[MAXFUNIX]; -extern char *ctlsock_path; +extern char *path_funix[MAXFUNIX]; +extern char *path_ctlsock; +extern int fd_udp, fd_udp6, fd_funix[MAXFUNIX], fd_klog, fd_pair; +extern int fd_ctlsock, fd_ctlconn; #define dprintf(_f...) do { if (Debug) printf(_f); } while (0) extern int Debug; extern int Startup; - -/* fds to poll */ -#define PFD_KLOG 0 /* Offset of /dev/klog entry */ -#define PFD_INET 1 /* Offset of inet socket entry */ -#define PFD_CTLSOCK 2 /* Offset of control socket entry */ -#define PFD_CTLCONN 3 /* Offset of control connection entry */ -#define PFD_INET6 4 /* Offset of inet6 socket entry */ -#define PFD_UNIX_0 5 /* Start of Unix socket entries */ -#define N_PFD (PFD_UNIX_0 + MAXFUNIX) /* # of pollfd entries */ -extern struct pollfd pfd[N_PFD]; struct ringbuf { char *buf;