]> git.wh0rd.org - dump.git/blob - rmt/rmt.c
LFS fixes (explicit use of open64/etc)
[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.14 2001/04/27 15:22:47 stelian Exp $";
45 #endif /* not linux */
46
47 /*
48 * rmt
49 */
50 #include <config.h>
51 #include <compatlfs.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/mtio.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #ifndef __linux__
58 #include <sgtty.h>
59 #endif
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 int tape = -1;
66
67 char *record;
68 int maxrecsize = -1;
69
70 #define SSIZE 64
71 char device[SSIZE];
72 char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
73
74 char resp[BUFSIZ];
75
76 FILE *debug;
77 #define DEBUG(f) if (debug) fprintf(debug, f)
78 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
79 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
80
81 char *checkbuf __P((char *, int));
82 void error __P((int));
83 void getstring __P((char *));
84
85 int
86 main(int argc, char *argv[])
87 {
88 int rval = 0;
89 char c;
90 int n, i, cc;
91 unsigned long block = 0;
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 block = 0;
122 goto respond;
123
124 case 'C':
125 DEBUG1("rmtd: C (%lu blocks)\n", block);
126 getstring(device); /* discard */
127 if (close(tape) < 0)
128 goto ioerror;
129 tape = -1;
130 block = 0;
131 goto respond;
132
133 case 'L':
134 getstring(count);
135 getstring(pos);
136 DEBUG2("rmtd: L %s %s\n", count, pos);
137 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
138 if (rval < 0)
139 goto ioerror;
140 goto respond;
141
142 case 'W':
143 getstring(count);
144 n = atoi(count);
145 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
146 record = checkbuf(record, n);
147 for (i = 0; i < n; i += cc) {
148 cc = read(0, &record[i], n - i);
149 if (cc <= 0) {
150 DEBUG("rmtd: premature eof\n");
151 exit(2);
152 }
153 }
154 rval = write(tape, record, n);
155 if (rval < 0)
156 goto ioerror;
157 block += n >> 10;
158 goto respond;
159
160 case 'R':
161 getstring(count);
162 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
163 n = atoi(count);
164 record = checkbuf(record, n);
165 rval = read(tape, record, n);
166 if (rval < 0)
167 goto ioerror;
168 (void)sprintf(resp, "A%d\n", rval);
169 (void)write(1, resp, strlen(resp));
170 (void)write(1, record, rval);
171 block += n >> 10;
172 goto top;
173
174 case 'I':
175 getstring(op);
176 getstring(count);
177 DEBUG2("rmtd: I %s %s\n", op, count);
178 { struct mtop mtop;
179 mtop.mt_op = atoi(op);
180 mtop.mt_count = atoi(count);
181 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
182 goto ioerror;
183 rval = mtop.mt_count;
184 }
185 goto respond;
186
187 case 'S': /* status */
188 DEBUG("rmtd: S\n");
189 { struct mtget mtget;
190 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
191 goto ioerror;
192 rval = sizeof (mtget);
193 (void)sprintf(resp, "A%d\n", rval);
194 (void)write(1, resp, strlen(resp));
195 (void)write(1, (char *)&mtget, sizeof (mtget));
196 goto top;
197 }
198
199 case 'V': /* version */
200 getstring(op);
201 DEBUG1("rmtd: V %s\n", op);
202 rval = 2;
203 goto respond;
204
205 default:
206 DEBUG1("rmtd: garbage command %c\n", c);
207 exit(3);
208 }
209 respond:
210 DEBUG1("rmtd: A %d\n", rval);
211 (void)sprintf(resp, "A%d\n", rval);
212 (void)write(1, resp, strlen(resp));
213 goto top;
214 ioerror:
215 error(errno);
216 goto top;
217 }
218
219 void getstring(char *bp)
220 {
221 int i;
222 char *cp = bp;
223
224 for (i = 0; i < SSIZE; i++) {
225 if (read(0, cp+i, 1) != 1)
226 exit(0);
227 if (cp[i] == '\n')
228 break;
229 }
230 cp[i] = '\0';
231 }
232
233 char *
234 checkbuf(char *record, int size)
235 {
236
237 if (size <= maxrecsize)
238 return (record);
239 if (record != 0)
240 free(record);
241 record = malloc(size);
242 if (record == 0) {
243 DEBUG("rmtd: cannot allocate buffer space\n");
244 exit(4);
245 }
246 maxrecsize = size;
247 while (size > 1024 &&
248 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
249 size -= 1024;
250 return (record);
251 }
252
253 void
254 error(int num)
255 {
256
257 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
258 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
259 (void)write(1, resp, strlen(resp));
260 }