xref: /titanic_51/usr/src/cmd/bnu/dio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:dio.c 2.11 */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "uucp.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #ifdef D_PROTOCOL
31*7c478bd9Sstevel@tonic-gate #include <dk.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #define XBUFSIZ 1024
34*7c478bd9Sstevel@tonic-gate time_t time();
35*7c478bd9Sstevel@tonic-gate static jmp_buf Dfailbuf;
36*7c478bd9Sstevel@tonic-gate extern int drdblk();
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Datakit protocol
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
42*7c478bd9Sstevel@tonic-gate static void
43*7c478bd9Sstevel@tonic-gate dalarm(sig)
44*7c478bd9Sstevel@tonic-gate int sig;
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	longjmp(Dfailbuf,1);
47*7c478bd9Sstevel@tonic-gate }
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static void (*dsig)();
50*7c478bd9Sstevel@tonic-gate #ifndef V8
51*7c478bd9Sstevel@tonic-gate static short dkrmode[3] = { DKR_BLOCK, 0, 0 };
52*7c478bd9Sstevel@tonic-gate static short dkeof[3] = { 106, 0, 0 };	/* End of File signal */
53*7c478bd9Sstevel@tonic-gate #endif
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * turn on protocol
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate dturnon()
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate #ifdef V8
62*7c478bd9Sstevel@tonic-gate 	extern int dkp_ld;
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	dsig=signal(SIGALRM, dalarm);
66*7c478bd9Sstevel@tonic-gate #ifdef V8
67*7c478bd9Sstevel@tonic-gate 	if (dkproto(Ofn, dkp_ld) < 0)
68*7c478bd9Sstevel@tonic-gate 	   {
69*7c478bd9Sstevel@tonic-gate 		DEBUG(3, "%s\n", "No dkp_ld");
70*7c478bd9Sstevel@tonic-gate 		return(-1);
71*7c478bd9Sstevel@tonic-gate 	   }
72*7c478bd9Sstevel@tonic-gate #else
73*7c478bd9Sstevel@tonic-gate 	if((*Ioctl)(Ofn, DIOCRMODE, dkrmode) < 0) {
74*7c478bd9Sstevel@tonic-gate 	    int ret;
75*7c478bd9Sstevel@tonic-gate 	    ret=(*Ioctl)(Ofn, DIOCRMODE, dkrmode);
76*7c478bd9Sstevel@tonic-gate 	    DEBUG(4, "dturnon: ret=%d, ", ret);
77*7c478bd9Sstevel@tonic-gate 	    DEBUG(4, "Ofn=%d, ", Ofn);
78*7c478bd9Sstevel@tonic-gate 	    DEBUG(4, "errno=%d\n", errno);
79*7c478bd9Sstevel@tonic-gate 	    return(-1);
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate #endif /* V8 */
82*7c478bd9Sstevel@tonic-gate 	return(0);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate dturnoff()
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, dsig);
89*7c478bd9Sstevel@tonic-gate 	return(0);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * write message across Datakit link
94*7c478bd9Sstevel@tonic-gate  *	type	-> message type
95*7c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
96*7c478bd9Sstevel@tonic-gate  *	fn	-> Datakit file descriptor
97*7c478bd9Sstevel@tonic-gate  * return
98*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> message sent
99*7c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate int
102*7c478bd9Sstevel@tonic-gate dwrmsg(type, str, fn)
103*7c478bd9Sstevel@tonic-gate register char *str;
104*7c478bd9Sstevel@tonic-gate int fn;
105*7c478bd9Sstevel@tonic-gate char type;
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	register char *s;
108*7c478bd9Sstevel@tonic-gate 	char bufr[XBUFSIZ];
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	bufr[0] = type;
111*7c478bd9Sstevel@tonic-gate 	s = &bufr[1];
112*7c478bd9Sstevel@tonic-gate 	while (*str)
113*7c478bd9Sstevel@tonic-gate 		*s++ = *str++;
114*7c478bd9Sstevel@tonic-gate 	*s = '\0';
115*7c478bd9Sstevel@tonic-gate 	if (*(--s) == '\n')
116*7c478bd9Sstevel@tonic-gate 		*s = '\0';
117*7c478bd9Sstevel@tonic-gate 	return((*Write)(fn, bufr, (unsigned) strlen(bufr) + 1) < 0 ? FAIL : SUCCESS);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * read message from Datakit link
122*7c478bd9Sstevel@tonic-gate  *	str	-> message buffer
123*7c478bd9Sstevel@tonic-gate  *	fn	-> Datakit file descriptor
124*7c478bd9Sstevel@tonic-gate  * return
125*7c478bd9Sstevel@tonic-gate  *	FAIL	-> send timed out
126*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
127*7c478bd9Sstevel@tonic-gate  */
128*7c478bd9Sstevel@tonic-gate int
129*7c478bd9Sstevel@tonic-gate drdmsg(str, fn)
130*7c478bd9Sstevel@tonic-gate register char *str;
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	register int len;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if(setjmp(Dfailbuf))
136*7c478bd9Sstevel@tonic-gate 		return(FAIL);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	(void) alarm(60);
139*7c478bd9Sstevel@tonic-gate 	for (;;) {
140*7c478bd9Sstevel@tonic-gate 		if( (len = (*Read)(fn, str, XBUFSIZ)) <= 0) {
141*7c478bd9Sstevel@tonic-gate 			(void) alarm(0);
142*7c478bd9Sstevel@tonic-gate 			return(FAIL);
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 		str += len;
145*7c478bd9Sstevel@tonic-gate 		if (*(str - 1) == '\0')
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	(void) alarm(0);
149*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * read data from file fp1 and write
154*7c478bd9Sstevel@tonic-gate  * on Datakit link
155*7c478bd9Sstevel@tonic-gate  *	fp1	-> file descriptor
156*7c478bd9Sstevel@tonic-gate  *	fn	-> Datakit descriptor
157*7c478bd9Sstevel@tonic-gate  * returns:
158*7c478bd9Sstevel@tonic-gate  *	FAIL	->failure in Datakit link
159*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate int
162*7c478bd9Sstevel@tonic-gate dwrdata(fp1, fn)
163*7c478bd9Sstevel@tonic-gate FILE *fp1;
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	register int fd1;
166*7c478bd9Sstevel@tonic-gate 	register int len, ret;
167*7c478bd9Sstevel@tonic-gate 	unsigned long bytes;
168*7c478bd9Sstevel@tonic-gate 	char bufr[XBUFSIZ];
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	bytes = 0L;
171*7c478bd9Sstevel@tonic-gate 	fd1 = fileno( fp1 );
172*7c478bd9Sstevel@tonic-gate 	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
173*7c478bd9Sstevel@tonic-gate 		bytes += len;
174*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
175*7c478bd9Sstevel@tonic-gate 		ret = (*Write)(fn, bufr, (unsigned) len);
176*7c478bd9Sstevel@tonic-gate 		if (ret != len) {
177*7c478bd9Sstevel@tonic-gate 			return(FAIL);
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 		if (len != XBUFSIZ)
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 	ASSERT(len >= 0, "DISK READ ERROR", sys_errlist[errno], len);
183*7c478bd9Sstevel@tonic-gate #ifndef V8
184*7c478bd9Sstevel@tonic-gate 	(*Ioctl)(fn, DIOCXCTL, dkeof);
185*7c478bd9Sstevel@tonic-gate #endif
186*7c478bd9Sstevel@tonic-gate 	ret = (*Write)(fn, bufr, (unsigned) 0);
187*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate  * read data from Datakit link and
192*7c478bd9Sstevel@tonic-gate  * write into file
193*7c478bd9Sstevel@tonic-gate  *	fp2	-> file descriptor
194*7c478bd9Sstevel@tonic-gate  *	fn	-> Datakit descriptor
195*7c478bd9Sstevel@tonic-gate  * returns:
196*7c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
197*7c478bd9Sstevel@tonic-gate  *	FAIL	-> failure on Datakit link
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate int
200*7c478bd9Sstevel@tonic-gate drddata(fn, fp2)
201*7c478bd9Sstevel@tonic-gate FILE *fp2;
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	register int fd2;
204*7c478bd9Sstevel@tonic-gate 	register int len;
205*7c478bd9Sstevel@tonic-gate 	register int ret = SUCCESS;
206*7c478bd9Sstevel@tonic-gate 	unsigned long bytes;
207*7c478bd9Sstevel@tonic-gate 	char bufr[XBUFSIZ];
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	bytes = 0L;
210*7c478bd9Sstevel@tonic-gate 	fd2 = fileno( fp2 );
211*7c478bd9Sstevel@tonic-gate 	for (;;) {
212*7c478bd9Sstevel@tonic-gate 		len = drdblk(bufr, XBUFSIZ, fn);
213*7c478bd9Sstevel@tonic-gate 		if (len < 0) {
214*7c478bd9Sstevel@tonic-gate 			return(FAIL);
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 		bytes += len;
217*7c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
218*7c478bd9Sstevel@tonic-gate 		if( ret == SUCCESS && write( fd2, bufr, len ) != len )
219*7c478bd9Sstevel@tonic-gate 			ret = errno;
220*7c478bd9Sstevel@tonic-gate 		if (len < XBUFSIZ)
221*7c478bd9Sstevel@tonic-gate 			break;
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	return(ret);
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate /*
227*7c478bd9Sstevel@tonic-gate  * read block from Datakit link
228*7c478bd9Sstevel@tonic-gate  * reads are timed
229*7c478bd9Sstevel@tonic-gate  *	blk	-> address of buffer
230*7c478bd9Sstevel@tonic-gate  *	len	-> size to read
231*7c478bd9Sstevel@tonic-gate  *	fn	-> Datakit descriptor
232*7c478bd9Sstevel@tonic-gate  * returns:
233*7c478bd9Sstevel@tonic-gate  *	FAIL	-> link error timeout on link
234*7c478bd9Sstevel@tonic-gate  *	i	-> # of bytes read
235*7c478bd9Sstevel@tonic-gate  */
236*7c478bd9Sstevel@tonic-gate int
237*7c478bd9Sstevel@tonic-gate drdblk(blk, len,  fn)
238*7c478bd9Sstevel@tonic-gate register char *blk;
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	register int i, ret;
241*7c478bd9Sstevel@tonic-gate 	struct dkqqabo	why;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	if(setjmp(Dfailbuf))
244*7c478bd9Sstevel@tonic-gate 		return(FAIL);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += ret) {
247*7c478bd9Sstevel@tonic-gate 		(void) alarm(60);
248*7c478bd9Sstevel@tonic-gate 		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
249*7c478bd9Sstevel@tonic-gate 			(void) alarm(0);
250*7c478bd9Sstevel@tonic-gate 			return(FAIL);
251*7c478bd9Sstevel@tonic-gate 		}
252*7c478bd9Sstevel@tonic-gate 		blk += ret;
253*7c478bd9Sstevel@tonic-gate 		if (ret == 0) {	/* zero length block contains only EOF signal */
254*7c478bd9Sstevel@tonic-gate 			ioctl(fn, DIOCQQABO, &why);
255*7c478bd9Sstevel@tonic-gate 			if (why.rcv_ctlchar != dkeof[0])
256*7c478bd9Sstevel@tonic-gate 				i = FAIL;
257*7c478bd9Sstevel@tonic-gate 			break;
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	(void) alarm(0);
261*7c478bd9Sstevel@tonic-gate 	return(i);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate #endif /* D_PROTOCOL */
264