xref: /titanic_52/usr/src/cmd/bnu/eio.c (revision 462be471126495414a94f9fa35e16c02dc462c04)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*462be471Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "uucp.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	E_PROTOCOL
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifndef MIN
387c478bd9Sstevel@tonic-gate #define     MIN(a,b) (((a)<(b))?(a):(b))
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined (ATTSVR4)
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #endif /* BSD4_2 || ATTSVR4 */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	EBUFSIZ	1024
467c478bd9Sstevel@tonic-gate #define	EMESGLEN 20
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define TBUFSIZE 1024
497c478bd9Sstevel@tonic-gate #define TPACKSIZE	512
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate extern long lseek();	/* Find offset into the file. */
527c478bd9Sstevel@tonic-gate static jmp_buf Failbuf;
537c478bd9Sstevel@tonic-gate extern int erdblk();
547c478bd9Sstevel@tonic-gate extern unsigned msgtime;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static char Erdstash[EBUFSIZ];
577c478bd9Sstevel@tonic-gate static int Erdlen;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * error-free channel protocol
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate /* ARGSUSED */
637c478bd9Sstevel@tonic-gate static void
647c478bd9Sstevel@tonic-gate ealarm(sig)
657c478bd9Sstevel@tonic-gate int sig;
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	longjmp(Failbuf, 1);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate static void (*esig)();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * turn on protocol timer
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate int
757c478bd9Sstevel@tonic-gate eturnon()
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	esig=signal(SIGALRM, ealarm);
787c478bd9Sstevel@tonic-gate 	return(0);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int
827c478bd9Sstevel@tonic-gate eturnoff()
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	signal(SIGALRM, esig);
857c478bd9Sstevel@tonic-gate 	return(0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * write message across link
907c478bd9Sstevel@tonic-gate  *	type	-> message type
917c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
927c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
937c478bd9Sstevel@tonic-gate  * return
947c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
957c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int
987c478bd9Sstevel@tonic-gate ewrmsg(type, str, fn)
99*462be471Sceastha char *str;
1007c478bd9Sstevel@tonic-gate int fn;
1017c478bd9Sstevel@tonic-gate char type;
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	return(etwrmsg(type, str, fn, 0));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * read message from link
1087c478bd9Sstevel@tonic-gate  *	str	-> message buffer
1097c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
1107c478bd9Sstevel@tonic-gate  * return
1117c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
1127c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate int
1157c478bd9Sstevel@tonic-gate erdmsg(str, fn)
116*462be471Sceastha char *str;
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	return(etrdmsg(str, fn, 0));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * read data from file fp1 and write
1237c478bd9Sstevel@tonic-gate  * on link
1247c478bd9Sstevel@tonic-gate  *	fp1	-> file descriptor
1257c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
1267c478bd9Sstevel@tonic-gate  * returns:
1277c478bd9Sstevel@tonic-gate  *	FAIL	->failure in link
1287c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate int
1317c478bd9Sstevel@tonic-gate ewrdata(fp1, fn)
132*462be471Sceastha FILE *fp1;
1337c478bd9Sstevel@tonic-gate int	fn;
1347c478bd9Sstevel@tonic-gate {
135*462be471Sceastha 	int ret;
1367c478bd9Sstevel@tonic-gate 	int	fd1;
1377c478bd9Sstevel@tonic-gate 	int len;
1387c478bd9Sstevel@tonic-gate 	unsigned long bytes;
1397c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ];
1407c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
1417c478bd9Sstevel@tonic-gate 	off_t	msglen;
1427c478bd9Sstevel@tonic-gate 	char	cmsglen[EMESGLEN];
1437c478bd9Sstevel@tonic-gate 	off_t	startPoint;	/* Offset from begining of the file in
1447c478bd9Sstevel@tonic-gate 				 *   case we are restarting from a check
1457c478bd9Sstevel@tonic-gate 				 *   point.
1467c478bd9Sstevel@tonic-gate 				 */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
1497c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata failed\n%s", "");
1507c478bd9Sstevel@tonic-gate 		return(FAIL);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	bytes = 0L;
1537c478bd9Sstevel@tonic-gate 	fd1 = fileno(fp1);
1547c478bd9Sstevel@tonic-gate 	fstat(fd1, &statbuf);
1557c478bd9Sstevel@tonic-gate 	startPoint = lseek(fd1, 0L, 1);
1567c478bd9Sstevel@tonic-gate 	if (startPoint < 0)
1577c478bd9Sstevel@tonic-gate 	{
1587c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata lseek failed.  Errno=%d\n", errno);
1597c478bd9Sstevel@tonic-gate 		return(FAIL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	msglen = statbuf.st_size - startPoint;
1627c478bd9Sstevel@tonic-gate 	if (msglen < 0)
1637c478bd9Sstevel@tonic-gate 	{
1647c478bd9Sstevel@tonic-gate 		DEBUG(7, "ewrdata: startPoint past end of file.\n%s", "");
1657c478bd9Sstevel@tonic-gate 		return(FAIL);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	sprintf(cmsglen, "%ld", (long) msglen);
1687c478bd9Sstevel@tonic-gate 	DEBUG(9, "ewrdata writing %d ...", sizeof(cmsglen));
1697c478bd9Sstevel@tonic-gate 	alarm(msgtime);
1707c478bd9Sstevel@tonic-gate 	ret = (*Write)(fn, cmsglen, sizeof(cmsglen));
1717c478bd9Sstevel@tonic-gate 	alarm(0);
1727c478bd9Sstevel@tonic-gate 	DEBUG(9, "ret %d\n", ret);
1737c478bd9Sstevel@tonic-gate 	if (ret != sizeof(cmsglen))
1747c478bd9Sstevel@tonic-gate 		return(FAIL);
1757c478bd9Sstevel@tonic-gate 	DEBUG(7, "ewrdata planning to send %ld bytes to remote.\n", msglen);
1767c478bd9Sstevel@tonic-gate 	while ((len = read( fd1, bufr, EBUFSIZ )) > 0) {
1777c478bd9Sstevel@tonic-gate 		DEBUG(9, "ewrdata writing %d ...", len);
1787c478bd9Sstevel@tonic-gate 		alarm(msgtime);
1797c478bd9Sstevel@tonic-gate 		bytes += len;
1807c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
1817c478bd9Sstevel@tonic-gate 		ret = (*Write)(fn, bufr, (unsigned) len);
1827c478bd9Sstevel@tonic-gate 		alarm(0);
1837c478bd9Sstevel@tonic-gate 		DEBUG(9, "ewrdata ret %d\n", ret);
1847c478bd9Sstevel@tonic-gate 		if (ret != len)
1857c478bd9Sstevel@tonic-gate 			return(FAIL);
1867c478bd9Sstevel@tonic-gate 		if ((msglen -= len) <= 0)
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	if (len < 0 || (len == 0 && msglen != 0)) return(FAIL);
1907c478bd9Sstevel@tonic-gate 	return(SUCCESS);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * read data from link and
1957c478bd9Sstevel@tonic-gate  * write into file
1967c478bd9Sstevel@tonic-gate  *	fp2	-> file descriptor
1977c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
1987c478bd9Sstevel@tonic-gate  * returns:
1997c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
2007c478bd9Sstevel@tonic-gate  *	FAIL	-> failure on link
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate int
2037c478bd9Sstevel@tonic-gate erddata(fn, fp2)
204*462be471Sceastha FILE *fp2;
2057c478bd9Sstevel@tonic-gate {
206*462be471Sceastha 	int ret;
2077c478bd9Sstevel@tonic-gate 	int	fd2;
2087c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ];
2097c478bd9Sstevel@tonic-gate 	int	len;
2107c478bd9Sstevel@tonic-gate 	long	msglen, bytes;
2117c478bd9Sstevel@tonic-gate 	char	cmsglen[EMESGLEN], *cptr, *erdptr = Erdstash;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	DEBUG(9, "erddata wants %d\n", sizeof(cmsglen));
2147c478bd9Sstevel@tonic-gate 	if (Erdlen > 0) {
2157c478bd9Sstevel@tonic-gate 		DEBUG(9, "%d bytes stashed\n", Erdlen);
2167c478bd9Sstevel@tonic-gate 		if (Erdlen >= sizeof(cmsglen)) {
2177c478bd9Sstevel@tonic-gate 			memcpy(cmsglen, erdptr, sizeof(cmsglen));
2187c478bd9Sstevel@tonic-gate 			Erdlen -= sizeof(cmsglen);
2197c478bd9Sstevel@tonic-gate 			erdptr += sizeof(cmsglen);
2207c478bd9Sstevel@tonic-gate 			ret = len = 0;
2217c478bd9Sstevel@tonic-gate 		} else {
2227c478bd9Sstevel@tonic-gate 			memcpy(cmsglen, Erdstash, Erdlen);
2237c478bd9Sstevel@tonic-gate 			cptr = cmsglen + Erdlen;
2247c478bd9Sstevel@tonic-gate 			len = sizeof(cmsglen) - Erdlen;
2257c478bd9Sstevel@tonic-gate 			ret = erdblk(cptr, len, fn);
2267c478bd9Sstevel@tonic-gate 			Erdlen = 0;
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 		len = sizeof(cmsglen);
2307c478bd9Sstevel@tonic-gate 		ret = erdblk(cmsglen, sizeof(cmsglen), fn);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	if (ret != len)
2337c478bd9Sstevel@tonic-gate 		return(FAIL);
2347c478bd9Sstevel@tonic-gate 	ret = SUCCESS;
2357c478bd9Sstevel@tonic-gate 	sscanf(cmsglen, "%ld", &msglen);
2367c478bd9Sstevel@tonic-gate 	if ( ((msglen-1)/512 +1) > Ulimit )
2377c478bd9Sstevel@tonic-gate 		ret = EFBIG;
2387c478bd9Sstevel@tonic-gate 	DEBUG(7, "erddata file is %ld bytes\n", msglen);
2397c478bd9Sstevel@tonic-gate 	fd2 = fileno( fp2 );
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (Erdlen > 0) {
2427c478bd9Sstevel@tonic-gate 		DEBUG(9, "%d bytes stashed\n", Erdlen);
2437c478bd9Sstevel@tonic-gate 		if (write(fileno(fp2), erdptr, Erdlen) != Erdlen)
2447c478bd9Sstevel@tonic-gate 			return(FAIL);
2457c478bd9Sstevel@tonic-gate 		msglen -= Erdlen;
2467c478bd9Sstevel@tonic-gate 		Erdlen = 0;
2477c478bd9Sstevel@tonic-gate 		DEBUG(7, "erddata remainder is %ld bytes\n", msglen);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	for (;;) {
2517c478bd9Sstevel@tonic-gate 		len = erdblk(bufr, (int) MIN(msglen, EBUFSIZ), fn);
2527c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk ret %d\n", len);
2537c478bd9Sstevel@tonic-gate 		if (len < 0) {
2547c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk failed\n%s", "");
2557c478bd9Sstevel@tonic-gate 			return(FAIL);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		bytes += len;
2587c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
2597c478bd9Sstevel@tonic-gate 		if ((msglen -= len) < 0) {
2607c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk read too much\n%s", "");
2617c478bd9Sstevel@tonic-gate 			return(FAIL);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		/* this write is to file -- use write(2), not (*Write) */
2647c478bd9Sstevel@tonic-gate 		if ( ret == SUCCESS && write( fd2, bufr, len ) != len ) {
2657c478bd9Sstevel@tonic-gate 			ret = errno;
2667c478bd9Sstevel@tonic-gate 			DEBUG(7, "erddata: write to file failed, errno %d\n", ret);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		if (msglen == 0)
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	return(ret);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * read block from link
2767c478bd9Sstevel@tonic-gate  * reads are timed
2777c478bd9Sstevel@tonic-gate  *	blk	-> address of buffer
2787c478bd9Sstevel@tonic-gate  *	len	-> size to read
2797c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
2807c478bd9Sstevel@tonic-gate  * returns:
2817c478bd9Sstevel@tonic-gate  *	FAIL	-> link error timeout on link
2827c478bd9Sstevel@tonic-gate  *	i	-> # of bytes read (must not be 0)
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate int
2857c478bd9Sstevel@tonic-gate erdblk(blk, len,  fn)
286*462be471Sceastha char *blk;
2877c478bd9Sstevel@tonic-gate {
288*462be471Sceastha 	int i, ret;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if(setjmp(Failbuf)) {
2917c478bd9Sstevel@tonic-gate 		DEBUG(7, "timeout (%d sec)\n", msgtime);
2927c478bd9Sstevel@tonic-gate 		return(FAIL);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	alarm(msgtime);
2967c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += ret) {
2977c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk ask %d ", len - i);
2987c478bd9Sstevel@tonic-gate 		if ((ret = (*Read)(fn, blk, (unsigned) len - i)) < 0) {
2997c478bd9Sstevel@tonic-gate 			alarm(0);
3007c478bd9Sstevel@tonic-gate 			DEBUG(7, "erdblk read failed\n%s", "");
3017c478bd9Sstevel@tonic-gate 			return(FAIL);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		DEBUG(9, "erdblk got %d\n", ret);
3047c478bd9Sstevel@tonic-gate 		if (ret == 0)
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 		blk += ret;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	alarm(0);
3097c478bd9Sstevel@tonic-gate 	return(i);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate struct tbuf {
3137c478bd9Sstevel@tonic-gate 	long t_nbytes;
3147c478bd9Sstevel@tonic-gate 	char t_data[TBUFSIZE];
3157c478bd9Sstevel@tonic-gate };
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * read message from link
3197c478bd9Sstevel@tonic-gate  *	str	-> message buffer
3207c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
3217c478bd9Sstevel@tonic-gate  * return
3227c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
3237c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
3247c478bd9Sstevel@tonic-gate  */
325*462be471Sceastha int
3267c478bd9Sstevel@tonic-gate trdmsg(str, fn)
3277c478bd9Sstevel@tonic-gate char *str;
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	return(etrdmsg(str, fn, TPACKSIZE));
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * write message across link
3347c478bd9Sstevel@tonic-gate  *	type	-> message type
3357c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
3367c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
3377c478bd9Sstevel@tonic-gate  * return
3387c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
3397c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
3407c478bd9Sstevel@tonic-gate  */
341*462be471Sceastha int
3427c478bd9Sstevel@tonic-gate twrmsg(type, str, fn)
3437c478bd9Sstevel@tonic-gate char type;
3447c478bd9Sstevel@tonic-gate char *str;
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	return(etwrmsg(type, str, fn, TPACKSIZE));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * read data from file fp1 and write on link
3517c478bd9Sstevel@tonic-gate  *	fp1	-> file descriptor
3527c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
3537c478bd9Sstevel@tonic-gate  * returns:
3547c478bd9Sstevel@tonic-gate  *	FAIL	->failure in link
3557c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
3567c478bd9Sstevel@tonic-gate  */
357*462be471Sceastha int
3587c478bd9Sstevel@tonic-gate twrdata(fp1, fn)
359*462be471Sceastha FILE *fp1;
3607c478bd9Sstevel@tonic-gate int	fn;
3617c478bd9Sstevel@tonic-gate {
362*462be471Sceastha 	int ret;
3637c478bd9Sstevel@tonic-gate 	int len;
3647c478bd9Sstevel@tonic-gate 	unsigned long bytes;
3657c478bd9Sstevel@tonic-gate 	struct tbuf bufr;
3667c478bd9Sstevel@tonic-gate 	struct stat statbuf;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
3697c478bd9Sstevel@tonic-gate 		DEBUG(7, "twrdata failed\n", 0);
3707c478bd9Sstevel@tonic-gate 		return(FAIL);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	fstat(fileno(fp1), &statbuf);
3737c478bd9Sstevel@tonic-gate 	bytes = 0L;
3747c478bd9Sstevel@tonic-gate 	while ((len = read(fileno(fp1), bufr.t_data, TBUFSIZE)) > 0) {
3757c478bd9Sstevel@tonic-gate 		bufr.t_nbytes = htonl((long)len);
3767c478bd9Sstevel@tonic-gate 		DEBUG(7, "twrdata writing %d ...", len);
3777c478bd9Sstevel@tonic-gate 		bytes += len;
3787c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
3797c478bd9Sstevel@tonic-gate 		len += sizeof(long);
3807c478bd9Sstevel@tonic-gate 		alarm(msgtime);
3817c478bd9Sstevel@tonic-gate 		ret = (*Write)(fn, (char *)&bufr, (unsigned) len);
3827c478bd9Sstevel@tonic-gate 		alarm(0);
3837c478bd9Sstevel@tonic-gate 		DEBUG(7, "ret %d\n", ret);
3847c478bd9Sstevel@tonic-gate 		if (ret != len)
3857c478bd9Sstevel@tonic-gate 			return(FAIL);
3867c478bd9Sstevel@tonic-gate 		if (len != TBUFSIZE+sizeof(long))
3877c478bd9Sstevel@tonic-gate 			break;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	bufr.t_nbytes = 0;
3907c478bd9Sstevel@tonic-gate 	alarm(msgtime);
3917c478bd9Sstevel@tonic-gate 	ret = write(fn, (char *)&bufr, sizeof(long));
3927c478bd9Sstevel@tonic-gate 	alarm(0);
3937c478bd9Sstevel@tonic-gate 	if (ret != sizeof(long))
3947c478bd9Sstevel@tonic-gate 		return FAIL;
3957c478bd9Sstevel@tonic-gate 	return(SUCCESS);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * read data from link and write into file
4007c478bd9Sstevel@tonic-gate  *	fp2	-> file descriptor
4017c478bd9Sstevel@tonic-gate  *	fn	-> link descriptor
4027c478bd9Sstevel@tonic-gate  * returns:
4037c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok
4047c478bd9Sstevel@tonic-gate  *	FAIL	-> failure on link
4057c478bd9Sstevel@tonic-gate  */
406*462be471Sceastha int
4077c478bd9Sstevel@tonic-gate trddata(fn, fp2)
408*462be471Sceastha FILE *fp2;
4097c478bd9Sstevel@tonic-gate {
410*462be471Sceastha 	int len, nread;
4117c478bd9Sstevel@tonic-gate 	long Nbytes;
4127c478bd9Sstevel@tonic-gate 	unsigned long bytes = 0L;
4137c478bd9Sstevel@tonic-gate 	char bufr[TBUFSIZE];
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	for (;;) {
4167c478bd9Sstevel@tonic-gate 		len = erdblk((char *)&Nbytes, sizeof(Nbytes), fn);
4177c478bd9Sstevel@tonic-gate 		DEBUG(7, "trddata ret %d\n", len);
4187c478bd9Sstevel@tonic-gate 		if (len != sizeof(Nbytes))
4197c478bd9Sstevel@tonic-gate 			return(FAIL);
4207c478bd9Sstevel@tonic-gate 		Nbytes = ntohl(Nbytes);
4217c478bd9Sstevel@tonic-gate 		DEBUG(7,"trddata expecting %ld bytes\n", Nbytes);
4227c478bd9Sstevel@tonic-gate 		nread = Nbytes;
4237c478bd9Sstevel@tonic-gate 		if (nread == 0)
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 		len = erdblk(bufr, nread, fn);
4267c478bd9Sstevel@tonic-gate 		if (len != Nbytes)
4277c478bd9Sstevel@tonic-gate 			return(FAIL);
4287c478bd9Sstevel@tonic-gate 		bytes += len;
4297c478bd9Sstevel@tonic-gate 		putfilesize(bytes);
4307c478bd9Sstevel@tonic-gate 		if (write(fileno(fp2), bufr, len) != len)
4317c478bd9Sstevel@tonic-gate 			return(FAIL);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	return(SUCCESS);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * read message from link
4387c478bd9Sstevel@tonic-gate  *	str	-> message buffer
4397c478bd9Sstevel@tonic-gate  *	fn	-> file descriptor
4407c478bd9Sstevel@tonic-gate  *	i	-> if non-zero, amount to read; o.w., read up to '\0'
4417c478bd9Sstevel@tonic-gate  * return
4427c478bd9Sstevel@tonic-gate  *	FAIL	-> read timed out
4437c478bd9Sstevel@tonic-gate  *	SUCCESS	-> ok message in str
4447c478bd9Sstevel@tonic-gate  *
4457c478bd9Sstevel@tonic-gate  * 'e' is fatally flawed -- in a byte stream world, rdmsg can pick up
4467c478bd9Sstevel@tonic-gate  * the cmsglen on a R request.  if this happens, we stash the excess
4477c478bd9Sstevel@tonic-gate  * where rddata can pick it up.
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate 
450*462be471Sceastha int
4517c478bd9Sstevel@tonic-gate etrdmsg(str, fn, i)
452*462be471Sceastha char *str;
453*462be471Sceastha int i;
4547c478bd9Sstevel@tonic-gate {
455*462be471Sceastha 	int len;
4567c478bd9Sstevel@tonic-gate 	int nullterm = 0;
4577c478bd9Sstevel@tonic-gate 	char *null, *argstr;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (i == 0) {
4617c478bd9Sstevel@tonic-gate 		DEBUG(9, "etrdmsg looking for null terminator\n", 0);
4627c478bd9Sstevel@tonic-gate 		nullterm++;
4637c478bd9Sstevel@tonic-gate 		i = EBUFSIZ;
4647c478bd9Sstevel@tonic-gate 		argstr = str;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if(setjmp(Failbuf)) {
4687c478bd9Sstevel@tonic-gate 		DEBUG(7, "timeout (%d sec)\n", msgtime);
4697c478bd9Sstevel@tonic-gate 		return(FAIL);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	alarm(msgtime);
4737c478bd9Sstevel@tonic-gate 	for (;;) {
4747c478bd9Sstevel@tonic-gate 		DEBUG(9, "etrdmsg want %d ...", i);
4757c478bd9Sstevel@tonic-gate 		len = (*Read)(fn, str, i);
4767c478bd9Sstevel@tonic-gate 		DEBUG(9, "got %d\n", len);
4777c478bd9Sstevel@tonic-gate 		if (len == 0)
4787c478bd9Sstevel@tonic-gate 			continue;	/* timeout will get this */
4797c478bd9Sstevel@tonic-gate 		if (len < 0) {
4807c478bd9Sstevel@tonic-gate 			alarm(0);
4817c478bd9Sstevel@tonic-gate 			return(FAIL);
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 		str += len;
4847c478bd9Sstevel@tonic-gate 		i -= len;
4857c478bd9Sstevel@tonic-gate 		if (nullterm) {
4867c478bd9Sstevel@tonic-gate 			/* no way can a msg be as long as EBUFSIZ-1 ... */
4877c478bd9Sstevel@tonic-gate 			*str = 0;
4887c478bd9Sstevel@tonic-gate 			null = strchr(argstr, '\0');
4897c478bd9Sstevel@tonic-gate 			if (null != str) {
4907c478bd9Sstevel@tonic-gate 				null++;	/* start of stash */
4917c478bd9Sstevel@tonic-gate 				memcpy(Erdstash + Erdlen, null, str - null);
4927c478bd9Sstevel@tonic-gate 				Erdlen += str - null;
4937c478bd9Sstevel@tonic-gate 				break;
4947c478bd9Sstevel@tonic-gate 			} else
4957c478bd9Sstevel@tonic-gate 				argstr = str;
4967c478bd9Sstevel@tonic-gate 		} else {
4977c478bd9Sstevel@tonic-gate 			if (i == 0)
4987c478bd9Sstevel@tonic-gate 				break;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	alarm(0);
5027c478bd9Sstevel@tonic-gate 	return(SUCCESS);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * write message across link
5077c478bd9Sstevel@tonic-gate  *	type	-> message type
5087c478bd9Sstevel@tonic-gate  *	str	-> message body (ascii string)
5097c478bd9Sstevel@tonic-gate  *	fn	-> link file descriptor
5107c478bd9Sstevel@tonic-gate  *	len	-> if non-zero, amount to write;
5117c478bd9Sstevel@tonic-gate 		   o.w., write up to '\0' (inclusive)
5127c478bd9Sstevel@tonic-gate  * return
5137c478bd9Sstevel@tonic-gate  *	FAIL	-> write failed
5147c478bd9Sstevel@tonic-gate  *	SUCCESS	-> write succeeded
5157c478bd9Sstevel@tonic-gate  */
516*462be471Sceastha int
5177c478bd9Sstevel@tonic-gate etwrmsg(type, str, fn, len)
5187c478bd9Sstevel@tonic-gate char type;
519*462be471Sceastha char *str;
5207c478bd9Sstevel@tonic-gate int fn, len;
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	char bufr[EBUFSIZ], *endstr;
5237c478bd9Sstevel@tonic-gate 	int ret;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	bufr[0] = type;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/* point endstr to last character to be sent */
5287c478bd9Sstevel@tonic-gate 	if ((endstr = strchr(str, '\n')) != 0)
5297c478bd9Sstevel@tonic-gate 		*endstr = 0;
5307c478bd9Sstevel@tonic-gate 	else
5317c478bd9Sstevel@tonic-gate 		endstr = str + strlen(str);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	memcpy(bufr+1, str, (endstr - str) + 1);	/* include '\0' */
5347c478bd9Sstevel@tonic-gate 	if (len == 0)
5357c478bd9Sstevel@tonic-gate 		len = (endstr - str) + 2;	/* include bufr[0] and '\0' */
5367c478bd9Sstevel@tonic-gate 	else
5377c478bd9Sstevel@tonic-gate 		bufr[len-1] = 0;		/* 't' needs this terminator */
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (setjmp(Failbuf)) {
5417c478bd9Sstevel@tonic-gate 		DEBUG(7, "etwrmsg write failed\n", 0);
5427c478bd9Sstevel@tonic-gate 		return(FAIL);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	DEBUG(9, "etwrmsg want %d ... ", len);
5457c478bd9Sstevel@tonic-gate 	alarm(msgtime);
5467c478bd9Sstevel@tonic-gate 	ret = (*Write)(fn, bufr, (unsigned) len);
5477c478bd9Sstevel@tonic-gate 	alarm(0);
5487c478bd9Sstevel@tonic-gate 	DEBUG(9, "sent %d\n", ret);
5497c478bd9Sstevel@tonic-gate 	if (ret != len)
5507c478bd9Sstevel@tonic-gate 		return(FAIL);
5517c478bd9Sstevel@tonic-gate 	return(SUCCESS);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate #endif	/* E_PROTOCOL */
554