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