]>
Commit | Line | Data |
---|---|---|
1227625a SP |
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, 1995, 1996 | |
5 | * | |
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 char copyright[] = | |
43 | "@(#) Copyright (c) 1983, 1993\n\ | |
44 | The Regents of the University of California. All rights reserved.\n"; | |
45 | #endif /* not lint */ | |
46 | ||
47 | #ifndef lint | |
48 | static char sccsid[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93"; | |
49 | #endif /* not lint */ | |
50 | ||
51 | /* | |
52 | * rmt | |
53 | */ | |
54 | #include <sys/types.h> | |
55 | #include <sys/socket.h> | |
56 | #include <sys/mtio.h> | |
57 | #include <errno.h> | |
58 | #include <fcntl.h> | |
59 | #include <sgtty.h> | |
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], mode[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(argc, argv) | |
87 | int argc; | |
88 | char **argv; | |
89 | { | |
90 | int rval; | |
91 | char c; | |
92 | int n, i, cc; | |
93 | ||
94 | argc--, argv++; | |
95 | if (argc > 0) { | |
96 | debug = fopen(*argv, "w"); | |
97 | if (debug == 0) | |
98 | exit(1); | |
99 | (void)setbuf(debug, (char *)0); | |
100 | } | |
101 | top: | |
102 | errno = 0; | |
103 | rval = 0; | |
104 | if (read(0, &c, 1) != 1) | |
105 | exit(0); | |
106 | switch (c) { | |
107 | ||
108 | case 'O': | |
109 | if (tape >= 0) | |
110 | (void) close(tape); | |
111 | getstring(device); | |
112 | getstring(mode); | |
113 | DEBUG2("rmtd: O %s %s\n", device, mode); | |
114 | tape = open(device, atoi(mode)); | |
115 | if (tape < 0) | |
116 | goto ioerror; | |
117 | goto respond; | |
118 | ||
119 | case 'C': | |
120 | DEBUG("rmtd: C\n"); | |
121 | getstring(device); /* discard */ | |
122 | if (close(tape) < 0) | |
123 | goto ioerror; | |
124 | tape = -1; | |
125 | goto respond; | |
126 | ||
127 | case 'L': | |
128 | getstring(count); | |
129 | getstring(pos); | |
130 | DEBUG2("rmtd: L %s %s\n", count, pos); | |
131 | rval = lseek(tape, (off_t)atol(count), atoi(pos)); | |
132 | if (rval < 0) | |
133 | goto ioerror; | |
134 | goto respond; | |
135 | ||
136 | case 'W': | |
137 | getstring(count); | |
138 | n = atoi(count); | |
139 | DEBUG1("rmtd: W %s\n", count); | |
140 | record = checkbuf(record, n); | |
141 | for (i = 0; i < n; i += cc) { | |
142 | cc = read(0, &record[i], n - i); | |
143 | if (cc <= 0) { | |
144 | DEBUG("rmtd: premature eof\n"); | |
145 | exit(2); | |
146 | } | |
147 | } | |
148 | rval = write(tape, record, n); | |
149 | if (rval < 0) | |
150 | goto ioerror; | |
151 | goto respond; | |
152 | ||
153 | case 'R': | |
154 | getstring(count); | |
155 | DEBUG1("rmtd: R %s\n", count); | |
156 | n = atoi(count); | |
157 | record = checkbuf(record, n); | |
158 | rval = read(tape, record, n); | |
159 | if (rval < 0) | |
160 | goto ioerror; | |
161 | (void)sprintf(resp, "A%d\n", rval); | |
162 | (void)write(1, resp, strlen(resp)); | |
163 | (void)write(1, record, rval); | |
164 | goto top; | |
165 | ||
166 | case 'I': | |
167 | getstring(op); | |
168 | getstring(count); | |
169 | DEBUG2("rmtd: I %s %s\n", op, count); | |
170 | { struct mtop mtop; | |
171 | mtop.mt_op = atoi(op); | |
172 | mtop.mt_count = atoi(count); | |
173 | if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0) | |
174 | goto ioerror; | |
175 | rval = mtop.mt_count; | |
176 | } | |
177 | goto respond; | |
178 | ||
179 | case 'S': /* status */ | |
180 | DEBUG("rmtd: S\n"); | |
181 | { struct mtget mtget; | |
182 | if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0) | |
183 | goto ioerror; | |
184 | rval = sizeof (mtget); | |
185 | (void)sprintf(resp, "A%d\n", rval); | |
186 | (void)write(1, resp, strlen(resp)); | |
187 | (void)write(1, (char *)&mtget, sizeof (mtget)); | |
188 | goto top; | |
189 | } | |
190 | ||
191 | default: | |
192 | DEBUG1("rmtd: garbage command %c\n", c); | |
193 | exit(3); | |
194 | } | |
195 | respond: | |
196 | DEBUG1("rmtd: A %d\n", rval); | |
197 | (void)sprintf(resp, "A%d\n", rval); | |
198 | (void)write(1, resp, strlen(resp)); | |
199 | goto top; | |
200 | ioerror: | |
201 | error(errno); | |
202 | goto top; | |
203 | } | |
204 | ||
205 | void | |
206 | getstring(bp) | |
207 | char *bp; | |
208 | { | |
209 | int i; | |
210 | char *cp = bp; | |
211 | ||
212 | for (i = 0; i < SSIZE; i++) { | |
213 | if (read(0, cp+i, 1) != 1) | |
214 | exit(0); | |
215 | if (cp[i] == '\n') | |
216 | break; | |
217 | } | |
218 | cp[i] = '\0'; | |
219 | } | |
220 | ||
221 | char * | |
222 | checkbuf(record, size) | |
223 | char *record; | |
224 | int size; | |
225 | { | |
226 | ||
227 | if (size <= maxrecsize) | |
228 | return (record); | |
229 | if (record != 0) | |
230 | free(record); | |
231 | record = malloc(size); | |
232 | if (record == 0) { | |
233 | DEBUG("rmtd: cannot allocate buffer space\n"); | |
234 | exit(4); | |
235 | } | |
236 | maxrecsize = size; | |
237 | while (size > 1024 && | |
238 | setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0) | |
239 | size -= 1024; | |
240 | return (record); | |
241 | } | |
242 | ||
243 | void | |
244 | error(num) | |
245 | int num; | |
246 | { | |
247 | ||
248 | DEBUG2("rmtd: E %d (%s)\n", num, strerror(num)); | |
249 | (void)sprintf(resp, "E%d\n%s\n", num, strerror(num)); | |
250 | (void)write(1, resp, strlen(resp)); | |
251 | } |