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