2001-01-04 18:30:15 +03:00
|
|
|
/* $NetBSD: rmtlib.c,v 1.14 2001/01/04 15:30:15 lukem Exp $ */
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* rmt --- remote tape emulator subroutines
|
|
|
|
*
|
|
|
|
* Originally written by Jeff Lee, modified some by Arnold Robbins
|
|
|
|
*
|
|
|
|
* WARNING: 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
|
|
|
|
*
|
|
|
|
* Redone as a library that can replace open, read, write, etc, by
|
|
|
|
* Fred Fish, with some additional work by Arnold Robbins.
|
|
|
|
*/
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* MAXUNIT --- Maximum number of remote tape file units
|
|
|
|
*
|
|
|
|
* READ --- Return the number of the read side file descriptor
|
|
|
|
* WRITE --- Return the number of the write side file descriptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define RMTIOCTL 1
|
1997-06-20 08:24:23 +04:00
|
|
|
/* #define USE_REXEC 1 */ /* rexec code courtesy of Dan Kegel, srs!dan */
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <sys/stat.h>
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
#ifdef RMTIOCTL
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mtio.h>
|
|
|
|
#endif
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <assert.h>
|
1996-08-09 07:35:17 +04:00
|
|
|
#include <errno.h>
|
1996-08-09 07:39:00 +04:00
|
|
|
#include <fcntl.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <signal.h>
|
2001-01-04 18:30:15 +03:00
|
|
|
#include <stdarg.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-08-09 07:39:00 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#ifdef USE_REXEC
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
1997-10-21 23:58:21 +04:00
|
|
|
#define __RMTLIB_PRIVATE
|
|
|
|
#include <rmt.h> /* get prototypes for remapped functions */
|
|
|
|
|
1998-03-09 02:36:46 +03:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
static int _rmt_close(int);
|
|
|
|
static int _rmt_ioctl(int, unsigned long, void *);
|
|
|
|
static off_t _rmt_lseek(int, off_t, int);
|
|
|
|
static int _rmt_open(const char *, int, int);
|
|
|
|
static ssize_t _rmt_read(int, void *, size_t);
|
|
|
|
static ssize_t _rmt_write(int, const void *, size_t);
|
|
|
|
static int command(int, char *);
|
|
|
|
static int remdev(const char *);
|
|
|
|
static void rmtabort(int);
|
|
|
|
static int status(int);
|
1997-10-09 15:58:15 +04:00
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
int isrmt(int);
|
1997-10-09 15:58:15 +04:00
|
|
|
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
#define BUFMAGIC 64 /* a magic number for buffer sizes */
|
|
|
|
#define MAXUNIT 4
|
|
|
|
|
|
|
|
#define READ(fd) (Ctp[fd][0])
|
|
|
|
#define WRITE(fd) (Ptc[fd][1])
|
|
|
|
|
1997-06-20 08:24:23 +04:00
|
|
|
static int Ctp[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
|
|
|
|
static int Ptc[MAXUNIT][2] = { {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1} };
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
1996-08-09 07:39:00 +04:00
|
|
|
* rmtabort --- close off a remote tape connection
|
1996-08-09 07:35:17 +04:00
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
static void
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtabort(int fildes)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
close(READ(fildes));
|
|
|
|
close(WRITE(fildes));
|
|
|
|
READ(fildes) = -1;
|
|
|
|
WRITE(fildes) = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* command --- attempt to perform a remote tape command
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
command(int fildes, char *buf)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
size_t blen;
|
|
|
|
void (*pstat)(int);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* save current pipe status and try to make the request
|
|
|
|
*/
|
|
|
|
|
|
|
|
blen = strlen(buf);
|
|
|
|
pstat = signal(SIGPIPE, SIG_IGN);
|
2001-01-04 18:30:15 +03:00
|
|
|
if (write(WRITE(fildes), buf, blen) == blen) {
|
1996-08-09 07:35:17 +04:00
|
|
|
signal(SIGPIPE, pstat);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* something went wrong. close down and go home
|
|
|
|
*/
|
|
|
|
|
|
|
|
signal(SIGPIPE, pstat);
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* status --- retrieve the status from the pipe
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
status(int fildes)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c, *cp;
|
|
|
|
char buffer[BUFMAGIC];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read the reply command line
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
for (i = 0, cp = buffer; i < BUFMAGIC; i++, cp++) {
|
|
|
|
if (read(READ(fildes), cp, 1) != 1) {
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
2001-01-04 18:30:15 +03:00
|
|
|
if (*cp == '\n') {
|
1996-08-09 07:35:17 +04:00
|
|
|
*cp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (i == BUFMAGIC) {
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the return status
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (cp = buffer; *cp; cp++)
|
|
|
|
if (*cp != ' ')
|
|
|
|
break;
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (*cp == 'E' || *cp == 'F') {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = atoi(cp + 1);
|
|
|
|
while (read(READ(fildes), &c, 1) == 1)
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*cp == 'F')
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for mis-synced pipes
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (*cp != 'A') {
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(atoi(cp + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
#ifdef USE_REXEC
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* _rmt_rexec
|
|
|
|
*
|
|
|
|
* execute /etc/rmt on a remote system using rexec().
|
|
|
|
* Return file descriptor of bidirectional socket for stdin and stdout
|
|
|
|
* If username is NULL, or an empty string, uses current username.
|
|
|
|
*
|
|
|
|
* ADR: 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.
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
static int _rmt_rexec(const char *, const char *);
|
1997-10-09 15:58:15 +04:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
_rmt_rexec(const char *host, const char *user)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
struct servent *rexecserv;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(host != NULL);
|
|
|
|
/* user may be NULL */
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
rexecserv = getservbyname("exec", "tcp");
|
|
|
|
if (NULL == rexecserv) {
|
2001-01-04 18:30:15 +03:00
|
|
|
fprintf(stderr, "? exec/tcp: service not available.");
|
|
|
|
exit(-1);
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
if ((user != NULL) && *user == '\0')
|
|
|
|
user = (char *) NULL;
|
2001-01-04 18:30:15 +03:00
|
|
|
return (rexec(&host, rexecserv->s_port, user, NULL,
|
|
|
|
"/etc/rmt", (int *)NULL));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
#endif /* USE_REXEC */
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* _rmt_open --- open a magtape device on system specified, as given user
|
|
|
|
*
|
|
|
|
* file name has the form [user@]system:/dev/????
|
|
|
|
#ifdef COMPAT
|
|
|
|
* file name has the form system[.user]:/dev/????
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAXHOSTLEN 257 /* BSD allows very long host names... */
|
|
|
|
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
_rmt_open(const char *path, int oflag, int mode)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
int i, rc;
|
|
|
|
char buffer[BUFMAGIC];
|
|
|
|
char system[MAXHOSTLEN];
|
|
|
|
char device[BUFMAGIC];
|
|
|
|
char login[BUFMAGIC];
|
|
|
|
char *sys, *dev, *user;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
sys = system;
|
|
|
|
dev = device;
|
|
|
|
user = login;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, find an open pair of file descriptors
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < MAXUNIT; i++)
|
|
|
|
if (READ(i) == -1 && WRITE(i) == -1)
|
|
|
|
break;
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (i == MAXUNIT) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EMFILE;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pull apart system and device, and optional user
|
|
|
|
* don't munge original string
|
|
|
|
* if COMPAT is defined, also handle old (4.2) style person.site notation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*path != '@'
|
|
|
|
#ifdef COMPAT
|
|
|
|
&& *path != '.'
|
|
|
|
#endif
|
|
|
|
&& *path != ':') {
|
|
|
|
*sys++ = *path++;
|
|
|
|
}
|
|
|
|
*sys = '\0';
|
|
|
|
path++;
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (*(path - 1) == '@') {
|
1997-01-23 17:01:45 +03:00
|
|
|
(void)strncpy(user, system, sizeof(login) - 1);
|
|
|
|
/* saw user part of user@host */
|
1996-08-09 07:35:17 +04:00
|
|
|
sys = system; /* start over */
|
|
|
|
while (*path != ':') {
|
|
|
|
*sys++ = *path++;
|
|
|
|
}
|
|
|
|
*sys = '\0';
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
#ifdef COMPAT
|
2001-01-04 18:30:15 +03:00
|
|
|
else if (*(path - 1) == '.') {
|
1996-08-09 07:35:17 +04:00
|
|
|
while (*path != ':') {
|
|
|
|
*user++ = *path++;
|
|
|
|
}
|
|
|
|
*user = '\0';
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
*user = '\0';
|
|
|
|
|
|
|
|
while (*path) {
|
|
|
|
*dev++ = *path++;
|
|
|
|
}
|
|
|
|
*dev = '\0';
|
|
|
|
|
|
|
|
#ifdef USE_REXEC
|
1999-07-02 19:37:33 +04:00
|
|
|
/*
|
|
|
|
* Execute the remote command using rexec
|
1996-08-09 07:35:17 +04:00
|
|
|
*/
|
|
|
|
READ(i) = WRITE(i) = _rmt_rexec(system, login);
|
|
|
|
if (READ(i) < 0)
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* setup the pipes for the 'rsh' command and fork
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if ((rc = fork()) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (rc == 0) {
|
1998-03-09 02:36:46 +03:00
|
|
|
char *rshpath, *rsh;
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
close(0);
|
|
|
|
dup(Ptc[i][0]);
|
|
|
|
close(Ptc[i][0]); close(Ptc[i][1]);
|
|
|
|
close(1);
|
|
|
|
dup(Ctp[i][1]);
|
|
|
|
close(Ctp[i][0]); close(Ctp[i][1]);
|
2001-01-04 18:30:15 +03:00
|
|
|
(void) setuid(getuid());
|
|
|
|
(void) setgid(getgid());
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1998-03-09 02:36:46 +03:00
|
|
|
if ((rshpath = getenv("RCMD_CMD")) == NULL)
|
|
|
|
rshpath = _PATH_RSH;
|
|
|
|
if ((rsh = strrchr(rshpath, '/')) == NULL)
|
|
|
|
rsh = rshpath;
|
|
|
|
else
|
|
|
|
rsh++;
|
1999-07-02 19:37:33 +04:00
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (*login) {
|
1998-03-09 02:36:46 +03:00
|
|
|
execl(rshpath, rsh, system, "-l", login,
|
|
|
|
_PATH_RMT, (char *) 0);
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
1998-03-09 02:36:46 +03:00
|
|
|
execl(rshpath, rsh, system,
|
|
|
|
_PATH_RMT, (char *) 0);
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bad problems if we get here
|
|
|
|
*/
|
|
|
|
|
|
|
|
perror("exec");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(Ptc[i][0]); close(Ctp[i][1]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now attempt to open the tape device
|
|
|
|
*/
|
|
|
|
|
1997-01-23 17:01:45 +03:00
|
|
|
(void)snprintf(buffer, sizeof(buffer), "O%s\n%d\n", device, oflag);
|
1996-08-09 07:35:17 +04:00
|
|
|
if (command(i, buffer) == -1 || status(i) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _rmt_close --- close a remote magtape unit and shut down
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
_rmt_close(int fildes)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (command(fildes, "C\n") != -1) {
|
1996-08-09 07:35:17 +04:00
|
|
|
rc = status(fildes);
|
|
|
|
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
return(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _rmt_read --- read a buffer from a remote tape
|
|
|
|
*/
|
2001-01-04 18:30:15 +03:00
|
|
|
static ssize_t
|
|
|
|
_rmt_read(int fildes, void *buf, size_t nbyte)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
size_t rc, i;
|
|
|
|
char *p;
|
1996-08-09 07:35:17 +04:00
|
|
|
char buffer[BUFMAGIC];
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
1997-01-23 17:01:45 +03:00
|
|
|
(void)snprintf(buffer, sizeof buffer, "R%d\n", nbyte);
|
1996-08-09 07:35:17 +04:00
|
|
|
if (command(fildes, buffer) == -1 || (rc = status(fildes)) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
p = buf;
|
|
|
|
for (i = 0; i < rc; i += nbyte, p += nbyte) {
|
|
|
|
nbyte = read(READ(fildes), p, rc);
|
|
|
|
if (nbyte <= 0) {
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _rmt_write --- write a buffer to the remote tape
|
|
|
|
*/
|
2001-01-04 18:30:15 +03:00
|
|
|
static ssize_t
|
|
|
|
_rmt_write(int fildes, const void *buf, size_t nbyte)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
char buffer[BUFMAGIC];
|
2001-01-04 18:30:15 +03:00
|
|
|
void (*pstat)(int);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
1997-01-23 17:01:45 +03:00
|
|
|
(void)snprintf(buffer, sizeof buffer, "W%d\n", nbyte);
|
1996-08-09 07:35:17 +04:00
|
|
|
if (command(fildes, buffer) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
pstat = signal(SIGPIPE, SIG_IGN);
|
2001-01-04 18:30:15 +03:00
|
|
|
if (write(WRITE(fildes), buf, nbyte) == nbyte) {
|
|
|
|
signal(SIGPIPE, pstat);
|
1996-08-09 07:35:17 +04:00
|
|
|
return(status(fildes));
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
signal(SIGPIPE, pstat);
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _rmt_lseek --- perform an imitation lseek operation remotely
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
static off_t
|
2001-01-04 18:30:15 +03:00
|
|
|
_rmt_lseek(int fildes, off_t offset, int whence)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
char buffer[BUFMAGIC];
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
(void)snprintf(buffer, sizeof buffer, "L%lld\n%d\n", (long long)offset,
|
1997-10-10 12:45:29 +04:00
|
|
|
whence);
|
1996-08-09 07:35:17 +04:00
|
|
|
if (command(fildes, buffer) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(status(fildes));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _rmt_ioctl --- perform raw tape operations remotely
|
|
|
|
*/
|
|
|
|
#ifdef RMTIOCTL
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
_rmt_ioctl(int fildes, unsigned long op, void *arg)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
|
|
|
char c;
|
2001-01-04 18:30:15 +03:00
|
|
|
size_t rc, cnt;
|
|
|
|
char buffer[BUFMAGIC], *p;
|
1996-08-09 07:35:17 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(arg != NULL);
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* MTIOCOP is the easy one. nothing is transfered in binary
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (op == MTIOCTOP) {
|
1997-01-23 17:01:45 +03:00
|
|
|
(void)snprintf(buffer, sizeof buffer, "I%d\n%d\n",
|
|
|
|
((struct mtop *)arg)->mt_op,
|
|
|
|
((struct mtop *)arg)->mt_count);
|
1996-08-09 07:35:17 +04:00
|
|
|
if (command(fildes, buffer) == -1)
|
|
|
|
return(-1);
|
|
|
|
return(status(fildes));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we can only handle 2 ops, if not the other one, punt
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (op != MTIOCGET) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EINVAL;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grab the status and read it directly into the structure
|
|
|
|
* this assumes that the status buffer is (hopefully) not
|
|
|
|
* padded and that 2 shorts fit in a long without any word
|
|
|
|
* alignment problems, ie - the whole struct is contiguous
|
|
|
|
* NOTE - this is probably NOT a good assumption.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (command(fildes, "S") == -1 || (rc = status(fildes)) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
p = arg;
|
|
|
|
for (; rc > 0; rc -= cnt, p += cnt) {
|
|
|
|
cnt = read(READ(fildes), p, rc);
|
|
|
|
if (cnt <= 0) {
|
1996-08-09 07:39:00 +04:00
|
|
|
rmtabort(fildes);
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EIO;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now we check for byte position. mt_type is a small integer field
|
|
|
|
* (normally) so we will check its magnitude. if it is larger than
|
|
|
|
* 256, we will assume that the bytes are swapped and go through
|
|
|
|
* and reverse all the bytes
|
|
|
|
*/
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (((struct mtget *) p)->mt_type < 256)
|
1996-08-09 07:35:17 +04:00
|
|
|
return(0);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
for (cnt = 0; cnt < rc; cnt += 2) {
|
|
|
|
c = p[cnt];
|
|
|
|
p[cnt] = p[cnt+1];
|
|
|
|
p[cnt+1] = c;
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#endif /* RMTIOCTL */
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Added routines to replace open(), close(), lseek(), ioctl(), etc.
|
|
|
|
* The preprocessor can be used to remap these the rmtopen(), etc
|
|
|
|
* thus minimizing source changes:
|
|
|
|
*
|
|
|
|
* #ifdef <something>
|
|
|
|
* # define access rmtaccess
|
|
|
|
* # define close rmtclose
|
|
|
|
* # define creat rmtcreat
|
|
|
|
* # define dup rmtdup
|
|
|
|
* # define fcntl rmtfcntl
|
|
|
|
* # define fstat rmtfstat
|
|
|
|
* # define ioctl rmtioctl
|
|
|
|
* # define isatty rmtisatty
|
|
|
|
* # define lseek rmtlseek
|
|
|
|
* # define lstat rmtlstat
|
|
|
|
* # define open rmtopen
|
|
|
|
* # define read rmtread
|
|
|
|
* # define stat rmtstat
|
|
|
|
* # define write rmtwrite
|
|
|
|
* #endif
|
|
|
|
*
|
|
|
|
* -- Fred Fish
|
|
|
|
*
|
|
|
|
* ADR --- I set up a <rmt.h> include file for this
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that local vs remote file descriptors are distinquished
|
|
|
|
* by adding a bias to the remote descriptors. This is a quick
|
|
|
|
* and dirty trick that may not be portable to some systems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define REM_BIAS 128
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test pathname to see if it is local or remote. A remote device
|
|
|
|
* is any string that contains ":/dev/". Returns 1 if remote,
|
|
|
|
* 0 otherwise.
|
|
|
|
*/
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1997-10-09 15:58:15 +04:00
|
|
|
static int
|
2001-01-04 18:30:15 +03:00
|
|
|
remdev(const char *path)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if ((path = strchr(path, ':')) != NULL) {
|
|
|
|
if (strncmp(path + 1, "/dev/", 5) == 0) {
|
1996-08-09 07:35:17 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a local or remote file. Looks just like open(2) to
|
|
|
|
* caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
1997-10-21 23:58:21 +04:00
|
|
|
rmtopen(const char *path, int oflag, ...)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1997-10-21 23:58:21 +04:00
|
|
|
mode_t mode;
|
1996-08-09 07:35:17 +04:00
|
|
|
int fd;
|
1997-10-21 23:58:21 +04:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, oflag);
|
|
|
|
|
|
|
|
mode = va_arg(ap, mode_t);
|
|
|
|
va_end(ap);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (remdev(path)) {
|
|
|
|
fd = _rmt_open(path, oflag, mode);
|
1996-08-09 07:35:17 +04:00
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
return ((fd == -1) ? -1 : (fd + REM_BIAS));
|
|
|
|
} else {
|
|
|
|
return (open(path, oflag, mode));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test pathname for specified access. Looks just like access(2)
|
|
|
|
* to caller.
|
|
|
|
*/
|
1999-07-02 19:37:33 +04:00
|
|
|
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtaccess(const char *path, int amode)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (remdev(path)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
return (0); /* Let /etc/rmt find out */
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (access(path, amode));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-06-20 08:24:23 +04:00
|
|
|
/*
|
|
|
|
* Isrmt. Let a programmer know he has a remote device.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
isrmt(int fd)
|
1997-06-20 08:24:23 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1997-06-20 08:24:23 +04:00
|
|
|
return (fd >= REM_BIAS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Read from stream. Looks just like read(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
ssize_t
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtread(int fildes, void *buf, size_t nbyte)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
|
|
|
return (_rmt_read(fildes - REM_BIAS, buf, nbyte));
|
|
|
|
} else {
|
|
|
|
return (read(fildes, buf, nbyte));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write to stream. Looks just like write(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
ssize_t
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtwrite(int fildes, const void *buf, size_t nbyte)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
|
|
|
return (_rmt_write(fildes - REM_BIAS, buf, nbyte));
|
|
|
|
} else {
|
|
|
|
return (write(fildes, buf, nbyte));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform lseek on file. Looks just like lseek(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
off_t
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtlseek(int fildes, off_t offset, int whence)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
|
|
|
return (_rmt_lseek(fildes - REM_BIAS, offset, whence));
|
|
|
|
} else {
|
|
|
|
return (lseek(fildes, offset, whence));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a file. Looks just like close(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtclose(int fildes)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
|
|
|
return (_rmt_close(fildes - REM_BIAS));
|
|
|
|
} else {
|
|
|
|
return (close(fildes));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Do ioctl on file. Looks just like ioctl(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
1997-10-21 23:58:21 +04:00
|
|
|
rmtioctl(int fildes, unsigned long request, ...)
|
|
|
|
{
|
|
|
|
char *arg;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, request);
|
|
|
|
|
|
|
|
arg = va_arg(ap, char *);
|
|
|
|
va_end(ap);
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
/* XXX: arg may be NULL ? */
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
#ifdef RMTIOCTL
|
2001-01-04 18:30:15 +03:00
|
|
|
return (_rmt_ioctl(fildes - REM_BIAS, request, arg));
|
1996-08-09 07:35:17 +04:00
|
|
|
#else
|
|
|
|
errno = EOPNOTSUPP;
|
2001-01-04 18:30:15 +03:00
|
|
|
return (-1); /* For now (fnf) */
|
1996-08-09 07:35:17 +04:00
|
|
|
#endif
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (ioctl(fildes, request, arg));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Duplicate an open file descriptor. Looks just like dup(2)
|
|
|
|
* to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtdup(int fildes)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return (-1); /* For now (fnf) */
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (dup(fildes));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Get file status. Looks just like fstat(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtfstat(int fildes, struct stat *buf)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fildes)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return (-1); /* For now (fnf) */
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (fstat(fildes, buf));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get file status. Looks just like stat(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtstat(const char *path, struct stat *buf)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (remdev(path)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return (-1); /* For now (fnf) */
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (stat(path, buf));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a file from scratch. Looks just like creat(2) to the caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtcreat(const char *path, mode_t mode)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (remdev(path)) {
|
|
|
|
return (rmtopen(path, 1 | O_CREAT, mode));
|
|
|
|
} else {
|
|
|
|
return (creat(path, mode));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Rmtfcntl. Do a remote fcntl operation.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
1997-10-21 23:58:21 +04:00
|
|
|
rmtfcntl(int fd, int cmd, ...)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1997-10-21 23:58:21 +04:00
|
|
|
void *arg;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, cmd);
|
|
|
|
|
|
|
|
arg = va_arg(ap, void *);
|
|
|
|
va_end(ap);
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
/* XXX: arg may be NULL ? */
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (isrmt(fd)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return (-1);
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (fcntl(fd, cmd, arg));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
|
1996-08-09 07:35:17 +04:00
|
|
|
/*
|
|
|
|
* Rmtisatty. Do the isatty function.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtisatty(int fd)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
2001-01-04 18:30:15 +03:00
|
|
|
|
|
|
|
if (isrmt(fd))
|
1996-08-09 07:35:17 +04:00
|
|
|
return (0);
|
|
|
|
else
|
2001-01-04 18:30:15 +03:00
|
|
|
return (isatty(fd));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get file status, even if symlink. Looks just like lstat(2) to caller.
|
|
|
|
*/
|
1997-10-09 15:58:15 +04:00
|
|
|
int
|
2001-01-04 18:30:15 +03:00
|
|
|
rmtlstat(const char *path, struct stat *buf)
|
1996-08-09 07:35:17 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(path != NULL);
|
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
|
2001-01-04 18:30:15 +03:00
|
|
|
if (remdev(path)) {
|
1996-08-09 07:35:17 +04:00
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return (-1); /* For now (fnf) */
|
2001-01-04 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
return (lstat(path, buf));
|
1996-08-09 07:35:17 +04:00
|
|
|
}
|
|
|
|
}
|