]>
Dogcows Code - chaz/tar/blob - rtapelib.c
138425fcfe075b33a128faa8bf00f8f7df64acc3
1 /* Functions for communicating with a remote tape drive.
2 Copyright 1988, 1992, 1994, 1996, 1997, 1999 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
19 which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
20 The author of the routines I'm including originally wrote his code just
21 based on the man page, and it didn't work, so he went to the rdump source
22 to figure out why. The only thing he had to change was to check for the
23 'F' return code in addition to the 'E', and to separate the various
24 arguments with \n instead of a space. I personally don't think that this
25 is much of a problem, but I wanted to point it out. -- Arnold Robbins
27 Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
28 as a library that can replace open, read, write, etc., by Fred Fish, with
29 some additional work by Arnold Robbins. Modified to make all rmt* calls
30 into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec
31 code, courtesy of Dan Kegel. */
36 #include "safe-read.h"
38 /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
39 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
43 # include <net/errno.h>
46 # include <sys/inet.h>
49 # define EOPNOTSUPP EINVAL
61 /* FIXME: Just to shut up -Wall. */
64 /* Exit status if exec errors. */
65 #define EXIT_ON_EXEC_ERROR 128
67 /* FIXME: Size of buffers for reading and writing commands to rmt. */
68 #define COMMAND_BUFFER_SIZE 64
71 # define RETSIGTYPE void
74 /* FIXME: Maximum number of simultaneous remote tape connections. */
77 #define PREAD 0 /* read file descriptor from pipe() */
78 #define PWRITE 1 /* write file descriptor from pipe() */
80 /* Return the parent's read side of remote tape connection Fd. */
81 #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
83 /* Return the parent's write side of remote tape connection Fd. */
84 #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
86 /* The pipes for receiving data from remote tape drives. */
87 static int from_remote
[MAXUNIT
][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
89 /* The pipes for sending data to remote tape drives. */
90 static int to_remote
[MAXUNIT
][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
92 /* Temporary variable used by macros in rmt.h. */
96 /*----------------------------------------------------------------------.
97 | Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. |
98 `----------------------------------------------------------------------*/
101 _rmt_shutdown (int handle
, int errno_value
)
103 close (READ_SIDE (handle
));
104 close (WRITE_SIDE (handle
));
105 READ_SIDE (handle
) = -1;
106 WRITE_SIDE (handle
) = -1;
107 errno
= errno_value
; /* FIXME: errno should be read-only */
110 /*-------------------------------------------------------------------------.
111 | Attempt to perform the remote tape command specified in BUFFER on remote |
112 | tape connection HANDLE. Return 0 if successful, -1 on error. |
113 `-------------------------------------------------------------------------*/
116 do_command (int handle
, const char *buffer
)
119 RETSIGTYPE (*pipe_handler
) ();
121 /* Save the current pipe handler and try to make the request. */
123 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
124 length
= strlen (buffer
);
125 if (full_write (WRITE_SIDE (handle
), buffer
, length
) == length
)
127 signal (SIGPIPE
, pipe_handler
);
131 /* Something went wrong. Close down and go home. */
133 signal (SIGPIPE
, pipe_handler
);
134 _rmt_shutdown (handle
, EIO
);
139 get_status_string (int handle
, char *command_buffer
)
144 /* Read the reply command line. */
146 for (counter
= 0, cursor
= command_buffer
;
147 counter
< COMMAND_BUFFER_SIZE
;
150 if (safe_read (READ_SIDE (handle
), cursor
, 1) != 1)
152 _rmt_shutdown (handle
, EIO
);
162 if (counter
== COMMAND_BUFFER_SIZE
)
164 _rmt_shutdown (handle
, EIO
);
168 /* Check the return status. */
170 for (cursor
= command_buffer
; *cursor
; cursor
++)
174 if (*cursor
== 'E' || *cursor
== 'F')
176 errno
= atoi (cursor
+ 1); /* FIXME: errno should be read-only */
178 /* Skip the error message line. */
180 /* FIXME: there is better to do than merely ignoring error messages
181 coming from the remote end. Translate them, too... */
186 while (safe_read (READ_SIDE (handle
), &character
, 1) == 1)
187 if (character
== '\n')
192 _rmt_shutdown (handle
, errno
);
197 /* Check for mis-synced pipes. */
201 _rmt_shutdown (handle
, EIO
);
205 /* Got an `A' (success) response. */
210 /*----------------------------------------------------------------------.
211 | Read and return the status from remote tape connection HANDLE. If an |
212 | error occurred, return -1 and set errno. |
213 `----------------------------------------------------------------------*/
216 get_status (int handle
)
218 char command_buffer
[COMMAND_BUFFER_SIZE
];
219 const char *status
= get_status_string (handle
, command_buffer
);
220 return status
? atol (status
) : -1L;
224 get_status_off (int handle
)
226 char command_buffer
[COMMAND_BUFFER_SIZE
];
227 const char *status
= get_status_string (handle
, command_buffer
);
233 /* Parse status, taking care to check for overflow.
234 We can't use standard functions,
235 since off_t might be longer than long. */
240 for (; *status
== ' ' || *status
== '\t'; status
++)
243 negative
= *status
== '-';
244 status
+= negative
|| *status
== '+';
248 int digit
= *status
++ - '0';
249 if (9 < (unsigned) digit
)
253 off_t c10
= 10 * count
;
254 off_t nc
= negative
? c10
- digit
: c10
+ digit
;
255 if (c10
/ 10 != count
|| (negative
? c10
< nc
: nc
< c10
))
267 /*-------------------------------------------------------------------------.
268 | Execute /etc/rmt as user USER on remote system HOST using rexec. Return |
269 | a file descriptor of a bidirectional socket for stdin and stdout. If |
270 | USER is NULL, use the current username. |
272 | By default, this code is not used, since it requires that the user have |
273 | a .netrc file in his/her home directory, or that the application |
274 | designer be willing to have rexec prompt for login and password info. |
275 | This may be unacceptable, and .rhosts files for use with rsh are much |
276 | more common on BSD systems. |
277 `-------------------------------------------------------------------------*/
280 _rmt_rexec (char *host
, char *user
)
282 int saved_stdin
= dup (STDIN_FILENO
);
283 int saved_stdout
= dup (STDOUT_FILENO
);
284 struct servent
*rexecserv
;
287 /* When using cpio -o < filename, stdin is no longer the tty. But the
288 rexec subroutine reads the login and the passwd on stdin, to allow
289 remote execution of the command. So, reopen stdin and stdout on
290 /dev/tty before the rexec and give them back their original value
293 if (freopen ("/dev/tty", "r", stdin
) == NULL
)
294 freopen ("/dev/null", "r", stdin
);
295 if (freopen ("/dev/tty", "w", stdout
) == NULL
)
296 freopen ("/dev/null", "w", stdout
);
298 if (rexecserv
= getservbyname ("exec", "tcp"), !rexecserv
)
299 error (EXIT_ON_EXEC_ERROR
, 0, _("exec/tcp: Service not available"));
301 result
= rexec (&host
, rexecserv
->s_port
, user
, NULL
,
302 "/etc/rmt", (int *) NULL
);
303 if (fclose (stdin
) == EOF
)
304 error (0, errno
, _("stdin"));
305 fdopen (saved_stdin
, "r");
306 if (fclose (stdout
) == EOF
)
307 error (0, errno
, _("stdout"));
308 fdopen (saved_stdout
, "w");
313 #endif /* WITH_REXEC */
315 /* Place into BUF a string representing OFLAG, which must be suitable
316 as argument 2 of `open'. BUF must be large enough to hold the
317 result. This function should generate a string that decode_oflag
320 encode_oflag (char *buf
, int oflag
)
322 sprintf (buf
, "%d ", oflag
);
324 switch (oflag
& O_ACCMODE
)
326 case O_RDONLY
: strcat (buf
, "O_RDONLY"); break;
327 case O_RDWR
: strcat (buf
, "O_RDWR"); break;
328 case O_WRONLY
: strcat (buf
, "O_WRONLY"); break;
332 if (oflag
& O_APPEND
) strcat (buf
, "|O_APPEND");
333 if (oflag
& O_CREAT
) strcat (buf
, "|O_CREAT");
335 if (oflag
& O_DSYNC
) strcat (buf
, "|O_DSYNC");
337 if (oflag
& O_EXCL
) strcat (buf
, "|O_EXCL");
339 if (oflag
& O_LARGEFILE
) strcat (buf
, "|O_LARGEFILE");
342 if (oflag
& O_NOCTTY
) strcat (buf
, "|O_NOCTTY");
345 if (oflag
& O_NONBLOCK
) strcat (buf
, "|O_NONBLOCK");
348 if (oflag
& O_RSYNC
) strcat (buf
, "|O_RSYNC");
351 if (oflag
& O_SYNC
) strcat (buf
, "|O_SYNC");
353 if (oflag
& O_TRUNC
) strcat (buf
, "|O_TRUNC");
356 /*------------------------------------------------------------------------.
357 | Open a file (a magnetic tape device?) on the system specified in PATH, |
358 | as the given user. PATH has the form `[USER@]HOST:FILE'. OPEN_MODE is |
359 | O_RDONLY, O_WRONLY, etc. If successful, return the remote pipe number |
360 | plus BIAS. REMOTE_SHELL may be overriden. On error, return -1. |
361 `------------------------------------------------------------------------*/
364 rmt_open__ (const char *path
, int open_mode
, int bias
, const char *remote_shell
)
366 int remote_pipe_number
; /* pseudo, biased file descriptor */
367 char *path_copy
; /* copy of path string */
368 char *remote_host
; /* remote host name */
369 char *remote_file
; /* remote file name (often a device) */
370 char *remote_user
; /* remote user name */
372 /* Find an unused pair of file descriptors. */
374 for (remote_pipe_number
= 0;
375 remote_pipe_number
< MAXUNIT
;
376 remote_pipe_number
++)
377 if (READ_SIDE (remote_pipe_number
) == -1
378 && WRITE_SIDE (remote_pipe_number
) == -1)
381 if (remote_pipe_number
== MAXUNIT
)
383 errno
= EMFILE
; /* FIXME: errno should be read-only */
387 /* Pull apart the system and device, and optional user. */
392 path_copy
= xstrdup (path
);
393 remote_host
= path_copy
;
397 for (cursor
= path_copy
; *cursor
; cursor
++)
404 /* Do not allow newlines in the path, since the protocol
405 uses newline delimiters. */
413 remote_user
= remote_host
;
415 remote_host
= cursor
+ 1;
423 remote_file
= cursor
+ 1;
429 /* FIXME: Should somewhat validate the decoding, here. */
431 if (remote_user
&& *remote_user
== '\0')
436 /* Execute the remote command using rexec. */
438 READ_SIDE (remote_pipe_number
) = _rmt_rexec (remote_host
, remote_user
);
439 if (READ_SIDE (remote_pipe_number
) < 0)
447 WRITE_SIDE (remote_pipe_number
) = READ_SIDE (remote_pipe_number
);
449 #else /* not WITH_REXEC */
451 const char *remote_shell_basename
;
454 /* Identify the remote command to be executed. */
459 remote_shell
= REMOTE_SHELL
;
466 remote_shell_basename
= base_name (remote_shell
);
468 /* Set up the pipes for the `rsh' command, and fork. */
470 if (pipe (to_remote
[remote_pipe_number
]) == -1
471 || pipe (from_remote
[remote_pipe_number
]) == -1)
492 close (STDIN_FILENO
);
493 dup (to_remote
[remote_pipe_number
][PREAD
]);
494 close (to_remote
[remote_pipe_number
][PREAD
]);
495 close (to_remote
[remote_pipe_number
][PWRITE
]);
497 close (STDOUT_FILENO
);
498 dup (from_remote
[remote_pipe_number
][PWRITE
]);
499 close (from_remote
[remote_pipe_number
][PREAD
]);
500 close (from_remote
[remote_pipe_number
][PWRITE
]);
508 execl (remote_shell
, remote_shell_basename
, remote_host
,
509 "-l", remote_user
, "/etc/rmt", (char *) 0);
511 execl (remote_shell
, remote_shell_basename
, remote_host
,
512 "/etc/rmt", (char *) 0);
514 /* Bad problems if we get here. */
516 /* In a previous version, _exit was used here instead of exit. */
517 error (EXIT_ON_EXEC_ERROR
, errno
, _("Cannot execute remote shell"));
522 close (from_remote
[remote_pipe_number
][PWRITE
]);
523 close (to_remote
[remote_pipe_number
][PREAD
]);
525 #endif /* not WITH_REXEC */
527 /* Attempt to open the tape device. */
530 size_t remote_file_len
= strlen (remote_file
);
531 char *command_buffer
= xmalloc (remote_file_len
+ 1000);
532 sprintf (command_buffer
, "O%s\n", remote_file
);
533 encode_oflag (command_buffer
+ remote_file_len
+ 2, open_mode
);
534 strcat (command_buffer
, "\n");
535 if (do_command (remote_pipe_number
, command_buffer
) == -1
536 || get_status (remote_pipe_number
) == -1)
539 free (command_buffer
);
541 _rmt_shutdown (remote_pipe_number
, e
);
544 free (command_buffer
);
548 return remote_pipe_number
+ bias
;
551 /*----------------------------------------------------------------.
552 | Close remote tape connection HANDLE and shut down. Return 0 if |
553 | successful, -1 on error. |
554 `----------------------------------------------------------------*/
557 rmt_close__ (int handle
)
561 if (do_command (handle
, "C\n") == -1)
564 status
= get_status (handle
);
565 _rmt_shutdown (handle
, errno
);
569 /*-------------------------------------------------------------------------.
570 | Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE. |
571 | Return the number of bytes read on success, -1 on error. |
572 `-------------------------------------------------------------------------*/
575 rmt_read__ (int handle
, char *buffer
, size_t length
)
577 char command_buffer
[COMMAND_BUFFER_SIZE
];
578 ssize_t status
, rlen
;
581 sprintf (command_buffer
, "R%lu\n", (unsigned long) length
);
582 if (do_command (handle
, command_buffer
) == -1
583 || (status
= get_status (handle
)) == -1)
586 for (counter
= 0; counter
< status
; counter
+= rlen
, buffer
+= rlen
)
588 rlen
= safe_read (READ_SIDE (handle
), buffer
, status
- counter
);
591 _rmt_shutdown (handle
, EIO
);
599 /*-------------------------------------------------------------------------.
600 | Write LENGTH bytes from BUFFER to remote tape connection HANDLE. Return |
601 | the number of bytes written on success, -1 on error. |
602 `-------------------------------------------------------------------------*/
605 rmt_write__ (int handle
, char *buffer
, size_t length
)
607 char command_buffer
[COMMAND_BUFFER_SIZE
];
608 RETSIGTYPE (*pipe_handler
) ();
610 sprintf (command_buffer
, "W%lu\n", (unsigned long) length
);
611 if (do_command (handle
, command_buffer
) == -1)
614 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
615 if (full_write (WRITE_SIDE (handle
), buffer
, length
) == length
)
617 signal (SIGPIPE
, pipe_handler
);
618 return get_status (handle
);
623 signal (SIGPIPE
, pipe_handler
);
624 _rmt_shutdown (handle
, EIO
);
628 /*------------------------------------------------------------------------.
629 | Perform an imitation lseek operation on remote tape connection HANDLE. |
630 | Return the new file offset if successful, -1 if on error. |
631 `------------------------------------------------------------------------*/
634 rmt_lseek__ (int handle
, off_t offset
, int whence
)
636 char command_buffer
[COMMAND_BUFFER_SIZE
];
637 char operand_buffer
[UINTMAX_STRSIZE_BOUND
];
638 uintmax_t u
= offset
< 0 ? - (uintmax_t) offset
: (uintmax_t) offset
;
639 char *p
= operand_buffer
+ sizeof operand_buffer
;
642 *--p
= '0' + (int) (u
% 10);
643 while ((u
/= 10) != 0);
649 case SEEK_SET
: whence
= 0; break;
650 case SEEK_CUR
: whence
= 1; break;
651 case SEEK_END
: whence
= 2; break;
655 sprintf (command_buffer
, "L%s\n%d\n", p
, whence
);
657 if (do_command (handle
, command_buffer
) == -1)
660 return get_status_off (handle
);
663 /*-----------------------------------------------------------------------.
664 | Perform a raw tape operation on remote tape connection HANDLE. Return |
665 | the results of the ioctl, or -1 on error. |
666 `-----------------------------------------------------------------------*/
669 rmt_ioctl__ (int handle
, int operation
, char *argument
)
674 errno
= EOPNOTSUPP
; /* FIXME: errno should be read-only */
680 char command_buffer
[COMMAND_BUFFER_SIZE
];
681 char operand_buffer
[UINTMAX_STRSIZE_BOUND
];
682 uintmax_t u
= (((struct mtop
*) argument
)->mt_count
< 0
683 ? - (uintmax_t) ((struct mtop
*) argument
)->mt_count
684 : (uintmax_t) ((struct mtop
*) argument
)->mt_count
);
685 char *p
= operand_buffer
+ sizeof operand_buffer
;
688 *--p
= '0' + (int) (u
% 10);
689 while ((u
/= 10) != 0);
690 if (((struct mtop
*) argument
)->mt_count
< 0)
693 /* MTIOCTOP is the easy one. Nothing is transfered in binary. */
695 sprintf (command_buffer
, "I%d\n%s\n",
696 ((struct mtop
*) argument
)->mt_op
, p
);
697 if (do_command (handle
, command_buffer
) == -1)
700 return get_status (handle
);
702 #endif /* MTIOCTOP */
710 /* Grab the status and read it directly into the structure. This
711 assumes that the status buffer is not padded and that 2 shorts
712 fit in a long without any word alignment problems; i.e., the
713 whole struct is contiguous. NOTE - this is probably NOT a good
716 if (do_command (handle
, "S") == -1
717 || (status
= get_status (handle
), status
== -1))
720 for (; status
> 0; status
-= counter
, argument
+= counter
)
722 counter
= safe_read (READ_SIDE (handle
),
723 argument
, (size_t) status
);
726 _rmt_shutdown (handle
, EIO
);
731 /* Check for byte position. mt_type (or mt_model) is a small integer
732 field (normally) so we will check its magnitude. If it is larger
733 than 256, we will assume that the bytes are swapped and go through
734 and reverse all the bytes. */
736 if (((struct mtget
*) argument
)->MTIO_CHECK_FIELD
< 256)
739 for (counter
= 0; counter
< status
; counter
+= 2)
741 char copy
= argument
[counter
];
743 argument
[counter
] = argument
[counter
+ 1];
744 argument
[counter
+ 1] = copy
;
749 #endif /* MTIOCGET */
This page took 0.074214 seconds and 3 git commands to generate.