Module Name:    src
Committed By:   snj
Date:           Sun Jan 29 05:17:33 UTC 2017

Modified Files:
        src/lib/libutil [netbsd-7]: opendisk.3 opendisk.c

Log Message:
Pull up following revision(s) (requested by pgoyette in ticket #1349):
        lib/libutil/opendisk.c: revision 1.14
        lib/libutil/opendisk.3: revision 1.12, 1.13
PR/51216: Instead of trying to open files in the current working
directory first for paths that don't contain "/", first try the
/dev paths to avoid confusion with files in the working directory
that happen to match disk names.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.11.42.1 src/lib/libutil/opendisk.3
cvs rdiff -u -r1.12 -r1.12.24.1 src/lib/libutil/opendisk.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/libutil/opendisk.3
diff -u src/lib/libutil/opendisk.3:1.11 src/lib/libutil/opendisk.3:1.11.42.1
--- src/lib/libutil/opendisk.3:1.11	Wed Apr 30 13:10:52 2008
+++ src/lib/libutil/opendisk.3	Sun Jan 29 05:17:33 2017
@@ -1,4 +1,4 @@
-.\"	$NetBSD: opendisk.3,v 1.11 2008/04/30 13:10:52 martin Exp $
+.\"	$NetBSD: opendisk.3,v 1.11.42.1 2017/01/29 05:17:33 snj Exp $
 .\"
 .\" Copyright (c) 1997, 2001 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd December 11, 2001
+.Dd June 6, 2016
 .Dt OPENDISK 3
 .Os
 .Sh NAME
@@ -71,20 +71,6 @@ are tried.
 attempts to open the following variations of
 .Fa path ,
 in order:
-.Bl -tag -width "/dev/rpathX"
-.It Pa path
-The pathname as given.
-.It Pa path Ns Em X
-.Fa path
-with a suffix of
-.Sq Em X ,
-where
-.Sq Em X
-represents the raw partition of the device, as determined by
-.Xr getrawpartition 3 ,
-usually
-.Dq c .
-.El
 .Pp
 If
 .Fa path
@@ -93,13 +79,12 @@ slash
 .Pq Dq / ,
 the following variations are attempted:
 .Pp
-.Bl -dash -offset indent
+.Bl -dash -compact
 .It
 If
 .Fa iscooked
 is zero:
-.Pp
-.Bl -tag -width "/dev/rpathX"
+.Bl -tag -compact -width "/dev/rpathX"
 .It Pa /dev/rpath
 .Fa path
 with a prefix of
@@ -116,7 +101,7 @@ and a suffix of
 If
 .Fa iscooked
 is non-zero:
-.Bl -tag -width "/dev/rpathX"
+.Bl -tag -compact -width "/dev/rpathX"
 .It Pa /dev/path
 .Fa path
 with a prefix of
@@ -130,6 +115,31 @@ and a suffix of
 (q.v.).
 .El
 .El
+.Pp
+If the above fails, than the original
+.Fa path
+is tried using the following two variations:
+.Pp
+.Bl -dash -compact
+.It
+The
+.Fa iscooked
+value is ignored:
+.Bl -tag -compact -width "/dev/rpathX"
+.It Pa path
+The pathname as given.
+.It Pa path Ns Em X
+.Fa path
+with a suffix of
+.Sq Em X ,
+where
+.Sq Em X
+represents the raw partition of the device, as determined by
+.Xr getrawpartition 3 ,
+usually
+.Dq c .
+.El
+.El
 .Sh RETURN VALUES
 An open file descriptor, or -1 if the
 .Xr open 2
@@ -170,3 +180,11 @@ The
 .Fn opendisk
 function first appeared in
 .Nx 1.3 .
+.Pp
+The lookup order of
+.Fn opendisk
+was changed in
+.Nx 7.1
+to first look in
+.Pa /dev
+in order to avoid opening random files in the current working directory.

Index: src/lib/libutil/opendisk.c
diff -u src/lib/libutil/opendisk.c:1.12 src/lib/libutil/opendisk.c:1.12.24.1
--- src/lib/libutil/opendisk.c:1.12	Tue Oct 13 22:00:31 2009
+++ src/lib/libutil/opendisk.c	Sun Jan 29 05:17:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: opendisk.c,v 1.12 2009/10/13 22:00:31 pooka Exp $	*/
+/*	$NetBSD: opendisk.c,v 1.12.24.1 2017/01/29 05:17:33 snj Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -31,61 +31,76 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: opendisk.c,v 1.12 2009/10/13 22:00:31 pooka Exp $");
+__RCSID("$NetBSD: opendisk.c,v 1.12.24.1 2017/01/29 05:17:33 snj Exp $");
 #endif
 
 #include <sys/param.h>
 
 #include <assert.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <fcntl.h>
 #include <util.h>
 #include <paths.h>
 #include <stdio.h>
 #include <string.h>
 
+static int __printflike(5, 6)
+opd(char *buf, size_t len, int (*ofn)(const char *, int, ...),
+    int flags, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, len, fmt, ap);
+	va_end(ap);
+
+	return (*ofn)(buf, flags, 0);
+}
+
 static int
 __opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
 	int (*ofn)(const char *, int, ...))
 {
-	int f, rawpart;
+	int f, part;
 
 	if (buf == NULL) {
 		errno = EFAULT;
-		return (-1);
+		return -1;
 	}
-	snprintf(buf, buflen, "%s", path);
 
 	if ((flags & O_CREAT) != 0) {
 		errno = EINVAL;
-		return (-1);
+		return -1;
 	}
 
-	rawpart = getrawpartition();
-	if (rawpart < 0)
-		return (-1);	/* sysctl(3) in getrawpartition sets errno */
-
-	f = ofn(buf, flags, 0);
-	if (f != -1 || errno != ENOENT)
-		return (f);
-
-	snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
-	f = ofn(buf, flags, 0);
-	if (f != -1 || errno != ENOENT)
-		return (f);
-
-	if (strchr(path, '/') != NULL)
-		return (-1);
+	part = getrawpartition();
+	if (part < 0)
+		return -1;	/* sysctl(3) in getrawpartition sets errno */
+	part += 'a';
+
+	/*
+	 * If we are passed a plain name, first try /dev to avoid accidents
+	 * with files in the same directory that happen to match disk names.
+	 */
+	if (strchr(path, '/') == NULL) {
+		const char *r = iscooked ? "" : "r";
+		const char *d = _PATH_DEV;
+
+		f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
+		if (f != -1 || errno != ENOENT)
+			return f;
+
+		f = opd(buf, buflen, ofn, flags, "%s%s%s%c", d, r, path, part);
+		if (f != -1 || errno != ENOENT)
+			return f;
+	}
 
-	snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
-	f = ofn(buf, flags, 0);
+	f = opd(buf, buflen, ofn, flags, "%s", path);
 	if (f != -1 || errno != ENOENT)
-		return (f);
+		return f;
 
-	snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
-	    'a' + rawpart);
-	f = ofn(buf, flags, 0);
-	return (f);
+	return opd(buf, buflen, ofn, flags, "%s%c", path, part);
 }
 
 int

Reply via email to