X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/ab46a7872ac5f10d0c96e7bba5b927b186442a2f..7f81b1b4496591f4e23da1051c0c15229e8cb1db:/client/tripectl.c diff --git a/client/tripectl.c b/client/tripectl.c index 6a2f82ab..438edf37 100644 --- a/client/tripectl.c +++ b/client/tripectl.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -74,6 +75,7 @@ /*----- Static variables --------------------------------------------------*/ +static sel_state sel; static const char *pidfile = 0; static const char *logname = 0; static FILE *logfp = 0; @@ -90,6 +92,7 @@ static const char *bgtag = 0; #define f_noinput 64u #define f_warn 128u #define f_uclose 256u +#define f_losing 512u /*----- Main code ---------------------------------------------------------*/ @@ -119,6 +122,30 @@ static void checkbg(char **p) die(EXIT_FAILURE, "unexpected background tag `%s'", q); } +static void dolog(int prio, const char *msg, ...) +{ + va_list ap; + dstr d = DSTR_INIT; + const char *cat; + + va_start(ap, msg); + dstr_vputf(&d, msg, &ap); + va_end(ap); + if (f & f_syslog) syslog(prio, "%s", d.buf); + if (logfp) { + switch (prio) { + case LOG_WARNING: cat = "warning"; break; + case LOG_DEBUG: cat = "debug"; break; + case LOG_ERR: cat = "error"; break; + default: cat = "message"; break; + } + writelog(cat, d.buf); + } + if (prio == LOG_WARNING && (f & f_warn)) + fprintf(stderr, "Warning: %s\n", d.buf); + dstr_destroy(&d); +} + static void checkfg(void) { if (bgtag) die(EXIT_FAILURE, "unexpected foreground response"); } @@ -128,33 +155,19 @@ static void cline(char *p, size_t len, void *b) if (!p) { if (f & f_command) die(EXIT_FAILURE, "server dropped the connection"); + f &= ~f_losing; exit(0); } q = str_getword(&p); if (!q) return; - if (strcmp(q, "WARN") == 0) { - if (f & f_syslog) - syslog(LOG_WARNING, "%s", p); - if (logfp) - writelog("warning", p); - if (f & f_warn) - fprintf(stderr, "Warning: %s\n", p); - } else if (strcmp(q, "TRACE") == 0) { - if (f & f_syslog) - syslog(LOG_DEBUG, "%s", p); - if (logfp) - writelog("debug", p); - } else if (!(f & f_command)) { - if (f & f_syslog) - syslog(LOG_ERR, "unexpected output `%s %s'", q, p); - if (logfp) { - dstr d = DSTR_INIT; - dstr_putf(&d, "unexpected output `%s %s'", q, p); - writelog("error", d.buf); - dstr_destroy(&d); - } - } else if (strcmp(q, "FAIL") == 0) { + if (strcmp(q, "WARN") == 0) + dolog(LOG_WARNING, p); + else if (strcmp(q, "TRACE") == 0) + dolog(LOG_DEBUG, p); + else if (!(f & f_command)) + dolog(LOG_ERR, "unexpected output `%s %s'", q, p); + else if (strcmp(q, "FAIL") == 0) { checkfg(); die(EXIT_FAILURE, "%s", p); } else if (strcmp(q, "INFO") == 0) { @@ -207,6 +220,16 @@ static void uline(char *p, size_t len, void *b) } } +static void eline(char *p, size_t len, void *b) +{ + if (p) + dolog(LOG_WARNING, "(stderr): %s", p); + else { + selbuf_destroy(b); + close(fd); + } +} + static void setup(const char *cmd) { dstr d = DSTR_INIT; @@ -263,7 +286,7 @@ static void logfile(const char *name) else if (logname) die(EXIT_FAILURE, d.buf); if (f & f_syslog) - syslog(LOG_ERR, d.buf); + syslog(LOG_ERR, "%s", d.buf); dstr_destroy(&d); } } @@ -342,8 +365,19 @@ int main(int argc, char *argv[]) string_v spawnopts = DA_INIT; char *p; FILE *pidfp = 0; + int i; + size_t sz; uid_t u = -1; gid_t g = -1; + int pfd[2], efd[2]; + mdup_fd md[3]; + pid_t kid; + struct sigaction sa; + sigset_t newmask, oldmask; + struct sockaddr_un sun; + selbuf bu, bs, be; + dstr d = DSTR_INIT; + sig hup; ego(argv[0]); @@ -376,7 +410,7 @@ int main(int argc, char *argv[]) { 0, 0, 0, 0 } }; - int i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts, 0, 0, 0); + i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts, 0, 0, 0); if (i < 0) break; switch (i) { @@ -445,14 +479,14 @@ int main(int argc, char *argv[]) die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s", dir, strerror(errno)); } - if (logname) - logfile(logname); if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname)) pidfile = "tripectl.pid"; if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) { die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", pidfile, strerror(errno)); } + sel_init(&sel); + sig_init(&sel); signal(SIGINT, sigdie); signal(SIGQUIT, sigdie); signal(SIGTERM, sigdie); @@ -461,11 +495,6 @@ int main(int argc, char *argv[]) /* --- Connect to the server --- */ if (f & f_spawn) { - int pfd[2]; - pid_t kid; - struct sigaction sa; - sigset_t newmask, oldmask; - sa.sa_handler = reap; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_NOCLDSTOP; @@ -481,7 +510,7 @@ int main(int argc, char *argv[]) putarg(&spawnopts, "-d."); putarg(&spawnopts, "-F"); putarg(&spawnopts, "%s", spawnpath); - if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd)) + if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) || pipe(efd)) die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno)); sigemptyset(&newmask); sigaddset(&newmask, SIGCHLD); @@ -489,11 +518,12 @@ int main(int argc, char *argv[]) if ((kid = fork()) < 0) die(EXIT_FAILURE, "fork failed: %s", strerror(errno)); if (!kid) { - dup2(pfd[1], STDIN_FILENO); - dup2(pfd[1], STDOUT_FILENO); - close(pfd[0]); - close(pfd[1]); - if (logfp) fclose(logfp); + close(pfd[0]); close(efd[0]); + sigprocmask(SIG_SETMASK, &oldmask, 0); + md[0].cur = pfd[1]; md[0].want = STDIN_FILENO; + md[1].cur = pfd[1]; md[1].want = STDOUT_FILENO; + md[2].cur = efd[1]; md[2].want = STDERR_FILENO; + mdup(md, 3); if (pidfp) fclose(pidfp); closelog(); if (f & f_daemon) detachtty(); @@ -502,10 +532,10 @@ int main(int argc, char *argv[]) } sigprocmask(SIG_SETMASK, &oldmask, 0); fd = pfd[0]; - close(pfd[1]); + close(pfd[1]); close(efd[1]); + selbuf_init(&be, &sel, efd[0], eline, &be); } else { - struct sockaddr_un sun; - size_t sz = strlen(sock) + 1; + sz = strlen(sock) + 1; if (sz > sizeof(sun.sun_path)) die(EXIT_FAILURE, "socket name `%s' too long", sock); memset(&sun, 0, sizeof(sun)); @@ -520,7 +550,10 @@ int main(int argc, char *argv[]) } } + f |= f_losing; /* pessimism */ u_setugid(u, g); + if (logname) + logfile(logname); if (f & f_daemon) { if (daemonize()) die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno)); @@ -536,10 +569,6 @@ int main(int argc, char *argv[]) if (optind == argc) setup("WATCH -A+tw"); if (!(f & f_noinput) && optind == argc) { - sel_state sel; - selbuf bu, bs; - - sel_init(&sel); selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu); selbuf_init(&bs, &sel, fd, sline, &bs); for (;;) { @@ -551,7 +580,6 @@ int main(int argc, char *argv[]) /* --- If there's a command, submit it --- */ if (optind < argc) { - dstr d = DSTR_INIT; setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A"); while (optind < argc) u_quotify(&d, argv[optind++]); @@ -565,24 +593,15 @@ int main(int argc, char *argv[]) /* --- Pull everything else out of the box --- */ - { - sel_state sel; - selbuf b; - sig hup; - - sel_init(&sel); - selbuf_init(&b, &sel, fd, cline, 0); + selbuf_init(&bs, &sel, fd, cline, 0); - if (f & f_syslog) - openlog(QUIS, 0, LOG_DAEMON); - if (logfp) { - sig_init(&sel); - sig_add(&hup, SIGHUP, sighup, 0); - } - for (;;) { - if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) - die(EXIT_FAILURE, "select failed: %s", strerror(errno)); - } + if (f & f_syslog) + openlog(QUIS, 0, LOG_DAEMON); + if (logfp) + sig_add(&hup, SIGHUP, sighup, 0); + for (;;) { + if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) + die(EXIT_FAILURE, "select failed: %s", strerror(errno)); } return (0);