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