]> git.wh0rd.org - dump.git/blame - rmt/rmt.c
Version 0.4b5.
[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[] =
53 "$Id: rmt.c,v 1.2 1999/10/11 12:53:25 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
70int tape = -1;
71
72char *record;
73int maxrecsize = -1;
74
75#define SSIZE 64
76char device[SSIZE];
77char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
78
79char resp[BUFSIZ];
80
81FILE *debug;
82#define DEBUG(f) if (debug) fprintf(debug, f)
83#define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
84#define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
85
86char *checkbuf __P((char *, int));
87void error __P((int));
88void getstring __P((char *));
89
90int
91main(argc, argv)
92 int argc;
93 char **argv;
94{
95 int rval;
96 char c;
97 int n, i, cc;
98
99 argc--, argv++;
100 if (argc > 0) {
101 debug = fopen(*argv, "w");
102 if (debug == 0)
103 exit(1);
104 (void)setbuf(debug, (char *)0);
105 }
106top:
107 errno = 0;
108 rval = 0;
109 if (read(0, &c, 1) != 1)
110 exit(0);
111 switch (c) {
112
113 case 'O':
114 if (tape >= 0)
115 (void) close(tape);
116 getstring(device);
117 getstring(mode);
118 DEBUG2("rmtd: O %s %s\n", device, mode);
b45f51d6
SP
119 /*
120 * XXX the rmt protocol does not provide a means to
121 * specify the permission bits; allow rw for everyone,
122 * as modified by the users umask
123 */
124 tape = open(device, atoi(mode), 0666);
1227625a
SP
125 if (tape < 0)
126 goto ioerror;
127 goto respond;
128
129 case 'C':
130 DEBUG("rmtd: C\n");
131 getstring(device); /* discard */
132 if (close(tape) < 0)
133 goto ioerror;
134 tape = -1;
135 goto respond;
136
137 case 'L':
138 getstring(count);
139 getstring(pos);
140 DEBUG2("rmtd: L %s %s\n", count, pos);
141 rval = lseek(tape, (off_t)atol(count), atoi(pos));
142 if (rval < 0)
143 goto ioerror;
144 goto respond;
145
146 case 'W':
147 getstring(count);
148 n = atoi(count);
149 DEBUG1("rmtd: W %s\n", count);
150 record = checkbuf(record, n);
151 for (i = 0; i < n; i += cc) {
152 cc = read(0, &record[i], n - i);
153 if (cc <= 0) {
154 DEBUG("rmtd: premature eof\n");
155 exit(2);
156 }
157 }
158 rval = write(tape, record, n);
159 if (rval < 0)
160 goto ioerror;
161 goto respond;
162
163 case 'R':
164 getstring(count);
165 DEBUG1("rmtd: R %s\n", count);
166 n = atoi(count);
167 record = checkbuf(record, n);
168 rval = read(tape, record, n);
169 if (rval < 0)
170 goto ioerror;
171 (void)sprintf(resp, "A%d\n", rval);
172 (void)write(1, resp, strlen(resp));
173 (void)write(1, record, rval);
174 goto top;
175
176 case 'I':
177 getstring(op);
178 getstring(count);
179 DEBUG2("rmtd: I %s %s\n", op, count);
180 { struct mtop mtop;
181 mtop.mt_op = atoi(op);
182 mtop.mt_count = atoi(count);
183 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
184 goto ioerror;
185 rval = mtop.mt_count;
186 }
187 goto respond;
188
189 case 'S': /* status */
190 DEBUG("rmtd: S\n");
191 { struct mtget mtget;
192 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
193 goto ioerror;
194 rval = sizeof (mtget);
195 (void)sprintf(resp, "A%d\n", rval);
196 (void)write(1, resp, strlen(resp));
197 (void)write(1, (char *)&mtget, sizeof (mtget));
198 goto top;
199 }
200
b45f51d6
SP
201 case 'V': /* version */
202 getstring(op);
203 DEBUG1("rmtd: V %s\n", op);
204 rval = 2;
205 goto respond;
206
1227625a
SP
207 default:
208 DEBUG1("rmtd: garbage command %c\n", c);
209 exit(3);
210 }
211respond:
212 DEBUG1("rmtd: A %d\n", rval);
213 (void)sprintf(resp, "A%d\n", rval);
214 (void)write(1, resp, strlen(resp));
215 goto top;
216ioerror:
217 error(errno);
218 goto top;
219}
220
221void
222getstring(bp)
223 char *bp;
224{
225 int i;
226 char *cp = bp;
227
228 for (i = 0; i < SSIZE; i++) {
229 if (read(0, cp+i, 1) != 1)
230 exit(0);
231 if (cp[i] == '\n')
232 break;
233 }
234 cp[i] = '\0';
235}
236
237char *
238checkbuf(record, size)
239 char *record;
240 int size;
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
260error(num)
261 int num;
262{
263
264 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
b45f51d6 265 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
1227625a
SP
266 (void)write(1, resp, strlen(resp));
267}