From 88b197ec7f861a0a652b8971ceed4c3d5c971e4b Mon Sep 17 00:00:00 2001 From: christos Date: Tue, 31 May 2011 12:24:33 +0000 Subject: [PATCH] PR/38413: Takahiro Kambe: mt(1) print some junk output when using remote tape Not all fields are valid in the ioctl to get tape info in the rmt protocol. Zero out the struct so that we don't print junk. While here, KNF. --- lib/librmt/rmtlib.c | 184 ++++++++++++++++++++++---------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/lib/librmt/rmtlib.c b/lib/librmt/rmtlib.c index 5180f223ecea..6a4c451ea408 100644 --- a/lib/librmt/rmtlib.c +++ b/lib/librmt/rmtlib.c @@ -1,4 +1,4 @@ -/* $NetBSD: rmtlib.c,v 1.23 2011/02/18 16:10:09 pooka Exp $ */ +/* $NetBSD: rmtlib.c,v 1.24 2011/05/31 12:24:33 christos Exp $ */ /* * rmt --- remote tape emulator subroutines @@ -28,7 +28,7 @@ */ #include -__RCSID("$NetBSD: rmtlib.c,v 1.23 2011/02/18 16:10:09 pooka Exp $"); +__RCSID("$NetBSD: rmtlib.c,v 1.24 2011/05/31 12:24:33 christos Exp $"); #define RMTIOCTL 1 /* #define USE_REXEC 1 */ /* rexec code courtesy of Dan Kegel, srs!dan */ @@ -50,6 +50,7 @@ __RCSID("$NetBSD: rmtlib.c,v 1.23 2011/02/18 16:10:09 pooka Exp $"); #include #include #include +#include #ifdef USE_REXEC #include @@ -66,7 +67,7 @@ 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 command(int, const char *); static int remdev(const char *); static void rmtabort(int); static int status(int); @@ -100,10 +101,10 @@ rmtabort(int fildes) * command --- attempt to perform a remote tape command */ static int -command(int fildes, char *buf) +command(int fildes, const char *buf) { size_t blen; - void (*pstat)(int); + sig_t pstat; _DIAGASSERT(buf != NULL); @@ -115,7 +116,7 @@ command(int fildes, char *buf) pstat = signal(SIGPIPE, SIG_IGN); if (write(WRITE(fildes), buf, blen) == blen) { signal(SIGPIPE, pstat); - return (0); + return 0; } /* @@ -126,7 +127,7 @@ command(int fildes, char *buf) rmtabort(fildes); errno = EIO; - return (-1); + return -1; } @@ -148,7 +149,7 @@ status(int fildes) if (read(READ(fildes), cp, 1) != 1) { rmtabort(fildes); errno = EIO; - return (-1); + return -1; } if (*cp == '\n') { *cp = 0; @@ -159,7 +160,7 @@ status(int fildes) if (i == BUFMAGIC) { rmtabort(fildes); errno = EIO; - return (-1); + return -1; } /* @@ -179,7 +180,7 @@ status(int fildes) if (*cp == 'F') rmtabort(fildes); - return (-1); + return -1; } /* @@ -189,10 +190,10 @@ status(int fildes) if (*cp != 'A') { rmtabort(fildes); errno = EIO; - return (-1); + return -1; } - return (atoi(cp + 1)); + return atoi(cp + 1); } @@ -222,14 +223,12 @@ _rmt_rexec(const char *host, const char *user) /* user may be NULL */ rexecserv = getservbyname("exec", "tcp"); - if (rexecserv == NULL) { - fprintf(stderr, "? exec/tcp: service not available."); - exit(1); - } + if (rexecserv == NULL) + errx(1, exec/tcp: service not available."); if ((user != NULL) && *user == '\0') user = NULL; - return (rexec(&host, rexecserv->s_port, user, NULL, - "/etc/rmt", NULL)); + return rexec(&host, rexecserv->s_port, user, NULL, + "/etc/rmt", NULL); } #endif /* USE_REXEC */ @@ -249,12 +248,13 @@ static int /*ARGSUSED*/ _rmt_open(const char *path, int oflag, int mode) { - int i, rc; + int i; char buffer[BUFMAGIC]; char host[MAXHOSTLEN]; char device[BUFMAGIC]; char login[BUFMAGIC]; char *sys, *dev, *user; + char *rshpath, *rsh; _DIAGASSERT(path != NULL); @@ -272,7 +272,7 @@ _rmt_open(const char *path, int oflag, int mode) if (i == MAXUNIT) { errno = EMFILE; - return (-1); + return -1; } /* @@ -324,21 +324,20 @@ _rmt_open(const char *path, int oflag, int mode) */ READ(i) = WRITE(i) = _rmt_rexec(host, login); if (READ(i) < 0) - return (-1); + return -1; #else /* * setup the pipes for the 'rsh' command and fork */ if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1) - return (-1); + return -1; - if ((rc = fork()) == -1) - return (-1); - - if (rc == 0) { - char *rshpath, *rsh; + switch (fork()) { + case -1: + return -1; + case 0: close(0); dup(Ptc[i][0]); close(Ptc[i][0]); close(Ptc[i][1]); @@ -356,19 +355,19 @@ _rmt_open(const char *path, int oflag, int mode) rsh++; if (*login) { - execl(rshpath, rsh, host, "-l", login, - _PATH_RMT, NULL); + execl(rshpath, rsh, host, "-l", login, _PATH_RMT, NULL); } else { - execl(rshpath, rsh, host, - _PATH_RMT, NULL); + execl(rshpath, rsh, host, _PATH_RMT, NULL); } /* * bad problems if we get here */ - perror("exec"); - exit(1); + err(1, "Cannnot exec %s", rshpath); + /*FALLTHROUGH*/ + default: + break; } close(Ptc[i][0]); close(Ctp[i][1]); @@ -380,9 +379,9 @@ _rmt_open(const char *path, int oflag, int mode) (void)snprintf(buffer, sizeof(buffer), "O%s\n%d\n", device, oflag); if (command(i, buffer) == -1 || status(i) == -1) - return (-1); + return -1; - return (i); + return i; } @@ -398,10 +397,10 @@ _rmt_close(int fildes) rc = status(fildes); rmtabort(fildes); - return (rc); + return rc; } - return (-1); + return -1; } @@ -419,18 +418,18 @@ _rmt_read(int fildes, void *buf, size_t nbyte) _DIAGASSERT(buf != NULL); - (void)snprintf(buffer, sizeof buffer, "R%lu\n", (u_long)nbyte); + (void)snprintf(buffer, sizeof buffer, "R%zu\n", nbyte); if (command(fildes, buffer) == -1 || (rv = status(fildes)) == -1) - return (-1); + return -1; if (rv > nbyte) - rv = nbyte; + rv = (int)nbyte; for (rc = rv, p = buf; rc > 0; rc -= nread, p += nread) { if ((nread = read(READ(fildes), p, rc)) <= 0) { rmtabort(fildes); errno = EIO; - return (-1); + return -1; } } @@ -445,24 +444,24 @@ static ssize_t _rmt_write(int fildes, const void *buf, size_t nbyte) { char buffer[BUFMAGIC]; - void (*pstat)(int); + sig_t pstat; _DIAGASSERT(buf != NULL); - (void)snprintf(buffer, sizeof buffer, "W%lu\n", (u_long)nbyte); + (void)snprintf(buffer, sizeof buffer, "W%zu\n", nbyte); if (command(fildes, buffer) == -1) - return (-1); + return -1; pstat = signal(SIGPIPE, SIG_IGN); if (write(WRITE(fildes), buf, nbyte) == nbyte) { signal(SIGPIPE, pstat); - return (status(fildes)); + return status(fildes); } signal(SIGPIPE, pstat); rmtabort(fildes); errno = EIO; - return (-1); + return -1; } @@ -478,9 +477,9 @@ _rmt_lseek(int fildes, off_t offset, int whence) (void)snprintf(buffer, sizeof buffer, "L%lld\n%d\n", (long long)offset, whence); if (command(fildes, buffer) == -1) - return (-1); + return -1; - return (status(fildes)); + return status(fildes); } @@ -496,6 +495,7 @@ _rmt_ioctl(int fildes, unsigned long op, void *arg) size_t rc; ssize_t cnt; char buffer[BUFMAGIC], *p; + struct mtop *mtop = arg; _DIAGASSERT(arg != NULL); @@ -505,11 +505,10 @@ _rmt_ioctl(int fildes, unsigned long op, void *arg) if (op == MTIOCTOP) { (void)snprintf(buffer, sizeof buffer, "I%d\n%d\n", - ((struct mtop *)arg)->mt_op, - ((struct mtop *)arg)->mt_count); + mtop->mt_op, mtop->mt_count); if (command(fildes, buffer) == -1) - return (-1); - return (status(fildes)); + return -1; + return status(fildes); } /* @@ -518,7 +517,7 @@ _rmt_ioctl(int fildes, unsigned long op, void *arg) if (op != MTIOCGET) { errno = EINVAL; - return (-1); + return -1; } /* @@ -530,13 +529,14 @@ _rmt_ioctl(int fildes, unsigned long op, void *arg) */ if (command(fildes, "S") == -1 || (rv = status(fildes)) == -1) - return (-1); + return -1; + memset(arg, 0, sizeof(struct mtget)); for (rc = rv, p = arg; rc > 0; rc -= cnt, p += cnt) { if ((cnt = read(READ(fildes), p, rc)) <= 0) { rmtabort(fildes); errno = EIO; - return (-1); + return -1; } } @@ -548,15 +548,15 @@ _rmt_ioctl(int fildes, unsigned long op, void *arg) */ if (((struct mtget *)(void *)p)->mt_type < 256) - return (0); + return 0; for (cnt = 0; cnt < rv; cnt += 2) { c = p[cnt]; - p[cnt] = p[cnt+1]; - p[cnt+1] = c; + p[cnt] = p[cnt + 1]; + p[cnt + 1] = c; } - return (0); + return 0; } #endif /* RMTIOCTL */ @@ -612,10 +612,10 @@ remdev(const char *path) if ((path = strchr(path, ':')) != NULL) { if (strncmp(path + 1, "/dev/", 5) == 0) { - return (1); + return 1; } } - return (0); + return 0; } @@ -639,9 +639,9 @@ rmtopen(const char *path, int oflag, ...) if (remdev(path)) { fd = _rmt_open(path, oflag, (int)mode); - return ((fd == -1) ? -1 : (fd + REM_BIAS)); + return (fd == -1) ? -1 : (fd + REM_BIAS); } else { - return (open(path, oflag, mode)); + return open(path, oflag, mode); } } @@ -657,9 +657,9 @@ rmtaccess(const char *path, int amode) _DIAGASSERT(path != NULL); if (remdev(path)) { - return (0); /* Let /etc/rmt find out */ + return 0; /* Let /etc/rmt find out */ } else { - return (access(path, amode)); + return access(path, amode); } } @@ -687,9 +687,9 @@ rmtread(int fildes, void *buf, size_t nbyte) _DIAGASSERT(buf != NULL); if (isrmt(fildes)) { - return (_rmt_read(fildes - REM_BIAS, buf, nbyte)); + return _rmt_read(fildes - REM_BIAS, buf, nbyte); } else { - return (read(fildes, buf, nbyte)); + return read(fildes, buf, nbyte); } } @@ -704,9 +704,9 @@ rmtwrite(int fildes, const void *buf, size_t nbyte) _DIAGASSERT(buf != NULL); if (isrmt(fildes)) { - return (_rmt_write(fildes - REM_BIAS, buf, nbyte)); + return _rmt_write(fildes - REM_BIAS, buf, nbyte); } else { - return (write(fildes, buf, nbyte)); + return write(fildes, buf, nbyte); } } @@ -718,9 +718,9 @@ rmtlseek(int fildes, off_t offset, int whence) { if (isrmt(fildes)) { - return (_rmt_lseek(fildes - REM_BIAS, offset, whence)); + return _rmt_lseek(fildes - REM_BIAS, offset, whence); } else { - return (lseek(fildes, offset, whence)); + return lseek(fildes, offset, whence); } } @@ -733,9 +733,9 @@ rmtclose(int fildes) { if (isrmt(fildes)) { - return (_rmt_close(fildes - REM_BIAS)); + return _rmt_close(fildes - REM_BIAS); } else { - return (close(fildes)); + return close(fildes); } } @@ -746,24 +746,24 @@ rmtclose(int fildes) int rmtioctl(int fildes, unsigned long request, ...) { - char *arg; + void *arg; va_list ap; va_start(ap, request); - arg = va_arg(ap, char *); + arg = va_arg(ap, void *); va_end(ap); /* XXX: arg may be NULL ? */ if (isrmt(fildes)) { #ifdef RMTIOCTL - return (_rmt_ioctl(fildes - REM_BIAS, request, arg)); + return _rmt_ioctl(fildes - REM_BIAS, request, arg); #else errno = EOPNOTSUPP; - return (-1); /* For now (fnf) */ + return -1; /* For now (fnf) */ #endif } else { - return (ioctl(fildes, request, arg)); + return ioctl(fildes, request, arg); } } @@ -778,9 +778,9 @@ rmtdup(int fildes) if (isrmt(fildes)) { errno = EOPNOTSUPP; - return (-1); /* For now (fnf) */ + return -1; /* For now (fnf) */ } else { - return (dup(fildes)); + return dup(fildes); } } @@ -796,9 +796,9 @@ rmtfstat(int fildes, struct stat *buf) if (isrmt(fildes)) { errno = EOPNOTSUPP; - return (-1); /* For now (fnf) */ + return -1; /* For now (fnf) */ } else { - return (fstat(fildes, buf)); + return fstat(fildes, buf); } } @@ -815,9 +815,9 @@ rmtstat(const char *path, struct stat *buf) if (remdev(path)) { errno = EOPNOTSUPP; - return (-1); /* For now (fnf) */ + return -1; /* For now (fnf) */ } else { - return (stat(path, buf)); + return stat(path, buf); } } @@ -832,9 +832,9 @@ rmtcreat(const char *path, mode_t mode) _DIAGASSERT(path != NULL); if (remdev(path)) { - return (rmtopen(path, 1 | O_CREAT, mode)); + return rmtopen(path, O_WRONLY | O_CREAT, mode); } else { - return (creat(path, mode)); + return open(path, O_CREAT | O_TRUNC | O_WRONLY, mode); } } @@ -856,9 +856,9 @@ rmtfcntl(int fd, int cmd, ...) if (isrmt(fd)) { errno = EOPNOTSUPP; - return (-1); + return -1; } else { - return (fcntl(fd, cmd, arg)); + return fcntl(fd, cmd, arg); } } @@ -871,9 +871,9 @@ rmtisatty(int fd) { if (isrmt(fd)) - return (0); + return 0; else - return (isatty(fd)); + return isatty(fd); } @@ -889,8 +889,8 @@ rmtlstat(const char *path, struct stat *buf) if (remdev(path)) { errno = EOPNOTSUPP; - return (-1); /* For now (fnf) */ + return -1; /* For now (fnf) */ } else { - return (lstat(path, buf)); + return lstat(path, buf); } }