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