]> git.wh0rd.org - dump.git/blob - rmt/rmt.c
d94d48fe866d647b93bf207c599b5abf57a28be9
[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.16 2002/01/16 09:32:14 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 */
84 #define RMTI_VERSION -1
85 #define RMT_VERSION 1
86
87 /* Extended 'i' commands */
88 #define RMTI_CACHE 0
89 #define RMTI_NOCACHE 1
90 #define RMTI_RETEN 2
91 #define RMTI_ERASE 3
92 #define RMTI_EOM 4
93 #define RMTI_NBSF 5
94
95 /* Extended 's' comands */
96 #define MTS_TYPE 'T'
97 #define MTS_DSREG 'D'
98 #define MTS_ERREG 'E'
99 #define MTS_RESID 'R'
100 #define MTS_FILENO 'F'
101 #define MTS_BLKNO 'B'
102 #define MTS_FLAGS 'f'
103 #define MTS_BF 'b'
104
105 char *checkbuf __P((char *, int));
106 void error __P((int));
107 void getstring __P((char *));
108
109 int
110 main(int argc, char *argv[])
111 {
112 int rval = 0;
113 char c;
114 int n, i, cc;
115 unsigned long block = 0;
116
117 argc--, argv++;
118 if (argc > 0) {
119 debug = fopen(*argv, "w");
120 if (debug == 0)
121 exit(1);
122 (void)setbuf(debug, (char *)0);
123 }
124 top:
125 errno = 0;
126 rval = 0;
127 if (read(0, &c, 1) != 1)
128 exit(0);
129 switch (c) {
130
131 case 'O':
132 if (tape >= 0)
133 (void) close(tape);
134 getstring(device);
135 getstring(filemode);
136 DEBUG2("rmtd: O %s %s\n", device, filemode);
137 /*
138 * XXX the rmt protocol does not provide a means to
139 * specify the permission bits; allow rw for everyone,
140 * as modified by the users umask
141 */
142 tape = OPEN(device, atoi(filemode), 0666);
143 if (tape < 0)
144 goto ioerror;
145 block = 0;
146 goto respond;
147
148 case 'C':
149 DEBUG1("rmtd: C (%lu blocks)\n", block);
150 getstring(device); /* discard */
151 if (close(tape) < 0)
152 goto ioerror;
153 tape = -1;
154 block = 0;
155 goto respond;
156
157 case 'L':
158 getstring(count);
159 getstring(pos);
160 DEBUG2("rmtd: L %s %s\n", count, pos);
161 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
162 if (rval < 0)
163 goto ioerror;
164 goto respond;
165
166 case 'W':
167 getstring(count);
168 n = atoi(count);
169 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
170 record = checkbuf(record, n);
171 for (i = 0; i < n; i += cc) {
172 cc = read(0, &record[i], n - i);
173 if (cc <= 0) {
174 DEBUG("rmtd: premature eof\n");
175 exit(2);
176 }
177 }
178 rval = write(tape, record, n);
179 if (rval < 0)
180 goto ioerror;
181 block += n >> 10;
182 goto respond;
183
184 case 'R':
185 getstring(count);
186 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
187 n = atoi(count);
188 record = checkbuf(record, n);
189 rval = read(tape, record, n);
190 if (rval < 0)
191 goto ioerror;
192 (void)sprintf(resp, "A%d\n", rval);
193 (void)write(1, resp, strlen(resp));
194 (void)write(1, record, rval);
195 block += n >> 10;
196 goto top;
197
198 case 'I':
199 getstring(op);
200 getstring(count);
201 DEBUG2("rmtd: I %s %s\n", op, count);
202 if (atoi(op) == RMTI_VERSION) {
203 rval = RMT_VERSION;
204 } else {
205 struct mtop mtop;
206 mtop.mt_op = atoi(op);
207 mtop.mt_count = atoi(count);
208 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
209 goto ioerror;
210 rval = mtop.mt_count;
211 }
212 goto respond;
213
214 case 'i':
215 { struct mtop mtop;
216
217 getstring (op);
218 getstring (count);
219 DEBUG2 ("rmtd: i %s %s\n", op, count);
220 switch (atoi(op)) {
221 #ifdef MTCACHE
222 case RMTI_CACHE:
223 mtop.mt_op = MTCACHE;
224 break;
225 #endif
226 #ifdef MTNOCACHE
227 case RMTI_NOCACHE:
228 mtop.mt_op = MTNOCACHE;
229 break;
230 #endif
231 #ifdef MTRETEN
232 case RMTI_RETEN:
233 mtop.mt_op = MTRETEN;
234 break;
235 #endif
236 #ifdef MTERASE
237 case RMTI_ERASE:
238 mtop.mt_op = MTERASE;
239 break;
240 #endif
241 #ifdef MTEOM
242 case RMTI_EOM:
243 mtop.mt_op = MTEOM;
244 break;
245 #endif
246 #ifdef MTNBSF
247 case RMTI_NBSF:
248 mtop.mt_op = MTNBSF;
249 break;
250 #endif
251 default:
252 errno = EINVAL;
253 goto ioerror;
254 }
255 mtop.mt_count = atoi (count);
256 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
257 goto ioerror;
258
259 rval = mtop.mt_count;
260
261 goto respond;
262 }
263
264 case 'S': /* status */
265 DEBUG("rmtd: S\n");
266 { struct mtget mtget;
267 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
268 goto ioerror;
269 rval = sizeof (mtget);
270 (void)sprintf(resp, "A%d\n", rval);
271 (void)write(1, resp, strlen(resp));
272 (void)write(1, (char *)&mtget, sizeof (mtget));
273 goto top;
274 }
275
276 case 's':
277 { char s;
278 struct mtget mtget;
279
280 if (read (0, &s, 1) != 1)
281 goto top;
282
283 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
284 goto ioerror;
285
286 switch (s) {
287 case MTS_TYPE:
288 rval = mtget.mt_type;
289 break;
290 case MTS_DSREG:
291 rval = mtget.mt_dsreg;
292 break;
293 case MTS_ERREG:
294 rval = mtget.mt_erreg;
295 break;
296 case MTS_RESID:
297 rval = mtget.mt_resid;
298 break;
299 case MTS_FILENO:
300 rval = mtget.mt_fileno;
301 break;
302 case MTS_BLKNO:
303 rval = mtget.mt_blkno;
304 break;
305 case MTS_FLAGS:
306 rval = mtget.mt_gstat;
307 break;
308 case MTS_BF:
309 rval = 0;
310 break;
311 default:
312 errno = EINVAL;
313 goto ioerror;
314 }
315
316 goto respond;
317 }
318
319 case 'V': /* version */
320 getstring(op);
321 DEBUG1("rmtd: V %s\n", op);
322 rval = 2;
323 goto respond;
324
325 default:
326 DEBUG1("rmtd: garbage command %c\n", c);
327 exit(3);
328 }
329 respond:
330 DEBUG1("rmtd: A %d\n", rval);
331 (void)sprintf(resp, "A%d\n", rval);
332 (void)write(1, resp, strlen(resp));
333 goto top;
334 ioerror:
335 error(errno);
336 goto top;
337 }
338
339 void getstring(char *bp)
340 {
341 int i;
342 char *cp = bp;
343
344 for (i = 0; i < SSIZE; i++) {
345 if (read(0, cp+i, 1) != 1)
346 exit(0);
347 if (cp[i] == '\n')
348 break;
349 }
350 cp[i] = '\0';
351 }
352
353 char *
354 checkbuf(char *record, int size)
355 {
356
357 if (size <= maxrecsize)
358 return (record);
359 if (record != 0)
360 free(record);
361 record = malloc(size);
362 if (record == 0) {
363 DEBUG("rmtd: cannot allocate buffer space\n");
364 exit(4);
365 }
366 maxrecsize = size;
367 while (size > 1024 &&
368 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
369 size -= 1024;
370 return (record);
371 }
372
373 void
374 error(int num)
375 {
376
377 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
378 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
379 (void)write(1, resp, strlen(resp));
380 }