Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7]: src/lib/libutil Pull up following revision(s) (requested by p...



details:   https://anonhg.NetBSD.org/src/rev/385221545bf3
branches:  netbsd-7
changeset: 800116:385221545bf3
user:      snj <snj%NetBSD.org@localhost>
date:      Sun Jan 29 05:17:33 2017 +0000

description:
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.

diffstat:

 lib/libutil/opendisk.3 |  58 ++++++++++++++++++++++++++++--------------
 lib/libutil/opendisk.c |  67 ++++++++++++++++++++++++++++++-------------------
 2 files changed, 79 insertions(+), 46 deletions(-)

diffs (219 lines):

diff -r 5d703fc8fa6a -r 385221545bf3 lib/libutil/opendisk.3
--- a/lib/libutil/opendisk.3    Sun Jan 22 05:53:52 2017 +0000
+++ b/lib/libutil/opendisk.3    Sun Jan 29 05:17:33 2017 +0000
@@ -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 @@
 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 @@
 .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 @@
 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 @@
 (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 @@
 .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.
diff -r 5d703fc8fa6a -r 385221545bf3 lib/libutil/opendisk.c
--- a/lib/libutil/opendisk.c    Sun Jan 22 05:53:52 2017 +0000
+++ b/lib/libutil/opendisk.c    Sun Jan 29 05:17:33 2017 +0000
@@ -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 */
+       part = getrawpartition();
+       if (part < 0)
+               return -1;      /* sysctl(3) in getrawpartition sets errno */
+       part += 'a';
 
-       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 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;
 
-       if (strchr(path, '/') != NULL)
-               return (-1);
+               f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
+               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%s%s%c", d, r, path, part);
+               if (f != -1 || errno != ENOENT)
+                       return f;
+       }
+
+       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



Home | Main Index | Thread Index | Old Index