xref: /illumos-gate/usr/src/cmd/bnu/dkdial.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	create a Datakit connection to a remote destination
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate #ifndef DIAL
35*7c478bd9Sstevel@tonic-gate 	static char	SCCSID[] = "@(#)dkdial.c	2.7+BNU DKHOST 87/03/09";
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  *	COMMKIT(TM) Software - Datakit(R) VCS Interface Release 2.0 V1
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
42*7c478bd9Sstevel@tonic-gate #include "dk.h"
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <signal.h>
45*7c478bd9Sstevel@tonic-gate #define	SIGRTN	void
46*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
47*7c478bd9Sstevel@tonic-gate #include <sysexits.h>
48*7c478bd9Sstevel@tonic-gate #include <errno.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define DK_DEFWAIT	89	/* default time to wait for dial return */
52*7c478bd9Sstevel@tonic-gate #define	DK_MAXWAIT	600	/* maximum wait to allow the caller - 10 min */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate GLOBAL unsigned int	dk_timewait = DK_DEFWAIT; /* Caller to dkdial might modify */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static char	Conn_Msg[] = "Can't connect to %s: %s\n";
58*7c478bd9Sstevel@tonic-gate static char	Resp_Msg[] = "No response from Datakit";
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static SIGRTN	timout();	/* Alarm signal handler */
61*7c478bd9Sstevel@tonic-gate static void	setalarm(), usralarm();
62*7c478bd9Sstevel@tonic-gate EXTERN int	dkndial();
63*7c478bd9Sstevel@tonic-gate static int	Elapsed;	/* Alarm time elapsed during dial */
64*7c478bd9Sstevel@tonic-gate static int	Timer;		/* Current alarm setting */
65*7c478bd9Sstevel@tonic-gate static short	TimeErr;	/* Alarm clock rang */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate extern char	*getenv();
68*7c478bd9Sstevel@tonic-gate EXTERN int	dk_verbose, dk_errno;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate GLOBAL int
dkdial(dest)71*7c478bd9Sstevel@tonic-gate dkdial(dest)
72*7c478bd9Sstevel@tonic-gate 	char *dest;
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	return(dkndial(dest, atoi(getenv("DKINTF"))));
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate GLOBAL int
dkndial(dest,intf)78*7c478bd9Sstevel@tonic-gate dkndial(dest, intf)
79*7c478bd9Sstevel@tonic-gate 	char *dest;
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	short		fd;		/* Channel Descriptor	*/
82*7c478bd9Sstevel@tonic-gate 	SIGRTN		(*SigWas)();	/* Caller's alarm handler */
83*7c478bd9Sstevel@tonic-gate 	unsigned int	TimWas;		/* Caller's alarm clock */
84*7c478bd9Sstevel@tonic-gate 	char		*key;
85*7c478bd9Sstevel@tonic-gate 	struct diocdial {
86*7c478bd9Sstevel@tonic-gate 			struct	diocreq iocb;
87*7c478bd9Sstevel@tonic-gate 			char	dialstring[128];
88*7c478bd9Sstevel@tonic-gate 		}	ioreq;
89*7c478bd9Sstevel@tonic-gate 	char		dial_dev[32];
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	sprintf(dial_dev, "/dev/dk/dial%d", intf);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	/*
95*7c478bd9Sstevel@tonic-gate 	** Clear our elapsed time and save caller's alarm stuff.
96*7c478bd9Sstevel@tonic-gate 	*/
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	Timer = Elapsed = 0;
99*7c478bd9Sstevel@tonic-gate 	SigWas = signal(SIGALRM, timout);
100*7c478bd9Sstevel@tonic-gate 	TimWas = alarm(0);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	** If requested timeout interval is unreasonable, use the default.
104*7c478bd9Sstevel@tonic-gate 	*/
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if ((dk_timewait == 0)  || (dk_timewait > DK_MAXWAIT))
107*7c478bd9Sstevel@tonic-gate 		dk_timewait = DK_DEFWAIT;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	/*
110*7c478bd9Sstevel@tonic-gate 	** Do an alarm protected open of the dial device
111*7c478bd9Sstevel@tonic-gate 	*/
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	setalarm(dk_timewait);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if ((fd = open(dial_dev, O_RDWR)) < 0) {
116*7c478bd9Sstevel@tonic-gate 		setalarm(0);
117*7c478bd9Sstevel@tonic-gate 		if (dk_verbose)
118*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "dkdial: Can't open %s\n", dial_dev);
119*7c478bd9Sstevel@tonic-gate 		usralarm(TimWas, SigWas);
120*7c478bd9Sstevel@tonic-gate 		if (errno == EBUSY)
121*7c478bd9Sstevel@tonic-gate 			return(dk_errno = -EX_TEMPFAIL);
122*7c478bd9Sstevel@tonic-gate 		else
123*7c478bd9Sstevel@tonic-gate 			return(dk_errno = -EX_OSFILE);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	/*
127*7c478bd9Sstevel@tonic-gate 	** If the caller has a DKKEY, use it.
128*7c478bd9Sstevel@tonic-gate 	*/
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if((key = getenv("DKKEY")) != NULL && getuid() == geteuid())
131*7c478bd9Sstevel@tonic-gate 		sprintf(ioreq.dialstring, "%s\n%s", dest, key);
132*7c478bd9Sstevel@tonic-gate 	else
133*7c478bd9Sstevel@tonic-gate 		strcpy(ioreq.dialstring, dest);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	ioreq.iocb.req_traffic = 0;
136*7c478bd9Sstevel@tonic-gate 	ioreq.iocb.req_1param = 0;
137*7c478bd9Sstevel@tonic-gate 	ioreq.iocb.req_2param = 0;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	** Try to dial the call.  If the alarm expires during the ioctl,
141*7c478bd9Sstevel@tonic-gate 	** the ioctl will return in error.
142*7c478bd9Sstevel@tonic-gate 	*/
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIODIAL, &ioreq) < 0) {
145*7c478bd9Sstevel@tonic-gate 		setalarm(0);
146*7c478bd9Sstevel@tonic-gate 		if (dk_verbose)
147*7c478bd9Sstevel@tonic-gate 		if (TimeErr)
148*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, Conn_Msg, Resp_Msg, ioreq.dialstring);
149*7c478bd9Sstevel@tonic-gate 		else
150*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, Conn_Msg, ioreq.dialstring, dkerr(ioreq.iocb.req_error));
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		setalarm(2);		/* Don't wait forever on close */
153*7c478bd9Sstevel@tonic-gate 		close(fd);
154*7c478bd9Sstevel@tonic-gate 		usralarm(TimWas, SigWas);
155*7c478bd9Sstevel@tonic-gate 		if (errno == EBUSY)
156*7c478bd9Sstevel@tonic-gate 			return(-dkerrmap(dk_errno = -EX_TEMPFAIL));
157*7c478bd9Sstevel@tonic-gate 		else
158*7c478bd9Sstevel@tonic-gate 			return(-dkerrmap(dk_errno = ioreq.iocb.req_error));
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	usralarm(TimWas, SigWas);
161*7c478bd9Sstevel@tonic-gate 	return (fd);
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate /*
165*7c478bd9Sstevel@tonic-gate ** timout() is the alarm clock signal handling routine.  It is called
166*7c478bd9Sstevel@tonic-gate ** whenever the alarm clock expires during dial processing.
167*7c478bd9Sstevel@tonic-gate */
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
170*7c478bd9Sstevel@tonic-gate static SIGRTN
timout(arg)171*7c478bd9Sstevel@tonic-gate timout(arg)
172*7c478bd9Sstevel@tonic-gate int arg;
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	TimeErr++;
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate /*
178*7c478bd9Sstevel@tonic-gate ** setalarm() is called to request an alarm at a future time.  The residual
179*7c478bd9Sstevel@tonic-gate ** from the previous alarm (if any) is added to the elapsed time counter.
180*7c478bd9Sstevel@tonic-gate */
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static void
setalarm(Seconds)183*7c478bd9Sstevel@tonic-gate setalarm(Seconds)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	TimeErr = 0;
186*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, timout);
187*7c478bd9Sstevel@tonic-gate 	Elapsed += Timer - alarm(Seconds);
188*7c478bd9Sstevel@tonic-gate 	Timer = Seconds;
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate ** usralarm() is used to restore the alarm service for the caller.
193*7c478bd9Sstevel@tonic-gate */
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate static void
usralarm(TimWas,SigWas)196*7c478bd9Sstevel@tonic-gate usralarm(TimWas, SigWas)
197*7c478bd9Sstevel@tonic-gate 	int		TimWas;		/* Caller's alarm clock */
198*7c478bd9Sstevel@tonic-gate 	SIGRTN		(*SigWas)();	/* Caller's alarm handler */
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate 	Elapsed += Timer - alarm(0);
201*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, SigWas);
202*7c478bd9Sstevel@tonic-gate 	if (TimWas > 0) {
203*7c478bd9Sstevel@tonic-gate 		TimWas -= Elapsed;
204*7c478bd9Sstevel@tonic-gate 		if (TimWas < 2)
205*7c478bd9Sstevel@tonic-gate 			TimWas = 2;
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	alarm(TimWas);
208*7c478bd9Sstevel@tonic-gate }
209