]> git.wh0rd.org - dump.git/blob - rmt/rmt.c
2af48e3e0cd7f8560c2946a1faeb449c5da66169
[dump.git] / rmt / rmt.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - Alcôve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: rmt.c,v 1.17 2002/04/12 13:02:16 stelian Exp $";
45 #endif /* not linux */
46
47 /*
48 * rmt
49 */
50 #include <config.h>
51 #include <compatlfs.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/mtio.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #ifndef __linux__
58 #include <sgtty.h>
59 #endif
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 int tape = -1;
66
67 char *record;
68 int maxrecsize = -1;
69
70 #define SSIZE 64
71 char device[SSIZE];
72 char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
73
74 char resp[BUFSIZ];
75
76 FILE *debug;
77 #define DEBUG(f) if (debug) fprintf(debug, f)
78 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
79 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
80
81 /*
82 * Support for Sun's extended RMT protocol
83 * code originally written by Jörg Schilling <schilling@fokus.gmd.de>
84 * and relicensed by his permission from GPL to BSD for use in dump.
85 */
86 #define RMTI_VERSION -1
87 #define RMT_VERSION 1
88
89 /* Extended 'i' commands */
90 #define RMTI_CACHE 0
91 #define RMTI_NOCACHE 1
92 #define RMTI_RETEN 2
93 #define RMTI_ERASE 3
94 #define RMTI_EOM 4
95 #define RMTI_NBSF 5
96
97 /* Extended 's' comands */
98 #define MTS_TYPE 'T'
99 #define MTS_DSREG 'D'
100 #define MTS_ERREG 'E'
101 #define MTS_RESID 'R'
102 #define MTS_FILENO 'F'
103 #define MTS_BLKNO 'B'
104 #define MTS_FLAGS 'f'
105 #define MTS_BF 'b'
106
107 char *checkbuf __P((char *, int));
108 void error __P((int));
109 void getstring __P((char *));
110
111 int
112 main(int argc, char *argv[])
113 {
114 int rval = 0;
115 char c;
116 int n, i, cc;
117 unsigned long block = 0;
118
119 argc--, argv++;
120 if (argc > 0) {
121 debug = fopen(*argv, "w");
122 if (debug == 0)
123 exit(1);
124 (void)setbuf(debug, (char *)0);
125 }
126 top:
127 errno = 0;
128 rval = 0;
129 if (read(0, &c, 1) != 1)
130 exit(0);
131 switch (c) {
132
133 case 'O':
134 if (tape >= 0)
135 (void) close(tape);
136 getstring(device);
137 getstring(filemode);
138 DEBUG2("rmtd: O %s %s\n", device, filemode);
139 /*
140 * XXX the rmt protocol does not provide a means to
141 * specify the permission bits; allow rw for everyone,
142 * as modified by the users umask
143 */
144 tape = OPEN(device, atoi(filemode), 0666);
145 if (tape < 0)
146 goto ioerror;
147 block = 0;
148 goto respond;
149
150 case 'C':
151 DEBUG1("rmtd: C (%lu blocks)\n", block);
152 getstring(device); /* discard */
153 if (close(tape) < 0)
154 goto ioerror;
155 tape = -1;
156 block = 0;
157 goto respond;
158
159 case 'L':
160 getstring(count);
161 getstring(pos);
162 DEBUG2("rmtd: L %s %s\n", count, pos);
163 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
164 if (rval < 0)
165 goto ioerror;
166 goto respond;
167
168 case 'W':
169 getstring(count);
170 n = atoi(count);
171 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
172 record = checkbuf(record, n);
173 for (i = 0; i < n; i += cc) {
174 cc = read(0, &record[i], n - i);
175 if (cc <= 0) {
176 DEBUG("rmtd: premature eof\n");
177 exit(2);
178 }
179 }
180 rval = write(tape, record, n);
181 if (rval < 0)
182 goto ioerror;
183 block += n >> 10;
184 goto respond;
185
186 case 'R':
187 getstring(count);
188 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
189 n = atoi(count);
190 record = checkbuf(record, n);
191 rval = read(tape, record, n);
192 if (rval < 0)
193 goto ioerror;
194 (void)sprintf(resp, "A%d\n", rval);
195 (void)write(1, resp, strlen(resp));
196 (void)write(1, record, rval);
197 block += n >> 10;
198 goto top;
199
200 case 'I':
201 getstring(op);
202 getstring(count);
203 DEBUG2("rmtd: I %s %s\n", op, count);
204 if (atoi(op) == RMTI_VERSION) {
205 rval = RMT_VERSION;
206 } else {
207 struct mtop mtop;
208 mtop.mt_op = atoi(op);
209 mtop.mt_count = atoi(count);
210 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
211 goto ioerror;
212 rval = mtop.mt_count;
213 }
214 goto respond;
215
216 case 'i':
217 { struct mtop mtop;
218
219 getstring (op);
220 getstring (count);
221 DEBUG2 ("rmtd: i %s %s\n", op, count);
222 switch (atoi(op)) {
223 #ifdef MTCACHE
224 case RMTI_CACHE:
225 mtop.mt_op = MTCACHE;
226 break;
227 #endif
228 #ifdef MTNOCACHE
229 case RMTI_NOCACHE:
230 mtop.mt_op = MTNOCACHE;
231 break;
232 #endif
233 #ifdef MTRETEN
234 case RMTI_RETEN:
235 mtop.mt_op = MTRETEN;
236 break;
237 #endif
238 #ifdef MTERASE
239 case RMTI_ERASE:
240 mtop.mt_op = MTERASE;
241 break;
242 #endif
243 #ifdef MTEOM
244 case RMTI_EOM:
245 mtop.mt_op = MTEOM;
246 break;
247 #endif
248 #ifdef MTNBSF
249 case RMTI_NBSF:
250 mtop.mt_op = MTNBSF;
251 break;
252 #endif
253 default:
254 errno = EINVAL;
255 goto ioerror;
256 }
257 mtop.mt_count = atoi (count);
258 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
259 goto ioerror;
260
261 rval = mtop.mt_count;
262
263 goto respond;
264 }
265
266 case 'S': /* status */
267 DEBUG("rmtd: S\n");
268 { struct mtget mtget;
269 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
270 goto ioerror;
271 rval = sizeof (mtget);
272 (void)sprintf(resp, "A%d\n", rval);
273 (void)write(1, resp, strlen(resp));
274 (void)write(1, (char *)&mtget, sizeof (mtget));
275 goto top;
276 }
277
278 case 's':
279 { char s;
280 struct mtget mtget;
281
282 if (read (0, &s, 1) != 1)
283 goto top;
284
285 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
286 goto ioerror;
287
288 switch (s) {
289 case MTS_TYPE:
290 rval = mtget.mt_type;
291 break;
292 case MTS_DSREG:
293 rval = mtget.mt_dsreg;
294 break;
295 case MTS_ERREG:
296 rval = mtget.mt_erreg;
297 break;
298 case MTS_RESID:
299 rval = mtget.mt_resid;
300 break;
301 case MTS_FILENO:
302 rval = mtget.mt_fileno;
303 break;
304 case MTS_BLKNO:
305 rval = mtget.mt_blkno;
306 break;
307 case MTS_FLAGS:
308 rval = mtget.mt_gstat;
309 break;
310 case MTS_BF:
311 rval = 0;
312 break;
313 default:
314 errno = EINVAL;
315 goto ioerror;
316 }
317
318 goto respond;
319 }
320
321 case 'V': /* version */
322 getstring(op);
323 DEBUG1("rmtd: V %s\n", op);
324 rval = 2;
325 goto respond;
326
327 default:
328 DEBUG1("rmtd: garbage command %c\n", c);
329 exit(3);
330 }
331 respond:
332 DEBUG1("rmtd: A %d\n", rval);
333 (void)sprintf(resp, "A%d\n", rval);
334 (void)write(1, resp, strlen(resp));
335 goto top;
336 ioerror:
337 error(errno);
338 goto top;
339 }
340
341 void getstring(char *bp)
342 {
343 int i;
344 char *cp = bp;
345
346 for (i = 0; i < SSIZE; i++) {
347 if (read(0, cp+i, 1) != 1)
348 exit(0);
349 if (cp[i] == '\n')
350 break;
351 }
352 cp[i] = '\0';
353 }
354
355 char *
356 checkbuf(char *record, int size)
357 {
358
359 if (size <= maxrecsize)
360 return (record);
361 if (record != 0)
362 free(record);
363 record = malloc(size);
364 if (record == 0) {
365 DEBUG("rmtd: cannot allocate buffer space\n");
366 exit(4);
367 }
368 maxrecsize = size;
369 while (size > 1024 &&
370 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
371 size -= 1024;
372 return (record);
373 }
374
375 void
376 error(int num)
377 {
378
379 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
380 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
381 (void)write(1, resp, strlen(resp));
382 }