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