]>
git.wh0rd.org - dump.git/blob - rmt/rmt.c
3bd6207406336874a960410a365583ecc597eed2
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char rcsid
[] =
40 "$Id: rmt.c,v 1.25 2003/03/31 10:09:41 stelian Exp $";
41 #endif /* not linux */
47 #include <compatlfs.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
65 static int maxrecsize
= -1;
68 static char device
[SSIZE
];
69 static char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
71 static char resp
[BUFSIZ
];
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)
79 * Support for Sun's extended RMT protocol
80 * code originally written by Jörg Schilling <schilling@fokus.gmd.de>
81 * and relicensed by his permission from GPL to BSD for use in dump.
83 * rmt_version is 0 for regular clients (Linux included)
84 * rmt_version is 1 for extended clients (Sun especially). In this case
85 * we support some extended commands (see below) and we remap
86 * the ioctl commands to the UNIX "standard", as per:
87 * ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
89 * In order to use rmt version 1, a client must send "I-1\n0\n"
90 * before issuing the other I commands.
92 static int rmt_version
= 0;
93 #define RMTI_VERSION -1
96 /* Extended 'i' commands */
98 #define RMTI_NOCACHE 1
104 /* Extended 's' comands */
106 #define MTS_DSREG 'D'
107 #define MTS_ERREG 'E'
108 #define MTS_RESID 'R'
109 #define MTS_FILENO 'F'
110 #define MTS_BLKNO 'B'
111 #define MTS_FLAGS 'f'
114 char *checkbuf
__P((char *, int));
115 void error
__P((int));
116 void getstring
__P((char *));
119 main(int argc
, char *argv
[])
123 int n
, i
, cc
, oflags
;
124 unsigned long block
= 0;
128 debug
= fopen(*argv
, "w");
131 (void)setbuf(debug
, (char *)0);
136 if (read(0, &c
, 1) != 1)
145 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
147 * Translate extended GNU syntax into its numeric platform equivalent
149 oflags
= rmtflags_toint(filemode
);
152 * Default to O_BINARY the client may not know that we need it.
154 if ((oflags
& O_TEXT
) == 0)
157 DEBUG2("rmtd: O %s %d\n", device
, oflags
);
159 * XXX the rmt protocol does not provide a means to
160 * specify the permission bits; allow rw for everyone,
161 * as modified by the users umask
163 tape
= OPEN(device
, oflags
, 0666);
170 DEBUG1("rmtd: C (%lu blocks)\n", block
);
171 getstring(device
); /* discard */
181 DEBUG2("rmtd: L %s %s\n", count
, pos
);
182 rval
= LSEEK(tape
, (OFF_T
)atoll(count
), atoi(pos
));
192 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
193 record
= checkbuf(record
, n
);
194 for (i
= 0; i
< n
; i
+= cc
) {
195 cc
= read(0, &record
[i
], n
- i
);
197 DEBUG("rmtd: premature eof\n");
201 rval
= write(tape
, record
, n
);
209 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
211 record
= checkbuf(record
, n
);
212 rval
= read(tape
, record
, n
);
215 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
216 (void)write(1, resp
, strlen(resp
));
217 (void)write(1, record
, rval
);
224 DEBUG2("rmtd: I %s %s\n", op
, count
);
225 if (atoi(op
) == RMTI_VERSION
) {
233 /* rmt version 1, assume UNIX client */
276 if (mtop
.mt_op
== -1) {
282 /* rmt version 0, assume linux client */
283 mtop
.mt_op
= atoi(op
);
285 mtop
.mt_count
= atoi(count
);
286 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
288 rval
= mtop
.mt_count
;
297 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
301 mtop
.mt_op
= MTCACHE
;
306 mtop
.mt_op
= MTNOCACHE
;
311 mtop
.mt_op
= MTRETEN
;
316 mtop
.mt_op
= MTERASE
;
333 mtop
.mt_count
= atoi (count
);
334 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0)
337 rval
= mtop
.mt_count
;
342 case 'S': /* status */
344 { struct mtget mtget
;
345 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
347 rval
= sizeof (mtget
);
348 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
349 (void)write(1, resp
, strlen(resp
));
350 (void)write(1, (char *)&mtget
, sizeof (mtget
));
358 if (read (0, &s
, 1) != 1)
361 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0)
366 rval
= mtget
.mt_type
;
369 rval
= mtget
.mt_dsreg
;
372 rval
= mtget
.mt_erreg
;
375 rval
= mtget
.mt_resid
;
378 rval
= mtget
.mt_fileno
;
381 rval
= mtget
.mt_blkno
;
384 rval
= mtget
.mt_gstat
;
397 case 'V': /* version */
399 DEBUG1("rmtd: V %s\n", op
);
404 DEBUG1("rmtd: garbage command %c\n", c
);
408 DEBUG1("rmtd: A %lld\n", (long long)rval
);
409 (void)sprintf(resp
, "A%lld\n", (long long)rval
);
410 (void)write(1, resp
, strlen(resp
));
417 void getstring(char *bp
)
422 for (i
= 0; i
< SSIZE
- 1; i
++) {
423 if (read(0, cp
+i
, 1) != 1)
432 checkbuf(char *record
, int size
)
435 if (size
<= maxrecsize
)
439 record
= malloc(size
);
441 DEBUG("rmtd: cannot allocate buffer space\n");
445 while (size
> 1024 &&
446 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
455 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
456 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
457 (void)write(1, resp
, strlen(resp
));