xref: /onnv-gate/usr/src/cmd/rmt/rmt.c (revision 1053:667012633a0d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1053Smaheshvs  * Common Development and Distribution License (the "License").
6*1053Smaheshvs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1053Smaheshvs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  *  Multi-process streaming 4.3bsd /etc/rmt server.
300Sstevel@tonic-gate  *  Has three locks (for stdin, stdout, and the tape)
310Sstevel@tonic-gate  *  that are passed by signals and received by sigpause().
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <setjmp.h>
420Sstevel@tonic-gate #include <sys/ioctl.h>
430Sstevel@tonic-gate #include <sys/mtio.h>
440Sstevel@tonic-gate #include <sys/wait.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static sigset_t	cmdmask, maskall, newmask;
500Sstevel@tonic-gate static sigset_t	sendmask, tapemask;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static struct mtop mtop;
530Sstevel@tonic-gate static struct mtget mtget;
540Sstevel@tonic-gate static jmp_buf sjbuf;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	RECV	SIGIO
570Sstevel@tonic-gate #define	TAPE	SIGURG
580Sstevel@tonic-gate #define	SEND	SIGALRM
590Sstevel@tonic-gate #define	ERROR	SIGTERM
600Sstevel@tonic-gate #define	OPEN	SIGUSR1
610Sstevel@tonic-gate #define	CLOSE	SIGUSR2
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Support for Version 1 of the extended RMT protocol:
650Sstevel@tonic-gate  * Placing RMTIVERSION (-1) into the mt_op field of the ioctl ('I')
660Sstevel@tonic-gate  * request will return the current version of the RMT protocol that
670Sstevel@tonic-gate  * the server supports.  For servers that don't support Version 1,
680Sstevel@tonic-gate  * an error is returned and the client knows to only use Version 0
690Sstevel@tonic-gate  * (stock BSD) calls, which include mt_op values in the range of [0-7].
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * Note: The RMTIVERSION request must be made in order for the extended
720Sstevel@tonic-gate  * protocol commands to be recognized.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate #define	RMTIVERSION	-1
750Sstevel@tonic-gate #define	RMT_VERSION	1
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * These requests are made to the extended RMT protocol by specifying the
790Sstevel@tonic-gate  * new 'i' command of RMT Protocol Version 1.  They are intended to allow
800Sstevel@tonic-gate  * an intelligent client to communicate with both BSD and Solaris RMT
810Sstevel@tonic-gate  * servers heterogeneously.  The 'i' command taks an mtop structure as
820Sstevel@tonic-gate  * argument, exactly like the 'I' command does.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #define	RMTICACHE	0
850Sstevel@tonic-gate #define	RMTINOCACHE	1
860Sstevel@tonic-gate #define	RMTIRETEN	2
870Sstevel@tonic-gate #define	RMTIERASE	3
880Sstevel@tonic-gate #define	RMTIEOM		4
890Sstevel@tonic-gate #define	RMTINBSF	5
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * These requests are made to the extended RMT protocol by specifying the
930Sstevel@tonic-gate  * new 's' command of RMT Protocol Version 1.  They are intended to allow
940Sstevel@tonic-gate  * an intelligent client to obtain "mt status" information with both BSD
950Sstevel@tonic-gate  * and Solaris RMT servers heterogeneously.  They return the requested
960Sstevel@tonic-gate  * piece of the mtget structure as an ascii integer.  The request is made
970Sstevel@tonic-gate  * by sending the required character immediately after the 's' character
980Sstevel@tonic-gate  * without any trailing newline.  A single ascii integer is returned, else
990Sstevel@tonic-gate  * an error is returned.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate #define	MTS_TYPE	'T'		/* mtget.mt_type */
1020Sstevel@tonic-gate #define	MTS_DSREG	'D'		/* mtget.mt_dsreg */
1030Sstevel@tonic-gate #define	MTS_ERREG	'E'		/* mtget.mt_erreg */
1040Sstevel@tonic-gate #define	MTS_RESID	'R'		/* mtget.mt_resid */
1050Sstevel@tonic-gate #define	MTS_FILENO	'F'		/* mtget.mt_fileno */
1060Sstevel@tonic-gate #define	MTS_BLKNO	'B'		/* mtget.mt_blkno */
1070Sstevel@tonic-gate #define	MTS_FLAGS	'f'		/* mtget.mt_flags */
1080Sstevel@tonic-gate #define	MTS_BF		'b'		/* mtget.mt_bf */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define	MAXCHILD 1
1110Sstevel@tonic-gate static pid_t	childpid[MAXCHILD];
1120Sstevel@tonic-gate static int	children;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static int	tape = -1;
1150Sstevel@tonic-gate static size_t	maxrecsize = 0;
1160Sstevel@tonic-gate static char	*record;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #define	SSIZE	64
1190Sstevel@tonic-gate static char	pos[SSIZE], op[SSIZE], mode[SSIZE], count[SSIZE];
1200Sstevel@tonic-gate static char	device[MAXPATHLEN];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static FILE	*debug;
1230Sstevel@tonic-gate #define	DEBUG(f)		if (debug) (void) fprintf(debug, (f))
1240Sstevel@tonic-gate #define	DEBUG1(f, a)		if (debug) (void) fprintf(debug, (f), (a))
1250Sstevel@tonic-gate #define	DEBUG2(f, a, b)		if (debug) (void) fprintf(debug, (f), (a), (b))
1260Sstevel@tonic-gate #define	DEBUG3(f, a, b, c)	if (debug) \
1270Sstevel@tonic-gate 				    (void) fprintf(debug, (f), (a), (b), (c))
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static char key;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #ifdef __STDC__
1320Sstevel@tonic-gate static void respond(offset_t, int);
1330Sstevel@tonic-gate static void getstring(char *, size_t);
1340Sstevel@tonic-gate static void checkbuf(size_t);
1350Sstevel@tonic-gate #else
1360Sstevel@tonic-gate static void respond();
1370Sstevel@tonic-gate static void getstring();
1380Sstevel@tonic-gate static void checkbuf();
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static void
catch(int sig)142*1053Smaheshvs catch(int sig)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	switch (sig) {
1450Sstevel@tonic-gate 	default:    return;
1460Sstevel@tonic-gate 	case OPEN:  key = 'O';	break;
1470Sstevel@tonic-gate 	case CLOSE: key = 'C';	break;
1480Sstevel@tonic-gate 	case ERROR: key = 'E';	break;
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &maskall, (sigset_t *)0);
1510Sstevel@tonic-gate 	longjmp(sjbuf, 1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
154*1053Smaheshvs int
main(int argc,char * argv[])155*1053Smaheshvs main(int argc, char *argv[])
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	struct sigaction sa;
1580Sstevel@tonic-gate 	pid_t parent = getpid(), next = parent;
1590Sstevel@tonic-gate 	int saverr;
1600Sstevel@tonic-gate 	offset_t rval;
1610Sstevel@tonic-gate 	ssize_t cc;
1620Sstevel@tonic-gate 	size_t n, i;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1650Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1660Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (argc > 1) {
1710Sstevel@tonic-gate 		if ((debug = fopen(argv[1], "w")) == NULL)
1720Sstevel@tonic-gate 			exit(1);
1730Sstevel@tonic-gate 		setbuf(debug, NULL);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	(void) sigemptyset(&maskall);
1760Sstevel@tonic-gate 	(void) sigaddset(&maskall, RECV);
1770Sstevel@tonic-gate 	(void) sigaddset(&maskall, OPEN);
1780Sstevel@tonic-gate 	(void) sigaddset(&maskall, CLOSE);
1790Sstevel@tonic-gate 	(void) sigaddset(&maskall, ERROR);
1800Sstevel@tonic-gate 	(void) sigaddset(&maskall, TAPE);
1810Sstevel@tonic-gate 	(void) sigaddset(&maskall, SEND);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	tapemask = maskall;
1840Sstevel@tonic-gate 	(void) sigdelset(&tapemask, TAPE);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	sendmask = maskall;
1870Sstevel@tonic-gate 	(void) sigdelset(&sendmask, SEND);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	(void) sigemptyset(&cmdmask);
1900Sstevel@tonic-gate 	(void) sigaddset(&cmdmask, TAPE);
1910Sstevel@tonic-gate 	(void) sigaddset(&cmdmask, SEND);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	sa.sa_handler = catch;
1960Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
1970Sstevel@tonic-gate 	(void) sigaction(RECV, &sa, (struct sigaction *)0);
1980Sstevel@tonic-gate 	(void) sigaction(SEND, &sa, (struct sigaction *)0);
1990Sstevel@tonic-gate 	(void) sigaction(TAPE, &sa, (struct sigaction *)0);
2000Sstevel@tonic-gate 	(void) sigaction(OPEN, &sa, (struct sigaction *)0);
2010Sstevel@tonic-gate 	(void) sigaction(CLOSE, &sa, (struct sigaction *)0);
2020Sstevel@tonic-gate 	(void) sigaction(ERROR, &sa, (struct sigaction *)0);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &maskall, (sigset_t *)0);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	(void) kill(parent, TAPE);
2070Sstevel@tonic-gate 	(void) kill(parent, SEND);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	while (read(0, &key, 1) == 1) {
2100Sstevel@tonic-gate 		switch (key) {
2110Sstevel@tonic-gate 		case 'L':		/* lseek */
2120Sstevel@tonic-gate 			getstring(count, sizeof (count));
2130Sstevel@tonic-gate 			getstring(pos, sizeof (pos));
2140Sstevel@tonic-gate 			DEBUG2("rmtd: L %s %s\n", count, pos);
2150Sstevel@tonic-gate 			(void) kill(next, RECV);
2160Sstevel@tonic-gate 			(void) sigsuspend(&tapemask);
2170Sstevel@tonic-gate 			rval = llseek(tape, atoll(count), atoi(pos));
2180Sstevel@tonic-gate 			saverr = errno;
2190Sstevel@tonic-gate 			(void) kill(next, TAPE);
2200Sstevel@tonic-gate 			(void) sigsuspend(&sendmask);
2210Sstevel@tonic-gate 			respond(rval, saverr);
2220Sstevel@tonic-gate 			break;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		case 'I':		/* ioctl */
2250Sstevel@tonic-gate 		case 'i': {		/* extended version ioctl */
2260Sstevel@tonic-gate 			int bad = 0;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 			getstring(op, sizeof (op));
2290Sstevel@tonic-gate 			getstring(count, sizeof (count));
2300Sstevel@tonic-gate 			DEBUG3("rmtd: %c %s %s\n", key, op, count);
2310Sstevel@tonic-gate 			mtop.mt_op = atoi(op);
2320Sstevel@tonic-gate 			mtop.mt_count = atoi(count);
2330Sstevel@tonic-gate 			if (key == 'i') {
2340Sstevel@tonic-gate 				/*
2350Sstevel@tonic-gate 				 * Map the supported compatibility defines
2360Sstevel@tonic-gate 				 * into real ioctl values.
2370Sstevel@tonic-gate 				 */
2380Sstevel@tonic-gate 				switch (mtop.mt_op) {
2390Sstevel@tonic-gate 				case RMTICACHE:
2400Sstevel@tonic-gate 				case RMTINOCACHE:	/* not support on Sun */
2410Sstevel@tonic-gate 					bad = 1;
2420Sstevel@tonic-gate 					break;
2430Sstevel@tonic-gate 				case RMTIRETEN:
2440Sstevel@tonic-gate 					mtop.mt_op = MTRETEN;
2450Sstevel@tonic-gate 					break;
2460Sstevel@tonic-gate 				case RMTIERASE:
2470Sstevel@tonic-gate 					mtop.mt_op = MTERASE;
2480Sstevel@tonic-gate 					break;
2490Sstevel@tonic-gate 				case RMTIEOM:
2500Sstevel@tonic-gate 					mtop.mt_op = MTEOM;
2510Sstevel@tonic-gate 					break;
2520Sstevel@tonic-gate 				case RMTINBSF:
2530Sstevel@tonic-gate 					mtop.mt_op = MTNBSF;
2540Sstevel@tonic-gate 					break;
2550Sstevel@tonic-gate 				default:
2560Sstevel@tonic-gate 					bad = 1;
2570Sstevel@tonic-gate 					break;
2580Sstevel@tonic-gate 				}
2590Sstevel@tonic-gate 			}
2600Sstevel@tonic-gate 			if (bad) {
2610Sstevel@tonic-gate 				respond(-1LL, EINVAL);
2620Sstevel@tonic-gate 			} else {
2630Sstevel@tonic-gate 				(void) kill(next, RECV);
2640Sstevel@tonic-gate 				(void) sigsuspend(&tapemask);
2650Sstevel@tonic-gate 				if (mtop.mt_op == RMTIVERSION) {
2660Sstevel@tonic-gate 					mtop.mt_count = RMT_VERSION;
2670Sstevel@tonic-gate 					rval = (offset_t)mtop.mt_count;
2680Sstevel@tonic-gate 				} else {
2690Sstevel@tonic-gate 					rval = (offset_t)ioctl(tape, MTIOCTOP,
2700Sstevel@tonic-gate 					    (char *)&mtop);
2710Sstevel@tonic-gate 				}
2720Sstevel@tonic-gate 				saverr = errno;
2730Sstevel@tonic-gate 				(void) kill(next, TAPE);
2740Sstevel@tonic-gate 				(void) sigsuspend(&sendmask);
2750Sstevel@tonic-gate 				respond(rval < 0 ?
2760Sstevel@tonic-gate 				    rval : (offset_t)mtop.mt_count,
2770Sstevel@tonic-gate 				    saverr);
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		case 'S':		/* status */
2830Sstevel@tonic-gate 		case 's': {		/* extended status */
2840Sstevel@tonic-gate 			char skey;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 			DEBUG1("rmtd: %c\n", key);
2870Sstevel@tonic-gate 			if (key == 's') {
2880Sstevel@tonic-gate 				if (read(0, &skey, 1) != 1)
2890Sstevel@tonic-gate 					continue;
2900Sstevel@tonic-gate 			}
2910Sstevel@tonic-gate 			(void) kill(next, RECV);
2920Sstevel@tonic-gate 			(void) sigsuspend(&tapemask);
2930Sstevel@tonic-gate 			errno = 0;
2940Sstevel@tonic-gate 			rval = (offset_t)ioctl(tape, MTIOCGET, (char *)&mtget);
2950Sstevel@tonic-gate 			saverr = errno;
2960Sstevel@tonic-gate 			(void) kill(next, TAPE);
2970Sstevel@tonic-gate 			(void) sigsuspend(&sendmask);
2980Sstevel@tonic-gate 			if (rval < 0)
2990Sstevel@tonic-gate 				respond(rval, saverr);
3000Sstevel@tonic-gate 			else {
3010Sstevel@tonic-gate 				if (key == 's') {	/* extended status */
3020Sstevel@tonic-gate 					DEBUG1("rmtd: s%c\n", key);
3030Sstevel@tonic-gate 					switch (skey) {
3040Sstevel@tonic-gate 					case MTS_TYPE:
3050Sstevel@tonic-gate 						respond(
3060Sstevel@tonic-gate 						    (offset_t)mtget.mt_type,
3070Sstevel@tonic-gate 						    saverr);
3080Sstevel@tonic-gate 						break;
3090Sstevel@tonic-gate 					case MTS_DSREG:
3100Sstevel@tonic-gate 						respond(
3110Sstevel@tonic-gate 						    (offset_t)mtget.mt_dsreg,
3120Sstevel@tonic-gate 						    saverr);
3130Sstevel@tonic-gate 						break;
3140Sstevel@tonic-gate 					case MTS_ERREG:
3150Sstevel@tonic-gate 						respond(
3160Sstevel@tonic-gate 						    (offset_t)mtget.mt_erreg,
3170Sstevel@tonic-gate 						    saverr);
3180Sstevel@tonic-gate 						break;
3190Sstevel@tonic-gate 					case MTS_RESID:
3200Sstevel@tonic-gate 						respond(
3210Sstevel@tonic-gate 						    (offset_t)mtget.mt_resid,
3220Sstevel@tonic-gate 						    saverr);
3230Sstevel@tonic-gate 						break;
3240Sstevel@tonic-gate 					case MTS_FILENO:
3250Sstevel@tonic-gate 						respond(
3260Sstevel@tonic-gate 						    (offset_t)mtget.mt_fileno,
3270Sstevel@tonic-gate 						    saverr);
3280Sstevel@tonic-gate 						break;
3290Sstevel@tonic-gate 					case MTS_BLKNO:
3300Sstevel@tonic-gate 						respond(
3310Sstevel@tonic-gate 						    (offset_t)mtget.mt_blkno,
3320Sstevel@tonic-gate 						    saverr);
3330Sstevel@tonic-gate 						break;
3340Sstevel@tonic-gate 					case MTS_FLAGS:
3350Sstevel@tonic-gate 						respond(
3360Sstevel@tonic-gate 						    (offset_t)mtget.mt_flags,
3370Sstevel@tonic-gate 						    saverr);
3380Sstevel@tonic-gate 						break;
3390Sstevel@tonic-gate 					case MTS_BF:
3400Sstevel@tonic-gate 						respond((offset_t)mtget.mt_bf,
3410Sstevel@tonic-gate 						    saverr);
3420Sstevel@tonic-gate 						break;
3430Sstevel@tonic-gate 					default:
3440Sstevel@tonic-gate 						respond(-1LL, EINVAL);
3450Sstevel@tonic-gate 						break;
3460Sstevel@tonic-gate 					}
3470Sstevel@tonic-gate 				} else {
3480Sstevel@tonic-gate 					respond((offset_t)sizeof (mtget),
3490Sstevel@tonic-gate 					    saverr);
3500Sstevel@tonic-gate 					(void) write(1, (char *)&mtget,
3510Sstevel@tonic-gate 					    sizeof (mtget));
3520Sstevel@tonic-gate 				}
3530Sstevel@tonic-gate 			}
3540Sstevel@tonic-gate 			break;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		case 'W':
3580Sstevel@tonic-gate 			getstring(count, sizeof (count));
3590Sstevel@tonic-gate 			n = (size_t)atol(count);
3600Sstevel@tonic-gate 			checkbuf(n);
3610Sstevel@tonic-gate 			DEBUG1("rmtd: W %s\n", count);
3620Sstevel@tonic-gate #ifdef lint
3630Sstevel@tonic-gate 			cc = 0;
3640Sstevel@tonic-gate #endif
3650Sstevel@tonic-gate 			for (i = 0; i < n; i += (size_t)cc) {
3660Sstevel@tonic-gate 				cc = read(0, &record[i], n - i);
3670Sstevel@tonic-gate 				if (cc <= 0) {
3680Sstevel@tonic-gate 					DEBUG1(gettext("%s: premature eof\n"),
3690Sstevel@tonic-gate 						"rmtd");
3700Sstevel@tonic-gate 					exit(2);
3710Sstevel@tonic-gate 				}
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			(void) kill(next, RECV);
3740Sstevel@tonic-gate 			(void) sigsuspend(&tapemask);
3750Sstevel@tonic-gate 			rval = (offset_t)write(tape, record, n);
3760Sstevel@tonic-gate 			saverr = errno;
3770Sstevel@tonic-gate 			(void) kill(next, TAPE);
3780Sstevel@tonic-gate 			(void) sigsuspend(&sendmask);
3790Sstevel@tonic-gate 			respond(rval, saverr);
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		case 'R':
3830Sstevel@tonic-gate 			getstring(count, sizeof (count));
3840Sstevel@tonic-gate 			n = (size_t)atol(count);
3850Sstevel@tonic-gate 			checkbuf(n);
3860Sstevel@tonic-gate 			DEBUG1("rmtd: R %s\n", count);
3870Sstevel@tonic-gate 			(void) kill(next, RECV);
3880Sstevel@tonic-gate 			(void) sigsuspend(&tapemask);
3890Sstevel@tonic-gate 			rval = (offset_t)read(tape, record, n);
3900Sstevel@tonic-gate 			saverr = errno;
3910Sstevel@tonic-gate 			(void) kill(next, TAPE);
3920Sstevel@tonic-gate 			(void) sigsuspend(&sendmask);
3930Sstevel@tonic-gate 			respond(rval, saverr);
3940Sstevel@tonic-gate 			(void) write(1, record, (size_t)rval);
3950Sstevel@tonic-gate 			break;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		default:
3980Sstevel@tonic-gate 			DEBUG2(gettext("%s: garbage command '%c'\n"),
3990Sstevel@tonic-gate 				"rmtd", key);
4000Sstevel@tonic-gate 			/*FALLTHROUGH*/
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		case 'C':
4030Sstevel@tonic-gate 		case 'O':
4040Sstevel@tonic-gate 			/* rendezvous back into a single process */
4050Sstevel@tonic-gate 			if (setjmp(sjbuf) == 0 || getpid() != parent) {
4060Sstevel@tonic-gate 				(void) sigsuspend(&tapemask);
4070Sstevel@tonic-gate 				(void) sigsuspend(&sendmask);
4080Sstevel@tonic-gate 				(void) kill(parent, key == 'O' ? OPEN :
4090Sstevel@tonic-gate 					key == 'C' ? CLOSE : ERROR);
4100Sstevel@tonic-gate 				(void) sigemptyset(&newmask);
4110Sstevel@tonic-gate 				(void) sigsuspend(&newmask);
4120Sstevel@tonic-gate 			}
4130Sstevel@tonic-gate 			while (children > 0) {
4140Sstevel@tonic-gate 				(void) kill(childpid[--children], SIGKILL);
4150Sstevel@tonic-gate 				while (wait(NULL) != childpid[children])
4160Sstevel@tonic-gate 					;
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 			next = parent;
4190Sstevel@tonic-gate 			if (key == 'C') {
4200Sstevel@tonic-gate 				getstring(device, sizeof (device));
4210Sstevel@tonic-gate 				DEBUG1("rmtd: C %s\n", device);
4220Sstevel@tonic-gate 				rval = (offset_t)close(tape);
4230Sstevel@tonic-gate 				respond(rval, errno);
4240Sstevel@tonic-gate 				(void) kill(parent, TAPE);
4250Sstevel@tonic-gate 				(void) kill(parent, SEND);
4260Sstevel@tonic-gate 				continue;
4270Sstevel@tonic-gate 			}
4280Sstevel@tonic-gate 			if (key != 'O') 		/* garbage command */
4290Sstevel@tonic-gate 				exit(3);
4300Sstevel@tonic-gate 			(void) close(tape);
4310Sstevel@tonic-gate 			getstring(device, sizeof (device));
4320Sstevel@tonic-gate 			getstring(mode, sizeof (mode));
4330Sstevel@tonic-gate 			DEBUG2("rmtd: O %s %s\n", device, mode);
4340Sstevel@tonic-gate 			/*
4350Sstevel@tonic-gate 			 * Due to incompatibilities in the
4360Sstevel@tonic-gate 			 * assignment of mode bits between
4370Sstevel@tonic-gate 			 * BSD and System V, we strip all
4380Sstevel@tonic-gate 			 * but the read/write bits.  However,
4390Sstevel@tonic-gate 			 * we also want to handle things larger
4400Sstevel@tonic-gate 			 * than 2GB, so we also force O_LARGEFILE.
4410Sstevel@tonic-gate 			 */
4420Sstevel@tonic-gate 			tape = open(device, O_LARGEFILE |
4430Sstevel@tonic-gate 			    (atoi(mode) & (O_RDONLY|O_WRONLY|O_RDWR)));
4440Sstevel@tonic-gate 			respond((offset_t)tape, errno);
4450Sstevel@tonic-gate 			if (tape >= 0)			/* fork off */
4460Sstevel@tonic-gate 				while (children < MAXCHILD &&
4470Sstevel@tonic-gate 					(childpid[children] = fork()) > 0)
4480Sstevel@tonic-gate 						next = childpid[children++];
4490Sstevel@tonic-gate 			if (next == parent) {
4500Sstevel@tonic-gate 				(void) kill(parent, RECV);
4510Sstevel@tonic-gate 				(void) kill(parent, TAPE);
4520Sstevel@tonic-gate 				(void) kill(parent, SEND);
4530Sstevel@tonic-gate 			}
4540Sstevel@tonic-gate 			(void) sigsuspend(&cmdmask);
4550Sstevel@tonic-gate 			continue;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		(void) kill(next, SEND);
4580Sstevel@tonic-gate 		(void) sigsuspend(&cmdmask);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	(void) kill(next, RECV);
4610Sstevel@tonic-gate 	return (0);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate static void
respond(offset_t rval,int Errno)465*1053Smaheshvs respond(offset_t rval, int Errno)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	char resp[SSIZE];
4680Sstevel@tonic-gate 	char *errstr = strerror(Errno);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (rval < 0) {
4710Sstevel@tonic-gate 		(void) snprintf(resp, SSIZE, "E%d\n%s\n", Errno, errstr);
4720Sstevel@tonic-gate 		DEBUG2("rmtd: E %d (%s)\n", Errno, errstr);
4730Sstevel@tonic-gate 	} else {
4740Sstevel@tonic-gate 		(void) snprintf(resp, SSIZE, "A%lld\n", rval);
4750Sstevel@tonic-gate 		DEBUG1("rmtd: A %lld\n", rval);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	resp[SSIZE - 1] = '\0';
4780Sstevel@tonic-gate 	(void) write(1, resp, (int)strlen(resp));
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate static void
getstring(char * cp,size_t size)482*1053Smaheshvs getstring(char *cp, size_t size)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	char *limit = cp + size - 1;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	cp--;			/* nullify first increment */
4870Sstevel@tonic-gate 	do {
4880Sstevel@tonic-gate 		cp++;
4890Sstevel@tonic-gate 		if (read(0, cp, 1) != 1)
4900Sstevel@tonic-gate 			exit(0);
4910Sstevel@tonic-gate 	} while ((*cp != '\n') && (cp < limit));
4920Sstevel@tonic-gate 	*cp = '\0';
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static void
checkbuf(size_t size)496*1053Smaheshvs checkbuf(size_t size)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	if (size <= maxrecsize)
4990Sstevel@tonic-gate 		return;
5000Sstevel@tonic-gate 	if (record != 0)
5010Sstevel@tonic-gate 		free(record);
5020Sstevel@tonic-gate 	if ((record = malloc(size)) == NULL) {
5030Sstevel@tonic-gate 		DEBUG2(gettext("%s: cannot allocate %ld-byte buffer\n"),
5040Sstevel@tonic-gate 		    size, "rmtd");
5050Sstevel@tonic-gate 		exit(4);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	maxrecsize = size;
5080Sstevel@tonic-gate }
509