No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / dump / dumprmt.c
blobe31a569888f768d3834fdeda69c58839496dfd31
1 /* $NetBSD: dumprmt.c,v 1.32 2006/12/18 20:07:32 christos Exp $ */
3 /*-
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: dumprmt.c,v 1.32 2006/12/18 20:07:32 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/param.h>
42 #include <sys/mtio.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #include <sys/time.h>
46 #include <ufs/ufs/dinode.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/tcp.h>
53 #include <protocols/dumprestore.h>
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <pwd.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
66 #include "pathnames.h"
67 #include "dump.h"
69 #define TS_CLOSED 0
70 #define TS_OPEN 1
72 static int rmtstate = TS_CLOSED;
73 static int rmtape;
74 static char *rmtpeer;
76 static int okname(char *);
77 static int rmtcall(const char *, const char *, int);
78 static void rmtconnaborted(int);
79 static int rmtgetb(void);
80 static void rmtgetconn(void);
81 static void rmtgets(char *, int);
82 int rmtread(char *, int);
83 static int rmtreply(const char *, int);
84 int rmtseek(int, int);
86 extern int ntrec; /* blocking factor on tape */
88 int
89 rmthost(const char *host)
92 if ((rmtpeer = strdup(host)) == NULL)
93 err(X_STARTUP, "strdup");
94 signal(SIGPIPE, rmtconnaborted);
95 rmtgetconn();
96 if (rmtape < 0)
97 return (0);
98 return (1);
101 static void
102 rmtconnaborted(int dummy __unused)
105 errx(X_ABORT, "Lost connection to remote host.");
108 void
109 rmtgetconn(void)
111 char *cp;
112 static struct servent *sp = NULL;
113 static struct passwd *pwd = NULL;
114 char *tuser, *name;
115 int size, opt;
117 if (sp == NULL) {
118 sp = getservbyname("shell", "tcp");
119 if (sp == NULL)
120 errx(X_STARTUP, "shell/tcp: unknown service");
121 pwd = getpwuid(getuid());
122 if (pwd == NULL)
123 errx(X_STARTUP, "who are you?");
125 if ((name = strdup(pwd->pw_name)) == NULL)
126 err(X_STARTUP, "strdup");
127 if ((cp = strchr(rmtpeer, '@')) != NULL) {
128 tuser = rmtpeer;
129 *cp = '\0';
130 if (!okname(tuser))
131 exit(X_STARTUP);
132 rmtpeer = ++cp;
133 } else
134 tuser = name;
136 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, tuser, _PATH_RMT,
137 (int *)0);
138 (void)free(name);
139 if (rmtape < 0)
140 return;
142 size = ntrec * TP_BSIZE;
143 if (size > 60 * 1024) /* XXX */
144 size = 60 * 1024;
145 /* Leave some space for rmt request/response protocol */
146 size += 2 * 1024;
147 while (size > TP_BSIZE &&
148 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
149 size -= TP_BSIZE;
150 (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
152 opt = IPTOS_THROUGHPUT;
153 (void)setsockopt(rmtape, IPPROTO_IP, IP_TOS, &opt, sizeof (opt));
155 opt = 1;
156 (void)setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof (opt));
159 static int
160 okname(char *cp0)
162 char *cp;
163 int c;
165 for (cp = cp0; *cp; cp++) {
166 c = *cp;
167 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
168 warnx("invalid user name: %s", cp0);
169 return (0);
172 return (1);
176 rmtopen(const char *tapedevice, int mode, int verbose)
178 char buf[256];
180 (void)snprintf(buf, sizeof buf, "O%s\n%d\n", tapedevice, mode);
181 rmtstate = TS_OPEN;
182 return (rmtcall(tapedevice, buf, verbose));
185 void
186 rmtclose(void)
189 if (rmtstate != TS_OPEN)
190 return;
191 rmtcall("close", "C\n", 1);
192 rmtstate = TS_CLOSED;
196 rmtread(char *buf, int count)
198 char line[30];
199 int n, i, cc;
201 (void)snprintf(line, sizeof line, "R%d\n", count);
202 n = rmtcall("read", line, 1);
203 if (n < 0) {
204 /* rmtcall() properly sets errno for us on errors. */
205 return (n);
207 for (i = 0; i < n; i += cc) {
208 cc = read(rmtape, buf+i, n - i);
209 if (cc <= 0) {
210 rmtconnaborted(0);
213 return (n);
217 rmtwrite(const char *buf, int count)
219 char line[30];
221 (void)snprintf(line, sizeof line, "W%d\n", count);
222 write(rmtape, line, strlen(line));
223 write(rmtape, buf, count);
224 return (rmtreply("write", 1));
227 #if 0 /* XXX unused? */
228 void
229 rmtwrite0(int count)
231 char line[30];
233 (void)snprintf(line, sizeof line, "W%d\n", count);
234 write(rmtape, line, strlen(line));
237 void
238 rmtwrite1(char *buf, int count)
241 write(rmtape, buf, count);
245 rmtwrite2(void)
248 return (rmtreply("write", 1));
250 #endif
253 rmtseek(int offset, int pos)
255 char line[80];
257 (void)snprintf(line, sizeof line, "L%d\n%d\n", offset, pos);
258 return (rmtcall("seek", line, 1));
262 #if 0 /* XXX unused? */
263 struct mtget *
264 rmtstatus(void)
266 struct mtget mts;
267 int i;
268 char *cp;
270 if (rmtstate != TS_OPEN)
271 return (NULL);
272 rmtcall("status", "S\n", 1);
273 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
274 *cp++ = rmtgetb();
275 return (&mts);
277 #endif
280 rmtioctl(int cmd, int count)
282 char buf[256];
284 if (count < 0)
285 return (-1);
286 (void)snprintf(buf, sizeof buf, "I%d\n%d\n", cmd, count);
287 return (rmtcall("ioctl", buf, 1));
290 static int
291 rmtcall(const char *cmd, const char *buf, int verbose)
294 if ((size_t)write(rmtape, buf, strlen(buf)) != strlen(buf))
295 rmtconnaborted(0);
296 return (rmtreply(cmd, verbose));
299 static int
300 rmtreply(const char *cmd, int verbose)
302 char *cp;
303 char code[30], emsg[BUFSIZ];
305 rmtgets(code, sizeof (code));
306 if (*code == 'E' || *code == 'F') {
307 rmtgets(emsg, sizeof (emsg));
308 if (verbose)
309 msg("%s: %s", cmd, emsg);
310 errno = atoi(code + 1);
311 if (*code == 'F')
312 rmtstate = TS_CLOSED;
313 return (-1);
315 if (*code != 'A') {
316 /* Kill trailing newline */
317 cp = code + strlen(code);
318 if (cp > code && *--cp == '\n')
319 *cp = '\0';
321 msg("Protocol to remote tape server botched (code \"%s\").\n",
322 code);
323 rmtconnaborted(0);
325 return (atoi(code + 1));
329 rmtgetb(void)
331 char c;
333 if (read(rmtape, &c, 1) != 1)
334 rmtconnaborted(0);
335 return (c);
338 /* Get a line (guaranteed to have a trailing newline). */
339 void
340 rmtgets(char *line, int len)
342 char *cp = line;
344 while (len > 1) {
345 *cp = rmtgetb();
346 if (*cp == '\n') {
347 cp[1] = '\0';
348 return;
350 cp++;
351 len--;
353 *cp = '\0';
354 msg("Protocol to remote tape server botched.\n");
355 msg("(rmtgets got \"%s\").\n", line);
356 rmtconnaborted(0);