1/*	$NetBSD: rmt.c,v 1.15 2008/07/21 13:36:59 lukem Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California.  All rights reserved.");
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)rmt.c	8.1 (Berkeley) 6/6/93";
41#else
42__RCSID("$NetBSD: rmt.c,v 1.15 2008/07/21 13:36:59 lukem Exp $");
43#endif
44#endif /* not lint */
45
46/*
47 * rmt
48 */
49#include <sys/types.h>
50#include <sys/ioctl.h>
51#include <sys/mtio.h>
52#include <sys/socket.h>
53#include <sys/stat.h>
54
55#include <errno.h>
56#include <fcntl.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62int	tape = -1;
63
64int	maxrecsize = -1;
65
66#define	SSIZE	64
67char	device[SSIZE];
68char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
69
70char	resp[BUFSIZ];
71
72FILE	*debug;
73#define	DEBUG(f)	if (debug) fprintf(debug, f)
74#define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
75#define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
76
77char	*checkbuf __P((char *, int));
78void	 error __P((int));
79int	 main __P((int, char **));
80void	 getstring __P((char *, size_t));
81
82int
83main(argc, argv)
84	int argc;
85	char **argv;
86{
87	char *record = NULL;
88	int rval;
89	char c;
90	int n, i, cc;
91
92	argc--, argv++;
93	if (argc > 0) {
94		debug = fopen(*argv, "w");
95		if (debug == 0)
96			exit(1);
97		(void)setbuf(debug, (char *)0);
98	}
99top:
100	errno = 0;
101	rval = 0;
102	if (read(STDIN_FILENO, &c, 1) != 1)
103		exit(0);
104	switch (c) {
105
106	case 'O':
107		if (tape >= 0)
108			(void) close(tape);
109		getstring(device, sizeof(device));
110		getstring(mode, sizeof(mode));
111		DEBUG2("rmtd: O %s %s\n", device, mode);
112		tape = open(device, atoi(mode),
113		    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
114		if (tape < 0)
115			goto ioerror;
116		goto respond;
117
118	case 'C':
119		DEBUG("rmtd: C\n");
120		getstring(device, sizeof(device));	/* discard */
121		if (close(tape) < 0)
122			goto ioerror;
123		tape = -1;
124		goto respond;
125
126	case 'L':
127		getstring(count, sizeof(count));
128		getstring(pos, sizeof(pos));
129		DEBUG2("rmtd: L %s %s\n", count, pos);
130		rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
131		if (rval < 0)
132			goto ioerror;
133		goto respond;
134
135	case 'W':
136		getstring(count, sizeof(count));
137		n = atoi(count);
138		DEBUG1("rmtd: W %s\n", count);
139		record = checkbuf(record, n);
140		for (i = 0; i < n; i += cc) {
141			cc = read(STDIN_FILENO, &record[i], n - i);
142			if (cc <= 0) {
143				DEBUG("rmtd: premature eof\n");
144				exit(2);
145			}
146		}
147		rval = write(tape, record, n);
148		if (rval < 0)
149			goto ioerror;
150		goto respond;
151
152	case 'R':
153		getstring(count, sizeof(count));
154		DEBUG1("rmtd: R %s\n", count);
155		n = atoi(count);
156		record = checkbuf(record, n);
157		rval = read(tape, record, n);
158		if (rval < 0)
159			goto ioerror;
160		(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
161		(void)write(STDOUT_FILENO, resp, strlen(resp));
162		(void)write(STDOUT_FILENO, record, rval);
163		goto top;
164
165	case 'I':
166		getstring(op, sizeof(op));
167		getstring(count, sizeof(count));
168		DEBUG2("rmtd: I %s %s\n", op, count);
169		{
170			struct mtop mtop;
171
172			mtop.mt_op = atoi(op);
173			mtop.mt_count = atoi(count);
174			if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
175				goto ioerror;
176			rval = mtop.mt_count;
177		}
178		goto respond;
179
180	case 'S':		/* status */
181		DEBUG("rmtd: S\n");
182		{
183			struct mtget mtget;
184
185			if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
186				goto ioerror;
187			rval = sizeof (mtget);
188			/* limit size to 'original' mtget size */
189			if (rval > 24)
190				rval = 24;
191			(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
192			(void)write(STDOUT_FILENO, resp, strlen(resp));
193			(void)write(STDOUT_FILENO, (char *)&mtget, rval);
194			goto top;
195		}
196
197	default:
198		DEBUG1("rmtd: garbage command %c\n", c);
199		exit(3);
200	}
201respond:
202	DEBUG1("rmtd: A %d\n", rval);
203	(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
204	(void)write(STDOUT_FILENO, resp, strlen(resp));
205	goto top;
206ioerror:
207	error(errno);
208	goto top;
209}
210
211void
212getstring(bp, size)
213	char *bp;
214	size_t size;
215{
216	char *cp = bp;
217	char *ep = bp + size - 1;
218
219	do {
220		if (read(STDIN_FILENO, cp, 1) != 1)
221			exit(0);
222	} while (*cp != '\n' && ++cp < ep);
223	*cp = '\0';
224}
225
226char *
227checkbuf(record, size)
228	char *record;
229	int size;
230{
231
232	if (size <= maxrecsize)
233		return (record);
234	if (record != 0)
235		free(record);
236	record = malloc(size);
237	if (record == 0) {
238		DEBUG("rmtd: cannot allocate buffer space\n");
239		exit(4);
240	}
241	maxrecsize = size;
242	while (size > 1024 &&
243	    setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
244		size -= 1024;
245	return (record);
246}
247
248void
249error(num)
250	int num;
251{
252
253	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
254	(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
255	(void)write(STDOUT_FILENO, resp, strlen(resp));
256}
257