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