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