Module Name:    src
Committed By:   christos
Date:           Sat Apr  7 03:13:32 UTC 2012

Modified Files:
        src/sbin/mount: Makefile mount.c

Log Message:
Accept NAME=label for special so that we can wire down our wedges.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sbin/mount/Makefile
cvs rdiff -u -r1.93 -r1.94 src/sbin/mount/mount.c

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

Modified files:

Index: src/sbin/mount/Makefile
diff -u src/sbin/mount/Makefile:1.29 src/sbin/mount/Makefile:1.30
--- src/sbin/mount/Makefile:1.29	Thu Aug 19 19:02:27 2004
+++ src/sbin/mount/Makefile	Fri Apr  6 23:13:32 2012
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.29 2004/08/19 23:02:27 christos Exp $
+#	$NetBSD: Makefile,v 1.30 2012/04/07 03:13:32 christos Exp $
 #	@(#)Makefile	8.6 (Berkeley) 5/8/95
 
 .include <bsd.own.mk>
@@ -6,5 +6,6 @@
 PROG=	mount
 MAN=	mount.8
 SRCS=	mount.c vfslist.c
+LDADD+= -lutil
 
 .include <bsd.prog.mk>

Index: src/sbin/mount/mount.c
diff -u src/sbin/mount/mount.c:1.93 src/sbin/mount/mount.c:1.94
--- src/sbin/mount/mount.c:1.93	Mon Aug 29 10:35:00 2011
+++ src/sbin/mount/mount.c	Fri Apr  6 23:13:32 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: mount.c,v 1.93 2011/08/29 14:35:00 joerg Exp $	*/
+/*	$NetBSD: mount.c,v 1.94 2012/04/07 03:13:32 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1989, 1993, 1994
@@ -39,13 +39,15 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
 #else
-__RCSID("$NetBSD: mount.c,v 1.93 2011/08/29 14:35:00 joerg Exp $");
+__RCSID("$NetBSD: mount.c,v 1.94 2012/04/07 03:13:32 christos Exp $");
 #endif
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/mount.h>
 #include <sys/wait.h>
+#include <sys/disk.h>
+#include <sys/sysctl.h>
 
 #include <fs/puffs/puffs_msgif.h>
 
@@ -58,6 +60,8 @@ __RCSID("$NetBSD: mount.c,v 1.93 2011/08
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <util.h>
+#include <vis.h>
 
 #define MOUNTNAMES
 #include <fcntl.h>
@@ -76,6 +80,8 @@ static const char *
 static struct statvfs *
 		getmntpt(const char *);
 static int 	getmntargs(struct statvfs *, char *, size_t);
+static const char *
+		getspec(char *, size_t, const char *);
 static int	hasopt(const char *, const char *);
 static void	mangle(char *, int *, const char ** volatile *, int *);
 static int	mountfs(const char *, const char *, const char *,
@@ -93,7 +99,7 @@ static const struct opt {
 	__MNT_FLAGS
 };
 
-static char ffs_fstype[] = "ffs";
+static const char ffs_fstype[] = "ffs";
 
 int
 main(int argc, char *argv[])
@@ -107,7 +113,7 @@ main(int argc, char *argv[])
 	int all, ch, forceall, i, init_flags, mntsize, rval;
 	char *options;
 	const char *mountopts, *fstypename;
-	char canonical_path_buf[MAXPATHLEN];
+	char canonical_path_buf[MAXPATHLEN], buf[MAXPATHLEN];
 	char *canonical_path;
 
 	/* started as "mount" */
@@ -138,8 +144,8 @@ main(int argc, char *argv[])
 			break;
 		case 't':
 			if (vfslist != NULL)
-				errx(1,
-				    "only one -t option may be specified.");
+				errx(EXIT_FAILURE,
+				    "Only one -t option may be specified.");
 			vfslist = makevfslist(optarg);
 			vfstype = optarg;
 			break;
@@ -176,13 +182,16 @@ main(int argc, char *argv[])
 				if (hasopt(fs->fs_mntops, "noauto"))
 					continue;
 				if (strcmp(fs->fs_spec, "from_mount") == 0) {
-					if ((mntbuf = getmntpt(fs->fs_file)) == NULL)
-						errx(1,
-						    "unknown file system %s.",
+					if ((mntbuf = getmntpt(fs->fs_file))
+					    == NULL)
+						errx(EXIT_FAILURE,
+						    "Unknown file system %s",
 						    fs->fs_file);
 					mntfromname = mntbuf->f_mntfromname;
 				} else
 					mntfromname = fs->fs_spec;
+				mntfromname =
+				    getspec(buf, sizeof(buf), mntfromname);
 				if (mountfs(fs->fs_vfstype, mntfromname,
 				    fs->fs_file, init_flags, options,
 				    fs->fs_mntops, !forceall, NULL, 0))
@@ -190,7 +199,7 @@ main(int argc, char *argv[])
 			}
 		else {
 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
-				err(1, "getmntinfo");
+				err(EXIT_FAILURE, "getmntinfo");
 			for (i = 0; i < mntsize; i++) {
 				if (checkvfsname(mntbuf[i].f_fstypename,
 				    vfslist))
@@ -219,12 +228,11 @@ main(int argc, char *argv[])
 			 * then try exactly what the user entered.
 			 */
 			if ((canonical_path == NULL ||
-			     (mntbuf = getmntpt(canonical_path)) == NULL) &&
-			    (mntbuf = getmntpt(*argv)) == NULL
-			   )
-			{
-				errx(1,
-				    "unknown special file or file system %s.",
+			    (mntbuf = getmntpt(canonical_path)) == NULL) &&
+			    (mntbuf = getmntpt(*argv)) == NULL) {
+out:
+				errx(EXIT_FAILURE,
+				    "Unknown special file or file system `%s'",
 				    *argv);
 			}
 			mntfromname = mntbuf->f_mntfromname;
@@ -247,26 +255,20 @@ main(int argc, char *argv[])
 			     (fs = getfsspec(canonical_path)) == NULL))
 			{
 				if ((fs = getfsfile(*argv)) == NULL &&
-				    (fs = getfsspec(*argv)) == NULL)
-				{
-					errx(1,
-					    "%s: unknown special file or file system.",
-					    *argv);
+				    (fs = getfsspec(*argv)) == NULL) {
+					goto out;
 				}
 			}
 			if (BADTYPE(fs->fs_type))
-				errx(1, "%s has unknown file system type.",
+				errx(EXIT_FAILURE,
+				    "Unknown file system type for `%s'",
 				    *argv);
 			if (strcmp(fs->fs_spec, "from_mount") == 0) {
 				if ((canonical_path == NULL ||
-				     (mntbuf = getmntpt(canonical_path)) == NULL) &&
-				    (mntbuf = getmntpt(*argv)) == NULL
-				   )
-				{
-					errx(1,
-					    "unknown special file or file system %s.",
-					    *argv);
-				}
+				    (mntbuf = getmntpt(canonical_path))
+				     == NULL) &&
+				    (mntbuf = getmntpt(*argv)) == NULL)
+					goto out;
 				mntfromname = mntbuf->f_mntfromname;
 			} else
 				mntfromname = fs->fs_spec;
@@ -274,6 +276,7 @@ main(int argc, char *argv[])
 			fstypename  = fs->fs_vfstype;
 			mountopts   = fs->fs_mntops;
 		}
+		mntfromname = getspec(buf, sizeof(buf), mntfromname);
 		rval = mountfs(fstypename, mntfromname,
 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
 		break;
@@ -283,6 +286,7 @@ main(int argc, char *argv[])
 		 * a ':' or a '@' then assume that an NFS filesystem is being
 		 * specified ala Sun.
 		 */
+		mntfromname = getspec(buf, sizeof(buf), argv[0]);
 		if (vfslist == NULL) {
 			if (strpbrk(argv[0], ":@") != NULL) {
 				fprintf(stderr, "WARNING: autoselecting nfs "
@@ -292,17 +296,16 @@ main(int argc, char *argv[])
 				    "in a future release\n");
 				vfstype = "nfs";
 			} else {
-				vfstype = getfslab(argv[0]);
+				vfstype = getfslab(mntfromname);
 				if (vfstype == NULL)
 					vfstype = ffs_fstype;
 			}
 		}
-		rval = mountfs(vfstype,
-		    argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
+		rval = mountfs(vfstype, mntfromname, argv[1], init_flags,
+		    options, NULL, 0, NULL, 0);
 		break;
 	default:
 		usage();
-		/* NOTREACHED */
 	}
 
 #if 0	/* disabled because it interferes the service. */
@@ -316,13 +319,12 @@ main(int argc, char *argv[])
 
 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
-			err(1, "signal mountd");
+			err(EXIT_FAILURE, "signal mountd");
 		(void)fclose(mountdfp);
 	}
 #endif
 
-	exit(rval);
-	/* NOTREACHED */
+	return 0;
 }
 
 int
@@ -336,7 +338,7 @@ hasopt(const char *mntopts, const char *
 		option += 2;
 	} else
 		negative = 0;
-	optbuf = strdup(mntopts);
+	optbuf = estrdup(mntopts);
 	found = 0;
 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
 		if (opt[0] == 'n' && opt[1] == 'o') {
@@ -351,8 +353,8 @@ hasopt(const char *mntopts, const char *
 
 static int
 mountfs(const char *vfstype, const char *spec, const char *name, 
-	int flags, const char *options, const char *mntopts,
-	int skipmounted, char *buf, size_t buflen)
+    int flags, const char *options, const char *mntopts,
+    int skipmounted, char *buf, size_t buflen)
 {
 	/* List of directories containing mount_xxx subcommands. */
 	static const char *edirs[] = {
@@ -373,7 +375,7 @@ mountfs(const char *vfstype, const char 
 	volatile int getargs;
 
 	if (realpath(name, mntpath) == NULL) {
-		warn("realpath %s", name);
+		warn("realpath `%s'", name);
 		return (1);
 	}
 
@@ -442,9 +444,7 @@ mountfs(const char *vfstype, const char 
 	}
 
 	maxargc = 64;
-	argv = malloc(sizeof(char *) * maxargc);
-	if (argv == NULL)
-		err(1, "malloc");
+	argv = ecalloc(maxargc, sizeof(*argv));
 
 	if (getargs &&
 	    strncmp(vfstype, PUFFS_TYPEPREFIX, sizeof(PUFFS_TYPEPREFIX)-1) == 0)
@@ -538,7 +538,7 @@ mountfs(const char *vfstype, const char 
 				(void)fprintf(stdout, "%s", tbuf);
 		}
 
-		if (waitpid(pid, &status, 0) < 0) {
+		if (waitpid(pid, &status, 0) == -1) {
 			warn("waitpid");
 			return (1);
 		}
@@ -553,7 +553,7 @@ mountfs(const char *vfstype, const char 
 
 		if (buf == NULL) {
 			if (verbose) {
-				if (statvfs(name, &sf) < 0) {
+				if (statvfs(name, &sf) == -1) {
 					warn("statvfs %s", name);
 					return (1);
 				}
@@ -656,12 +656,11 @@ catopt(char **sp, const char *o)
 
 	s = *sp;
 	if (s) {
-		if (asprintf(&n, "%s,%s", s, o) < 0)
-			err(1, "asprintf");
+		easprintf(&n, "%s,%s", s, o);
 		free(s);
 		s = n;
 	} else
-		s = strdup(o);
+		s = estrdup(o);
 	*sp = s;
 }
 
@@ -679,9 +678,7 @@ mangle(char *options, int *argcp, const 
 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
 		/* Always leave space for one more argument and the NULL. */
 		if (argc >= maxargc - 4) {
-			nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
-			if (!nargv)
-				err(1, "realloc");
+			nargv = erealloc(argv, (maxargc << 1) * sizeof(nargv));
 			argv = nargv;
 			maxargc <<= 1;
 		}
@@ -724,7 +721,7 @@ getfslab(const char *str)
 		 * the block device we know we are never passed a raw device.
 		 */
 		if (errno != EBUSY)
-			err(1, "cannot open `%s'", str);
+			err(EXIT_FAILURE, "cannot open `%s'", str);
 		strlcpy(buf, str, MAXPATHLEN);
 		if ((sp = strrchr(buf, '/')) != NULL)
 			++sp;
@@ -767,15 +764,59 @@ getfslab(const char *str)
 	return (vfstype);
 }
 
+/* Query device path from disk name */
+static const char *
+getspec(char *buf, size_t bufsiz, const char *name)
+{
+	static const int mib[] = { CTL_HW, HW_DISKNAMES };
+	char *drives, *dk;
+	size_t len;
+	int fd;
+	char *vname;
+
+	if (strncasecmp(name, "NAME=", 5) != 0)
+		return name;
+	name += 5;
+	vname = emalloc(strlen(name) * 4 + 1);
+	strunvis(vname, name);
+
+	if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1)
+		err(EXIT_FAILURE, "sysctl hw.disknames failed");
+
+	drives = emalloc(len);
+	if (sysctl(mib, __arraycount(mib), drives, &len, NULL, 0) == -1)
+		err(EXIT_FAILURE, "sysctl hw.disknames failed");
+
+	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
+		struct dkwedge_info dkw;
+		if (strncmp(dk, "dk", 2) != 0)
+			continue;
+		fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
+		if (fd == -1)
+			continue;
+		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1)
+			err(EXIT_FAILURE, "%s: getwedgeinfo", dk);
+		(void)close(fd);
+		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
+			free(drives);
+			free(vname);
+			vname = strstr(buf, "/rdk");
+			if (vname++ == NULL) 
+				return buf;
+			strcpy(vname, vname + 1);
+			return buf;
+		}
+	}
+	err(EXIT_FAILURE, "Cannot find a match for `%s'", vname);
+}
+
 static void
 usage(void)
 {
 
-	(void)fprintf(stderr,
-	    "usage: mount %s\n       mount %s\n       mount %s\n",
-	    "[-Aadfruvw] [-t type]",
-	    "[-dfruvw] special | node",
-	    "[-dfruvw] [-o options] [-t type] special node");
+	(void)fprintf(stderr, "Usage: %s [-Aadfruvw] [-t type]\n"
+	    "\t%s [-dfruvw] special | node\n"
+	    "\t%s [-dfruvw] [-o options] [-t type] special node\n",
+	    getprogname(), getprogname(), getprogname());
 	exit(1);
-	/* NOTREACHED */
 }

Reply via email to