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
5*fe0e7ec4Smaheshvs * Common Development and Distribution License (the "License").
6*fe0e7ec4Smaheshvs * 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 /*
22*fe0e7ec4Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Multi-process streaming 4.3bsd /etc/rmt server.
287c478bd9Sstevel@tonic-gate * Has three locks (for stdin, stdout, and the tape)
297c478bd9Sstevel@tonic-gate * that are passed by signals and received by sigpause().
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <setjmp.h>
407c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
427c478bd9Sstevel@tonic-gate #include <sys/wait.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate static sigset_t cmdmask, maskall, newmask;
487c478bd9Sstevel@tonic-gate static sigset_t sendmask, tapemask;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static struct mtop mtop;
517c478bd9Sstevel@tonic-gate static struct mtget mtget;
527c478bd9Sstevel@tonic-gate static jmp_buf sjbuf;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #define RECV SIGIO
557c478bd9Sstevel@tonic-gate #define TAPE SIGURG
567c478bd9Sstevel@tonic-gate #define SEND SIGALRM
577c478bd9Sstevel@tonic-gate #define ERROR SIGTERM
587c478bd9Sstevel@tonic-gate #define OPEN SIGUSR1
597c478bd9Sstevel@tonic-gate #define CLOSE SIGUSR2
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * Support for Version 1 of the extended RMT protocol:
637c478bd9Sstevel@tonic-gate * Placing RMTIVERSION (-1) into the mt_op field of the ioctl ('I')
647c478bd9Sstevel@tonic-gate * request will return the current version of the RMT protocol that
657c478bd9Sstevel@tonic-gate * the server supports. For servers that don't support Version 1,
667c478bd9Sstevel@tonic-gate * an error is returned and the client knows to only use Version 0
677c478bd9Sstevel@tonic-gate * (stock BSD) calls, which include mt_op values in the range of [0-7].
687c478bd9Sstevel@tonic-gate *
697c478bd9Sstevel@tonic-gate * Note: The RMTIVERSION request must be made in order for the extended
707c478bd9Sstevel@tonic-gate * protocol commands to be recognized.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate #define RMTIVERSION -1
737c478bd9Sstevel@tonic-gate #define RMT_VERSION 1
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * These requests are made to the extended RMT protocol by specifying the
777c478bd9Sstevel@tonic-gate * new 'i' command of RMT Protocol Version 1. They are intended to allow
787c478bd9Sstevel@tonic-gate * an intelligent client to communicate with both BSD and Solaris RMT
797c478bd9Sstevel@tonic-gate * servers heterogeneously. The 'i' command taks an mtop structure as
807c478bd9Sstevel@tonic-gate * argument, exactly like the 'I' command does.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate #define RMTICACHE 0
837c478bd9Sstevel@tonic-gate #define RMTINOCACHE 1
847c478bd9Sstevel@tonic-gate #define RMTIRETEN 2
857c478bd9Sstevel@tonic-gate #define RMTIERASE 3
867c478bd9Sstevel@tonic-gate #define RMTIEOM 4
877c478bd9Sstevel@tonic-gate #define RMTINBSF 5
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * These requests are made to the extended RMT protocol by specifying the
917c478bd9Sstevel@tonic-gate * new 's' command of RMT Protocol Version 1. They are intended to allow
927c478bd9Sstevel@tonic-gate * an intelligent client to obtain "mt status" information with both BSD
937c478bd9Sstevel@tonic-gate * and Solaris RMT servers heterogeneously. They return the requested
947c478bd9Sstevel@tonic-gate * piece of the mtget structure as an ascii integer. The request is made
957c478bd9Sstevel@tonic-gate * by sending the required character immediately after the 's' character
967c478bd9Sstevel@tonic-gate * without any trailing newline. A single ascii integer is returned, else
977c478bd9Sstevel@tonic-gate * an error is returned.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate #define MTS_TYPE 'T' /* mtget.mt_type */
1007c478bd9Sstevel@tonic-gate #define MTS_DSREG 'D' /* mtget.mt_dsreg */
1017c478bd9Sstevel@tonic-gate #define MTS_ERREG 'E' /* mtget.mt_erreg */
1027c478bd9Sstevel@tonic-gate #define MTS_RESID 'R' /* mtget.mt_resid */
1037c478bd9Sstevel@tonic-gate #define MTS_FILENO 'F' /* mtget.mt_fileno */
1047c478bd9Sstevel@tonic-gate #define MTS_BLKNO 'B' /* mtget.mt_blkno */
1057c478bd9Sstevel@tonic-gate #define MTS_FLAGS 'f' /* mtget.mt_flags */
1067c478bd9Sstevel@tonic-gate #define MTS_BF 'b' /* mtget.mt_bf */
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate #define MAXCHILD 1
1097c478bd9Sstevel@tonic-gate static pid_t childpid[MAXCHILD];
1107c478bd9Sstevel@tonic-gate static int children;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate static int tape = -1;
1137c478bd9Sstevel@tonic-gate static size_t maxrecsize = 0;
1147c478bd9Sstevel@tonic-gate static char *record;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate #define SSIZE 64
1177c478bd9Sstevel@tonic-gate static char pos[SSIZE], op[SSIZE], mode[SSIZE], count[SSIZE];
1187c478bd9Sstevel@tonic-gate static char device[MAXPATHLEN];
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate static FILE *debug;
1217c478bd9Sstevel@tonic-gate #define DEBUG(f) if (debug) (void) fprintf(debug, (f))
1227c478bd9Sstevel@tonic-gate #define DEBUG1(f, a) if (debug) (void) fprintf(debug, (f), (a))
1237c478bd9Sstevel@tonic-gate #define DEBUG2(f, a, b) if (debug) (void) fprintf(debug, (f), (a), (b))
1247c478bd9Sstevel@tonic-gate #define DEBUG3(f, a, b, c) if (debug) \
1257c478bd9Sstevel@tonic-gate (void) fprintf(debug, (f), (a), (b), (c))
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate static char key;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate #ifdef __STDC__
1307c478bd9Sstevel@tonic-gate static void respond(offset_t, int);
1317c478bd9Sstevel@tonic-gate static void getstring(char *, size_t);
1327c478bd9Sstevel@tonic-gate static void checkbuf(size_t);
1337c478bd9Sstevel@tonic-gate #else
1347c478bd9Sstevel@tonic-gate static void respond();
1357c478bd9Sstevel@tonic-gate static void getstring();
1367c478bd9Sstevel@tonic-gate static void checkbuf();
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate static void
catch(int sig)140*fe0e7ec4Smaheshvs catch(int sig)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate switch (sig) {
1437c478bd9Sstevel@tonic-gate default: return;
1447c478bd9Sstevel@tonic-gate case OPEN: key = 'O'; break;
1457c478bd9Sstevel@tonic-gate case CLOSE: key = 'C'; break;
1467c478bd9Sstevel@tonic-gate case ERROR: key = 'E'; break;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &maskall, (sigset_t *)0);
1497c478bd9Sstevel@tonic-gate longjmp(sjbuf, 1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
152*fe0e7ec4Smaheshvs int
main(int argc,char * argv[])153*fe0e7ec4Smaheshvs main(int argc, char *argv[])
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate struct sigaction sa;
1567c478bd9Sstevel@tonic-gate pid_t parent = getpid(), next = parent;
1577c478bd9Sstevel@tonic-gate int saverr;
1587c478bd9Sstevel@tonic-gate offset_t rval;
1597c478bd9Sstevel@tonic-gate ssize_t cc;
1607c478bd9Sstevel@tonic-gate size_t n, i;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1637c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1647c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (argc > 1) {
1697c478bd9Sstevel@tonic-gate if ((debug = fopen(argv[1], "w")) == NULL)
1707c478bd9Sstevel@tonic-gate exit(1);
1717c478bd9Sstevel@tonic-gate setbuf(debug, NULL);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate (void) sigemptyset(&maskall);
1747c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, RECV);
1757c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, OPEN);
1767c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, CLOSE);
1777c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, ERROR);
1787c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, TAPE);
1797c478bd9Sstevel@tonic-gate (void) sigaddset(&maskall, SEND);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate tapemask = maskall;
1827c478bd9Sstevel@tonic-gate (void) sigdelset(&tapemask, TAPE);
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate sendmask = maskall;
1857c478bd9Sstevel@tonic-gate (void) sigdelset(&sendmask, SEND);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate (void) sigemptyset(&cmdmask);
1887c478bd9Sstevel@tonic-gate (void) sigaddset(&cmdmask, TAPE);
1897c478bd9Sstevel@tonic-gate (void) sigaddset(&cmdmask, SEND);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask);
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate sa.sa_handler = catch;
1947c478bd9Sstevel@tonic-gate sa.sa_flags = SA_RESTART;
1957c478bd9Sstevel@tonic-gate (void) sigaction(RECV, &sa, (struct sigaction *)0);
1967c478bd9Sstevel@tonic-gate (void) sigaction(SEND, &sa, (struct sigaction *)0);
1977c478bd9Sstevel@tonic-gate (void) sigaction(TAPE, &sa, (struct sigaction *)0);
1987c478bd9Sstevel@tonic-gate (void) sigaction(OPEN, &sa, (struct sigaction *)0);
1997c478bd9Sstevel@tonic-gate (void) sigaction(CLOSE, &sa, (struct sigaction *)0);
2007c478bd9Sstevel@tonic-gate (void) sigaction(ERROR, &sa, (struct sigaction *)0);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &maskall, (sigset_t *)0);
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate (void) kill(parent, TAPE);
2057c478bd9Sstevel@tonic-gate (void) kill(parent, SEND);
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate while (read(0, &key, 1) == 1) {
2087c478bd9Sstevel@tonic-gate switch (key) {
2097c478bd9Sstevel@tonic-gate case 'L': /* lseek */
2107c478bd9Sstevel@tonic-gate getstring(count, sizeof (count));
2117c478bd9Sstevel@tonic-gate getstring(pos, sizeof (pos));
2127c478bd9Sstevel@tonic-gate DEBUG2("rmtd: L %s %s\n", count, pos);
2137c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
2147c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
2157c478bd9Sstevel@tonic-gate rval = llseek(tape, atoll(count), atoi(pos));
2167c478bd9Sstevel@tonic-gate saverr = errno;
2177c478bd9Sstevel@tonic-gate (void) kill(next, TAPE);
2187c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
2197c478bd9Sstevel@tonic-gate respond(rval, saverr);
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate case 'I': /* ioctl */
2237c478bd9Sstevel@tonic-gate case 'i': { /* extended version ioctl */
2247c478bd9Sstevel@tonic-gate int bad = 0;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate getstring(op, sizeof (op));
2277c478bd9Sstevel@tonic-gate getstring(count, sizeof (count));
2287c478bd9Sstevel@tonic-gate DEBUG3("rmtd: %c %s %s\n", key, op, count);
2297c478bd9Sstevel@tonic-gate mtop.mt_op = atoi(op);
2307c478bd9Sstevel@tonic-gate mtop.mt_count = atoi(count);
2317c478bd9Sstevel@tonic-gate if (key == 'i') {
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * Map the supported compatibility defines
2347c478bd9Sstevel@tonic-gate * into real ioctl values.
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate switch (mtop.mt_op) {
2377c478bd9Sstevel@tonic-gate case RMTICACHE:
2387c478bd9Sstevel@tonic-gate case RMTINOCACHE: /* not support on Sun */
2397c478bd9Sstevel@tonic-gate bad = 1;
2407c478bd9Sstevel@tonic-gate break;
2417c478bd9Sstevel@tonic-gate case RMTIRETEN:
2427c478bd9Sstevel@tonic-gate mtop.mt_op = MTRETEN;
2437c478bd9Sstevel@tonic-gate break;
2447c478bd9Sstevel@tonic-gate case RMTIERASE:
2457c478bd9Sstevel@tonic-gate mtop.mt_op = MTERASE;
2467c478bd9Sstevel@tonic-gate break;
2477c478bd9Sstevel@tonic-gate case RMTIEOM:
2487c478bd9Sstevel@tonic-gate mtop.mt_op = MTEOM;
2497c478bd9Sstevel@tonic-gate break;
2507c478bd9Sstevel@tonic-gate case RMTINBSF:
2517c478bd9Sstevel@tonic-gate mtop.mt_op = MTNBSF;
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate default:
2547c478bd9Sstevel@tonic-gate bad = 1;
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate if (bad) {
2597c478bd9Sstevel@tonic-gate respond(-1LL, EINVAL);
2607c478bd9Sstevel@tonic-gate } else {
2617c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
2627c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
2637c478bd9Sstevel@tonic-gate if (mtop.mt_op == RMTIVERSION) {
2647c478bd9Sstevel@tonic-gate mtop.mt_count = RMT_VERSION;
2657c478bd9Sstevel@tonic-gate rval = (offset_t)mtop.mt_count;
2667c478bd9Sstevel@tonic-gate } else {
2677c478bd9Sstevel@tonic-gate rval = (offset_t)ioctl(tape, MTIOCTOP,
2687c478bd9Sstevel@tonic-gate (char *)&mtop);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate saverr = errno;
2717c478bd9Sstevel@tonic-gate (void) kill(next, TAPE);
2727c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
2737c478bd9Sstevel@tonic-gate respond(rval < 0 ?
2747c478bd9Sstevel@tonic-gate rval : (offset_t)mtop.mt_count,
2757c478bd9Sstevel@tonic-gate saverr);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate break;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate case 'S': /* status */
2817c478bd9Sstevel@tonic-gate case 's': { /* extended status */
2827c478bd9Sstevel@tonic-gate char skey;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate DEBUG1("rmtd: %c\n", key);
2857c478bd9Sstevel@tonic-gate if (key == 's') {
2867c478bd9Sstevel@tonic-gate if (read(0, &skey, 1) != 1)
2877c478bd9Sstevel@tonic-gate continue;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
2907c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
2917c478bd9Sstevel@tonic-gate errno = 0;
2927c478bd9Sstevel@tonic-gate rval = (offset_t)ioctl(tape, MTIOCGET, (char *)&mtget);
2937c478bd9Sstevel@tonic-gate saverr = errno;
2947c478bd9Sstevel@tonic-gate (void) kill(next, TAPE);
2957c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
2967c478bd9Sstevel@tonic-gate if (rval < 0)
2977c478bd9Sstevel@tonic-gate respond(rval, saverr);
2987c478bd9Sstevel@tonic-gate else {
2997c478bd9Sstevel@tonic-gate if (key == 's') { /* extended status */
3007c478bd9Sstevel@tonic-gate DEBUG1("rmtd: s%c\n", key);
3017c478bd9Sstevel@tonic-gate switch (skey) {
3027c478bd9Sstevel@tonic-gate case MTS_TYPE:
3037c478bd9Sstevel@tonic-gate respond(
3047c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_type,
3057c478bd9Sstevel@tonic-gate saverr);
3067c478bd9Sstevel@tonic-gate break;
3077c478bd9Sstevel@tonic-gate case MTS_DSREG:
3087c478bd9Sstevel@tonic-gate respond(
3097c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_dsreg,
3107c478bd9Sstevel@tonic-gate saverr);
3117c478bd9Sstevel@tonic-gate break;
3127c478bd9Sstevel@tonic-gate case MTS_ERREG:
3137c478bd9Sstevel@tonic-gate respond(
3147c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_erreg,
3157c478bd9Sstevel@tonic-gate saverr);
3167c478bd9Sstevel@tonic-gate break;
3177c478bd9Sstevel@tonic-gate case MTS_RESID:
3187c478bd9Sstevel@tonic-gate respond(
3197c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_resid,
3207c478bd9Sstevel@tonic-gate saverr);
3217c478bd9Sstevel@tonic-gate break;
3227c478bd9Sstevel@tonic-gate case MTS_FILENO:
3237c478bd9Sstevel@tonic-gate respond(
3247c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_fileno,
3257c478bd9Sstevel@tonic-gate saverr);
3267c478bd9Sstevel@tonic-gate break;
3277c478bd9Sstevel@tonic-gate case MTS_BLKNO:
3287c478bd9Sstevel@tonic-gate respond(
3297c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_blkno,
3307c478bd9Sstevel@tonic-gate saverr);
3317c478bd9Sstevel@tonic-gate break;
3327c478bd9Sstevel@tonic-gate case MTS_FLAGS:
3337c478bd9Sstevel@tonic-gate respond(
3347c478bd9Sstevel@tonic-gate (offset_t)mtget.mt_flags,
3357c478bd9Sstevel@tonic-gate saverr);
3367c478bd9Sstevel@tonic-gate break;
3377c478bd9Sstevel@tonic-gate case MTS_BF:
3387c478bd9Sstevel@tonic-gate respond((offset_t)mtget.mt_bf,
3397c478bd9Sstevel@tonic-gate saverr);
3407c478bd9Sstevel@tonic-gate break;
3417c478bd9Sstevel@tonic-gate default:
3427c478bd9Sstevel@tonic-gate respond(-1LL, EINVAL);
3437c478bd9Sstevel@tonic-gate break;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate } else {
3467c478bd9Sstevel@tonic-gate respond((offset_t)sizeof (mtget),
3477c478bd9Sstevel@tonic-gate saverr);
3487c478bd9Sstevel@tonic-gate (void) write(1, (char *)&mtget,
3497c478bd9Sstevel@tonic-gate sizeof (mtget));
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate break;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate case 'W':
3567c478bd9Sstevel@tonic-gate getstring(count, sizeof (count));
3577c478bd9Sstevel@tonic-gate n = (size_t)atol(count);
3587c478bd9Sstevel@tonic-gate checkbuf(n);
3597c478bd9Sstevel@tonic-gate DEBUG1("rmtd: W %s\n", count);
3607c478bd9Sstevel@tonic-gate #ifdef lint
3617c478bd9Sstevel@tonic-gate cc = 0;
3627c478bd9Sstevel@tonic-gate #endif
3637c478bd9Sstevel@tonic-gate for (i = 0; i < n; i += (size_t)cc) {
3647c478bd9Sstevel@tonic-gate cc = read(0, &record[i], n - i);
3657c478bd9Sstevel@tonic-gate if (cc <= 0) {
3667c478bd9Sstevel@tonic-gate DEBUG1(gettext("%s: premature eof\n"),
3677c478bd9Sstevel@tonic-gate "rmtd");
3687c478bd9Sstevel@tonic-gate exit(2);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
3727c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
3737c478bd9Sstevel@tonic-gate rval = (offset_t)write(tape, record, n);
3747c478bd9Sstevel@tonic-gate saverr = errno;
3757c478bd9Sstevel@tonic-gate (void) kill(next, TAPE);
3767c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
3777c478bd9Sstevel@tonic-gate respond(rval, saverr);
3787c478bd9Sstevel@tonic-gate break;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate case 'R':
3817c478bd9Sstevel@tonic-gate getstring(count, sizeof (count));
3827c478bd9Sstevel@tonic-gate n = (size_t)atol(count);
3837c478bd9Sstevel@tonic-gate checkbuf(n);
3847c478bd9Sstevel@tonic-gate DEBUG1("rmtd: R %s\n", count);
3857c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
3867c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
3877c478bd9Sstevel@tonic-gate rval = (offset_t)read(tape, record, n);
3887c478bd9Sstevel@tonic-gate saverr = errno;
3897c478bd9Sstevel@tonic-gate (void) kill(next, TAPE);
3907c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
3917c478bd9Sstevel@tonic-gate respond(rval, saverr);
3927c478bd9Sstevel@tonic-gate (void) write(1, record, (size_t)rval);
3937c478bd9Sstevel@tonic-gate break;
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate default:
3967c478bd9Sstevel@tonic-gate DEBUG2(gettext("%s: garbage command '%c'\n"),
3977c478bd9Sstevel@tonic-gate "rmtd", key);
3987c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate case 'C':
4017c478bd9Sstevel@tonic-gate case 'O':
4027c478bd9Sstevel@tonic-gate /* rendezvous back into a single process */
4037c478bd9Sstevel@tonic-gate if (setjmp(sjbuf) == 0 || getpid() != parent) {
4047c478bd9Sstevel@tonic-gate (void) sigsuspend(&tapemask);
4057c478bd9Sstevel@tonic-gate (void) sigsuspend(&sendmask);
4067c478bd9Sstevel@tonic-gate (void) kill(parent, key == 'O' ? OPEN :
4077c478bd9Sstevel@tonic-gate key == 'C' ? CLOSE : ERROR);
4087c478bd9Sstevel@tonic-gate (void) sigemptyset(&newmask);
4097c478bd9Sstevel@tonic-gate (void) sigsuspend(&newmask);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate while (children > 0) {
4127c478bd9Sstevel@tonic-gate (void) kill(childpid[--children], SIGKILL);
4137c478bd9Sstevel@tonic-gate while (wait(NULL) != childpid[children])
4147c478bd9Sstevel@tonic-gate ;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate next = parent;
4177c478bd9Sstevel@tonic-gate if (key == 'C') {
4187c478bd9Sstevel@tonic-gate getstring(device, sizeof (device));
4197c478bd9Sstevel@tonic-gate DEBUG1("rmtd: C %s\n", device);
4207c478bd9Sstevel@tonic-gate rval = (offset_t)close(tape);
4217c478bd9Sstevel@tonic-gate respond(rval, errno);
4227c478bd9Sstevel@tonic-gate (void) kill(parent, TAPE);
4237c478bd9Sstevel@tonic-gate (void) kill(parent, SEND);
4247c478bd9Sstevel@tonic-gate continue;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate if (key != 'O') /* garbage command */
4277c478bd9Sstevel@tonic-gate exit(3);
4287c478bd9Sstevel@tonic-gate (void) close(tape);
4297c478bd9Sstevel@tonic-gate getstring(device, sizeof (device));
4307c478bd9Sstevel@tonic-gate getstring(mode, sizeof (mode));
4317c478bd9Sstevel@tonic-gate DEBUG2("rmtd: O %s %s\n", device, mode);
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate * Due to incompatibilities in the
4347c478bd9Sstevel@tonic-gate * assignment of mode bits between
4357c478bd9Sstevel@tonic-gate * BSD and System V, we strip all
4367c478bd9Sstevel@tonic-gate * but the read/write bits. However,
4377c478bd9Sstevel@tonic-gate * we also want to handle things larger
4387c478bd9Sstevel@tonic-gate * than 2GB, so we also force O_LARGEFILE.
4397c478bd9Sstevel@tonic-gate */
4407c478bd9Sstevel@tonic-gate tape = open(device, O_LARGEFILE |
4417c478bd9Sstevel@tonic-gate (atoi(mode) & (O_RDONLY|O_WRONLY|O_RDWR)));
4427c478bd9Sstevel@tonic-gate respond((offset_t)tape, errno);
4437c478bd9Sstevel@tonic-gate if (tape >= 0) /* fork off */
4447c478bd9Sstevel@tonic-gate while (children < MAXCHILD &&
4457c478bd9Sstevel@tonic-gate (childpid[children] = fork()) > 0)
4467c478bd9Sstevel@tonic-gate next = childpid[children++];
4477c478bd9Sstevel@tonic-gate if (next == parent) {
4487c478bd9Sstevel@tonic-gate (void) kill(parent, RECV);
4497c478bd9Sstevel@tonic-gate (void) kill(parent, TAPE);
4507c478bd9Sstevel@tonic-gate (void) kill(parent, SEND);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate (void) sigsuspend(&cmdmask);
4537c478bd9Sstevel@tonic-gate continue;
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate (void) kill(next, SEND);
4567c478bd9Sstevel@tonic-gate (void) sigsuspend(&cmdmask);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate (void) kill(next, RECV);
4597c478bd9Sstevel@tonic-gate return (0);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate static void
respond(offset_t rval,int Errno)463*fe0e7ec4Smaheshvs respond(offset_t rval, int Errno)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate char resp[SSIZE];
4667c478bd9Sstevel@tonic-gate char *errstr = strerror(Errno);
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate if (rval < 0) {
4697c478bd9Sstevel@tonic-gate (void) snprintf(resp, SSIZE, "E%d\n%s\n", Errno, errstr);
4707c478bd9Sstevel@tonic-gate DEBUG2("rmtd: E %d (%s)\n", Errno, errstr);
4717c478bd9Sstevel@tonic-gate } else {
4727c478bd9Sstevel@tonic-gate (void) snprintf(resp, SSIZE, "A%lld\n", rval);
4737c478bd9Sstevel@tonic-gate DEBUG1("rmtd: A %lld\n", rval);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate resp[SSIZE - 1] = '\0';
4767c478bd9Sstevel@tonic-gate (void) write(1, resp, (int)strlen(resp));
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate static void
getstring(char * cp,size_t size)480*fe0e7ec4Smaheshvs getstring(char *cp, size_t size)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate char *limit = cp + size - 1;
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate cp--; /* nullify first increment */
4857c478bd9Sstevel@tonic-gate do {
4867c478bd9Sstevel@tonic-gate cp++;
4877c478bd9Sstevel@tonic-gate if (read(0, cp, 1) != 1)
4887c478bd9Sstevel@tonic-gate exit(0);
4897c478bd9Sstevel@tonic-gate } while ((*cp != '\n') && (cp < limit));
4907c478bd9Sstevel@tonic-gate *cp = '\0';
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate static void
checkbuf(size_t size)494*fe0e7ec4Smaheshvs checkbuf(size_t size)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate if (size <= maxrecsize)
4977c478bd9Sstevel@tonic-gate return;
4987c478bd9Sstevel@tonic-gate if (record != 0)
4997c478bd9Sstevel@tonic-gate free(record);
5007c478bd9Sstevel@tonic-gate if ((record = malloc(size)) == NULL) {
5017c478bd9Sstevel@tonic-gate DEBUG2(gettext("%s: cannot allocate %ld-byte buffer\n"),
5027c478bd9Sstevel@tonic-gate size, "rmtd");
5037c478bd9Sstevel@tonic-gate exit(4);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate maxrecsize = size;
5067c478bd9Sstevel@tonic-gate }
507