xref: /original-bsd/sbin/dump/dumprmt.c (revision 9a96b58b)
1 static	char *sccsid = "@(#)dumprmt.c	1.1 (Berkeley) 05/07/82";
2 #include "dump.h"
3 
4 #include <sys/mtio.h>
5 #include <sys/ioctl.h>
6 #include <net/in.h>
7 
8 #define	TS_CLOSED	0
9 #define	TS_OPEN		1
10 
11 static	int rmtstate = TS_CLOSED;
12 int	rmtape;
13 int	rmtconnaborted();
14 char	*rmtpeer;
15 
16 rmthost(host)
17 	char *host;
18 {
19 
20 	rmtpeer = host;
21 	sigset(SIGPIPE, rmtconnaborted);
22 	rmtgetconn();
23 	if (rmtape < 0)
24 		rmtreconnect();
25 }
26 
27 rmtconnaborted()
28 {
29 
30 	msg("Lost connection to tape server.\n");
31 	if (rmtape >= 0) {
32 		close(rmtape);
33 		rmtape = -1;
34 	}
35 	exit(X_REWRITE);
36 }
37 
38 rmtreconnect()
39 {
40 
41 	do {
42 		if (query("Retry conection to remote host?") == 0)
43 			exit(X_ABORT);
44 		rmtgetconn();
45 	} while (rmtape < 0);
46 }
47 
48 rmtgetconn()
49 {
50 
51 	rmtape = rcmd(&rmtpeer, IPPORT_CMDSERVER,
52 	    "root", "root", "/etc/rmt", 0);
53 }
54 
55 rmtopen(tape, mode)
56 	char *tape;
57 	int mode;
58 {
59 	char buf[256];
60 
61 	sprintf(buf, "O%s\n%d\n", tape, mode);
62 	rmtcall(tape, buf);
63 	rmtstate = TS_OPEN;
64 }
65 
66 rmtclose()
67 {
68 
69 	if (rmtstate != TS_OPEN)
70 		return;
71 	rmtcall("close", "C\n");
72 	rmtstate = TS_CLOSED;
73 }
74 
75 rmtread(buf, count)
76 	char *buf;
77 	int count;
78 {
79 	char line[30];
80 	int n, i, cc;
81 
82 	sprintf(line, "R%d\n", count);
83 	n = rmtcall("read", line);
84 	if (n < 0)
85 		return (-1);
86 	for (i = 0; i < n; i += cc) {
87 		cc = read(rmtape, buf+i, n - i);
88 		if (cc <= 0)
89 			rmtconnaborted();
90 	}
91 	return (n);
92 }
93 
94 rmtwrite(buf, count)
95 	char *buf;
96 	int count;
97 {
98 	char line[30];
99 
100 	sprintf(line, "W%d\n", count);
101 	write(rmtape, line, strlen(line));
102 	write(rmtape, buf, count);
103 	return (rmtreply("write"));
104 }
105 
106 rmtwrite0(count)
107 	int count;
108 {
109 	char line[30];
110 
111 	sprintf(line, "W%d\n", count);
112 	write(rmtape, line, strlen(line));
113 }
114 
115 rmtwrite1(buf, count)
116 	char *buf;
117 	int count;
118 {
119 
120 	write(rmtape, buf, count);
121 }
122 
123 rmtwrite2()
124 {
125 	int i;
126 
127 	return (rmtreply("write"));
128 }
129 
130 rmtseek(offset, pos)
131 	int offset, pos;
132 {
133 	char line[80];
134 
135 	sprintf(line, "L%d\n%d\n", offset, pos);
136 	return (rmtcall("seek", line));
137 }
138 
139 struct	mtget mts;
140 
141 struct mtget *
142 rmtstatus()
143 {
144 	register int i;
145 	register char *cp;
146 
147 	if (rmtstate != TS_OPEN)
148 		return (0);
149 	rmtcall("status", "S\n");
150 	for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
151 		*cp++ = rmtgetb();
152 	return (&mts);
153 }
154 
155 rmtioctl(cmd, count)
156 	int cmd, count;
157 {
158 	char buf[256];
159 
160 	if (count < 0)
161 		return (-1);
162 	sprintf(buf, "I%d\n%d\n", cmd, count);
163 	rmtcall("ioctl", buf);
164 }
165 
166 rmtcall(cmd, buf)
167 	char *cmd, *buf;
168 {
169 
170 	if (write(rmtape, buf, strlen(buf)) != strlen(buf))
171 		rmtconnaborted();
172 	return (rmtreply(cmd));
173 }
174 
175 rmtreply(cmd)
176 	char *cmd;
177 {
178 	register int c;
179 	char code[30], emsg[BUFSIZ];
180 
181 	rmtgets(code, sizeof (code));
182 	if (*code == 'E' || *code == 'F') {
183 		rmtgets(emsg, sizeof (emsg));
184 		msg("%s: %s\n", cmd, emsg, code + 1);
185 		if (*code == 'F') {
186 			rmtstate = TS_CLOSED;
187 			return (-1);
188 		}
189 		return (-1);
190 	}
191 	if (*code != 'A') {
192 		msg("Protocol to remote tape server botched (code %s?).\n",
193 		    code);
194 		rmtconnaborted();
195 	}
196 	return (atoi(code + 1));
197 }
198 
199 rmtgetb()
200 {
201 	char c;
202 
203 	if (read(rmtape, &c, 1) != 1)
204 		rmtconnaborted();
205 	return (c);
206 }
207 
208 rmtgets(cp, len)
209 	char *cp;
210 	int len;
211 {
212 
213 	while (len > 1) {
214 		*cp = rmtgetb();
215 		if (*cp == '\n') {
216 			cp[1] = 0;
217 			return;
218 		}
219 		cp++;
220 		len--;
221 	}
222 	msg("Protocol to remote tape server botched (in rmtgets).\n");
223 	rmtconnaborted();
224 }
225