1 /* $NetBSD: dumprmt.c,v 1.32 2006/12/18 20:07:32 christos Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)dumprmt.c 8.3 (Berkeley) 4/28/95";
37 __RCSID("$NetBSD: dumprmt.c,v 1.32 2006/12/18 20:07:32 christos Exp $");
41 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.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>
66 #include "pathnames.h"
72 static int rmtstate
= TS_CLOSED
;
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 */
89 rmthost(const char *host
)
92 if ((rmtpeer
= strdup(host
)) == NULL
)
93 err(X_STARTUP
, "strdup");
94 signal(SIGPIPE
, rmtconnaborted
);
102 rmtconnaborted(int dummy __unused
)
105 errx(X_ABORT
, "Lost connection to remote host.");
112 static struct servent
*sp
= NULL
;
113 static struct passwd
*pwd
= NULL
;
118 sp
= getservbyname("shell", "tcp");
120 errx(X_STARTUP
, "shell/tcp: unknown service");
121 pwd
= getpwuid(getuid());
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
) {
136 rmtape
= rcmd(&rmtpeer
, (u_short
)sp
->s_port
, name
, tuser
, _PATH_RMT
,
142 size
= ntrec
* TP_BSIZE
;
143 if (size
> 60 * 1024) /* XXX */
145 /* Leave some space for rmt request/response protocol */
147 while (size
> TP_BSIZE
&&
148 setsockopt(rmtape
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof (size
)) < 0)
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
));
156 (void)setsockopt(rmtape
, IPPROTO_TCP
, TCP_NODELAY
, &opt
, sizeof (opt
));
165 for (cp
= cp0
; *cp
; cp
++) {
167 if (!isascii(c
) || !(isalnum(c
) || c
== '_' || c
== '-')) {
168 warnx("invalid user name: %s", cp0
);
176 rmtopen(const char *tapedevice
, int mode
, int verbose
)
180 (void)snprintf(buf
, sizeof buf
, "O%s\n%d\n", tapedevice
, mode
);
182 return (rmtcall(tapedevice
, buf
, verbose
));
189 if (rmtstate
!= TS_OPEN
)
191 rmtcall("close", "C\n", 1);
192 rmtstate
= TS_CLOSED
;
196 rmtread(char *buf
, int count
)
201 (void)snprintf(line
, sizeof line
, "R%d\n", count
);
202 n
= rmtcall("read", line
, 1);
204 /* rmtcall() properly sets errno for us on errors. */
207 for (i
= 0; i
< n
; i
+= cc
) {
208 cc
= read(rmtape
, buf
+i
, n
- i
);
217 rmtwrite(const char *buf
, int count
)
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? */
233 (void)snprintf(line
, sizeof line
, "W%d\n", count
);
234 write(rmtape
, line
, strlen(line
));
238 rmtwrite1(char *buf
, int count
)
241 write(rmtape
, buf
, count
);
248 return (rmtreply("write", 1));
253 rmtseek(int offset
, int pos
)
257 (void)snprintf(line
, sizeof line
, "L%d\n%d\n", offset
, pos
);
258 return (rmtcall("seek", line
, 1));
262 #if 0 /* XXX unused? */
270 if (rmtstate
!= TS_OPEN
)
272 rmtcall("status", "S\n", 1);
273 for (i
= 0, cp
= (char *)&mts
; i
< sizeof(mts
); i
++)
280 rmtioctl(int cmd
, int count
)
286 (void)snprintf(buf
, sizeof buf
, "I%d\n%d\n", cmd
, count
);
287 return (rmtcall("ioctl", buf
, 1));
291 rmtcall(const char *cmd
, const char *buf
, int verbose
)
294 if ((size_t)write(rmtape
, buf
, strlen(buf
)) != strlen(buf
))
296 return (rmtreply(cmd
, verbose
));
300 rmtreply(const char *cmd
, int verbose
)
303 char code
[30], emsg
[BUFSIZ
];
305 rmtgets(code
, sizeof (code
));
306 if (*code
== 'E' || *code
== 'F') {
307 rmtgets(emsg
, sizeof (emsg
));
309 msg("%s: %s", cmd
, emsg
);
310 errno
= atoi(code
+ 1);
312 rmtstate
= TS_CLOSED
;
316 /* Kill trailing newline */
317 cp
= code
+ strlen(code
);
318 if (cp
> code
&& *--cp
== '\n')
321 msg("Protocol to remote tape server botched (code \"%s\").\n",
325 return (atoi(code
+ 1));
333 if (read(rmtape
, &c
, 1) != 1)
338 /* Get a line (guaranteed to have a trailing newline). */
340 rmtgets(char *line
, int len
)
354 msg("Protocol to remote tape server botched.\n");
355 msg("(rmtgets got \"%s\").\n", line
);