]>
git.wh0rd.org - dump.git/blob - rmt/rmt.c
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.22 2003/01/10 14:42:51 stelian Exp $";
45 #endif /* not linux */
51 #include <compatlfs.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
69 static int maxrecsize
= -1;
72 static char device
[SSIZE
];
73 static char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
75 static char resp
[BUFSIZ
];
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)
83 * Support for Sun's extended RMT protocol
84 * code originally written by Jörg Schilling <schilling@fokus.gmd.de>
85 * and relicensed by his permission from GPL to BSD for use in dump.
87 * rmt_version is 0 for regular clients (Linux included)
88 * rmt_version is 1 for extended clients (Sun especially). In this case
89 * we support some extended commands (see below) and we remap
90 * the ioctl commands to the UNIX "standard", as per:
91 * ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
93 * In order to use rmt version 1, a client must send "I-1\n0\n"
94 * before issuing the other I commands.
96 static int rmt_version
= 0;
97 #define RMTI_VERSION -1
100 /* Extended 'i' commands */
102 #define RMTI_NOCACHE 1
108 /* Extended 's' comands */
110 #define MTS_DSREG 'D'
111 #define MTS_ERREG 'E'
112 #define MTS_RESID 'R'
113 #define MTS_FILENO 'F'
114 #define MTS_BLKNO 'B'
115 #define MTS_FLAGS 'f'
118 char *checkbuf
__P((char *, int));
119 void error
__P((int));
120 void getstring
__P((char *));
123 main(int argc
, char *argv
[])
127 int n
, i
, cc
, oflags
;
128 unsigned long block
= 0;
132 debug
= fopen(*argv
, "w");
135 (void)setbuf(debug
, (char *)0);
140 if (read(0, &c
, 1) != 1)
149 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
151 * Translate extended GNU syntax into its numeric platform equivalent
153 oflags
= rmtflags_toint(filemode
);
156 * Default to O_BINARY the client may not know that we need it.
158 if ((oflags
& O_TEXT
) == 0)
161 DEBUG2("rmtd: O %s %d\n", device
, oflags
);
163 * XXX the rmt protocol does not provide a means to
164 * specify the permission bits; allow rw for everyone,
165 * as modified by the users umask
167 tape
= OPEN(device
, oflags
, 0666);
174 DEBUG1("rmtd: C (%lu blocks)\n", block
);
175 getstring(device
); /* discard */
185 DEBUG2("rmtd: L %s %s\n", count
, pos
);
186 rval
= LSEEK(tape
, (OFF_T
)atoll(count
), atoi(pos
));
194 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
195 record
= checkbuf(record
, n
);
196 for (i
= 0; i
< n
; i
+= cc
) {
197 cc
= read(0, &record
[i
], n
- i
);
199 DEBUG("rmtd: premature eof\n");
203 rval
= write(tape
, record
, n
);
211 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
213 record
= checkbuf(record
, n
);
214 rval
= read(tape
, record
, n
);
217 (void)sprintf(resp
, "A%d\n", rval
);
218 (void)write(1, resp
, strlen(resp
));
219 (void)write(1, record
, rval
);
226 DEBUG2("rmtd: I %s %s\n", op
, count
);
227 if (atoi(op
) == RMTI_VERSION
) {
235 /* rmt version 1, assume UNIX client */
278 if (mtop
.mt_op
== -1) {
284 /* rmt version 0, assume linux client */
285 mtop
.mt_op
= atoi(op
);
287 mtop
.mt_count
= atoi(count
);
288 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
290 rval
= mtop
.mt_count
;
299 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
303 mtop
.mt_op
= MTCACHE
;
308 mtop
.mt_op
= MTNOCACHE
;
313 mtop
.mt_op
= MTRETEN
;
318 mtop
.mt_op
= MTERASE
;
335 mtop
.mt_count
= atoi (count
);
336 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0)
339 rval
= mtop
.mt_count
;
344 case 'S': /* status */
346 { struct mtget mtget
;
347 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
349 rval
= sizeof (mtget
);
350 (void)sprintf(resp
, "A%d\n", rval
);
351 (void)write(1, resp
, strlen(resp
));
352 (void)write(1, (char *)&mtget
, sizeof (mtget
));
360 if (read (0, &s
, 1) != 1)
363 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0)
368 rval
= mtget
.mt_type
;
371 rval
= mtget
.mt_dsreg
;
374 rval
= mtget
.mt_erreg
;
377 rval
= mtget
.mt_resid
;
380 rval
= mtget
.mt_fileno
;
383 rval
= mtget
.mt_blkno
;
386 rval
= mtget
.mt_gstat
;
399 case 'V': /* version */
401 DEBUG1("rmtd: V %s\n", op
);
406 DEBUG1("rmtd: garbage command %c\n", c
);
410 DEBUG1("rmtd: A %d\n", rval
);
411 (void)sprintf(resp
, "A%d\n", rval
);
412 (void)write(1, resp
, strlen(resp
));
419 void getstring(char *bp
)
424 for (i
= 0; i
< SSIZE
; i
++) {
425 if (read(0, cp
+i
, 1) != 1)
434 checkbuf(char *record
, int size
)
437 if (size
<= maxrecsize
)
441 record
= malloc(size
);
443 DEBUG("rmtd: cannot allocate buffer space\n");
447 while (size
> 1024 &&
448 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
457 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
458 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
459 (void)write(1, resp
, strlen(resp
));