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