/* -*-c-*-
*
- * $Id: client.c,v 1.5 2001/02/16 21:23:39 mdw Exp $
+ * $Id$
*
* Client for TrIPE
*
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- 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.
- *
- * 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"
/*----- Static variables --------------------------------------------------*/
+static const char *pidfile = 0;
+static const char *logname = 0;
static FILE *logfp = 0;
static unsigned f = 0;
-static const char *logname = 0;
static int fd;
#define f_bogus 1u
static void reap(int sig)
{
- int s;
int e = errno;
- while (waitpid(-1, &s, WNOHANG) > 0)
+ while (waitpid(-1, 0, WNOHANG) > 0)
;
errno = e;
}
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) {
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))
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';
+ write(fd, p, len + 1);
}
}
static void logfile(const char *name)
{
- if (logfp)
- fclose(logfp);
- if ((logfp = fopen(name, "a")) == 0) {
- die(EXIT_FAILURE, "error opening logfile `%s': %s",
- name, strerror(errno));
+ FILE *fp;
+
+ if ((fp = fopen(name, "a")) != 0) {
+ if (logfp)
+ fclose(logfp);
+ logfp = fp;
+ setvbuf(logfp, 0, _IOLBF, BUFSIZ);
+ } else {
+ dstr d = DSTR_INIT;
+ dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno));
+ if (logfp)
+ writelog("error", d.buf);
+ else if (logname)
+ die(EXIT_FAILURE, d.buf);
+ if (f & f_syslog)
+ syslog(LOG_ERR, d.buf);
+ dstr_destroy(&d);
}
- setvbuf(logfp, 0, _IOLBF, BUFSIZ);
}
static void sighup(int sig, void *v)
logfile(logname);
}
+static void cleanup(void)
+{
+ if (pidfile)
+ unlink(pidfile);
+}
+
+static void sigdie(int sig)
+{
+ cleanup();
+ signal(sig, SIG_DFL);
+ raise(sig);
+}
+
static void version(FILE *fp)
{
pquis(fp, "$, TrIPE version " VERSION "\n");
{
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 program] [-S arg,arg,...]\n\
+ [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\
+ [-p PROGRAM] [-S ARG,ARG,...]\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
+ [default " SOCKETDIR "/tripesock].\n\
+-P, --pidfile=FILE Write process-id to FILE.\n\
\n\
-s, --spawn Start server rather than connecting.\n\
-p, --spawn-path=PATH Specify path to executable.\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;
+ FILE *pidfp = 0;
ego(argv[0]);
{ "syslog", 0, 0, 'l' },
{ "logfile", OPTF_ARGREQ, 0, 'f' },
{ "warnings", 0, 0, 'w' },
+ { "pidfile", OPTF_ARGREQ, 0, 'P' },
{ 0, 0, 0, 0 }
};
- int i = mdwopt(argc, argv, "hvuDd:a:sp:S:lwf:n", opts, 0, 0, 0);
+ int i = mdwopt(argc, argv, "+hvuDd:a:sp:S:lwf:nP:", opts, 0, 0, 0);
if (i < 0)
break;
switch (i) {
break;
case 'f':
logname = optarg;
- logfile(logname);
f |= f_noinput;
break;
+ case 'P':
+ pidfile = optarg;
+ break;
default:
f |= f_bogus;
break;
exit(EXIT_FAILURE);
}
+ /* --- Set various things up --- */
+
+ if (chdir(dir)) {
+ 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));
+ }
+ signal(SIGINT, sigdie);
+ signal(SIGQUIT, sigdie);
+ signal(SIGTERM, sigdie);
+ atexit(cleanup);
+
/* --- Connect to the server --- */
if (f & f_spawn) {
#endif
sigaction(SIGCHLD, &sa, 0);
+ DA_UNSHIFT(&spawnopts, (char *)sock);
+ DA_UNSHIFT(&spawnopts, "-a");
+ DA_UNSHIFT(&spawnopts, "-d.");
DA_UNSHIFT(&spawnopts, (char *)spawnpath);
- if (!(f & f_spawnopts)) {
- DA_PUSH(&spawnopts, "-d");
- DA_PUSH(&spawnopts, (char *)dir);
- DA_PUSH(&spawnopts, "-a");
- DA_PUSH(&spawnopts, (char *)sock);
- }
DA_PUSH(&spawnopts, 0);
if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
close(pfd[0]);
if (logfp)
fclose(logfp);
+ if (pidfp)
+ fclose(pidfp);
closelog();
+ if (f & f_daemon)
+ u_detach();
execvp(DA(&spawnopts)[0], DA(&spawnopts));
die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
}
} else {
struct sockaddr_un sun;
size_t sz = strlen(sock) + 1;
- dstr d = DSTR_INIT;
- dstr_putf(&d, "%s/%s", dir, sock);
- if (d.sz + 1 > sizeof(sun.sun_path))
+ if (sz > sizeof(sun.sun_path))
die(EXIT_FAILURE, "socket name `%s' too long", sock);
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
- memcpy(sun.sun_path, d.buf, d.sz + 1);
- sz = d.sz + offsetof(struct sockaddr_un, sun_path) + 1;
- dstr_destroy(&d);
+ memcpy(sun.sun_path, sock, sz);
+ sz = sz + offsetof(struct sockaddr_un, sun_path);
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
if (connect(fd, (struct sockaddr *)&sun, sz)) {
if (u_daemon())
die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
}
+ if (pidfp) {
+ fprintf(pidfp, "%li\n", (long)getpid());
+ fclose(pidfp);
+ }
/* --- If we're meant to be interactive, do that --- */