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