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