]> git.wh0rd.org Git - dump.git/blob - rmt/rmt.c
Copyright notice for Alcôve, for paying me to work on dump...
[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 <pop@cybercable.fr>, 1999-2000
6  *      Stelian Pop <pop@cybercable.fr> - Alcôve <www.alcove.fr>, 2000
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.10 2000/11/10 14:51:42 stelian Exp $";
45 #endif /* not linux */
46
47 /*
48  * rmt
49  */
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/mtio.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #ifndef __linux__
56 #include <sgtty.h>
57 #endif
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 int     tape = -1;
64
65 char    *record;
66 int     maxrecsize = -1;
67
68 #define SSIZE   64
69 char    device[SSIZE];
70 char    count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
71
72 char    resp[BUFSIZ];
73
74 FILE    *debug;
75 #define DEBUG(f)        if (debug) fprintf(debug, f)
76 #define DEBUG1(f,a)     if (debug) fprintf(debug, f, a)
77 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
78
79 char    *checkbuf __P((char *, int));
80 void     error __P((int));
81 void     getstring __P((char *));
82
83 int
84 main(int argc, char *argv[])
85 {
86         int rval = 0;
87         char c;
88         int n, i, cc;
89
90         argc--, argv++;
91         if (argc > 0) {
92                 debug = fopen(*argv, "w");
93                 if (debug == 0)
94                         exit(1);
95                 (void)setbuf(debug, (char *)0);
96         }
97 top:
98         errno = 0;
99         rval = 0;
100         if (read(0, &c, 1) != 1)
101                 exit(0);
102         switch (c) {
103
104         case 'O':
105                 if (tape >= 0)
106                         (void) close(tape);
107                 getstring(device);
108                 getstring(filemode);
109                 DEBUG2("rmtd: O %s %s\n", device, filemode);
110                 /*
111                  * XXX the rmt protocol does not provide a means to
112                  * specify the permission bits; allow rw for everyone,
113                  * as modified by the users umask
114                  */
115                 tape = open(device, atoi(filemode), 0666);
116                 if (tape < 0)
117                         goto ioerror;
118                 goto respond;
119
120         case 'C':
121                 DEBUG("rmtd: C\n");
122                 getstring(device);              /* discard */
123                 if (close(tape) < 0)
124                         goto ioerror;
125                 tape = -1;
126                 goto respond;
127
128         case 'L':
129                 getstring(count);
130                 getstring(pos);
131                 DEBUG2("rmtd: L %s %s\n", count, pos);
132                 rval = lseek(tape, (off_t)atol(count), atoi(pos));
133                 if (rval < 0)
134                         goto ioerror;
135                 goto respond;
136
137         case 'W':
138                 getstring(count);
139                 n = atoi(count);
140                 DEBUG1("rmtd: W %s\n", count);
141                 record = checkbuf(record, n);
142                 for (i = 0; i < n; i += cc) {
143                         cc = read(0, &record[i], n - i);
144                         if (cc <= 0) {
145                                 DEBUG("rmtd: premature eof\n");
146                                 exit(2);
147                         }
148                 }
149                 rval = write(tape, record, n);
150                 if (rval < 0)
151                         goto ioerror;
152                 goto respond;
153
154         case 'R':
155                 getstring(count);
156                 DEBUG1("rmtd: R %s\n", count);
157                 n = atoi(count);
158                 record = checkbuf(record, n);
159                 rval = read(tape, record, n);
160                 if (rval < 0)
161                         goto ioerror;
162                 (void)sprintf(resp, "A%d\n", rval);
163                 (void)write(1, resp, strlen(resp));
164                 (void)write(1, record, rval);
165                 goto top;
166
167         case 'I':
168                 getstring(op);
169                 getstring(count);
170                 DEBUG2("rmtd: I %s %s\n", op, count);
171                 { struct mtop mtop;
172                   mtop.mt_op = atoi(op);
173                   mtop.mt_count = atoi(count);
174                   if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
175                         goto ioerror;
176                   rval = mtop.mt_count;
177                 }
178                 goto respond;
179
180         case 'S':               /* status */
181                 DEBUG("rmtd: S\n");
182                 { struct mtget mtget;
183                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
184                         goto ioerror;
185                   rval = sizeof (mtget);
186                   (void)sprintf(resp, "A%d\n", rval);
187                   (void)write(1, resp, strlen(resp));
188                   (void)write(1, (char *)&mtget, sizeof (mtget));
189                   goto top;
190                 }
191
192         case 'V':               /* version */
193                 getstring(op);
194                 DEBUG1("rmtd: V %s\n", op);
195                 rval = 2;
196                 goto respond;
197
198         default:
199                 DEBUG1("rmtd: garbage command %c\n", c);
200                 exit(3);
201         }
202 respond:
203         DEBUG1("rmtd: A %d\n", rval);
204         (void)sprintf(resp, "A%d\n", rval);
205         (void)write(1, resp, strlen(resp));
206         goto top;
207 ioerror:
208         error(errno);
209         goto top;
210 }
211
212 void getstring(char *bp)
213 {
214         int i;
215         char *cp = bp;
216
217         for (i = 0; i < SSIZE; i++) {
218                 if (read(0, cp+i, 1) != 1)
219                         exit(0);
220                 if (cp[i] == '\n')
221                         break;
222         }
223         cp[i] = '\0';
224 }
225
226 char *
227 checkbuf(char *record, int size)
228 {
229
230         if (size <= maxrecsize)
231                 return (record);
232         if (record != 0)
233                 free(record);
234         record = malloc(size);
235         if (record == 0) {
236                 DEBUG("rmtd: cannot allocate buffer space\n");
237                 exit(4);
238         }
239         maxrecsize = size;
240         while (size > 1024 &&
241                setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
242                 size -= 1024;
243         return (record);
244 }
245
246 void
247 error(int num)
248 {
249
250         DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
251         (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
252         (void)write(1, resp, strlen(resp));
253 }