X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/06b2a088f26208fa8974f8326f01fdfa7908f08b..d59d4fe9e094b3ce28c47c89197a0c8ad79fc35f:/client.c diff --git a/client.c b/client.c index db58ed5f..314bb87c 100644 --- a/client.c +++ b/client.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: client.c,v 1.7 2001/02/22 09:07:54 mdw Exp $ + * $Id$ * * Client for TrIPE * @@ -26,33 +26,6 @@ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: client.c,v $ - * Revision 1.7 2001/02/22 09:07:54 mdw - * Write a pidfile on request, and delete it when finished. - * - * Revision 1.6 2001/02/22 09:06:08 mdw - * Fix logfile rotation to avoid program collapse. - * - * 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. - * - * Revision 1.3 2001/02/04 17:10:15 mdw - * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't - * change directory -- just mangle pathnames instead. - * - * Revision 1.2 2001/02/04 01:17:54 mdw - * Create a configuration header file to tidy up command lines. - * - * Revision 1.1 2001/02/03 20:26:37 mdw - * Initial checkin. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "config.h" @@ -136,7 +109,7 @@ static void writelog(const char *cat, const char *msg) fprintf(logfp, "%s %s: %s\n", buf, cat, msg); } -static void cline(char *p, void *b) +static void cline(char *p, size_t len, void *b) { char *q; if (!p) { @@ -178,7 +151,7 @@ static void cline(char *p, void *b) die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); } -static void sline(char *p, void *b) +static void sline(char *p, size_t len, void *b) { if (!p) { if (!(f & f_uclose)) @@ -188,17 +161,56 @@ static void sline(char *p, void *b) puts(p); } -static void uline(char *p, void *b) +static void uline(char *p, size_t len, void *b) { - size_t sz; if (!p) { selbuf_destroy(b); shutdown(fd, 1); f |= f_uclose; } else { - sz = strlen(p); - p[sz] = '\n'; - write(fd, p, sz + 1); + p[len] = '\n'; + errno = EIO; + if (write(fd, p, len + 1) != len + 1) + moan("write failed: %s", strerror(errno)); + } +} + +static void setup(const char *cmd) +{ + dstr d = DSTR_INIT; + char ch; + char *p, *q; + int n; + + dstr_puts(&d, cmd); + dstr_putc(&d, '\n'); + errno = EIO; /* Relax: be vague */ + if (write(fd, d.buf, d.len) != d.len) { + die(EXIT_FAILURE, "error sending setup command `%s': %s", + cmd, strerror(errno)); + } + dstr_reset(&d); + for (;;) { + n = read(fd, &ch, 1); + if (!n) + die(EXIT_FAILURE, "unexpected EOF during setup"); + if (n < 0) { + die(EXIT_FAILURE, "error receiving reply to `%s': %s", + cmd, strerror(errno)); + } + if (d.len < 256) + dstr_putc(&d, ch); + if (ch == '\n') { + p = d.buf; + q = str_getword(&p); + if (!q) + ; + else if (strcmp(q, "OK") == 0) + return; + else if (strcmp(q, "FAIL") == 0) + die(EXIT_FAILURE, "setup command `%s' failed: %s", cmd, p); + dstr_reset(&d); + } } } @@ -251,11 +263,11 @@ static void usage(FILE *fp) { pquis(fp, "\ Usage:\n\ - $ [-w] [-options] [command [args]...]\n\ - $ [-Dl] [-f file] [-options]\n\ + $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\ + $ [-Dl] [-f FILE] [-OPTIONS]\n\ Options:\n\ - [-s] [-d directory] [-a socket] [-P pidfile]\n\ - [-p program] [-S arg,arg,...]\n\ + [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\ + [-p PROGRAM] [-S ARG,ARG,...]\n\ "); } @@ -273,8 +285,9 @@ Options in full:\n\ -u, --usage Show brief usage message.\n\ \n\ -D, --daemon Become a background task after connecting.\n\ --d, --directory=DIR Select current directory [default /var/lib/tripe]\n\ --a, --admin-socket=FILE Select socket to connect to.\n\ +-d, --directory=DIR Select current directory [default " CONFIGDIR "].\n\ +-a, --admin-socket=FILE Select socket to connect to\n\ + [default " SOCKETDIR "/tripesock].\n\ -P, --pidfile=FILE Write process-id to FILE.\n\ \n\ -s, --spawn Start server rather than connecting.\n\ @@ -289,8 +302,8 @@ Options in full:\n\ int main(int argc, char *argv[]) { - const char *dir = "/var/lib/tripe"; - const char *sock = "tripesock"; + const char *dir = CONFIGDIR; + const char *sock = SOCKETDIR "/tripesock"; const char *spawnpath = "tripe"; string_v spawnopts = DA_INIT; char *p; @@ -413,10 +426,10 @@ int main(int argc, char *argv[]) #endif sigaction(SIGCHLD, &sa, 0); - DA_UNSHIFT(&spawnopts, (char *)spawnpath); DA_UNSHIFT(&spawnopts, (char *)sock); DA_UNSHIFT(&spawnopts, "-a"); DA_UNSHIFT(&spawnopts, "-d."); + DA_UNSHIFT(&spawnopts, (char *)spawnpath); DA_PUSH(&spawnopts, 0); if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd)) die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno)); @@ -432,6 +445,8 @@ int main(int argc, char *argv[]) close(pfd[0]); if (logfp) fclose(logfp); + if (pidfp) + fclose(pidfp); closelog(); if (f & f_daemon) u_detach(); @@ -463,12 +478,15 @@ int main(int argc, char *argv[]) die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno)); } if (pidfp) { - fprintf(pidfp, "%li", (long)getpid()); + fprintf(pidfp, "%li\n", (long)getpid()); fclose(pidfp); } + signal(SIGPIPE, SIG_IGN); /* --- If we're meant to be interactive, do that --- */ + if (optind == argc) + setup("WATCH -A+tw"); if (!(f & f_noinput) && optind == argc) { sel_state sel; selbuf bu, bs; @@ -486,14 +504,16 @@ int main(int argc, char *argv[]) if (optind < argc) { dstr d = DSTR_INIT; + setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A"); dstr_puts(&d, argv[optind++]); while (optind < argc) { dstr_putc(&d, ' '); dstr_puts(&d, argv[optind++]); } dstr_putc(&d, '\n'); - write(fd, d.buf, d.len); - shutdown(fd, 1); + errno = EIO; + if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1)) + die(EXIT_FAILURE, "write failed: %s", strerror(errno)); dstr_destroy(&d); f |= f_command; }