xref: /onnv-gate/usr/src/cmd/tip/hunt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /*
6*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
7*0Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*0Sstevel@tonic-gate  */
10*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from UCB 4.7 6/25/83 */
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include "tip.h"
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate extern char *getremote();
15*0Sstevel@tonic-gate extern int errno;
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate static	sigjmp_buf deadline;
18*0Sstevel@tonic-gate static	int deadfl;
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate void
21*0Sstevel@tonic-gate dead()
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate 	deadfl = 1;
25*0Sstevel@tonic-gate 	siglongjmp(deadline, 1);
26*0Sstevel@tonic-gate }
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate hunt(name)
29*0Sstevel@tonic-gate 	char *name;
30*0Sstevel@tonic-gate {
31*0Sstevel@tonic-gate 	register char *cp;
32*0Sstevel@tonic-gate 	void (*f)();
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate 	f = signal(SIGALRM, (sig_handler_t)dead);
35*0Sstevel@tonic-gate 	while (cp = getremote(name)) {
36*0Sstevel@tonic-gate 		deadfl = 0;
37*0Sstevel@tonic-gate 		uucplock = cp;
38*0Sstevel@tonic-gate 		if (mlock(uucplock) < 0) {
39*0Sstevel@tonic-gate 			delock(uucplock);
40*0Sstevel@tonic-gate 			continue;
41*0Sstevel@tonic-gate 		}
42*0Sstevel@tonic-gate 		/*
43*0Sstevel@tonic-gate 		 * Straight through call units, such as the BIZCOMP,
44*0Sstevel@tonic-gate 		 * VADIC and the DF, must indicate they're hardwired in
45*0Sstevel@tonic-gate 		 *  order to get an open file descriptor placed in FD.
46*0Sstevel@tonic-gate 		 * Otherwise, as for a DN-11, the open will have to
47*0Sstevel@tonic-gate 		 *  be done in the "open" routine.
48*0Sstevel@tonic-gate 		 */
49*0Sstevel@tonic-gate 		if (!HW)
50*0Sstevel@tonic-gate 			break;
51*0Sstevel@tonic-gate 		if (sigsetjmp(deadline, 1) == 0) {
52*0Sstevel@tonic-gate 			alarm(10);
53*0Sstevel@tonic-gate 			if (!trusted_device)
54*0Sstevel@tonic-gate 				userperm();
55*0Sstevel@tonic-gate 			errno = 0;
56*0Sstevel@tonic-gate 			if ((FD = open(cp, O_RDWR)) < 0 && errno != EBUSY) {
57*0Sstevel@tonic-gate 				fprintf(stderr, "tip: ");
58*0Sstevel@tonic-gate 				perror(cp);
59*0Sstevel@tonic-gate 			}
60*0Sstevel@tonic-gate 			if (!trusted_device)
61*0Sstevel@tonic-gate 				myperm();
62*0Sstevel@tonic-gate 			if (FD >= 0 && !isatty(FD)) {
63*0Sstevel@tonic-gate 				fprintf(stderr, "tip: %s: not a tty\n", cp);
64*0Sstevel@tonic-gate 				close(FD);
65*0Sstevel@tonic-gate 				FD = -1;
66*0Sstevel@tonic-gate 			}
67*0Sstevel@tonic-gate 		}
68*0Sstevel@tonic-gate 		alarm(0);
69*0Sstevel@tonic-gate 		if (!deadfl && FD >= 0) {
70*0Sstevel@tonic-gate 			struct termios t;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 			ioctl(FD, TCGETS, &t);
73*0Sstevel@tonic-gate 			t.c_cflag |= XCLUDE|HUPCL;
74*0Sstevel@tonic-gate 			ioctl(FD, TCSETSF, &t);
75*0Sstevel@tonic-gate 			signal(SIGALRM, f);
76*0Sstevel@tonic-gate 			return ((int)cp);
77*0Sstevel@tonic-gate 		}
78*0Sstevel@tonic-gate 		delock(uucplock);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	signal(SIGALRM, f);
81*0Sstevel@tonic-gate 	return (deadfl ? -1 : (int)cp);
82*0Sstevel@tonic-gate }
83