📄 rtapelib.c
字号:
/* Functions for communicating with a remote tape drive. Copyright (C) 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *//* The man page rmt(8) for /etc/rmt documents the remote mag tape protocol which rdump and rrestore use. Unfortunately, the man page is *WRONG*. The author of the routines I'm including originally wrote his code just based on the man page, and it didn't work, so he went to the rdump source to figure out why. The only thing he had to change was to check for the 'F' return code in addition to the 'E', and to separate the various arguments with \n instead of a space. I personally don't think that this is much of a problem, but I wanted to point it out. -- Arnold Robbins Originally written by Jeff Lee, modified some by Arnold Robbins. Redone as a library that can replace open, read, write, etc., by Fred Fish, with some additional work by Arnold Robbins. Modified to make all rmt* calls into macros for speed by Jay Fenlason. Use -DWITH_REXEC for rexec code, courtesy of Dan Kegel. */#include "system.h"#include "system-ioctl.h"#include <safe-read.h>#include <full-write.h>/* Try hard to get EOPNOTSUPP defined. 486/ISC has it in net/errno.h, 3B2/SVR3 has it in sys/inet.h. Otherwise, like on MSDOS, use EINVAL. */#ifndef EOPNOTSUPP# if HAVE_NET_ERRNO_H# include <net/errno.h># endif# if HAVE_SYS_INET_H# include <sys/inet.h># endif# ifndef EOPNOTSUPP# define EOPNOTSUPP EINVAL# endif#endif#include <signal.h>#if HAVE_NETDB_H# include <netdb.h>#endif#include <rmt.h>#include <rmt-command.h>/* Exit status if exec errors. */#define EXIT_ON_EXEC_ERROR 128/* FIXME: Size of buffers for reading and writing commands to rmt. */#define COMMAND_BUFFER_SIZE 64#ifndef RETSIGTYPE# define RETSIGTYPE void#endif/* FIXME: Maximum number of simultaneous remote tape connections. */#define MAXUNIT 4#define PREAD 0 /* read file descriptor from pipe() */#define PWRITE 1 /* write file descriptor from pipe() *//* Return the parent's read side of remote tape connection Fd. */#define READ_SIDE(Fd) (from_remote[Fd][PREAD])/* Return the parent's write side of remote tape connection Fd. */#define WRITE_SIDE(Fd) (to_remote[Fd][PWRITE])/* The pipes for receiving data from remote tape drives. */static int from_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};/* The pipes for sending data to remote tape drives. */static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};char *rmt_command = DEFAULT_RMT_COMMAND;/* Temporary variable used by macros in rmt.h. */char *rmt_dev_name__;/* If true, always consider file names to be local, even if they contain colons */bool force_local_option;/* Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE. */static void_rmt_shutdown (int handle, int errno_value){ close (READ_SIDE (handle)); close (WRITE_SIDE (handle)); READ_SIDE (handle) = -1; WRITE_SIDE (handle) = -1; errno = errno_value;}/* Attempt to perform the remote tape command specified in BUFFER on remote tape connection HANDLE. Return 0 if successful, -1 on error. */static intdo_command (int handle, const char *buffer){ /* Save the current pipe handler and try to make the request. */ size_t length = strlen (buffer); RETSIGTYPE (*pipe_handler) () = signal (SIGPIPE, SIG_IGN); ssize_t written = full_write (WRITE_SIDE (handle), buffer, length); signal (SIGPIPE, pipe_handler); if (written == length) return 0; /* Something went wrong. Close down and go home. */ _rmt_shutdown (handle, EIO); return -1;}static char *get_status_string (int handle, char *command_buffer){ char *cursor; int counter; /* Read the reply command line. */ for (counter = 0, cursor = command_buffer; counter < COMMAND_BUFFER_SIZE; counter++, cursor++) { if (safe_read (READ_SIDE (handle), cursor, 1) != 1) { _rmt_shutdown (handle, EIO); return 0; } if (*cursor == '\n') { *cursor = '\0'; break; } } if (counter == COMMAND_BUFFER_SIZE) { _rmt_shutdown (handle, EIO); return 0; } /* Check the return status. */ for (cursor = command_buffer; *cursor; cursor++) if (*cursor != ' ') break; if (*cursor == 'E' || *cursor == 'F') { /* Skip the error message line. */ /* FIXME: there is better to do than merely ignoring error messages coming from the remote end. Translate them, too... */ { char character; while (safe_read (READ_SIDE (handle), &character, 1) == 1) if (character == '\n') break; } errno = atoi (cursor + 1); if (*cursor == 'F') _rmt_shutdown (handle, errno); return 0; } /* Check for mis-synced pipes. */ if (*cursor != 'A') { _rmt_shutdown (handle, EIO); return 0; } /* Got an `A' (success) response. */ return cursor + 1;}/* Read and return the status from remote tape connection HANDLE. If an error occurred, return -1 and set errno. */static long intget_status (int handle){ char command_buffer[COMMAND_BUFFER_SIZE]; const char *status = get_status_string (handle, command_buffer); if (status) { long int result = atol (status); if (0 <= result) return result; errno = EIO; } return -1;}static off_tget_status_off (int handle){ char command_buffer[COMMAND_BUFFER_SIZE]; const char *status = get_status_string (handle, command_buffer); if (! status) return -1; else { /* Parse status, taking care to check for overflow. We can't use standard functions, since off_t might be longer than long. */ off_t count = 0; int negative; for (; *status == ' ' || *status == '\t'; status++) continue; negative = *status == '-'; status += negative || *status == '+'; for (;;) { int digit = *status++ - '0'; if (9 < (unsigned) digit) break; else { off_t c10 = 10 * count; off_t nc = negative ? c10 - digit : c10 + digit; if (c10 / 10 != count || (negative ? c10 < nc : nc < c10)) return -1; count = nc; } } return count; }}#if WITH_REXEC/* Execute /etc/rmt as user USER on remote system HOST using rexec. Return a file descriptor of a bidirectional socket for stdin and stdout. If USER is zero, use the current username. By default, this code is not used, since it requires that the user have a .netrc file in his/her home directory, or that the application designer be willing to have rexec prompt for login and password info. This may be unacceptable, and .rhosts files for use with rsh are much more common on BSD systems. */static int_rmt_rexec (char *host, char *user){ int saved_stdin = dup (STDIN_FILENO); int saved_stdout = dup (STDOUT_FILENO); struct servent *rexecserv; int result; /* When using cpio -o < filename, stdin is no longer the tty. But the rexec subroutine reads the login and the passwd on stdin, to allow remote execution of the command. So, reopen stdin and stdout on /dev/tty before the rexec and give them back their original value after. */ if (! freopen ("/dev/tty", "r", stdin)) freopen ("/dev/null", "r", stdin); if (! freopen ("/dev/tty", "w", stdout)) freopen ("/dev/null", "w", stdout); if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv) error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available")); result = rexec (&host, rexecserv->s_port, user, 0, rmt_command, 0); if (fclose (stdin) == EOF) error (0, errno, _("stdin")); fdopen (saved_stdin, "r"); if (fclose (stdout) == EOF) error (0, errno, _("stdout")); fdopen (saved_stdout, "w"); return result;}#endif /* WITH_REXEC *//* Place into BUF a string representing OFLAG, which must be suitable as argument 2 of `open'. BUF must be large enough to hold the result. This function should generate a string that decode_oflag can parse. */static voidencode_oflag (char *buf, int oflag){ sprintf (buf, "%d ", oflag); switch (oflag & O_ACCMODE) { case O_RDONLY: strcat (buf, "O_RDONLY"); break; case O_RDWR: strcat (buf, "O_RDWR"); break; case O_WRONLY: strcat (buf, "O_WRONLY"); break; default: abort (); }#ifdef O_APPEND if (oflag & O_APPEND) strcat (buf, "|O_APPEND");#endif if (oflag & O_CREAT) strcat (buf, "|O_CREAT");#ifdef O_DSYNC if (oflag & O_DSYNC) strcat (buf, "|O_DSYNC");#endif if (oflag & O_EXCL) strcat (buf, "|O_EXCL");#ifdef O_LARGEFILE if (oflag & O_LARGEFILE) strcat (buf, "|O_LARGEFILE");#endif#ifdef O_NOCTTY if (oflag & O_NOCTTY) strcat (buf, "|O_NOCTTY");#endif if (oflag & O_NONBLOCK) strcat (buf, "|O_NONBLOCK");#ifdef O_RSYNC if (oflag & O_RSYNC) strcat (buf, "|O_RSYNC");#endif#ifdef O_SYNC if (oflag & O_SYNC) strcat (buf, "|O_SYNC");#endif if (oflag & O_TRUNC) strcat (buf, "|O_TRUNC");}/* Open a file (a magnetic tape device?) on the system specified in FILE_NAME, as the given user. FILE_NAME has the form `[USER@]HOST:FILE'. OPEN_MODE is O_RDONLY, O_WRONLY, etc. If successful, return the remote pipe number plus BIAS. REMOTE_SHELL may be overridden. On error, return -1. */intrmt_open__ (const char *file_name, int open_mode, int bias, const char *remote_shell){ int remote_pipe_number; /* pseudo, biased file descriptor */ char *file_name_copy; /* copy of file_name string */ char *remote_host; /* remote host name */ char *remote_file; /* remote file name (often a device) */ char *remote_user; /* remote user name */ /* Find an unused pair of file descriptors. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -