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