diff options
author | 2010-07-02 05:52:48 +0000 | |
---|---|---|
committer | 2010-07-02 05:52:48 +0000 | |
commit | 46210a436118a84103c68b41fe4207a634a768df (patch) | |
tree | fd2895d2ec4518714beddad5531862f0b8f22b53 | |
parent | timeout_add -> timeout_add_msec (diff) | |
download | wireguard-openbsd-46210a436118a84103c68b41fe4207a634a768df.tar.xz wireguard-openbsd-46210a436118a84103c68b41fe4207a634a768df.zip |
Rewrite hunt() to put the "rotary action" into it rather than remote.c, and so
it isn't so stupid (returning a char * as a long which goes into an int to use
as a flag, ugh).
-rw-r--r-- | usr.bin/tip/cu.c | 12 | ||||
-rw-r--r-- | usr.bin/tip/hunt.c | 91 | ||||
-rw-r--r-- | usr.bin/tip/remote.c | 41 | ||||
-rw-r--r-- | usr.bin/tip/tip.c | 12 | ||||
-rw-r--r-- | usr.bin/tip/tip.h | 4 |
5 files changed, 73 insertions, 87 deletions
diff --git a/usr.bin/tip/cu.c b/usr.bin/tip/cu.c index c935acdbdc1..3b511ba7ea6 100644 --- a/usr.bin/tip/cu.c +++ b/usr.bin/tip/cu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cu.c,v 1.34 2010/07/01 21:47:09 nicm Exp $ */ +/* $OpenBSD: cu.c,v 1.35 2010/07/02 05:52:48 nicm Exp $ */ /* $NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $ */ /* @@ -153,15 +153,7 @@ getopt: * attributes of the generic dialer. */ (void)snprintf(sbuf, sizeof(sbuf), "cu%d", vgetnum(BAUDRATE)); - if ((i = hunt(sbuf)) == 0) { - printf("all ports busy\n"); - exit(3); - } - if (i == -1) { - printf("link down\n"); - (void)uu_unlock(uucplock); - exit(3); - } + FD = hunt(sbuf); setbuf(stdout, NULL); loginit(); vinit(); diff --git a/usr.bin/tip/hunt.c b/usr.bin/tip/hunt.c index 0f7b784cd65..92170e33bfa 100644 --- a/usr.bin/tip/hunt.c +++ b/usr.bin/tip/hunt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hunt.c,v 1.17 2010/07/01 21:28:01 nicm Exp $ */ +/* $OpenBSD: hunt.c,v 1.18 2010/07/02 05:52:48 nicm Exp $ */ /* $NetBSD: hunt.c,v 1.6 1997/04/20 00:02:10 mellon Exp $ */ /* @@ -34,8 +34,8 @@ #include "tip.h" -static jmp_buf deadline; -static int deadfl; +static jmp_buf deadline; +static int deadflag; static void dead(int); @@ -43,50 +43,81 @@ static void dead(int); static void dead(int signo) { - deadfl = 1; + deadflag = 1; longjmp(deadline, 1); } -long -hunt(char *name) +/* Find and open the host. Returns the fd, or exits on error. */ +int +hunt(char *hosts) { - char *cp; - sig_t f; + char *copy, *last, *host, *device; + struct termios tio; + int fd, tried; + sig_t old_alrm; - f = signal(SIGALRM, dead); - while ((cp = getremote(name))) { - deadfl = 0; - uucplock = strrchr(cp, '/'); + if (hosts == NULL) { + hosts = getenv("HOST"); + if (hosts == NULL) + errx(3, "no host specified"); + } + + if ((copy = strdup(hosts)) == NULL) + err(1, "strdup"); + last = copy; + + old_alrm = signal(SIGALRM, dead); + + tried = 0; + while ((host = strsep(&last, ",")) != NULL) { + device = getremote(host); + + uucplock = strrchr(device, '/'); if (uucplock == NULL) - uucplock = cp; + uucplock = strdup(device); else - uucplock++; + uucplock = strdup(uucplock + 1); + if (uucplock == NULL) + err(1, "strdup"); if (uu_lock(uucplock) != UU_LOCK_OK) continue; + deadflag = 0; if (setjmp(deadline) == 0) { alarm(10); - FD = open(cp, O_RDWR | (vgetnum(DC) ? O_NONBLOCK : 0)); + + fd = open(device, + O_RDWR | (vgetnum(DC) ? O_NONBLOCK : 0)); + if (fd < 0) + perror(host); } alarm(0); - if (FD < 0) { - perror(cp); - deadfl = 1; - } - if (!deadfl) { - struct termios cntrl; - tcgetattr(FD, &cntrl); + tried++; + if (fd >= 0 && !deadflag) { + tcgetattr(fd, &tio); if (!vgetnum(DC)) - cntrl.c_cflag |= HUPCL; - tcsetattr(FD, TCSAFLUSH, &cntrl); - ioctl(FD, TIOCEXCL, 0); - signal(SIGALRM, SIG_DFL); - return ((long)cp); + tio.c_cflag |= HUPCL; + if (tcsetattr(fd, TCSAFLUSH, &tio) != 0) + errx(1, "tcsetattr"); + + if (ioctl(fd, TIOCEXCL) != 0) + errx(1, "ioctl"); + + signal(SIGALRM, old_alrm); + return (fd); } - (void)uu_unlock(uucplock); + uu_unlock(uucplock); + free(uucplock); + } + free(copy); + + signal(SIGALRM, old_alrm); + if (tried == 0) { + printf("all ports busy\n"); + exit(3); } - signal(SIGALRM, f); - return (deadfl ? -1 : (long)cp); + printf("link down\n"); + exit(3); } diff --git a/usr.bin/tip/remote.c b/usr.bin/tip/remote.c index e75c550684c..c54987f9f6b 100644 --- a/usr.bin/tip/remote.c +++ b/usr.bin/tip/remote.c @@ -1,4 +1,4 @@ -/* $OpenBSD: remote.c,v 1.28 2010/07/01 23:41:42 nicm Exp $ */ +/* $OpenBSD: remote.c,v 1.29 2010/07/02 05:52:48 nicm Exp $ */ /* $NetBSD: remote.c,v 1.5 1997/04/20 00:02:45 mellon Exp $ */ /* @@ -43,8 +43,8 @@ static char *db_array[3] = { _PATH_REMOTE, 0, 0 }; static void getremcap(char *); -static void -getremcap(char *host) +char * +getremote(char *host) { char *bp, *rempath, *strval; int stat; @@ -64,11 +64,13 @@ getremcap(char *host) if ((stat = cgetent(&bp, db_array, host)) < 0) { if (vgetstr(DEVICE) != NULL || (host[0] == '/' && access(host, R_OK | W_OK) == 0)) { + if (vgetstr(DEVICE) == NULL) + vsetstr(DEVICE, host); vsetstr(HOST, host); if (!vgetnum(BAUDRATE)) vsetnum(BAUDRATE, DEFBR); vsetnum(FRAMESIZE, DEFFS); - return; + return (vgetstr(DEVICE)); } switch (stat) { case -1: @@ -193,37 +195,6 @@ getremcap(char *host) vsetnum(ETIMEOUT, 0); else vsetnum(ETIMEOUT, val); -} - -char * -getremote(char *host) -{ - char *cp; - static char *next; - static int lookedup = 0; - if (!lookedup) { - if (host == NULL && (host = getenv("HOST")) == NULL) { - fprintf(stderr, "%s: no host specified\n", __progname); - exit(3); - } - getremcap(host); - next = vgetstr(DEVICE); - lookedup++; - } - /* - * We return a new device each time we're called (to allow - * a rotary action to be simulated) - */ - if (next == NULL) - return (NULL); - if ((cp = strchr(next, ',')) == NULL) { - vsetstr(DEVICE, next); - next = NULL; - } else { - *cp++ = '\0'; - vsetstr(DEVICE, next); - next = cp; - } return (vgetstr(DEVICE)); } diff --git a/usr.bin/tip/tip.c b/usr.bin/tip/tip.c index 8a13a8f2349..1272ec71738 100644 --- a/usr.bin/tip/tip.c +++ b/usr.bin/tip/tip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tip.c,v 1.49 2010/07/01 21:47:09 nicm Exp $ */ +/* $OpenBSD: tip.c,v 1.50 2010/07/02 05:52:48 nicm Exp $ */ /* $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $ */ /* @@ -104,15 +104,7 @@ main(int argc, char *argv[]) (void)signal(SIGTERM, cleanup); (void)signal(SIGCHLD, SIG_DFL); - if ((i = hunt(sys)) == 0) { - printf("all ports busy\n"); - exit(3); - } - if (i == -1) { - printf("link down\n"); - (void)uu_unlock(uucplock); - exit(3); - } + FD = hunt(sys); setbuf(stdout, NULL); loginit(); vinit(); /* init variables */ diff --git a/usr.bin/tip/tip.h b/usr.bin/tip/tip.h index 4645d5c4442..6a01ca642ac 100644 --- a/usr.bin/tip/tip.h +++ b/usr.bin/tip/tip.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tip.h,v 1.49 2010/07/01 21:43:38 nicm Exp $ */ +/* $OpenBSD: tip.h,v 1.50 2010/07/02 05:52:48 nicm Exp $ */ /* $NetBSD: tip.h,v 1.7 1997/04/20 00:02:46 mellon Exp $ */ /* @@ -193,7 +193,7 @@ void variable(int); void cumain(int, char **); /* hunt.c */ -long hunt(char *); +int hunt(char *); /* log.c */ void logent(char *, char *, char *); |