]>
Dogcows Code - chaz/tar/blob - src/rtapelib.c
1 /* Functions for communicating with a remote tape drive.
3 Copyright 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001 Free Software
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol
21 which rdump and rrestore use. Unfortunately, the man page is *WRONG*.
22 The author of the routines I'm including originally wrote his code just
23 based on the man page, and it didn't work, so he went to the rdump source
24 to figure out why. The only thing he had to change was to check for the
25 'F' return code in addition to the 'E', and to separate the various
26 arguments with \n instead of a space. I personally don't think that this
27 is much of a problem, but I wanted to point it out. -- Arnold Robbins
29 Originally written by Jeff Lee, modified some by Arnold Robbins. Redone
30 as a library that can replace open, read, write, etc., by Fred Fish, with
31 some additional work by Arnold Robbins. Modified to make all rmt* calls
32 into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec
33 code, courtesy of Dan Kegel. */
37 #include <safe-read.h>
38 #include <full-write.h>
40 /* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h,
41 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */
45 # include <net/errno.h>
48 # include <sys/inet.h>
51 # define EOPNOTSUPP EINVAL
63 /* Exit status if exec errors. */
64 #define EXIT_ON_EXEC_ERROR 128
66 /* FIXME: Size of buffers for reading and writing commands to rmt. */
67 #define COMMAND_BUFFER_SIZE 64
70 # define RETSIGTYPE void
73 /* FIXME: Maximum number of simultaneous remote tape connections. */
76 #define PREAD 0 /* read file descriptor from pipe() */
77 #define PWRITE 1 /* write file descriptor from pipe() */
79 /* Return the parent's read side of remote tape connection Fd. */
80 #define READ_SIDE(Fd) (from_remote[Fd][PREAD])
82 /* Return the parent's write side of remote tape connection Fd. */
83 #define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])
85 /* The pipes for receiving data from remote tape drives. */
86 static int from_remote
[MAXUNIT
][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
88 /* The pipes for sending data to remote tape drives. */
89 static int to_remote
[MAXUNIT
][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
91 /* Temporary variable used by macros in rmt.h. */
95 /* Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. */
97 _rmt_shutdown (int handle
, int errno_value
)
99 close (READ_SIDE (handle
));
100 close (WRITE_SIDE (handle
));
101 READ_SIDE (handle
) = -1;
102 WRITE_SIDE (handle
) = -1;
106 /* Attempt to perform the remote tape command specified in BUFFER on
107 remote tape connection HANDLE. Return 0 if successful, -1 on
110 do_command (int handle
, const char *buffer
)
112 /* Save the current pipe handler and try to make the request. */
114 size_t length
= strlen (buffer
);
115 RETSIGTYPE (*pipe_handler
) () = signal (SIGPIPE
, SIG_IGN
);
116 ssize_t written
= full_write (WRITE_SIDE (handle
), buffer
, length
);
117 signal (SIGPIPE
, pipe_handler
);
119 if (written
== length
)
122 /* Something went wrong. Close down and go home. */
124 _rmt_shutdown (handle
, EIO
);
129 get_status_string (int handle
, char *command_buffer
)
134 /* Read the reply command line. */
136 for (counter
= 0, cursor
= command_buffer
;
137 counter
< COMMAND_BUFFER_SIZE
;
140 if (safe_read (READ_SIDE (handle
), cursor
, 1) != 1)
142 _rmt_shutdown (handle
, EIO
);
152 if (counter
== COMMAND_BUFFER_SIZE
)
154 _rmt_shutdown (handle
, EIO
);
158 /* Check the return status. */
160 for (cursor
= command_buffer
; *cursor
; cursor
++)
164 if (*cursor
== 'E' || *cursor
== 'F')
166 errno
= atoi (cursor
+ 1);
168 /* Skip the error message line. */
170 /* FIXME: there is better to do than merely ignoring error messages
171 coming from the remote end. Translate them, too... */
176 while (safe_read (READ_SIDE (handle
), &character
, 1) == 1)
177 if (character
== '\n')
182 _rmt_shutdown (handle
, errno
);
187 /* Check for mis-synced pipes. */
191 _rmt_shutdown (handle
, EIO
);
195 /* Got an `A' (success) response. */
200 /* Read and return the status from remote tape connection HANDLE. If
201 an error occurred, return -1 and set errno. */
203 get_status (int handle
)
205 char command_buffer
[COMMAND_BUFFER_SIZE
];
206 const char *status
= get_status_string (handle
, command_buffer
);
207 return status
? atol (status
) : -1L;
211 get_status_off (int handle
)
213 char command_buffer
[COMMAND_BUFFER_SIZE
];
214 const char *status
= get_status_string (handle
, command_buffer
);
220 /* Parse status, taking care to check for overflow.
221 We can't use standard functions,
222 since off_t might be longer than long. */
227 for (; *status
== ' ' || *status
== '\t'; status
++)
230 negative
= *status
== '-';
231 status
+= negative
|| *status
== '+';
235 int digit
= *status
++ - '0';
236 if (9 < (unsigned) digit
)
240 off_t c10
= 10 * count
;
241 off_t nc
= negative
? c10
- digit
: c10
+ digit
;
242 if (c10
/ 10 != count
|| (negative
? c10
< nc
: nc
< c10
))
256 /* Execute /etc/rmt as user USER on remote system HOST using rexec.
257 Return a file descriptor of a bidirectional socket for stdin and
258 stdout. If USER is zero, use the current username.
260 By default, this code is not used, since it requires that the user
261 have a .netrc file in his/her home directory, or that the
262 application designer be willing to have rexec prompt for login and
263 password info. This may be unacceptable, and .rhosts files for use
264 with rsh are much more common on BSD systems. */
266 _rmt_rexec (char *host
, char *user
)
268 int saved_stdin
= dup (STDIN_FILENO
);
269 int saved_stdout
= dup (STDOUT_FILENO
);
270 struct servent
*rexecserv
;
273 /* When using cpio -o < filename, stdin is no longer the tty. But the
274 rexec subroutine reads the login and the passwd on stdin, to allow
275 remote execution of the command. So, reopen stdin and stdout on
276 /dev/tty before the rexec and give them back their original value
279 if (! freopen ("/dev/tty", "r", stdin
))
280 freopen ("/dev/null", "r", stdin
);
281 if (! freopen ("/dev/tty", "w", stdout
))
282 freopen ("/dev/null", "w", stdout
);
284 if (rexecserv
= getservbyname ("exec", "tcp"), !rexecserv
)
285 error (EXIT_ON_EXEC_ERROR
, 0, _("exec/tcp: Service not available"));
287 result
= rexec (&host
, rexecserv
->s_port
, user
, 0, "/etc/rmt", 0);
288 if (fclose (stdin
) == EOF
)
289 error (0, errno
, _("stdin"));
290 fdopen (saved_stdin
, "r");
291 if (fclose (stdout
) == EOF
)
292 error (0, errno
, _("stdout"));
293 fdopen (saved_stdout
, "w");
298 #endif /* WITH_REXEC */
300 /* Place into BUF a string representing OFLAG, which must be suitable
301 as argument 2 of `open'. BUF must be large enough to hold the
302 result. This function should generate a string that decode_oflag
305 encode_oflag (char *buf
, int oflag
)
307 sprintf (buf
, "%d ", oflag
);
309 switch (oflag
& O_ACCMODE
)
311 case O_RDONLY
: strcat (buf
, "O_RDONLY"); break;
312 case O_RDWR
: strcat (buf
, "O_RDWR"); break;
313 case O_WRONLY
: strcat (buf
, "O_WRONLY"); break;
318 if (oflag
& O_APPEND
) strcat (buf
, "|O_APPEND");
320 if (oflag
& O_CREAT
) strcat (buf
, "|O_CREAT");
322 if (oflag
& O_DSYNC
) strcat (buf
, "|O_DSYNC");
324 if (oflag
& O_EXCL
) strcat (buf
, "|O_EXCL");
326 if (oflag
& O_LARGEFILE
) strcat (buf
, "|O_LARGEFILE");
329 if (oflag
& O_NOCTTY
) strcat (buf
, "|O_NOCTTY");
332 if (oflag
& O_NONBLOCK
) strcat (buf
, "|O_NONBLOCK");
335 if (oflag
& O_RSYNC
) strcat (buf
, "|O_RSYNC");
338 if (oflag
& O_SYNC
) strcat (buf
, "|O_SYNC");
340 if (oflag
& O_TRUNC
) strcat (buf
, "|O_TRUNC");
343 /* Open a file (a magnetic tape device?) on the system specified in
344 PATH, as the given user. PATH has the form `[USER@]HOST:FILE'.
345 OPEN_MODE is O_RDONLY, O_WRONLY, etc. If successful, return the
346 remote pipe number plus BIAS. REMOTE_SHELL may be overridden. On
349 rmt_open__ (const char *path
, int open_mode
, int bias
, const char *remote_shell
)
351 int remote_pipe_number
; /* pseudo, biased file descriptor */
352 char *path_copy
; /* copy of path string */
353 char *remote_host
; /* remote host name */
354 char *remote_file
; /* remote file name (often a device) */
355 char *remote_user
; /* remote user name */
357 /* Find an unused pair of file descriptors. */
359 for (remote_pipe_number
= 0;
360 remote_pipe_number
< MAXUNIT
;
361 remote_pipe_number
++)
362 if (READ_SIDE (remote_pipe_number
) == -1
363 && WRITE_SIDE (remote_pipe_number
) == -1)
366 if (remote_pipe_number
== MAXUNIT
)
372 /* Pull apart the system and device, and optional user. */
377 path_copy
= xstrdup (path
);
378 remote_host
= path_copy
;
382 for (cursor
= path_copy
; *cursor
; cursor
++)
389 /* Do not allow newlines in the path, since the protocol
390 uses newline delimiters. */
398 remote_user
= remote_host
;
400 remote_host
= cursor
+ 1;
408 remote_file
= cursor
+ 1;
414 /* FIXME: Should somewhat validate the decoding, here. */
416 if (remote_user
&& *remote_user
== '\0')
421 /* Execute the remote command using rexec. */
423 READ_SIDE (remote_pipe_number
) = _rmt_rexec (remote_host
, remote_user
);
424 if (READ_SIDE (remote_pipe_number
) < 0)
432 WRITE_SIDE (remote_pipe_number
) = READ_SIDE (remote_pipe_number
);
434 #else /* not WITH_REXEC */
436 const char *remote_shell_basename
;
439 /* Identify the remote command to be executed. */
444 remote_shell
= REMOTE_SHELL
;
451 remote_shell_basename
= base_name (remote_shell
);
453 /* Set up the pipes for the `rsh' command, and fork. */
455 if (pipe (to_remote
[remote_pipe_number
]) == -1
456 || pipe (from_remote
[remote_pipe_number
]) == -1)
477 close (STDIN_FILENO
);
478 dup (to_remote
[remote_pipe_number
][PREAD
]);
479 close (to_remote
[remote_pipe_number
][PREAD
]);
480 close (to_remote
[remote_pipe_number
][PWRITE
]);
482 close (STDOUT_FILENO
);
483 dup (from_remote
[remote_pipe_number
][PWRITE
]);
484 close (from_remote
[remote_pipe_number
][PREAD
]);
485 close (from_remote
[remote_pipe_number
][PWRITE
]);
493 execl (remote_shell
, remote_shell_basename
, remote_host
,
494 "-l", remote_user
, "/etc/rmt", (char *) 0);
496 execl (remote_shell
, remote_shell_basename
, remote_host
,
497 "/etc/rmt", (char *) 0);
499 /* Bad problems if we get here. */
501 /* In a previous version, _exit was used here instead of exit. */
502 error (EXIT_ON_EXEC_ERROR
, errno
, _("Cannot execute remote shell"));
507 close (from_remote
[remote_pipe_number
][PWRITE
]);
508 close (to_remote
[remote_pipe_number
][PREAD
]);
510 #endif /* not WITH_REXEC */
512 /* Attempt to open the tape device. */
515 size_t remote_file_len
= strlen (remote_file
);
516 char *command_buffer
= xmalloc (remote_file_len
+ 1000);
517 sprintf (command_buffer
, "O%s\n", remote_file
);
518 encode_oflag (command_buffer
+ remote_file_len
+ 2, open_mode
);
519 strcat (command_buffer
, "\n");
520 if (do_command (remote_pipe_number
, command_buffer
) == -1
521 || get_status (remote_pipe_number
) == -1)
524 free (command_buffer
);
526 _rmt_shutdown (remote_pipe_number
, e
);
529 free (command_buffer
);
533 return remote_pipe_number
+ bias
;
536 /* Close remote tape connection HANDLE and shut down. Return 0 if
537 successful, -1 on error. */
539 rmt_close__ (int handle
)
543 if (do_command (handle
, "C\n") == -1)
546 status
= get_status (handle
);
547 _rmt_shutdown (handle
, errno
);
551 /* Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.
552 Return the number of bytes read on success, -1 on error. */
554 rmt_read__ (int handle
, char *buffer
, size_t length
)
556 char command_buffer
[COMMAND_BUFFER_SIZE
];
557 ssize_t status
, rlen
;
560 sprintf (command_buffer
, "R%lu\n", (unsigned long) length
);
561 if (do_command (handle
, command_buffer
) == -1
562 || (status
= get_status (handle
)) == -1)
565 for (counter
= 0; counter
< status
; counter
+= rlen
, buffer
+= rlen
)
567 rlen
= safe_read (READ_SIDE (handle
), buffer
, status
- counter
);
570 _rmt_shutdown (handle
, EIO
);
578 /* Write LENGTH bytes from BUFFER to remote tape connection HANDLE.
579 Return the number of bytes written on success, -1 on error. */
581 rmt_write__ (int handle
, char *buffer
, size_t length
)
583 char command_buffer
[COMMAND_BUFFER_SIZE
];
584 RETSIGTYPE (*pipe_handler
) ();
587 sprintf (command_buffer
, "W%lu\n", (unsigned long) length
);
588 if (do_command (handle
, command_buffer
) == -1)
591 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
592 written
= full_write (WRITE_SIDE (handle
), buffer
, length
);
593 signal (SIGPIPE
, pipe_handler
);
594 if (written
== length
)
595 return get_status (handle
);
599 _rmt_shutdown (handle
, EIO
);
603 /* Perform an imitation lseek operation on remote tape connection
604 HANDLE. Return the new file offset if successful, -1 if on error. */
606 rmt_lseek__ (int handle
, off_t offset
, int whence
)
608 char command_buffer
[COMMAND_BUFFER_SIZE
];
609 char operand_buffer
[UINTMAX_STRSIZE_BOUND
];
610 uintmax_t u
= offset
< 0 ? - (uintmax_t) offset
: (uintmax_t) offset
;
611 char *p
= operand_buffer
+ sizeof operand_buffer
;
614 *--p
= '0' + (int) (u
% 10);
615 while ((u
/= 10) != 0);
621 case SEEK_SET
: whence
= 0; break;
622 case SEEK_CUR
: whence
= 1; break;
623 case SEEK_END
: whence
= 2; break;
627 sprintf (command_buffer
, "L%s\n%d\n", p
, whence
);
629 if (do_command (handle
, command_buffer
) == -1)
632 return get_status_off (handle
);
635 /* Perform a raw tape operation on remote tape connection HANDLE.
636 Return the results of the ioctl, or -1 on error. */
638 rmt_ioctl__ (int handle
, int operation
, char *argument
)
649 char command_buffer
[COMMAND_BUFFER_SIZE
];
650 char operand_buffer
[UINTMAX_STRSIZE_BOUND
];
651 uintmax_t u
= (((struct mtop
*) argument
)->mt_count
< 0
652 ? - (uintmax_t) ((struct mtop
*) argument
)->mt_count
653 : (uintmax_t) ((struct mtop
*) argument
)->mt_count
);
654 char *p
= operand_buffer
+ sizeof operand_buffer
;
657 *--p
= '0' + (int) (u
% 10);
658 while ((u
/= 10) != 0);
659 if (((struct mtop
*) argument
)->mt_count
< 0)
662 /* MTIOCTOP is the easy one. Nothing is transferred in binary. */
664 sprintf (command_buffer
, "I%d\n%s\n",
665 ((struct mtop
*) argument
)->mt_op
, p
);
666 if (do_command (handle
, command_buffer
) == -1)
669 return get_status (handle
);
671 #endif /* MTIOCTOP */
679 /* Grab the status and read it directly into the structure. This
680 assumes that the status buffer is not padded and that 2 shorts
681 fit in a long without any word alignment problems; i.e., the
682 whole struct is contiguous. NOTE - this is probably NOT a good
685 if (do_command (handle
, "S") == -1
686 || (status
= get_status (handle
), status
== -1))
689 for (; status
> 0; status
-= counter
, argument
+= counter
)
691 counter
= safe_read (READ_SIDE (handle
), argument
, status
);
694 _rmt_shutdown (handle
, EIO
);
699 /* Check for byte position. mt_type (or mt_model) is a small integer
700 field (normally) so we will check its magnitude. If it is larger
701 than 256, we will assume that the bytes are swapped and go through
702 and reverse all the bytes. */
704 if (((struct mtget
*) argument
)->MTIO_CHECK_FIELD
< 256)
707 for (counter
= 0; counter
< status
; counter
+= 2)
709 char copy
= argument
[counter
];
711 argument
[counter
] = argument
[counter
+ 1];
712 argument
[counter
+ 1] = copy
;
717 #endif /* MTIOCGET */
This page took 0.06888 seconds and 4 git commands to generate.