dumprmt.c revision 1.5 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1980 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.5 cgd /* from: static char sccsid[] = "@(#)dumprmt.c 5.15 (Berkeley) 6/18/92"; */
36 1.5 cgd static char *rcsid = "$Id: dumprmt.c,v 1.5 1993/12/22 10:24:42 cgd Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.5 cgd #ifdef sunos
40 1.5 cgd #include <stdio.h>
41 1.5 cgd #include <ctype.h>
42 1.5 cgd #include <sys/param.h>
43 1.5 cgd #include <sys/mtio.h>
44 1.5 cgd #include <sys/ioctl.h>
45 1.5 cgd #include <sys/socket.h>
46 1.5 cgd #include <sys/stat.h>
47 1.5 cgd #else
48 1.1 cgd #include <sys/param.h>
49 1.1 cgd #include <sys/mtio.h>
50 1.1 cgd #include <sys/ioctl.h>
51 1.1 cgd #include <sys/socket.h>
52 1.5 cgd #include <sys/time.h>
53 1.5 cgd #include <stdio.h>
54 1.5 cgd #endif
55 1.1 cgd #include <ufs/dinode.h>
56 1.1 cgd #include <signal.h>
57 1.1 cgd
58 1.1 cgd #include <netinet/in.h>
59 1.1 cgd
60 1.1 cgd #include <netdb.h>
61 1.1 cgd #include <protocols/dumprestore.h>
62 1.1 cgd #include <pwd.h>
63 1.1 cgd #ifdef __STDC__
64 1.1 cgd #include <unistd.h>
65 1.1 cgd #include <stdlib.h>
66 1.1 cgd #include <string.h>
67 1.1 cgd #endif
68 1.1 cgd #include "pathnames.h"
69 1.1 cgd
70 1.1 cgd #define TS_CLOSED 0
71 1.1 cgd #define TS_OPEN 1
72 1.1 cgd
73 1.1 cgd static int rmtstate = TS_CLOSED;
74 1.1 cgd int rmtape;
75 1.1 cgd void rmtgetconn();
76 1.1 cgd void rmtconnaborted();
77 1.1 cgd int rmtreply();
78 1.1 cgd int rmtgetb();
79 1.1 cgd void rmtgets();
80 1.1 cgd int rmtcall();
81 1.1 cgd char *rmtpeer;
82 1.1 cgd
83 1.1 cgd extern int ntrec; /* blocking factor on tape */
84 1.1 cgd extern void msg();
85 1.1 cgd
86 1.5 cgd extern void exit();
87 1.5 cgd
88 1.1 cgd int
89 1.1 cgd rmthost(host)
90 1.1 cgd char *host;
91 1.1 cgd {
92 1.1 cgd
93 1.1 cgd rmtpeer = host;
94 1.1 cgd signal(SIGPIPE, rmtconnaborted);
95 1.1 cgd rmtgetconn();
96 1.1 cgd if (rmtape < 0)
97 1.1 cgd return (0);
98 1.1 cgd return (1);
99 1.1 cgd }
100 1.1 cgd
101 1.1 cgd void
102 1.1 cgd rmtconnaborted()
103 1.1 cgd {
104 1.1 cgd
105 1.5 cgd (void) fprintf(stderr, "rdump: Lost connection to remote host.\n");
106 1.5 cgd (void) exit(1);
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd void
110 1.1 cgd rmtgetconn()
111 1.1 cgd {
112 1.1 cgd static struct servent *sp = 0;
113 1.1 cgd struct passwd *pw;
114 1.1 cgd char *name = "root";
115 1.1 cgd int size;
116 1.1 cgd
117 1.1 cgd if (sp == 0) {
118 1.1 cgd sp = getservbyname("shell", "tcp");
119 1.1 cgd if (sp == 0) {
120 1.5 cgd (void) fprintf(stderr,
121 1.5 cgd "rdump: shell/tcp: unknown service\n");
122 1.5 cgd (void) exit(1);
123 1.1 cgd }
124 1.1 cgd }
125 1.1 cgd pw = getpwuid(getuid());
126 1.1 cgd if (pw && pw->pw_name)
127 1.1 cgd name = pw->pw_name;
128 1.5 cgd rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, name, _PATH_RMT,
129 1.5 cgd (int *)0);
130 1.1 cgd size = ntrec * TP_BSIZE;
131 1.1 cgd while (size > TP_BSIZE &&
132 1.1 cgd setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
133 1.1 cgd size -= TP_BSIZE;
134 1.1 cgd }
135 1.1 cgd
136 1.1 cgd int
137 1.1 cgd rmtopen(tape, mode)
138 1.1 cgd char *tape;
139 1.1 cgd int mode;
140 1.1 cgd {
141 1.1 cgd char buf[256];
142 1.1 cgd
143 1.1 cgd (void)sprintf(buf, "O%s\n%d\n", tape, mode);
144 1.1 cgd rmtstate = TS_OPEN;
145 1.1 cgd return (rmtcall(tape, buf));
146 1.1 cgd }
147 1.1 cgd
148 1.1 cgd void
149 1.1 cgd rmtclose()
150 1.1 cgd {
151 1.1 cgd
152 1.1 cgd if (rmtstate != TS_OPEN)
153 1.1 cgd return;
154 1.1 cgd rmtcall("close", "C\n");
155 1.1 cgd rmtstate = TS_CLOSED;
156 1.1 cgd }
157 1.1 cgd
158 1.1 cgd int
159 1.1 cgd rmtread(buf, count)
160 1.1 cgd char *buf;
161 1.1 cgd int count;
162 1.1 cgd {
163 1.1 cgd char line[30];
164 1.1 cgd int n, i, cc;
165 1.1 cgd extern errno;
166 1.1 cgd
167 1.1 cgd (void)sprintf(line, "R%d\n", count);
168 1.1 cgd n = rmtcall("read", line);
169 1.1 cgd if (n < 0) {
170 1.1 cgd errno = n;
171 1.1 cgd return (-1);
172 1.1 cgd }
173 1.1 cgd for (i = 0; i < n; i += cc) {
174 1.1 cgd cc = read(rmtape, buf+i, n - i);
175 1.1 cgd if (cc <= 0) {
176 1.1 cgd rmtconnaborted();
177 1.1 cgd }
178 1.1 cgd }
179 1.1 cgd return (n);
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd int
183 1.1 cgd rmtwrite(buf, count)
184 1.1 cgd char *buf;
185 1.1 cgd int count;
186 1.1 cgd {
187 1.1 cgd char line[30];
188 1.1 cgd
189 1.1 cgd (void)sprintf(line, "W%d\n", count);
190 1.1 cgd write(rmtape, line, strlen(line));
191 1.1 cgd write(rmtape, buf, count);
192 1.1 cgd return (rmtreply("write"));
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd void
196 1.1 cgd rmtwrite0(count)
197 1.1 cgd int count;
198 1.1 cgd {
199 1.1 cgd char line[30];
200 1.1 cgd
201 1.1 cgd (void)sprintf(line, "W%d\n", count);
202 1.1 cgd write(rmtape, line, strlen(line));
203 1.1 cgd }
204 1.1 cgd
205 1.1 cgd void
206 1.1 cgd rmtwrite1(buf, count)
207 1.1 cgd char *buf;
208 1.1 cgd int count;
209 1.1 cgd {
210 1.1 cgd
211 1.1 cgd write(rmtape, buf, count);
212 1.1 cgd }
213 1.1 cgd
214 1.1 cgd int
215 1.1 cgd rmtwrite2()
216 1.1 cgd {
217 1.1 cgd
218 1.1 cgd return (rmtreply("write"));
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd int
222 1.1 cgd rmtseek(offset, pos)
223 1.1 cgd int offset, pos;
224 1.1 cgd {
225 1.1 cgd char line[80];
226 1.1 cgd
227 1.1 cgd (void)sprintf(line, "L%d\n%d\n", offset, pos);
228 1.1 cgd return (rmtcall("seek", line));
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd struct mtget mts;
232 1.1 cgd
233 1.1 cgd struct mtget *
234 1.1 cgd rmtstatus()
235 1.1 cgd {
236 1.1 cgd register int i;
237 1.1 cgd register char *cp;
238 1.1 cgd
239 1.1 cgd if (rmtstate != TS_OPEN)
240 1.1 cgd return (0);
241 1.1 cgd rmtcall("status", "S\n");
242 1.1 cgd for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
243 1.1 cgd *cp++ = rmtgetb();
244 1.1 cgd return (&mts);
245 1.1 cgd }
246 1.1 cgd
247 1.1 cgd int
248 1.1 cgd rmtioctl(cmd, count)
249 1.1 cgd int cmd, count;
250 1.1 cgd {
251 1.1 cgd char buf[256];
252 1.1 cgd
253 1.1 cgd if (count < 0)
254 1.1 cgd return (-1);
255 1.1 cgd (void)sprintf(buf, "I%d\n%d\n", cmd, count);
256 1.1 cgd return (rmtcall("ioctl", buf));
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd int
260 1.1 cgd rmtcall(cmd, buf)
261 1.1 cgd char *cmd, *buf;
262 1.1 cgd {
263 1.1 cgd
264 1.1 cgd if (write(rmtape, buf, strlen(buf)) != strlen(buf))
265 1.1 cgd rmtconnaborted();
266 1.1 cgd return (rmtreply(cmd));
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd int
270 1.1 cgd rmtreply(cmd)
271 1.1 cgd char *cmd;
272 1.1 cgd {
273 1.1 cgd char code[30], emsg[BUFSIZ];
274 1.1 cgd
275 1.1 cgd rmtgets(code, sizeof (code));
276 1.1 cgd if (*code == 'E' || *code == 'F') {
277 1.1 cgd rmtgets(emsg, sizeof (emsg));
278 1.1 cgd msg("%s: %s\n", cmd, emsg, code + 1);
279 1.1 cgd if (*code == 'F') {
280 1.1 cgd rmtstate = TS_CLOSED;
281 1.1 cgd return (-1);
282 1.1 cgd }
283 1.1 cgd return (-1);
284 1.1 cgd }
285 1.1 cgd if (*code != 'A') {
286 1.1 cgd msg("Protocol to remote tape server botched (code %s?).\n",
287 1.1 cgd code);
288 1.1 cgd rmtconnaborted();
289 1.1 cgd }
290 1.1 cgd return (atoi(code + 1));
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd int
294 1.1 cgd rmtgetb()
295 1.1 cgd {
296 1.1 cgd char c;
297 1.1 cgd
298 1.1 cgd if (read(rmtape, &c, 1) != 1)
299 1.1 cgd rmtconnaborted();
300 1.1 cgd return (c);
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd void
304 1.1 cgd rmtgets(cp, len)
305 1.1 cgd char *cp;
306 1.1 cgd int len;
307 1.1 cgd {
308 1.1 cgd
309 1.1 cgd while (len > 1) {
310 1.1 cgd *cp = rmtgetb();
311 1.1 cgd if (*cp == '\n') {
312 1.1 cgd cp[1] = 0;
313 1.1 cgd return;
314 1.1 cgd }
315 1.1 cgd cp++;
316 1.1 cgd len--;
317 1.1 cgd }
318 1.1 cgd msg("Protocol to remote tape server botched (in rmtgets).\n");
319 1.1 cgd rmtconnaborted();
320 1.1 cgd }
321