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
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
43 static const char rcsid[] =
44 "$Id: rmt.c,v 1.17 2002/04/12 13:02:16 stelian Exp $";
45 #endif /* not linux */
51 #include <compatlfs.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
72 char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
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)
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.
86 #define RMTI_VERSION -1
89 /* Extended 'i' commands */
91 #define RMTI_NOCACHE 1
97 /* Extended 's' comands */
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'
107 char *checkbuf __P((char *, int));
108 void error __P((int));
109 void getstring __P((char *));
112 main(int argc, char *argv[])
117 unsigned long block = 0;
121 debug = fopen(*argv, "w");
124 (void)setbuf(debug, (char *)0);
129 if (read(0, &c, 1) != 1)
138 DEBUG2("rmtd: O %s %s\n", device, filemode);
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
144 tape = OPEN(device, atoi(filemode), 0666);
151 DEBUG1("rmtd: C (%lu blocks)\n", block);
152 getstring(device); /* discard */
162 DEBUG2("rmtd: L %s %s\n", count, pos);
163 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
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);
176 DEBUG("rmtd: premature eof\n");
180 rval = write(tape, record, n);
188 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
190 record = checkbuf(record, n);
191 rval = read(tape, record, n);
194 (void)sprintf(resp, "A%d\n", rval);
195 (void)write(1, resp, strlen(resp));
196 (void)write(1, record, rval);
203 DEBUG2("rmtd: I %s %s\n", op, count);
204 if (atoi(op) == RMTI_VERSION) {
208 mtop.mt_op = atoi(op);
209 mtop.mt_count = atoi(count);
210 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
212 rval = mtop.mt_count;
221 DEBUG2 ("rmtd: i %s %s\n", op, count);
225 mtop.mt_op = MTCACHE;
230 mtop.mt_op = MTNOCACHE;
235 mtop.mt_op = MTRETEN;
240 mtop.mt_op = MTERASE;
257 mtop.mt_count = atoi (count);
258 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
261 rval = mtop.mt_count;
266 case 'S': /* status */
268 { struct mtget mtget;
269 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
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));
282 if (read (0, &s, 1) != 1)
285 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
290 rval = mtget.mt_type;
293 rval = mtget.mt_dsreg;
296 rval = mtget.mt_erreg;
299 rval = mtget.mt_resid;
302 rval = mtget.mt_fileno;
305 rval = mtget.mt_blkno;
308 rval = mtget.mt_gstat;
321 case 'V': /* version */
323 DEBUG1("rmtd: V %s\n", op);
328 DEBUG1("rmtd: garbage command %c\n", c);
332 DEBUG1("rmtd: A %d\n", rval);
333 (void)sprintf(resp, "A%d\n", rval);
334 (void)write(1, resp, strlen(resp));
341 void getstring(char *bp)
346 for (i = 0; i < SSIZE; i++) {
347 if (read(0, cp+i, 1) != 1)
356 checkbuf(char *record, int size)
359 if (size <= maxrecsize)
363 record = malloc(size);
365 DEBUG("rmtd: cannot allocate buffer space\n");
369 while (size > 1024 &&
370 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
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));