xref: /illumos-gate/usr/src/cmd/bnu/dio.c (revision ace1a5f1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "uucp.h"
33 
34 #ifdef D_PROTOCOL
35 #include <dk.h>
36 
37 #define XBUFSIZ 1024
38 time_t time();
39 static jmp_buf Dfailbuf;
40 extern int drdblk();
41 
42 /*
43  * Datakit protocol
44  */
45 /* ARGSUSED */
46 static void
dalarm(sig)47 dalarm(sig)
48 int sig;
49 {
50 	longjmp(Dfailbuf,1);
51 }
52 
53 static void (*dsig)();
54 #ifndef V8
55 static short dkrmode[3] = { DKR_BLOCK, 0, 0 };
56 static short dkeof[3] = { 106, 0, 0 };	/* End of File signal */
57 #endif
58 
59 /*
60  * turn on protocol
61  */
62 int
dturnon()63 dturnon()
64 {
65 #ifdef V8
66 	extern int dkp_ld;
67 #endif
68 
69 	dsig=signal(SIGALRM, dalarm);
70 #ifdef V8
71 	if (dkproto(Ofn, dkp_ld) < 0)
72 	   {
73 		DEBUG(3, "%s\n", "No dkp_ld");
74 		return(-1);
75 	   }
76 #else
77 	if((*Ioctl)(Ofn, DIOCRMODE, dkrmode) < 0) {
78 	    int ret;
79 	    ret=(*Ioctl)(Ofn, DIOCRMODE, dkrmode);
80 	    DEBUG(4, "dturnon: ret=%d, ", ret);
81 	    DEBUG(4, "Ofn=%d, ", Ofn);
82 	    DEBUG(4, "errno=%d\n", errno);
83 	    return(-1);
84 	}
85 #endif /* V8 */
86 	return(0);
87 }
88 
89 int
dturnoff()90 dturnoff()
91 {
92 	(void) signal(SIGALRM, dsig);
93 	return(0);
94 }
95 
96 /*
97  * write message across Datakit link
98  *	type	-> message type
99  *	str	-> message body (ascii string)
100  *	fn	-> Datakit file descriptor
101  * return
102  *	SUCCESS	-> message sent
103  *	FAIL	-> write failed
104  */
105 int
dwrmsg(type,str,fn)106 dwrmsg(type, str, fn)
107 register char *str;
108 int fn;
109 char type;
110 {
111 	register char *s;
112 	char bufr[XBUFSIZ];
113 
114 	bufr[0] = type;
115 	s = &bufr[1];
116 	while (*str)
117 		*s++ = *str++;
118 	*s = '\0';
119 	if (*(--s) == '\n')
120 		*s = '\0';
121 	return((*Write)(fn, bufr, (unsigned) strlen(bufr) + 1) < 0 ? FAIL : SUCCESS);
122 }
123 
124 /*
125  * read message from Datakit link
126  *	str	-> message buffer
127  *	fn	-> Datakit file descriptor
128  * return
129  *	FAIL	-> send timed out
130  *	SUCCESS	-> ok message in str
131  */
132 int
drdmsg(str,fn)133 drdmsg(str, fn)
134 register char *str;
135 {
136 
137 	register int len;
138 
139 	if(setjmp(Dfailbuf))
140 		return(FAIL);
141 
142 	(void) alarm(60);
143 	for (;;) {
144 		if( (len = (*Read)(fn, str, XBUFSIZ)) <= 0) {
145 			(void) alarm(0);
146 			return(FAIL);
147 		}
148 		str += len;
149 		if (*(str - 1) == '\0')
150 			break;
151 	}
152 	(void) alarm(0);
153 	return(SUCCESS);
154 }
155 
156 /*
157  * read data from file fp1 and write
158  * on Datakit link
159  *	fp1	-> file descriptor
160  *	fn	-> Datakit descriptor
161  * returns:
162  *	FAIL	->failure in Datakit link
163  *	SUCCESS	-> ok
164  */
165 int
dwrdata(fp1,fn)166 dwrdata(fp1, fn)
167 FILE *fp1;
168 {
169 	register int fd1;
170 	register int len, ret;
171 	unsigned long bytes;
172 	char bufr[XBUFSIZ];
173 
174 	bytes = 0L;
175 	fd1 = fileno( fp1 );
176 	while ((len = read( fd1, bufr, XBUFSIZ )) > 0) {
177 		bytes += len;
178 		putfilesize(bytes);
179 		ret = (*Write)(fn, bufr, (unsigned) len);
180 		if (ret != len) {
181 			return(FAIL);
182 		}
183 		if (len != XBUFSIZ)
184 			break;
185 	}
186 	ASSERT(len >= 0, "DISK READ ERROR", strerror(errno), len);
187 #ifndef V8
188 	(*Ioctl)(fn, DIOCXCTL, dkeof);
189 #endif
190 	ret = (*Write)(fn, bufr, (unsigned) 0);
191 	return(SUCCESS);
192 }
193 
194 /*
195  * read data from Datakit link and
196  * write into file
197  *	fp2	-> file descriptor
198  *	fn	-> Datakit descriptor
199  * returns:
200  *	SUCCESS	-> ok
201  *	FAIL	-> failure on Datakit link
202  */
203 int
drddata(fn,fp2)204 drddata(fn, fp2)
205 FILE *fp2;
206 {
207 	register int fd2;
208 	register int len;
209 	register int ret = SUCCESS;
210 	unsigned long bytes;
211 	char bufr[XBUFSIZ];
212 
213 	bytes = 0L;
214 	fd2 = fileno( fp2 );
215 	for (;;) {
216 		len = drdblk(bufr, XBUFSIZ, fn);
217 		if (len < 0) {
218 			return(FAIL);
219 		}
220 		bytes += len;
221 		putfilesize(bytes);
222 		if( ret == SUCCESS && write( fd2, bufr, len ) != len )
223 			ret = errno;
224 		if (len < XBUFSIZ)
225 			break;
226 	}
227 	return(ret);
228 }
229 
230 /*
231  * read block from Datakit link
232  * reads are timed
233  *	blk	-> address of buffer
234  *	len	-> size to read
235  *	fn	-> Datakit descriptor
236  * returns:
237  *	FAIL	-> link error timeout on link
238  *	i	-> # of bytes read
239  */
240 int
drdblk(blk,len,fn)241 drdblk(blk, len,  fn)
242 register char *blk;
243 {
244 	register int i, ret;
245 	struct dkqqabo	why;
246 
247 	if(setjmp(Dfailbuf))
248 		return(FAIL);
249 
250 	for (i = 0; i < len; i += ret) {
251 		(void) alarm(60);
252 		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
253 			(void) alarm(0);
254 			return(FAIL);
255 		}
256 		blk += ret;
257 		if (ret == 0) {	/* zero length block contains only EOF signal */
258 			ioctl(fn, DIOCQQABO, &why);
259 			if (why.rcv_ctlchar != dkeof[0])
260 				i = FAIL;
261 			break;
262 		}
263 	}
264 	(void) alarm(0);
265 	return(i);
266 }
267 #endif /* D_PROTOCOL */
268