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 <pop@cybercable.fr>, 1999-2000
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid[] =
43 "$Id: rmt.c,v 1.9 2000/01/21 10:17:41 stelian Exp $";
44 #endif /* not linux */
49 #include <sys/types.h>
50 #include <sys/socket.h>
69 char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
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)
78 char *checkbuf __P((char *, int));
79 void error __P((int));
80 void getstring __P((char *));
83 main(int argc, char *argv[])
91 debug = fopen(*argv, "w");
94 (void)setbuf(debug, (char *)0);
99 if (read(0, &c, 1) != 1)
108 DEBUG2("rmtd: O %s %s\n", device, filemode);
110 * XXX the rmt protocol does not provide a means to
111 * specify the permission bits; allow rw for everyone,
112 * as modified by the users umask
114 tape = open(device, atoi(filemode), 0666);
121 getstring(device); /* discard */
130 DEBUG2("rmtd: L %s %s\n", count, pos);
131 rval = lseek(tape, (off_t)atol(count), atoi(pos));
139 DEBUG1("rmtd: W %s\n", count);
140 record = checkbuf(record, n);
141 for (i = 0; i < n; i += cc) {
142 cc = read(0, &record[i], n - i);
144 DEBUG("rmtd: premature eof\n");
148 rval = write(tape, record, n);
155 DEBUG1("rmtd: R %s\n", count);
157 record = checkbuf(record, n);
158 rval = read(tape, record, n);
161 (void)sprintf(resp, "A%d\n", rval);
162 (void)write(1, resp, strlen(resp));
163 (void)write(1, record, rval);
169 DEBUG2("rmtd: I %s %s\n", op, count);
171 mtop.mt_op = atoi(op);
172 mtop.mt_count = atoi(count);
173 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
175 rval = mtop.mt_count;
179 case 'S': /* status */
181 { struct mtget mtget;
182 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
184 rval = sizeof (mtget);
185 (void)sprintf(resp, "A%d\n", rval);
186 (void)write(1, resp, strlen(resp));
187 (void)write(1, (char *)&mtget, sizeof (mtget));
191 case 'V': /* version */
193 DEBUG1("rmtd: V %s\n", op);
198 DEBUG1("rmtd: garbage command %c\n", c);
202 DEBUG1("rmtd: A %d\n", rval);
203 (void)sprintf(resp, "A%d\n", rval);
204 (void)write(1, resp, strlen(resp));
211 void getstring(char *bp)
216 for (i = 0; i < SSIZE; i++) {
217 if (read(0, cp+i, 1) != 1)
226 checkbuf(char *record, int size)
229 if (size <= maxrecsize)
233 record = malloc(size);
235 DEBUG("rmtd: cannot allocate buffer space\n");
239 while (size > 1024 &&
240 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
249 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
250 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
251 (void)write(1, resp, strlen(resp));