]>
Dogcows Code - chaz/tar/blob - src/rtapelib.c
1 /* Functions for communicating with a remote tape drive.
2 Copyright (C) 1988, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* The man page rmt(8) for /etc/rmt documents the remote mag tape
19 protocol which rdump and rrestore use. Unfortunately, the man
20 page is *WRONG*. The author of the routines I'm including originally
21 wrote his code just based on the man page, and it didn't work, so he
22 went to the rdump source to figure out why. The only thing he had to
23 change was to check for the 'F' return code in addition to the 'E',
24 and to separate the various arguments with \n instead of a space. I
25 personally don't think that this is much of a problem, but I wanted to
26 point it out. -- Arnold Robbins
28 Originally written by Jeff Lee, modified some by Arnold Robbins.
29 Redone as a library that can replace open, read, write, etc., by
30 Fred Fish, with some additional work by Arnold Robbins.
31 Modified to make all rmtXXX calls into macros for speed by Jay Fenlason.
32 Use -DUSE_REXEC for rexec code, courtesy of Dan Kegel, srs!dan. */
35 #include <sys/types.h>
39 #include <sys/ioctl.h>
55 /* Maximum size of a fully qualified host name. */
56 #define MAXHOSTLEN 257
58 /* Size of buffers for reading and writing commands to rmt.
59 (An arbitrary limit.) */
63 #define RETSIGTYPE void
66 /* Maximum number of simultaneous remote tape connections.
67 (Another arbitrary limit.) */
70 /* Return the parent's read side of remote tape connection FILDES. */
71 #define READ(fildes) (from_rmt[fildes][0])
73 /* Return the parent's write side of remote tape connection FILDES. */
74 #define WRITE(fildes) (to_rmt[fildes][1])
76 /* The pipes for receiving data from remote tape drives. */
77 static int from_rmt
[MAXUNIT
][2] = {-1, -1, -1, -1, -1, -1, -1, -1};
79 /* The pipes for sending data to remote tape drives. */
80 static int to_rmt
[MAXUNIT
][2] = {-1, -1, -1, -1, -1, -1, -1, -1};
82 /* Temporary variable used by macros in rmt.h. */
85 /* Close remote tape connection FILDES. */
88 _rmt_shutdown (fildes
)
91 close (READ (fildes
));
92 close (WRITE (fildes
));
97 /* Attempt to perform the remote tape command specified in BUF
98 on remote tape connection FILDES.
99 Return 0 if successful, -1 on error. */
102 command (fildes
, buf
)
107 RETSIGTYPE (*pipe_handler
) ();
109 /* Save the current pipe handler and try to make the request. */
111 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
112 buflen
= strlen (buf
);
113 if (write (WRITE (fildes
), buf
, buflen
) == buflen
)
115 signal (SIGPIPE
, pipe_handler
);
119 /* Something went wrong. Close down and go home. */
121 signal (SIGPIPE
, pipe_handler
);
122 _rmt_shutdown (fildes
);
127 /* Read and return the status from remote tape connection FILDES.
128 If an error occurred, return -1 and set errno. */
136 char buffer
[CMDBUFSIZE
];
138 /* Read the reply command line. */
140 for (i
= 0, cp
= buffer
; i
< CMDBUFSIZE
; i
++, cp
++)
142 if (read (READ (fildes
), cp
, 1) != 1)
144 _rmt_shutdown (fildes
);
157 _rmt_shutdown (fildes
);
162 /* Check the return status. */
164 for (cp
= buffer
; *cp
; cp
++)
168 if (*cp
== 'E' || *cp
== 'F')
170 errno
= atoi (cp
+ 1);
171 /* Skip the error message line. */
172 while (read (READ (fildes
), &c
, 1) == 1)
177 _rmt_shutdown (fildes
);
182 /* Check for mis-synced pipes. */
186 _rmt_shutdown (fildes
);
191 /* Got an `A' (success) response. */
192 return atoi (cp
+ 1);
196 /* Execute /etc/rmt as user USER on remote system HOST using rexec.
197 Return a file descriptor of a bidirectional socket for stdin and stdout.
198 If USER is NULL, or an empty string, use the current username.
200 By default, this code is not used, since it requires that
201 the user have a .netrc file in his/her home directory, or that the
202 application designer be willing to have rexec prompt for login and
203 password info. This may be unacceptable, and .rhosts files for use
204 with rsh are much more common on BSD systems. */
207 _rmt_rexec (host
, user
)
211 struct servent
*rexecserv
;
212 int save_stdin
= dup (fileno (stdin
));
213 int save_stdout
= dup (fileno (stdout
));
214 int tape_fd
; /* Return value. */
216 /* When using cpio -o < filename, stdin is no longer the tty.
217 But the rexec subroutine reads the login and the passwd on stdin,
218 to allow remote execution of the command.
219 So, reopen stdin and stdout on /dev/tty before the rexec and
220 give them back their original value after. */
221 if (freopen ("/dev/tty", "r", stdin
) == NULL
)
222 freopen ("/dev/null", "r", stdin
);
223 if (freopen ("/dev/tty", "w", stdout
) == NULL
)
224 freopen ("/dev/null", "w", stdout
);
226 rexecserv
= getservbyname ("exec", "tcp");
227 if (NULL
== rexecserv
)
229 fprintf (stderr
, "exec/tcp: service not available");
232 if (user
!= NULL
&& *user
== '\0')
234 tape_fd
= rexec (&host
, rexecserv
->s_port
, user
, NULL
,
235 "/etc/rmt", (int *) NULL
);
237 fdopen (save_stdin
, "r");
239 fdopen (save_stdout
, "w");
243 #endif /* USE_REXEC */
245 /* Open a magtape device on the system specified in PATH, as the given user.
246 PATH has the form `[user@]system:/dev/????'.
247 If COMPAT is defined, it can also have the form `system[.user]:/dev/????'.
249 OFLAG is O_RDONLY, O_WRONLY, etc.
250 MODE is ignored; 0666 is always used.
252 If successful, return the remote tape pipe number plus BIAS.
253 On error, return -1. */
256 __rmt_open (path
, oflag
, mode
, bias
)
263 char buffer
[CMDBUFSIZE
]; /* Command buffer. */
264 char system
[MAXHOSTLEN
]; /* The remote host name. */
265 char device
[CMDBUFSIZE
]; /* The remote device name. */
266 char login
[CMDBUFSIZE
]; /* The remote user name. */
267 char *sys
, *dev
, *user
; /* For copying into the above buffers. */
273 /* Find an unused pair of file descriptors. */
275 for (i
= 0; i
< MAXUNIT
; i
++)
276 if (READ (i
) == -1 && WRITE (i
) == -1)
285 /* Pull apart the system and device, and optional user.
286 Don't munge the original string. */
299 if (*(path
- 1) == '@')
301 /* Saw user part of user@host. Start over. */
302 strcpy (user
, system
);
312 else if (*(path
- 1) == '.')
332 /* Execute the remote command using rexec. */
333 READ (i
) = WRITE (i
) = _rmt_rexec (system
, login
);
336 #else /* !USE_REXEC */
337 /* Set up the pipes for the `rsh' command, and fork. */
339 if (pipe (to_rmt
[i
]) == -1 || pipe (from_rmt
[i
]) == -1)
351 close (to_rmt
[i
][0]);
352 close (to_rmt
[i
][1]);
355 dup (from_rmt
[i
][1]);
356 close (from_rmt
[i
][0]);
357 close (from_rmt
[i
][1]);
364 execl ("/usr/ucb/rsh", "rsh", system
, "-l", login
,
365 "/etc/rmt", (char *) 0);
366 execl ("/usr/bin/remsh", "remsh", system
, "-l", login
,
367 "/etc/rmt", (char *) 0);
368 execl ("/usr/bin/rsh", "rsh", system
, "-l", login
,
369 "/etc/rmt", (char *) 0);
370 execl ("/usr/bsd/rsh", "rsh", system
, "-l", login
,
371 "/etc/rmt", (char *) 0);
372 execl ("/usr/bin/nsh", "nsh", system
, "-l", login
,
373 "/etc/rmt", (char *) 0);
377 execl ("/usr/ucb/rsh", "rsh", system
,
378 "/etc/rmt", (char *) 0);
379 execl ("/usr/bin/remsh", "remsh", system
,
380 "/etc/rmt", (char *) 0);
381 execl ("/usr/bin/rsh", "rsh", system
,
382 "/etc/rmt", (char *) 0);
383 execl ("/usr/bsd/rsh", "rsh", system
,
384 "/etc/rmt", (char *) 0);
385 execl ("/usr/bin/nsh", "nsh", system
,
386 "/etc/rmt", (char *) 0);
389 /* Bad problems if we get here. */
391 perror ("cannot execute remote shell");
396 close (to_rmt
[i
][0]);
397 close (from_rmt
[i
][1]);
398 #endif /* !USE_REXEC */
400 /* Attempt to open the tape device. */
402 sprintf (buffer
, "O%s\n%d\n", device
, oflag
);
403 if (command (i
, buffer
) == -1 || status (i
) == -1)
409 /* Close remote tape connection FILDES and shut down.
410 Return 0 if successful, -1 on error. */
418 if (command (fildes
, "C\n") == -1)
421 rc
= status (fildes
);
422 _rmt_shutdown (fildes
);
426 /* Read up to NBYTE bytes into BUF from remote tape connection FILDES.
427 Return the number of bytes read on success, -1 on error. */
430 __rmt_read (fildes
, buf
, nbyte
)
436 char buffer
[CMDBUFSIZE
];
438 sprintf (buffer
, "R%d\n", nbyte
);
439 if (command (fildes
, buffer
) == -1 || (rc
= status (fildes
)) == -1)
442 for (i
= 0; i
< rc
; i
+= nbyte
, buf
+= nbyte
)
444 nbyte
= read (READ (fildes
), buf
, rc
);
447 _rmt_shutdown (fildes
);
456 /* Write NBYTE bytes from BUF to remote tape connection FILDES.
457 Return the number of bytes written on success, -1 on error. */
460 __rmt_write (fildes
, buf
, nbyte
)
465 char buffer
[CMDBUFSIZE
];
466 RETSIGTYPE (*pipe_handler
) ();
468 sprintf (buffer
, "W%d\n", nbyte
);
469 if (command (fildes
, buffer
) == -1)
472 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
473 if (write (WRITE (fildes
), buf
, nbyte
) == nbyte
)
475 signal (SIGPIPE
, pipe_handler
);
476 return status (fildes
);
480 signal (SIGPIPE
, pipe_handler
);
481 _rmt_shutdown (fildes
);
486 /* Perform an imitation lseek operation on remote tape connection FILDES.
487 Return the new file offset if successful, -1 if on error. */
490 __rmt_lseek (fildes
, offset
, whence
)
495 char buffer
[CMDBUFSIZE
];
497 sprintf (buffer
, "L%ld\n%d\n", offset
, whence
);
498 if (command (fildes
, buffer
) == -1)
501 return status (fildes
);
504 /* Perform a raw tape operation on remote tape connection FILDES.
505 Return the results of the ioctl, or -1 on error. */
508 __rmt_ioctl (fildes
, op
, arg
)
514 char buffer
[CMDBUFSIZE
];
523 /* MTIOCTOP is the easy one. Nothing is transfered in binary. */
524 sprintf (buffer
, "I%d\n%d\n", ((struct mtop
*) arg
)->mt_op
,
525 ((struct mtop
*) arg
)->mt_count
);
526 if (command (fildes
, buffer
) == -1)
528 return status (fildes
); /* Return the count. */
531 /* Grab the status and read it directly into the structure.
532 This assumes that the status buffer is not padded
533 and that 2 shorts fit in a long without any word
534 alignment problems; i.e., the whole struct is contiguous.
535 NOTE - this is probably NOT a good assumption. */
537 if (command (fildes
, "S") == -1 || (rc
= status (fildes
)) == -1)
540 for (; rc
> 0; rc
-= cnt
, arg
+= cnt
)
542 cnt
= read (READ (fildes
), arg
, rc
);
545 _rmt_shutdown (fildes
);
551 /* Check for byte position. mt_type is a small integer field
552 (normally) so we will check its magnitude. If it is larger than
553 256, we will assume that the bytes are swapped and go through
554 and reverse all the bytes. */
556 if (((struct mtget
*) arg
)->mt_type
< 256)
559 for (cnt
= 0; cnt
< rc
; cnt
+= 2)
562 arg
[cnt
] = arg
[cnt
+ 1];
This page took 0.057088 seconds and 4 git commands to generate.