Source-Changes-HG archive

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

[src/trunk]: src/lib/libutil Enforce is iscooked arg;



details:   https://anonhg.NetBSD.org/src/rev/a15b5e7313df
branches:  trunk
changeset: 516882:a15b5e7313df
user:      lukem <lukem%NetBSD.org@localhost>
date:      Thu Nov 01 06:53:24 2001 +0000

description:
Enforce is iscooked arg;
        if zero, a matching path must be of type S_IFCHR, and
        if non-zero, a matching path must be of type S_IFBLK.

diffstat:

 lib/libutil/opendisk.3 |  14 ++++++++-
 lib/libutil/opendisk.c |  69 +++++++++++++++++++++++++++++++------------------
 2 files changed, 55 insertions(+), 28 deletions(-)

diffs (143 lines):

diff -r 873dcb241e7e -r a15b5e7313df lib/libutil/opendisk.3
--- a/lib/libutil/opendisk.3    Thu Nov 01 03:49:30 2001 +0000
+++ b/lib/libutil/opendisk.3    Thu Nov 01 06:53:24 2001 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: opendisk.3,v 1.5 2001/09/26 07:21:42 lukem Exp $
+.\"    $NetBSD: opendisk.3,v 1.6 2001/11/01 06:53:24 lukem Exp $
 .\"
 .\" Copyright (c) 1997, 2001 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -34,7 +34,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd September 26, 2001
+.Dd November 1, 2001
 .Dt OPENDISK 3
 .Os
 .Sh NAME
@@ -164,6 +164,16 @@
 was the
 .Dv NULL
 pointer.
+.It Bq Er ENODEV
+A matching filename was found, but it was the incorrect file type;
+if
+.Fa iscooked
+is zero, the type must be
+.Dv S_IFCHR ,
+and if
+.Fa iscooked
+is non-zero, the type must be
+.Dv S_IFBLK .
 .El
 .Pp
 The
diff -r 873dcb241e7e -r a15b5e7313df lib/libutil/opendisk.c
--- a/lib/libutil/opendisk.c    Thu Nov 01 03:49:30 2001 +0000
+++ b/lib/libutil/opendisk.c    Thu Nov 01 06:53:24 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: opendisk.c,v 1.7 2000/07/05 11:46:41 ad Exp $  */
+/*     $NetBSD: opendisk.c,v 1.8 2001/11/01 06:53:25 lukem Exp $       */
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: opendisk.c,v 1.7 2000/07/05 11:46:41 ad Exp $");
+__RCSID("$NetBSD: opendisk.c,v 1.8 2001/11/01 06:53:25 lukem Exp $");
 #endif
 
 #include <sys/param.h>
@@ -46,22 +46,22 @@
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <util.h>
 #include <paths.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+#include <util.h>
 
 int
 opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked)
 {
-       int f, rawpart;
+       struct stat sb;
+       int     f, pass, rawpart, serrno;
 
        if (buf == NULL) {
                errno = EFAULT;
                return (-1);
        }
-       snprintf(buf, buflen, "%s", path);
-
        if ((flags & O_CREAT) != 0) {
                errno = EINVAL;
                return (-1);
@@ -71,25 +71,42 @@
        if (rawpart < 0)
                return (-1);    /* sysctl(3) in getrawpartition sets errno */
 
-       f = open(buf, flags);
-       if (f != -1 || errno != ENOENT)
-               return (f);
-
-       snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
-       f = open(buf, flags);
-       if (f != -1 || errno != ENOENT)
-               return (f);
-
-       if (strchr(path, '/') != NULL)
-               return (-1);
-
-       snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
-       f = open(buf, flags);
-       if (f != -1 || errno != ENOENT)
-               return (f);
-
-       snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
-           'a' + rawpart);
-       f = open(buf, flags);
+       for (pass = 0; pass < 4; pass++) {
+               switch (pass) {
+               case 0:
+                       snprintf(buf, buflen, "%s", path);
+                       break;
+               case 1:
+                       snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
+                       break;
+               case 2:
+                       if (strchr(path, '/') != NULL)
+                               return (-1);
+                       snprintf(buf, buflen, "%s%s%s", _PATH_DEV,
+                           iscooked ? "" : "r", path);
+                       break;
+               case 3:
+                       snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV,
+                           iscooked ? "" : "r", path, 'a' + rawpart);
+                       break;
+               }
+               f = open(buf, flags);
+               if (f == -1 && errno != ENOENT)
+                       return (-1);
+               if (f != -1) {
+                       if (fstat(f, &sb) == -1) {
+                               serrno = errno;
+                               close (f);
+                               errno = serrno;
+                               f = -1;
+                       } else if ((!iscooked && !S_ISCHR(sb.st_mode)) ||
+                                  ( iscooked && !S_ISBLK(sb.st_mode)) ) {
+                               close (f);
+                               errno = ENODEV;
+                               f = -1;
+                       }
+                       return (f);
+               }
+       }
        return (f);
 }



Home | Main Index | Thread Index | Old Index