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