Module Name:    src
Committed By:   christos
Date:           Tue May 31 12:24:33 UTC 2011

Modified Files:
        src/lib/librmt: rmtlib.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/lib/librmt/rmtlib.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/librmt/rmtlib.c
diff -u src/lib/librmt/rmtlib.c:1.23 src/lib/librmt/rmtlib.c:1.24
--- src/lib/librmt/rmtlib.c:1.23	Fri Feb 18 11:10:09 2011
+++ src/lib/librmt/rmtlib.c	Tue May 31 08:24:33 2011
@@ -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 <sys/cdefs.h>
-__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 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <err.h>
 
 #ifdef USE_REXEC
 #include <netdb.h>
@@ -66,7 +67,7 @@
 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 @@
  *	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 @@
 	pstat = signal(SIGPIPE, SIG_IGN);
 	if (write(WRITE(fildes), buf, blen) == blen) {
 		signal(SIGPIPE, pstat);
-		return (0);
+		return 0;
 	}
 
 /*
@@ -126,7 +127,7 @@
 	rmtabort(fildes);
 
 	errno = EIO;
-	return (-1);
+	return -1;
 }
 
 
@@ -148,7 +149,7 @@
 		if (read(READ(fildes), cp, 1) != 1) {
 			rmtabort(fildes);
 			errno = EIO;
-			return (-1);
+			return -1;
 		}
 		if (*cp == '\n') {
 			*cp = 0;
@@ -159,7 +160,7 @@
 	if (i == BUFMAGIC) {
 		rmtabort(fildes);
 		errno = EIO;
-		return (-1);
+		return -1;
 	}
 
 /*
@@ -179,7 +180,7 @@
 		if (*cp == 'F')
 			rmtabort(fildes);
 
-		return (-1);
+		return -1;
 	}
 
 /*
@@ -189,10 +190,10 @@
 	if (*cp != 'A') {
 		rmtabort(fildes);
 		errno = EIO;
-		return (-1);
+		return -1;
 	}
 
-	return (atoi(cp + 1));
+	return atoi(cp + 1);
 }
 
 
@@ -222,14 +223,12 @@
 	/* 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 @@
 /*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 @@
 
 	if (i == MAXUNIT) {
 		errno = EMFILE;
-		return (-1);
+		return -1;
 	}
 
 /*
@@ -324,21 +324,20 @@
  */
 	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);
-
-	if ((rc = fork()) == -1)
-		return (-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 @@
 			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 @@
 
 	(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 @@
 		rc = status(fildes);
 
 		rmtabort(fildes);
-		return (rc);
+		return rc;
 	}
 
-	return (-1);
+	return -1;
 }
 
 
@@ -419,18 +418,18 @@
 
 	_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 @@
 _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 @@
 	(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 @@
 	size_t rc;
 	ssize_t cnt;
 	char buffer[BUFMAGIC], *p;
+	struct mtop *mtop = arg;
 
 	_DIAGASSERT(arg != NULL);
 
@@ -505,11 +505,10 @@
 
 	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 @@
 
 	if (op != MTIOCGET) {
 		errno = EINVAL;
-		return (-1);
+		return -1;
 	}
 
 /*
@@ -530,13 +529,14 @@
  */
 
 	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 @@
  */
 
 	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 @@
 
 	if ((path = strchr(path, ':')) != NULL) {
 		if (strncmp(path + 1, "/dev/", 5) == 0) {
-			return (1);
+			return 1;
 		}
 	}
-	return (0);
+	return 0;
 }
 
 
@@ -639,9 +639,9 @@
 	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 @@
 	_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 @@
 	_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 @@
 	_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 @@
 {
 
 	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 @@
 {
 
 	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 @@
 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 @@
 
 	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 @@
 
 	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 @@
 
 	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 @@
 	_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 @@
 
 	if (isrmt(fd)) {
 		errno = EOPNOTSUPP;
-		return (-1);
+		return -1;
 	} else {
-		return (fcntl(fd, cmd, arg));
+		return fcntl(fd, cmd, arg);
 	}
 }
 
@@ -871,9 +871,9 @@
 {
 
 	if (isrmt(fd))
-		return (0);
+		return 0;
 	else
-		return (isatty(fd));
+		return isatty(fd);
 }
 
 
@@ -889,8 +889,8 @@
 
 	if (remdev(path)) {
 		errno = EOPNOTSUPP;
-		return (-1);		/* For now (fnf) */
+		return -1;		/* For now (fnf) */
 	} else {
-		return (lstat(path, buf));
+		return lstat(path, buf);
 	}
 }

Reply via email to