xref: /netbsd-src/usr.sbin/rmt/rmt.c (revision 421949a31fb0942d3d87278998c2d7d432d8b3cb)
1 /*	$NetBSD: rmt.c,v 1.17 2018/01/23 21:06:25 sevan 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
40 static char sccsid[] = "@(#)rmt.c	8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: rmt.c,v 1.17 2018/01/23 21:06:25 sevan 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 
62 int	tape = -1;
63 
64 int	maxrecsize = -1;
65 
66 #define	SSIZE	64
67 char	device[SSIZE];
68 char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
69 
70 char	resp[BUFSIZ];
71 
72 FILE	*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 
77 char	*checkbuf(char *, int);
78 void	 error(int);
79 void	 getstring(char *, size_t);
80 
81 int
main(int argc,char * argv[])82 main(int argc, char *argv[])
83 {
84 	char *record = NULL;
85 	int rval;
86 	char c;
87 	int n, i, cc;
88 
89 	argc--, argv++;
90 	if (argc > 0) {
91 		debug = fopen(*argv, "w");
92 		if (debug == 0)
93 			exit(1);
94 		(void)setbuf(debug, (char *)0);
95 	}
96 top:
97 	errno = 0;
98 	rval = 0;
99 	if (read(STDIN_FILENO, &c, 1) != 1)
100 		exit(0);
101 	switch (c) {
102 
103 	case 'O':
104 		if (tape >= 0)
105 			(void) close(tape);
106 		getstring(device, sizeof(device));
107 		getstring(mode, sizeof(mode));
108 		DEBUG2("rmtd: O %s %s\n", device, mode);
109 		tape = open(device, atoi(mode),
110 		    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
111 		if (tape < 0)
112 			goto ioerror;
113 		goto respond;
114 
115 	case 'C':
116 		DEBUG("rmtd: C\n");
117 		getstring(device, sizeof(device));	/* discard */
118 		if (close(tape) < 0)
119 			goto ioerror;
120 		tape = -1;
121 		goto respond;
122 
123 	case 'L':
124 		getstring(count, sizeof(count));
125 		getstring(pos, sizeof(pos));
126 		DEBUG2("rmtd: L %s %s\n", count, pos);
127 		rval = lseek(tape, (off_t)strtoll(count, NULL, 10), atoi(pos));
128 		if (rval < 0)
129 			goto ioerror;
130 		goto respond;
131 
132 	case 'W':
133 		getstring(count, sizeof(count));
134 		n = atoi(count);
135 		DEBUG1("rmtd: W %s\n", count);
136 		record = checkbuf(record, n);
137 		for (i = 0; i < n; i += cc) {
138 			cc = read(STDIN_FILENO, &record[i], n - i);
139 			if (cc <= 0) {
140 				DEBUG("rmtd: premature eof\n");
141 				exit(2);
142 			}
143 		}
144 		rval = write(tape, record, n);
145 		if (rval < 0)
146 			goto ioerror;
147 		goto respond;
148 
149 	case 'R':
150 		getstring(count, sizeof(count));
151 		DEBUG1("rmtd: R %s\n", count);
152 		n = atoi(count);
153 		record = checkbuf(record, n);
154 		rval = read(tape, record, n);
155 		if (rval < 0)
156 			goto ioerror;
157 		(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
158 		(void)write(STDOUT_FILENO, resp, strlen(resp));
159 		(void)write(STDOUT_FILENO, record, rval);
160 		goto top;
161 
162 	case 'I':
163 		getstring(op, sizeof(op));
164 		getstring(count, sizeof(count));
165 		DEBUG2("rmtd: I %s %s\n", op, count);
166 		{
167 			struct mtop mtop;
168 
169 			mtop.mt_op = atoi(op);
170 			mtop.mt_count = atoi(count);
171 			if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
172 				goto ioerror;
173 			rval = mtop.mt_count;
174 		}
175 		goto respond;
176 
177 	case 'S':		/* status */
178 		DEBUG("rmtd: S\n");
179 		{
180 			struct mtget mtget;
181 
182 			if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
183 				goto ioerror;
184 			rval = sizeof (mtget);
185 			/* limit size to 'original' mtget size */
186 			if (rval > 24)
187 				rval = 24;
188 			(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
189 			(void)write(STDOUT_FILENO, resp, strlen(resp));
190 			(void)write(STDOUT_FILENO, (char *)&mtget, rval);
191 			goto top;
192 		}
193 
194 	default:
195 		DEBUG1("rmtd: garbage command %c\n", c);
196 		exit(3);
197 	}
198 respond:
199 	DEBUG1("rmtd: A %d\n", rval);
200 	(void)snprintf(resp, sizeof(resp), "A%d\n", rval);
201 	(void)write(STDOUT_FILENO, resp, strlen(resp));
202 	goto top;
203 ioerror:
204 	error(errno);
205 	goto top;
206 }
207 
208 void
getstring(char * bp,size_t size)209 getstring(char *bp, size_t size)
210 {
211 	char *cp = bp;
212 	char *ep = bp + size - 1;
213 
214 	do {
215 		if (read(STDIN_FILENO, cp, 1) != 1)
216 			exit(0);
217 	} while (*cp != '\n' && ++cp < ep);
218 	*cp = '\0';
219 }
220 
221 char *
checkbuf(char * record,int size)222 checkbuf(char *record, int size)
223 {
224 
225 	if (size <= maxrecsize)
226 		return (record);
227 	if (record != 0)
228 		free(record);
229 	record = malloc(size);
230 	if (record == 0) {
231 		DEBUG("rmtd: cannot allocate buffer space\n");
232 		exit(4);
233 	}
234 	maxrecsize = size;
235 	while (size > 1024 &&
236 	    setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
237 		size -= 1024;
238 	return (record);
239 }
240 
241 void
error(int num)242 error(int num)
243 {
244 
245 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
246 	(void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
247 	(void)write(STDOUT_FILENO, resp, strlen(resp));
248 }
249