Commit | Line | Data |
---|---|---|
461723e7 KM |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * %sccs.include.redist.c% | |
76797561 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
e9345f05 | 9 | static char sccsid[] = "@(#)dumprmt.c 5.15 (Berkeley) %G%"; |
36783b71 | 10 | #endif /* not lint */ |
e23a0a9f | 11 | |
cdce5e6c KM |
12 | #ifdef sunos |
13 | #include <stdio.h> | |
14 | #include <ctype.h> | |
15 | #include <sys/param.h> | |
16 | #include <sys/mtio.h> | |
17 | #include <sys/ioctl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <sys/stat.h> | |
cdce5e6c | 20 | #else |
a920c801 | 21 | #include <sys/param.h> |
e23a0a9f SL |
22 | #include <sys/mtio.h> |
23 | #include <sys/ioctl.h> | |
4f411ff9 | 24 | #include <sys/socket.h> |
903c2c62 | 25 | #include <sys/time.h> |
cdce5e6c KM |
26 | #include <stdio.h> |
27 | #endif | |
e9345f05 | 28 | #include <ufs/ufs/dinode.h> |
13298603 | 29 | #include <signal.h> |
ba8a6acf SL |
30 | |
31 | #include <netinet/in.h> | |
32 | ||
ba8a6acf | 33 | #include <netdb.h> |
29df8dcd | 34 | #include <protocols/dumprestore.h> |
7abf8d65 | 35 | #include <pwd.h> |
13298603 KB |
36 | #ifdef __STDC__ |
37 | #include <unistd.h> | |
36783b71 CT |
38 | #include <stdlib.h> |
39 | #include <string.h> | |
13298603 | 40 | #endif |
7abf8d65 | 41 | #include "pathnames.h" |
e23a0a9f SL |
42 | |
43 | #define TS_CLOSED 0 | |
44 | #define TS_OPEN 1 | |
45 | ||
46 | static int rmtstate = TS_CLOSED; | |
47 | int rmtape; | |
36783b71 CT |
48 | void rmtgetconn(); |
49 | void rmtconnaborted(); | |
50 | int rmtreply(); | |
51 | int rmtgetb(); | |
52 | void rmtgets(); | |
53 | int rmtcall(); | |
e23a0a9f SL |
54 | char *rmtpeer; |
55 | ||
4f411ff9 | 56 | extern int ntrec; /* blocking factor on tape */ |
36783b71 | 57 | extern void msg(); |
4f411ff9 | 58 | |
e9345f05 KM |
59 | extern void exit(); |
60 | ||
36783b71 | 61 | int |
e23a0a9f SL |
62 | rmthost(host) |
63 | char *host; | |
64 | { | |
65 | ||
66 | rmtpeer = host; | |
23eeaca6 | 67 | signal(SIGPIPE, rmtconnaborted); |
e23a0a9f SL |
68 | rmtgetconn(); |
69 | if (rmtape < 0) | |
4f411ff9 RC |
70 | return (0); |
71 | return (1); | |
e23a0a9f SL |
72 | } |
73 | ||
36783b71 | 74 | void |
e23a0a9f SL |
75 | rmtconnaborted() |
76 | { | |
77 | ||
e9345f05 KM |
78 | (void) fprintf(stderr, "rdump: Lost connection to remote host.\n"); |
79 | (void) exit(1); | |
e23a0a9f SL |
80 | } |
81 | ||
36783b71 | 82 | void |
e23a0a9f SL |
83 | rmtgetconn() |
84 | { | |
ba8a6acf | 85 | static struct servent *sp = 0; |
eb34204a MK |
86 | struct passwd *pw; |
87 | char *name = "root"; | |
4f411ff9 | 88 | int size; |
e23a0a9f | 89 | |
ba8a6acf SL |
90 | if (sp == 0) { |
91 | sp = getservbyname("shell", "tcp"); | |
92 | if (sp == 0) { | |
e9345f05 KM |
93 | (void) fprintf(stderr, |
94 | "rdump: shell/tcp: unknown service\n"); | |
95 | (void) exit(1); | |
ba8a6acf SL |
96 | } |
97 | } | |
eb34204a MK |
98 | pw = getpwuid(getuid()); |
99 | if (pw && pw->pw_name) | |
100 | name = pw->pw_name; | |
e9345f05 KM |
101 | rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, name, _PATH_RMT, |
102 | (int *)0); | |
4f411ff9 | 103 | size = ntrec * TP_BSIZE; |
43c1836c KM |
104 | while (size > TP_BSIZE && |
105 | setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0) | |
106 | size -= TP_BSIZE; | |
e23a0a9f SL |
107 | } |
108 | ||
36783b71 | 109 | int |
e23a0a9f SL |
110 | rmtopen(tape, mode) |
111 | char *tape; | |
112 | int mode; | |
113 | { | |
114 | char buf[256]; | |
115 | ||
9bd38ba8 | 116 | (void)sprintf(buf, "O%s\n%d\n", tape, mode); |
e23a0a9f | 117 | rmtstate = TS_OPEN; |
f17cc02e | 118 | return (rmtcall(tape, buf)); |
e23a0a9f SL |
119 | } |
120 | ||
36783b71 | 121 | void |
e23a0a9f SL |
122 | rmtclose() |
123 | { | |
124 | ||
125 | if (rmtstate != TS_OPEN) | |
126 | return; | |
127 | rmtcall("close", "C\n"); | |
128 | rmtstate = TS_CLOSED; | |
129 | } | |
130 | ||
36783b71 | 131 | int |
e23a0a9f SL |
132 | rmtread(buf, count) |
133 | char *buf; | |
134 | int count; | |
135 | { | |
136 | char line[30]; | |
137 | int n, i, cc; | |
a920c801 | 138 | extern errno; |
e23a0a9f | 139 | |
9bd38ba8 | 140 | (void)sprintf(line, "R%d\n", count); |
e23a0a9f | 141 | n = rmtcall("read", line); |
a920c801 SL |
142 | if (n < 0) { |
143 | errno = n; | |
e23a0a9f | 144 | return (-1); |
a920c801 | 145 | } |
e23a0a9f SL |
146 | for (i = 0; i < n; i += cc) { |
147 | cc = read(rmtape, buf+i, n - i); | |
a920c801 | 148 | if (cc <= 0) { |
e23a0a9f | 149 | rmtconnaborted(); |
a920c801 | 150 | } |
e23a0a9f SL |
151 | } |
152 | return (n); | |
153 | } | |
154 | ||
36783b71 | 155 | int |
e23a0a9f SL |
156 | rmtwrite(buf, count) |
157 | char *buf; | |
158 | int count; | |
159 | { | |
160 | char line[30]; | |
161 | ||
9bd38ba8 | 162 | (void)sprintf(line, "W%d\n", count); |
e23a0a9f SL |
163 | write(rmtape, line, strlen(line)); |
164 | write(rmtape, buf, count); | |
165 | return (rmtreply("write")); | |
166 | } | |
167 | ||
36783b71 | 168 | void |
e23a0a9f SL |
169 | rmtwrite0(count) |
170 | int count; | |
171 | { | |
172 | char line[30]; | |
173 | ||
9bd38ba8 | 174 | (void)sprintf(line, "W%d\n", count); |
e23a0a9f SL |
175 | write(rmtape, line, strlen(line)); |
176 | } | |
177 | ||
36783b71 | 178 | void |
e23a0a9f SL |
179 | rmtwrite1(buf, count) |
180 | char *buf; | |
181 | int count; | |
182 | { | |
183 | ||
184 | write(rmtape, buf, count); | |
185 | } | |
186 | ||
36783b71 | 187 | int |
e23a0a9f SL |
188 | rmtwrite2() |
189 | { | |
e23a0a9f SL |
190 | |
191 | return (rmtreply("write")); | |
192 | } | |
193 | ||
36783b71 | 194 | int |
e23a0a9f SL |
195 | rmtseek(offset, pos) |
196 | int offset, pos; | |
197 | { | |
198 | char line[80]; | |
199 | ||
9bd38ba8 | 200 | (void)sprintf(line, "L%d\n%d\n", offset, pos); |
e23a0a9f SL |
201 | return (rmtcall("seek", line)); |
202 | } | |
203 | ||
204 | struct mtget mts; | |
205 | ||
206 | struct mtget * | |
207 | rmtstatus() | |
208 | { | |
209 | register int i; | |
210 | register char *cp; | |
211 | ||
212 | if (rmtstate != TS_OPEN) | |
213 | return (0); | |
214 | rmtcall("status", "S\n"); | |
215 | for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++) | |
216 | *cp++ = rmtgetb(); | |
217 | return (&mts); | |
218 | } | |
219 | ||
36783b71 | 220 | int |
e23a0a9f SL |
221 | rmtioctl(cmd, count) |
222 | int cmd, count; | |
223 | { | |
224 | char buf[256]; | |
225 | ||
226 | if (count < 0) | |
227 | return (-1); | |
9bd38ba8 | 228 | (void)sprintf(buf, "I%d\n%d\n", cmd, count); |
1ab82222 | 229 | return (rmtcall("ioctl", buf)); |
e23a0a9f SL |
230 | } |
231 | ||
36783b71 | 232 | int |
e23a0a9f SL |
233 | rmtcall(cmd, buf) |
234 | char *cmd, *buf; | |
235 | { | |
236 | ||
237 | if (write(rmtape, buf, strlen(buf)) != strlen(buf)) | |
238 | rmtconnaborted(); | |
239 | return (rmtreply(cmd)); | |
240 | } | |
241 | ||
36783b71 | 242 | int |
e23a0a9f SL |
243 | rmtreply(cmd) |
244 | char *cmd; | |
245 | { | |
e23a0a9f SL |
246 | char code[30], emsg[BUFSIZ]; |
247 | ||
248 | rmtgets(code, sizeof (code)); | |
249 | if (*code == 'E' || *code == 'F') { | |
250 | rmtgets(emsg, sizeof (emsg)); | |
251 | msg("%s: %s\n", cmd, emsg, code + 1); | |
252 | if (*code == 'F') { | |
253 | rmtstate = TS_CLOSED; | |
254 | return (-1); | |
255 | } | |
256 | return (-1); | |
257 | } | |
258 | if (*code != 'A') { | |
259 | msg("Protocol to remote tape server botched (code %s?).\n", | |
260 | code); | |
261 | rmtconnaborted(); | |
262 | } | |
263 | return (atoi(code + 1)); | |
264 | } | |
265 | ||
36783b71 | 266 | int |
e23a0a9f SL |
267 | rmtgetb() |
268 | { | |
269 | char c; | |
270 | ||
271 | if (read(rmtape, &c, 1) != 1) | |
272 | rmtconnaborted(); | |
273 | return (c); | |
274 | } | |
275 | ||
36783b71 | 276 | void |
e23a0a9f SL |
277 | rmtgets(cp, len) |
278 | char *cp; | |
279 | int len; | |
280 | { | |
281 | ||
282 | while (len > 1) { | |
283 | *cp = rmtgetb(); | |
284 | if (*cp == '\n') { | |
285 | cp[1] = 0; | |
286 | return; | |
287 | } | |
288 | cp++; | |
289 | len--; | |
290 | } | |
291 | msg("Protocol to remote tape server botched (in rmtgets).\n"); | |
292 | rmtconnaborted(); | |
293 | } |