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