]> git.wh0rd.org Git - dump.git/blob - rmt/rmt.c
3c8f5fd3dd3ab17f90af0d6ea5738b6d5106fa27
[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.19 2002/04/16 21:00:59 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 static int      tape = -1;
66
67 static char     *record;
68 static int      maxrecsize = -1;
69
70 #define SSIZE   64
71 static char     device[SSIZE];
72 static char     count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
73
74 static char     resp[BUFSIZ];
75
76 static 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  *      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.
85  *
86  *      rmt_version is 0 for regular clients (Linux included)
87  *      rmt_version is 1 for extended clients (Sun especially). In this case
88  *              we support some extended commands (see below) and we remap
89  *              the ioctl commands to the UNIX "standard", as per:
90  *                      ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
91  *
92  *      In order to use rmt version 1, a client must send "I-1\n0\n" 
93  *      before issuing the other I commands.
94  */
95 static int      rmt_version = 0;
96 #define RMTI_VERSION    -1
97 #define RMT_VERSION     1
98  
99 /* Extended 'i' commands */
100 #define RMTI_CACHE      0
101 #define RMTI_NOCACHE    1
102 #define RMTI_RETEN      2
103 #define RMTI_ERASE      3
104 #define RMTI_EOM        4
105 #define RMTI_NBSF       5
106  
107 /* Extended 's' comands */
108 #define MTS_TYPE        'T'
109 #define MTS_DSREG       'D'
110 #define MTS_ERREG       'E'
111 #define MTS_RESID       'R'
112 #define MTS_FILENO      'F'
113 #define MTS_BLKNO       'B'
114 #define MTS_FLAGS       'f'
115 #define MTS_BF          'b'
116
117 char    *checkbuf __P((char *, int));
118 void     error __P((int));
119 void     getstring __P((char *));
120
121 int
122 main(int argc, char *argv[])
123 {
124         int rval = 0;
125         char c;
126         int n, i, cc;
127         unsigned long block = 0;
128
129         argc--, argv++;
130         if (argc > 0) {
131                 debug = fopen(*argv, "w");
132                 if (debug == 0)
133                         exit(1);
134                 (void)setbuf(debug, (char *)0);
135         }
136 top:
137         errno = 0;
138         rval = 0;
139         if (read(0, &c, 1) != 1)
140                 exit(0);
141         switch (c) {
142
143         case 'O':
144                 if (tape >= 0)
145                         (void) close(tape);
146                 getstring(device);
147                 getstring(filemode);
148                 DEBUG2("rmtd: O %s %s\n", device, filemode);
149                 /*
150                  * XXX the rmt protocol does not provide a means to
151                  * specify the permission bits; allow rw for everyone,
152                  * as modified by the users umask
153                  */
154                 tape = OPEN(device, atoi(filemode), 0666);
155                 if (tape < 0)
156                         goto ioerror;
157                 block = 0;
158                 goto respond;
159
160         case 'C':
161                 DEBUG1("rmtd: C  (%lu blocks)\n", block);
162                 getstring(device);              /* discard */
163                 if (close(tape) < 0)
164                         goto ioerror;
165                 tape = -1;
166                 block = 0;
167                 goto respond;
168
169         case 'L':
170                 getstring(count);
171                 getstring(pos);
172                 DEBUG2("rmtd: L %s %s\n", count, pos);
173                 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
174                 if (rval < 0)
175                         goto ioerror;
176                 goto respond;
177
178         case 'W':
179                 getstring(count);
180                 n = atoi(count);
181                 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
182                 record = checkbuf(record, n);
183                 for (i = 0; i < n; i += cc) {
184                         cc = read(0, &record[i], n - i);
185                         if (cc <= 0) {
186                                 DEBUG("rmtd: premature eof\n");
187                                 exit(2);
188                         }
189                 }
190                 rval = write(tape, record, n);
191                 if (rval < 0)
192                         goto ioerror;
193                 block += n >> 10;
194                 goto respond;
195
196         case 'R':
197                 getstring(count);
198                 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
199                 n = atoi(count);
200                 record = checkbuf(record, n);
201                 rval = read(tape, record, n);
202                 if (rval < 0)
203                         goto ioerror;
204                 (void)sprintf(resp, "A%d\n", rval);
205                 (void)write(1, resp, strlen(resp));
206                 (void)write(1, record, rval);
207                 block += n >> 10;
208                 goto top;
209
210         case 'I':
211                 getstring(op);
212                 getstring(count);
213                 DEBUG2("rmtd: I %s %s\n", op, count);
214                 if (atoi(op) == RMTI_VERSION) {
215                         rval = RMT_VERSION;
216                         rmt_version = 1;
217                 } 
218                 else { 
219                         struct mtop mtop;
220                         mtop.mt_op = -1;
221                         if (rmt_version) {
222                                 /* rmt version 1, assume UNIX client */
223                                 switch (atoi(op)) {
224 #ifdef  MTWEOF
225                                         case 0:
226                                                 mtop.mt_op = MTWEOF;
227                                                 break;
228 #endif
229 #ifdef  MTFSF
230                                         case 1:
231                                                 mtop.mt_op = MTFSF;
232                                                 break;
233 #endif
234 #ifdef  MTBSF
235                                         case 2:
236                                                 mtop.mt_op = MTBSF;
237                                                 break;
238 #endif
239 #ifdef  MTFSR
240                                         case 3:
241                                                 mtop.mt_op = MTFSR;
242                                                 break;
243 #endif
244 #ifdef  MTBSR
245                                         case 4:
246                                                 mtop.mt_op = MTBSR;
247                                                 break;
248 #endif
249 #ifdef  MTREW
250                                         case 5:
251                                                 mtop.mt_op = MTREW;
252                                                 break;
253 #endif
254 #ifdef  MTOFFL
255                                         case 6:
256                                                 mtop.mt_op = MTOFFL;
257                                                 break;
258 #endif
259 #ifdef  MTNOP
260                                         case 7:
261                                                 mtop.mt_op = MTNOP;
262                                                 break;
263 #endif
264                                 }
265                                 if (mtop.mt_op == -1) {
266                                         errno = EINVAL;
267                                         goto ioerror;
268                                 }
269                         }
270                         else {
271                                 /* rmt version 0, assume linux client */
272                                 mtop.mt_op = atoi(op);
273                         }
274                         mtop.mt_count = atoi(count);
275                         if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
276                                 goto ioerror;
277                         rval = mtop.mt_count;
278                 }
279                 goto respond;
280
281         case 'i':
282         {       struct mtop mtop;
283  
284                 getstring (op);
285                 getstring (count);
286                 DEBUG2 ("rmtd: i %s %s\n", op, count);
287                 switch (atoi(op)) {
288 #ifdef MTCACHE
289                         case RMTI_CACHE:
290                                 mtop.mt_op = MTCACHE;
291                                 break;
292 #endif
293 #ifdef MTNOCACHE
294                         case RMTI_NOCACHE:
295                                 mtop.mt_op = MTNOCACHE;
296                                 break;
297 #endif
298 #ifdef MTRETEN
299                         case RMTI_RETEN:
300                                 mtop.mt_op = MTRETEN;
301                                 break;
302 #endif
303 #ifdef MTERASE
304                         case RMTI_ERASE:
305                                 mtop.mt_op = MTERASE;
306                                 break;
307 #endif
308 #ifdef MTEOM
309                         case RMTI_EOM:
310                                 mtop.mt_op = MTEOM;
311                                 break;
312 #endif
313 #ifdef MTNBSF
314                         case RMTI_NBSF:
315                                 mtop.mt_op = MTNBSF;
316                                 break;
317 #endif
318                         default:
319                                 errno = EINVAL;
320                                 goto ioerror;
321                 }
322                 mtop.mt_count = atoi (count);
323                 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
324                         goto ioerror;
325
326                 rval = mtop.mt_count;
327
328                 goto respond;
329         }
330
331         case 'S':               /* status */
332                 DEBUG("rmtd: S\n");
333                 { struct mtget mtget;
334                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
335                         goto ioerror;
336                   rval = sizeof (mtget);
337                   (void)sprintf(resp, "A%d\n", rval);
338                   (void)write(1, resp, strlen(resp));
339                   (void)write(1, (char *)&mtget, sizeof (mtget));
340                   goto top;
341                 }
342
343         case 's':
344         {       char s;
345                 struct mtget mtget;
346  
347                 if (read (0, &s, 1) != 1)
348                         goto top;
349  
350                 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
351                         goto ioerror;
352
353                 switch (s) {
354                         case MTS_TYPE:
355                                 rval = mtget.mt_type;
356                                 break;
357                         case MTS_DSREG:
358                                 rval = mtget.mt_dsreg;
359                                 break;
360                         case MTS_ERREG:
361                                 rval = mtget.mt_erreg;
362                                 break;
363                         case MTS_RESID:
364                                 rval = mtget.mt_resid;
365                                 break;
366                         case MTS_FILENO:
367                                 rval = mtget.mt_fileno;
368                                 break;
369                         case MTS_BLKNO:
370                                 rval = mtget.mt_blkno;
371                                 break;
372                         case MTS_FLAGS:
373                                 rval = mtget.mt_gstat;
374                                 break;
375                         case MTS_BF:
376                                 rval = 0;
377                                 break;
378                         default:
379                                 errno = EINVAL;
380                                 goto ioerror;
381                 }
382
383                 goto respond;
384         }
385
386         case 'V':               /* version */
387                 getstring(op);
388                 DEBUG1("rmtd: V %s\n", op);
389                 rval = 2;
390                 goto respond;
391
392         default:
393                 DEBUG1("rmtd: garbage command %c\n", c);
394                 exit(3);
395         }
396 respond:
397         DEBUG1("rmtd: A %d\n", rval);
398         (void)sprintf(resp, "A%d\n", rval);
399         (void)write(1, resp, strlen(resp));
400         goto top;
401 ioerror:
402         error(errno);
403         goto top;
404 }
405
406 void getstring(char *bp)
407 {
408         int i;
409         char *cp = bp;
410
411         for (i = 0; i < SSIZE; i++) {
412                 if (read(0, cp+i, 1) != 1)
413                         exit(0);
414                 if (cp[i] == '\n')
415                         break;
416         }
417         cp[i] = '\0';
418 }
419
420 char *
421 checkbuf(char *record, int size)
422 {
423
424         if (size <= maxrecsize)
425                 return (record);
426         if (record != 0)
427                 free(record);
428         record = malloc(size);
429         if (record == 0) {
430                 DEBUG("rmtd: cannot allocate buffer space\n");
431                 exit(4);
432         }
433         maxrecsize = size;
434         while (size > 1024 &&
435                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
436                 size -= 1024;
437         return (record);
438 }
439
440 void
441 error(int num)
442 {
443
444         DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
445         (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
446         (void)write(1, resp, strlen(resp));
447 }