From: mdw Date: Fri, 16 Feb 2001 21:23:39 +0000 (+0000) Subject: Use reliable signal handling for reopening logs. X-Git-Tag: 1.0.0pre1~27 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/commitdiff_plain/5f5150b1f977154c37269f1122b5fa7ad13032de?hp=cfc354fd52d515a886cd6556173a0c4d9dab915e Use reliable signal handling for reopening logs. --- diff --git a/client.c b/client.c index 270bbc23..019d68f1 100644 --- a/client.c +++ b/client.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: client.c,v 1.4 2001/02/06 09:34:53 mdw Exp $ + * $Id: client.c,v 1.5 2001/02/16 21:23:39 mdw Exp $ * * Client for TrIPE * @@ -29,6 +29,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: client.c,v $ + * Revision 1.5 2001/02/16 21:23:39 mdw + * Use reliable signal handling for reopening logs. + * * Revision 1.4 2001/02/06 09:34:53 mdw * Change ERR response to FAIL for consistency with other programs. * @@ -71,12 +74,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include "util.h" @@ -94,8 +97,8 @@ static FILE *logfp = 0; static unsigned f = 0; +static const char *logname = 0; static int fd; -static volatile sig_atomic_t reopen = 0; #define f_bogus 1u #define f_spawn 2u @@ -118,11 +121,6 @@ static void reap(int sig) errno = e; } -static void sighup(int sig) -{ - reopen = 1; -} - static void writelog(const char *cat, const char *msg) { char buf[256]; @@ -209,6 +207,11 @@ static void logfile(const char *name) setvbuf(logfp, 0, _IOLBF, BUFSIZ); } +static void sighup(int sig, void *v) +{ + logfile(logname); +} + static void version(FILE *fp) { pquis(fp, "$, TrIPE version " VERSION "\n"); @@ -258,7 +261,6 @@ int main(int argc, char *argv[]) const char *sock = "tripesock"; const char *spawnpath = "tripe"; string_v spawnopts = DA_INIT; - const char *logname = 0; char *p; ego(argv[0]); @@ -420,7 +422,7 @@ int main(int argc, char *argv[]) selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu); selbuf_init(&bs, &sel, fd, sline, &bs); for (;;) { - if (sel_select(&sel)) + if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) die(EXIT_FAILURE, "select failed: %s", strerror(errno)); } } @@ -444,36 +446,23 @@ int main(int argc, char *argv[]) /* --- Pull everything else out of the box --- */ { - lbuf b; - lbuf_init(&b, cline, 0); + sel_state sel; + selbuf b; + sig hup; + + sel_init(&sel); + selbuf_init(&b, &sel, fd, cline, 0); + if (f & f_syslog) openlog(QUIS, 0, LOG_DAEMON); if (logfp) { - struct sigaction sa; - sa.sa_handler = sighup; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sigaction(SIGHUP, &sa, 0); + sig_init(&sel); + sig_add(&hup, SIGHUP, sighup, 0); } for (;;) { - size_t sz; - ssize_t n; - if (reopen) { - logfile(logname); - reopen = 0; - } - sz = lbuf_free(&b, &p); - n = read(fd, p, sz); - if (n < 0) { - if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) - continue; - die(EXIT_FAILURE, "read failed: %s", strerror(errno)); - } - if (n == 0) - break; - lbuf_flush(&b, p, n); + if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) + die(EXIT_FAILURE, "select failed: %s", strerror(errno)); } - lbuf_close(&b); } return (0);