]> git.wh0rd.org - dump.git/blame_incremental - common/dumprmt.c
Fix the treatment for an interrupt signal when dump access the remote tape through...
[dump.git] / common / dumprmt.c
... / ...
CommitLineData
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
6 */
7
8/*-
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#ifndef lint
42static const char rcsid[] =
43 "$Id: dumprmt.c,v 1.9 2000/01/11 12:33:44 tiniou Exp $";
44#endif /* not lint */
45
46#ifdef __linux__
47#include <sys/types.h>
48#include <linux/types.h>
49#endif
50#include <sys/param.h>
51#include <sys/mtio.h>
52#include <sys/socket.h>
53#include <sys/time.h>
54#ifdef __linux__
55#include <linux/ext2_fs.h>
56#include <bsdcompat.h>
57#include <signal.h>
58#else
59#ifdef sunos
60#include <sys/vnode.h>
61
62#include <ufs/inode.h>
63#else
64#include <ufs/ufs/dinode.h>
65#endif
66#endif
67
68#include <netinet/in.h>
69#include <netinet/in_systm.h>
70#include <netinet/ip.h>
71#include <netinet/tcp.h>
72
73#include <protocols/dumprestore.h>
74
75#include <ctype.h>
76#include <errno.h>
77#include <compaterr.h>
78#include <netdb.h>
79#include <pwd.h>
80#include <stdio.h>
81#ifdef __STDC__
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85#endif
86
87#ifdef __linux__
88#include <ext2fs/ext2fs.h>
89#endif
90
91#include "pathnames.h"
92#include "dump.h"
93
94#define TS_CLOSED 0
95#define TS_OPEN 1
96
97static int rmtstate = TS_CLOSED;
98static int tormtape = -1;
99static int fromrmtape = -1;
100int rshpid = -1;
101static const char *rmtpeer = 0;
102
103static int okname __P((const char *));
104static int rmtcall __P((const char *, const char *));
105static void rmtconnaborted __P((int));
106static int rmtgetb __P((void));
107static int rmtgetconn __P((void));
108static void rmtgets __P((char *, size_t));
109static int rmtreply __P((const char *));
110static int piped_child __P((const char **command));
111#ifdef KERBEROS
112int krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
113#endif
114
115static int errfd = -1;
116extern int dokerberos;
117extern int ntrec; /* blocking factor on tape */
118#ifndef errno
119extern int errno;
120#endif
121
122int
123rmthost(const char *host)
124{
125 if (rmtpeer)
126 free((void *)rmtpeer);
127 if ((rmtpeer = strdup(host)) == NULL)
128 rmtpeer = host;
129 signal(SIGPIPE, rmtconnaborted);
130 return rmtgetconn();
131}
132
133static void
134rmtconnaborted(int signo)
135{
136 msg("Lost connection to remote host.\n");
137 if (errfd != -1) {
138 fd_set r;
139 struct timeval t;
140
141 FD_ZERO(&r);
142 FD_SET(errfd, &r);
143 t.tv_sec = 0;
144 t.tv_usec = 0;
145 if (select(errfd + 1, &r, NULL, NULL, &t)) {
146 int i;
147 char buf[2048];
148
149 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
150 buf[i] = '\0';
151 msg("on %s: %s%s", rmtpeer, buf,
152 buf[i - 1] == '\n' ? "" : "\n");
153 }
154 }
155 }
156
157 exit(X_ABORT);
158}
159
160static int
161rmtgetconn(void)
162{
163 register char *cp;
164 register const char *rmt;
165 static struct servent *sp = NULL;
166 static struct passwd *pwd = NULL;
167 const char *tuser;
168 const char *rsh;
169 int size;
170 int throughput;
171 int on;
172
173 rsh = getenv("RSH");
174
175 if (!rsh && sp == NULL) {
176 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
177 if (sp == NULL)
178 errx(1, "%s/tcp: unknown service",
179 dokerberos ? "kshell" : "shell");
180 }
181 if (pwd == NULL) {
182 pwd = getpwuid(getuid());
183 if (pwd == NULL)
184 errx(1, "who are you?");
185 }
186 if ((cp = strchr(rmtpeer, '@')) != NULL) {
187 tuser = rmtpeer;
188 *cp = '\0';
189 if (!okname(tuser))
190 exit(X_STARTUP);
191 rmtpeer = ++cp;
192 } else
193 tuser = pwd->pw_name;
194 if ((rmt = getenv("RMT")) == NULL)
195 rmt = _PATH_RMT;
196 msg("");
197
198 if (rsh) {
199 const char *rshcmd[6];
200 rshcmd[0] = rsh;
201 rshcmd[1] = rmtpeer;
202 rshcmd[2] = "-l";
203 rshcmd[3] = tuser;
204 rshcmd[4] = rmt;
205 rshcmd[5] = NULL;
206
207 if ((rshpid = piped_child(rshcmd)) < 0) {
208 msg("cannot open connection\n");
209 return 0;
210 }
211 }
212 else {
213#ifdef KERBEROS
214 if (dokerberos)
215 tormtape = krcmd((char **)&rmtpeer, sp->s_port, tuser, rmt, &errfd,
216 (char *)0);
217 else
218#endif
219 tormtape = rcmd((char **)&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
220 tuser, rmt, &errfd);
221 if (tormtape < 0) {
222 msg("login to %s as %s failed.\n", rmtpeer, tuser);
223 return 0;
224 }
225 size = ntrec * TP_BSIZE;
226 if (size > 60 * 1024) /* XXX */
227 size = 60 * 1024;
228 /* Leave some space for rmt request/response protocol */
229 size += 2 * 1024;
230 while (size > TP_BSIZE &&
231 setsockopt(tormtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
232 size -= TP_BSIZE;
233 (void)setsockopt(tormtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
234 throughput = IPTOS_THROUGHPUT;
235 if (setsockopt(tormtape, IPPROTO_IP, IP_TOS,
236 &throughput, sizeof(throughput)) < 0)
237 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
238 on = 1;
239 if (setsockopt(tormtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
240 perror("TCP_NODELAY setsockopt");
241 fromrmtape = tormtape;
242 }
243 (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
244 return 1;
245}
246
247static int
248okname(const char *cp0)
249{
250 register const char *cp;
251 register int c;
252
253 for (cp = cp0; *cp; cp++) {
254 c = *cp;
255 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
256 warnx("invalid user name %s\n", cp0);
257 return (0);
258 }
259 }
260 return (1);
261}
262
263int
264rmtopen(const char *tape, int mode)
265{
266 char buf[MAXPATHLEN];
267
268 (void)snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
269 rmtstate = TS_OPEN;
270 return (rmtcall(tape, buf));
271}
272
273void
274rmtclose(void)
275{
276
277 if (rmtstate != TS_OPEN)
278 return;
279 rmtcall("close", "C\n");
280 rmtstate = TS_CLOSED;
281}
282
283int
284rmtread(char *buf, size_t count)
285{
286 char line[30];
287 int n, i;
288 ssize_t cc;
289
290 (void)snprintf(line, sizeof (line), "R%u\n", (unsigned)count);
291 n = rmtcall("read", line);
292 if (n < 0)
293 /* rmtcall() properly sets errno for us on errors. */
294 return (n);
295 for (i = 0; i < n; i += cc) {
296 cc = read(fromrmtape, buf+i, n - i);
297 if (cc <= 0)
298 rmtconnaborted(0);
299 }
300 return (n);
301}
302
303int
304rmtwrite(const char *buf, size_t count)
305{
306 char line[30];
307
308 (void)snprintf(line, sizeof (line), "W%d\n", count);
309 write(tormtape, line, strlen(line));
310 write(tormtape, buf, count);
311 return (rmtreply("write"));
312}
313
314int
315rmtseek(int offset, int pos)
316{
317 char line[80];
318
319 (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
320 return (rmtcall("seek", line));
321}
322
323struct mtget mts;
324
325struct mtget *
326rmtstatus(void)
327{
328 register int i;
329 register char *cp;
330
331 if (rmtstate != TS_OPEN)
332 return (NULL);
333 rmtcall("status", "S\n");
334 for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
335 *cp++ = rmtgetb();
336 return (&mts);
337}
338
339int
340rmtioctl(int cmd, int count)
341{
342 char buf[256];
343
344 if (count < 0)
345 return (-1);
346 (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
347 return (rmtcall("ioctl", buf));
348}
349
350static int
351rmtcall(const char *cmd, const char *buf)
352{
353
354 if (write(tormtape, buf, strlen(buf)) != strlen(buf))
355 rmtconnaborted(0);
356 return (rmtreply(cmd));
357}
358
359static int
360rmtreply(const char *cmd)
361{
362 register char *cp;
363 char code[30], emsg[BUFSIZ];
364
365 rmtgets(code, sizeof (code));
366 if (*code == 'E' || *code == 'F') {
367 rmtgets(emsg, sizeof (emsg));
368 msg("%s: %s", cmd, emsg);
369 errno = atoi(code + 1);
370 if (*code == 'F')
371 rmtstate = TS_CLOSED;
372 return (-1);
373 }
374 if (*code != 'A') {
375 /* Kill trailing newline */
376 cp = code + strlen(code);
377 if (cp > code && *--cp == '\n')
378 *cp = '\0';
379
380 msg("Protocol to remote tape server botched (code \"%s\").\n",
381 code);
382 rmtconnaborted(0);
383 }
384 return (atoi(code + 1));
385}
386
387static int
388rmtgetb(void)
389{
390 char c;
391
392 if (read(fromrmtape, &c, 1) != 1)
393 rmtconnaborted(0);
394 return (c);
395}
396
397/* Get a line (guaranteed to have a trailing newline). */
398static void
399rmtgets(char *line, size_t len)
400{
401 register char *cp = line;
402
403 while (len > 1) {
404 *cp = rmtgetb();
405 if (*cp == '\n') {
406 cp[1] = '\0';
407 return;
408 }
409 cp++;
410 len--;
411 }
412 *cp = '\0';
413 msg("Protocol to remote tape server botched.\n");
414 msg("(rmtgets got \"%s\").\n", line);
415 rmtconnaborted(0);
416}
417
418int piped_child(const char **command) {
419 int pid;
420 int to_child_pipe[2];
421 int from_child_pipe[2];
422
423 if (pipe (to_child_pipe) < 0) {
424 msg ("cannot create pipe: %s\n", strerror(errno));
425 return -1;
426 }
427 if (pipe (from_child_pipe) < 0) {
428 msg ("cannot create pipe: %s\n", strerror(errno));
429 return -1;
430 }
431 pid = fork ();
432 if (pid < 0) {
433 msg ("cannot fork: %s\n", strerror(errno));
434 return -1;
435 }
436 if (pid == 0) {
437 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
438 msg ("cannot dup2 pipe: %s\n", strerror(errno));
439 exit(1);
440 }
441 if (close (to_child_pipe[1]) < 0) {
442 msg ("cannot close pipe: %s\n", strerror(errno));
443 exit(1);
444 }
445 if (close (from_child_pipe[0]) < 0) {
446 msg ("cannot close pipe: %s\n", strerror(errno));
447 exit(1);
448 }
449 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
450 msg ("cannot dup2 pipe: %s\n", strerror(errno));
451 exit(1);
452 }
453 setpgid(0, getpid());
454 execvp (command[0], (char *const *) command);
455 msg("cannot exec %s: %s\n", command[0], strerror(errno));
456 exit(1);
457 }
458 if (close (to_child_pipe[0]) < 0) {
459 msg ("cannot close pipe: %s\n", strerror(errno));
460 return -1;
461 }
462 if (close (from_child_pipe[1]) < 0) {
463 msg ("cannot close pipe: %s\n", strerror(errno));
464 return -1;
465 }
466 tormtape = to_child_pipe[1];
467 fromrmtape = from_child_pipe[0];
468 return pid;
469}