Annotation of src/usr.bin/tip/hunt.c, Revision 1.18
1.18 ! nicm 1: /* $OpenBSD: hunt.c,v 1.17 2010/07/01 21:28:01 nicm Exp $ */
1.5 millert 2: /* $NetBSD: hunt.c,v 1.6 1997/04/20 00:02:10 mellon Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.10 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.16 nicm 33: #include <util.h>
34:
1.1 deraadt 35: #include "tip.h"
36:
1.18 ! nicm 37: static jmp_buf deadline;
! 38: static int deadflag;
1.1 deraadt 39:
1.12 moritz 40: static void dead(int);
41:
1.13 deraadt 42: /*ARGSUSED*/
1.12 moritz 43: static void
1.11 deraadt 44: dead(int signo)
1.1 deraadt 45: {
1.18 ! nicm 46: deadflag = 1;
1.1 deraadt 47: longjmp(deadline, 1);
48: }
49:
1.18 ! nicm 50: /* Find and open the host. Returns the fd, or exits on error. */
! 51: int
! 52: hunt(char *hosts)
1.1 deraadt 53: {
1.18 ! nicm 54: char *copy, *last, *host, *device;
! 55: struct termios tio;
! 56: int fd, tried;
! 57: sig_t old_alrm;
! 58:
! 59: if (hosts == NULL) {
! 60: hosts = getenv("HOST");
! 61: if (hosts == NULL)
! 62: errx(3, "no host specified");
! 63: }
! 64:
! 65: if ((copy = strdup(hosts)) == NULL)
! 66: err(1, "strdup");
! 67: last = copy;
! 68:
! 69: old_alrm = signal(SIGALRM, dead);
1.1 deraadt 70:
1.18 ! nicm 71: tried = 0;
! 72: while ((host = strsep(&last, ",")) != NULL) {
! 73: device = getremote(host);
! 74:
! 75: uucplock = strrchr(device, '/');
1.7 deraadt 76: if (uucplock == NULL)
1.18 ! nicm 77: uucplock = strdup(device);
1.7 deraadt 78: else
1.18 ! nicm 79: uucplock = strdup(uucplock + 1);
! 80: if (uucplock == NULL)
! 81: err(1, "strdup");
1.16 nicm 82: if (uu_lock(uucplock) != UU_LOCK_OK)
1.1 deraadt 83: continue;
1.15 nicm 84:
1.18 ! nicm 85: deadflag = 0;
1.1 deraadt 86: if (setjmp(deadline) == 0) {
87: alarm(10);
1.18 ! nicm 88:
! 89: fd = open(device,
! 90: O_RDWR | (vgetnum(DC) ? O_NONBLOCK : 0));
! 91: if (fd < 0)
! 92: perror(host);
1.1 deraadt 93: }
94: alarm(0);
1.2 deraadt 95:
1.18 ! nicm 96: tried++;
! 97: if (fd >= 0 && !deadflag) {
! 98: tcgetattr(fd, &tio);
1.17 nicm 99: if (!vgetnum(DC))
1.18 ! nicm 100: tio.c_cflag |= HUPCL;
! 101: if (tcsetattr(fd, TCSAFLUSH, &tio) != 0)
! 102: errx(1, "tcsetattr");
! 103:
! 104: if (ioctl(fd, TIOCEXCL) != 0)
! 105: errx(1, "ioctl");
! 106:
! 107: signal(SIGALRM, old_alrm);
! 108: return (fd);
1.1 deraadt 109: }
1.15 nicm 110:
1.18 ! nicm 111: uu_unlock(uucplock);
! 112: free(uucplock);
! 113: }
! 114: free(copy);
! 115:
! 116: signal(SIGALRM, old_alrm);
! 117: if (tried == 0) {
! 118: printf("all ports busy\n");
! 119: exit(3);
1.1 deraadt 120: }
1.18 ! nicm 121: printf("link down\n");
! 122: exit(3);
1.1 deraadt 123: }