xref: /openbsd-src/usr.sbin/rmt/rmt.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: rmt.c,v 1.6 2000/07/20 01:41:13 jason Exp $	*/
2 
3 /*
4  * Copyright (c) 1983 Regents of the University of California.
5  * 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 #ifndef lint
37 char copyright[] =
38 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
39  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 /*static char sccsid[] = "from: @(#)rmt.c	5.6 (Berkeley) 6/1/90";*/
44 static char rcsid[] = "$Id: rmt.c,v 1.6 2000/07/20 01:41:13 jason Exp $";
45 #endif /* not lint */
46 
47 /*
48  * rmt
49  */
50 #include <stdio.h>
51 #include <sgtty.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/mtio.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <errno.h>
60 #include <string.h>
61 
62 int	tape = -1;
63 
64 char	*record;
65 int	maxrecsize = -1;
66 
67 #define	STRSIZE	64
68 char	device[STRSIZE];
69 char	count[STRSIZE], mode[STRSIZE], pos[STRSIZE], op[STRSIZE];
70 
71 char	resp[BUFSIZ];
72 
73 FILE	*debug;
74 #define	DEBUG(f)	if (debug) fprintf(debug, f)
75 #define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
76 #define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
77 
78 char	*checkbuf __P((char *, int));
79 void	getstring __P((char *));
80 void	error __P((int));
81 
82 int
83 main(argc, argv)
84 	int argc;
85 	char **argv;
86 {
87 	off_t orval;
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 	}
99 top:
100 	errno = 0;
101 	rval = 0;
102 	if (read(0, &c, 1) != 1)
103 		exit(0);
104 	switch (c) {
105 
106 	case 'O':
107 		if (tape >= 0)
108 			(void) close(tape);
109 		getstring(device); getstring(mode);
110 		DEBUG2("rmtd: O %s %s\n", device, mode);
111 		tape = open(device, atoi(mode),
112 		    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
113 		if (tape == -1)
114 			goto ioerror;
115 		goto respond;
116 
117 	case 'C':
118 		DEBUG("rmtd: C\n");
119 		getstring(device);		/* discard */
120 		if (close(tape) == -1)
121 			goto ioerror;
122 		tape = -1;
123 		goto respond;
124 
125 	case 'L':
126 		getstring(count); getstring(pos);
127 		DEBUG2("rmtd: L %s %s\n", count, pos);
128 		orval = lseek(tape, strtoq(count, NULL, 0), atoi(pos));
129 		if (orval == -1)
130 			goto ioerror;
131 		goto respond;
132 
133 	case 'W':
134 		getstring(count);
135 		n = atoi(count);
136 		DEBUG1("rmtd: W %s\n", count);
137 		record = checkbuf(record, n);
138 		for (i = 0; i < n; i += cc) {
139 			cc = read(0, &record[i], n - i);
140 			if (cc <= 0) {
141 				DEBUG("rmtd: premature eof\n");
142 				exit(2);
143 			}
144 		}
145 		rval = write(tape, record, n);
146 		if (rval < 0)
147 			goto ioerror;
148 		goto respond;
149 
150 	case 'R':
151 		getstring(count);
152 		DEBUG1("rmtd: R %s\n", count);
153 		n = atoi(count);
154 		record = checkbuf(record, n);
155 		rval = read(tape, record, n);
156 		if (rval < 0)
157 			goto ioerror;
158 		(void) sprintf(resp, "A%d\n", rval);
159 		(void) write(1, resp, strlen(resp));
160 		(void) write(1, record, rval);
161 		goto top;
162 
163 	case 'I':
164 		getstring(op); getstring(count);
165 		DEBUG2("rmtd: I %s %s\n", op, count);
166 		{ struct mtop mtop;
167 		  mtop.mt_op = atoi(op);
168 		  mtop.mt_count = atoi(count);
169 		  if (ioctl(tape, MTIOCTOP, (char *)&mtop) == -1)
170 			goto ioerror;
171 		  rval = mtop.mt_count;
172 		}
173 		goto respond;
174 
175 	case 'S':		/* status */
176 		DEBUG("rmtd: S\n");
177 		{ struct mtget mtget;
178 		  if (ioctl(tape, MTIOCGET, (char *)&mtget) == -1)
179 			goto ioerror;
180 		  rval = sizeof (mtget);
181 		  (void) sprintf(resp, "A%d\n", rval);
182 		  (void) write(1, resp, strlen(resp));
183 		  (void) write(1, (char *)&mtget, sizeof (mtget));
184 		  goto top;
185 		}
186 
187 	default:
188 		DEBUG1("rmtd: garbage command %c\n", c);
189 		exit(3);
190 	}
191 respond:
192 	DEBUG1("rmtd: A %d\n", rval);
193 	(void) sprintf(resp, "A%d\n", rval);
194 	(void) write(1, resp, strlen(resp));
195 	goto top;
196 ioerror:
197 	error(errno);
198 	goto top;
199 }
200 
201 void
202 getstring(bp)
203 	char *bp;
204 {
205 	int i;
206 	char *cp = bp;
207 
208 	for (i = 0; i < STRSIZE; i++) {
209 		if (read(0, cp+i, 1) != 1)
210 			exit(0);
211 		if (cp[i] == '\n')
212 			break;
213 	}
214 	cp[i] = '\0';
215 }
216 
217 char *
218 checkbuf(record, size)
219 	char *record;
220 	int size;
221 {
222 	if (size <= maxrecsize)
223 		return (record);
224 	if (record != 0)
225 		free(record);
226 	record = malloc(size);
227 	if (record == 0) {
228 		DEBUG("rmtd: cannot allocate buffer space\n");
229 		exit(4);
230 	}
231 	maxrecsize = size;
232 	while (size > 1024 &&
233 	       setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) == -1)
234 		size -= 1024;
235 	return (record);
236 }
237 
238 void
239 error(num)
240 	int num;
241 {
242 
243 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
244 	(void) snprintf(resp, sizeof (resp), "E%d\n%s\n", num, strerror(num));
245 	(void) write(1, resp, strlen(resp));
246 }
247