xref: /netbsd-src/usr.sbin/rmt/rmt.c (revision 421949a31fb0942d3d87278998c2d7d432d8b3cb)
1*421949a3Ssevan /*	$NetBSD: rmt.c,v 1.17 2018/01/23 21:06:25 sevan Exp $	*/
23bbeacd1Smrg 
361f28255Scgd /*
43bbeacd1Smrg  * Copyright (c) 1983, 1993
53bbeacd1Smrg  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32166448c2Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
349c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
359c194566Slukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3958aea378Smikel #if 0
403bbeacd1Smrg static char sccsid[] = "@(#)rmt.c	8.1 (Berkeley) 6/6/93";
4158aea378Smikel #else
42*421949a3Ssevan __RCSID("$NetBSD: rmt.c,v 1.17 2018/01/23 21:06:25 sevan Exp $");
4358aea378Smikel #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd /*
4761f28255Scgd  * rmt
4861f28255Scgd  */
4961f28255Scgd #include <sys/types.h>
5058aea378Smikel #include <sys/ioctl.h>
5158aea378Smikel #include <sys/mtio.h>
5261f28255Scgd #include <sys/socket.h>
53e6ae4619Smycroft #include <sys/stat.h>
5458aea378Smikel 
5561f28255Scgd #include <errno.h>
5658aea378Smikel #include <fcntl.h>
5758aea378Smikel #include <stdio.h>
5858aea378Smikel #include <stdlib.h>
5961f28255Scgd #include <string.h>
6058aea378Smikel #include <unistd.h>
6161f28255Scgd 
6261f28255Scgd int	tape = -1;
6361f28255Scgd 
6461f28255Scgd int	maxrecsize = -1;
6561f28255Scgd 
6661f28255Scgd #define	SSIZE	64
6761f28255Scgd char	device[SSIZE];
6861f28255Scgd char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
6961f28255Scgd 
7061f28255Scgd char	resp[BUFSIZ];
7161f28255Scgd 
7261f28255Scgd FILE	*debug;
7361f28255Scgd #define	DEBUG(f)	if (debug) fprintf(debug, f)
7461f28255Scgd #define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
7561f28255Scgd #define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
7661f28255Scgd 
77*421949a3Ssevan char	*checkbuf(char *, int);
78*421949a3Ssevan void	 error(int);
79*421949a3Ssevan void	 getstring(char *, size_t);
8058aea378Smikel 
8158aea378Smikel int
main(int argc,char * argv[])82*421949a3Ssevan main(int argc, char *argv[])
8361f28255Scgd {
8454f1cbbfSlukem 	char *record = NULL;
8561f28255Scgd 	int rval;
8661f28255Scgd 	char c;
8761f28255Scgd 	int n, i, cc;
8861f28255Scgd 
8961f28255Scgd 	argc--, argv++;
9061f28255Scgd 	if (argc > 0) {
9161f28255Scgd 		debug = fopen(*argv, "w");
9261f28255Scgd 		if (debug == 0)
9361f28255Scgd 			exit(1);
9461f28255Scgd 		(void)setbuf(debug, (char *)0);
9561f28255Scgd 	}
9661f28255Scgd top:
9761f28255Scgd 	errno = 0;
9861f28255Scgd 	rval = 0;
9958aea378Smikel 	if (read(STDIN_FILENO, &c, 1) != 1)
10061f28255Scgd 		exit(0);
10161f28255Scgd 	switch (c) {
10261f28255Scgd 
10361f28255Scgd 	case 'O':
10461f28255Scgd 		if (tape >= 0)
10561f28255Scgd 			(void) close(tape);
106aafb24a0Sitojun 		getstring(device, sizeof(device));
107aafb24a0Sitojun 		getstring(mode, sizeof(mode));
10861f28255Scgd 		DEBUG2("rmtd: O %s %s\n", device, mode);
109d092e7ebSmycroft 		tape = open(device, atoi(mode),
110d092e7ebSmycroft 		    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
11161f28255Scgd 		if (tape < 0)
11261f28255Scgd 			goto ioerror;
11361f28255Scgd 		goto respond;
11461f28255Scgd 
11561f28255Scgd 	case 'C':
11661f28255Scgd 		DEBUG("rmtd: C\n");
117aafb24a0Sitojun 		getstring(device, sizeof(device));	/* discard */
11861f28255Scgd 		if (close(tape) < 0)
11961f28255Scgd 			goto ioerror;
12061f28255Scgd 		tape = -1;
12161f28255Scgd 		goto respond;
12261f28255Scgd 
12361f28255Scgd 	case 'L':
124aafb24a0Sitojun 		getstring(count, sizeof(count));
125aafb24a0Sitojun 		getstring(pos, sizeof(pos));
12661f28255Scgd 		DEBUG2("rmtd: L %s %s\n", count, pos);
12728073e38Slukem 		rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
12861f28255Scgd 		if (rval < 0)
12961f28255Scgd 			goto ioerror;
13061f28255Scgd 		goto respond;
13161f28255Scgd 
13261f28255Scgd 	case 'W':
133aafb24a0Sitojun 		getstring(count, sizeof(count));
13461f28255Scgd 		n = atoi(count);
13561f28255Scgd 		DEBUG1("rmtd: W %s\n", count);
13661f28255Scgd 		record = checkbuf(record, n);
13761f28255Scgd 		for (i = 0; i < n; i += cc) {
13858aea378Smikel 			cc = read(STDIN_FILENO, &record[i], n - i);
13961f28255Scgd 			if (cc <= 0) {
14061f28255Scgd 				DEBUG("rmtd: premature eof\n");
14161f28255Scgd 				exit(2);
14261f28255Scgd 			}
14361f28255Scgd 		}
14461f28255Scgd 		rval = write(tape, record, n);
14561f28255Scgd 		if (rval < 0)
14661f28255Scgd 			goto ioerror;
14761f28255Scgd 		goto respond;
14861f28255Scgd 
14961f28255Scgd 	case 'R':
150aafb24a0Sitojun 		getstring(count, sizeof(count));
15161f28255Scgd 		DEBUG1("rmtd: R %s\n", count);
15261f28255Scgd 		n = atoi(count);
15361f28255Scgd 		record = checkbuf(record, n);
15461f28255Scgd 		rval = read(tape, record, n);
15561f28255Scgd 		if (rval < 0)
15661f28255Scgd 			goto ioerror;
157d9f2774cSitojun 		(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
15858aea378Smikel 		(void)write(STDOUT_FILENO, resp, strlen(resp));
15958aea378Smikel 		(void)write(STDOUT_FILENO, record, rval);
16061f28255Scgd 		goto top;
16161f28255Scgd 
16261f28255Scgd 	case 'I':
163aafb24a0Sitojun 		getstring(op, sizeof(op));
164aafb24a0Sitojun 		getstring(count, sizeof(count));
16561f28255Scgd 		DEBUG2("rmtd: I %s %s\n", op, count);
16658aea378Smikel 		{
16758aea378Smikel 			struct mtop mtop;
16858aea378Smikel 
16961f28255Scgd 			mtop.mt_op = atoi(op);
17061f28255Scgd 			mtop.mt_count = atoi(count);
17161f28255Scgd 			if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
17261f28255Scgd 				goto ioerror;
17361f28255Scgd 			rval = mtop.mt_count;
17461f28255Scgd 		}
17561f28255Scgd 		goto respond;
17661f28255Scgd 
17761f28255Scgd 	case 'S':		/* status */
17861f28255Scgd 		DEBUG("rmtd: S\n");
17958aea378Smikel 		{
18058aea378Smikel 			struct mtget mtget;
18158aea378Smikel 
18261f28255Scgd 			if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
18361f28255Scgd 				goto ioerror;
18461f28255Scgd 			rval = sizeof (mtget);
185dcdf7ba2Smjacob 			/* limit size to 'original' mtget size */
186dcdf7ba2Smjacob 			if (rval > 24)
187dcdf7ba2Smjacob 				rval = 24;
188d9f2774cSitojun 			(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
18958aea378Smikel 			(void)write(STDOUT_FILENO, resp, strlen(resp));
190dcdf7ba2Smjacob 			(void)write(STDOUT_FILENO, (char *)&mtget, rval);
19161f28255Scgd 			goto top;
19261f28255Scgd 		}
19361f28255Scgd 
19461f28255Scgd 	default:
19561f28255Scgd 		DEBUG1("rmtd: garbage command %c\n", c);
19661f28255Scgd 		exit(3);
19761f28255Scgd 	}
19861f28255Scgd respond:
19961f28255Scgd 	DEBUG1("rmtd: A %d\n", rval);
200d9f2774cSitojun 	(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
20158aea378Smikel 	(void)write(STDOUT_FILENO, resp, strlen(resp));
20261f28255Scgd 	goto top;
20361f28255Scgd ioerror:
20461f28255Scgd 	error(errno);
20561f28255Scgd 	goto top;
20661f28255Scgd }
20761f28255Scgd 
20858aea378Smikel void
getstring(char * bp,size_t size)209*421949a3Ssevan getstring(char *bp, size_t size)
21061f28255Scgd {
21161f28255Scgd 	char *cp = bp;
212aafb24a0Sitojun 	char *ep = bp + size - 1;
21361f28255Scgd 
214aafb24a0Sitojun 	do {
215aafb24a0Sitojun 		if (read(STDIN_FILENO, cp, 1) != 1)
21661f28255Scgd 			exit(0);
217aafb24a0Sitojun 	} while (*cp != '\n' && ++cp < ep);
218aafb24a0Sitojun 	*cp = '\0';
21961f28255Scgd }
22061f28255Scgd 
22161f28255Scgd char *
checkbuf(char * record,int size)222*421949a3Ssevan checkbuf(char *record, int size)
22361f28255Scgd {
22461f28255Scgd 
22561f28255Scgd 	if (size <= maxrecsize)
22661f28255Scgd 		return (record);
22761f28255Scgd 	if (record != 0)
22861f28255Scgd 		free(record);
22961f28255Scgd 	record = malloc(size);
23061f28255Scgd 	if (record == 0) {
23161f28255Scgd 		DEBUG("rmtd: cannot allocate buffer space\n");
23261f28255Scgd 		exit(4);
23361f28255Scgd 	}
23461f28255Scgd 	maxrecsize = size;
23561f28255Scgd 	while (size > 1024 &&
23661f28255Scgd 	    setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
23761f28255Scgd 		size -= 1024;
23861f28255Scgd 	return (record);
23961f28255Scgd }
24061f28255Scgd 
24158aea378Smikel void
error(int num)242*421949a3Ssevan error(int num)
24361f28255Scgd {
24461f28255Scgd 
24561f28255Scgd 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
24628073e38Slukem 	(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
24758aea378Smikel 	(void)write(STDOUT_FILENO, resp, strlen(resp));
24861f28255Scgd }
249