Module Name:    src
Committed By:   christos
Date:           Sat Apr  7 04:04:46 UTC 2012

Modified Files:
        src/lib/libutil: Makefile shlib_version
Added Files:
        src/lib/libutil: getfsspecname.3 getfsspecname.c

Log Message:
add getfsspecname


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/lib/libutil/Makefile
cvs rdiff -u -r0 -r1.1 src/lib/libutil/getfsspecname.3 \
    src/lib/libutil/getfsspecname.c
cvs rdiff -u -r1.49 -r1.50 src/lib/libutil/shlib_version

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/Makefile
diff -u src/lib/libutil/Makefile:1.71 src/lib/libutil/Makefile:1.72
--- src/lib/libutil/Makefile:1.71	Wed Mar 21 01:37:44 2012
+++ src/lib/libutil/Makefile	Sat Apr  7 00:04:45 2012
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.71 2012/03/21 05:37:44 matt Exp $
+#	$NetBSD: Makefile,v 1.72 2012/04/07 04:04:45 christos Exp $
 #	@(#)Makefile	8.1 (Berkeley) 6/4/93
 
 USE_SHLIBDIR=	yes
@@ -11,7 +11,7 @@ WARNS?=	5
 LIB=	util
 CPPFLAGS+=-DLIBC_SCCS -I${.CURDIR}
 SRCS+=	efun.c getbootfile.c getlabelsector.c getmaxpartitions.c \
-	getmntopts.c getrawpartition.c \
+	getfsspecname.c getmntopts.c getrawpartition.c \
 	disklabel_dkcksum.c disklabel_scan.c \
 	if_media.c \
 	login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \
@@ -23,6 +23,7 @@ SRCS+=	efun.c getbootfile.c getlabelsect
 
 MAN=	efun.3 getbootfile.3 getfstypename.3 getlabelsector.3 \
 	getmaxpartitions.3 getmntopts.3 getrawpartition.3 \
+	getfsspecname.3 \
 	login.3 login_cap.3 loginx.3 \
 	disklabel_dkcksum.3 disklabel_scan.3 \
 	opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \

Index: src/lib/libutil/shlib_version
diff -u src/lib/libutil/shlib_version:1.49 src/lib/libutil/shlib_version:1.50
--- src/lib/libutil/shlib_version:1.49	Tue Aug 30 08:39:52 2011
+++ src/lib/libutil/shlib_version	Sat Apr  7 00:04:45 2012
@@ -1,5 +1,5 @@
-#	$NetBSD: shlib_version,v 1.49 2011/08/30 12:39:52 bouyer Exp $
+#	$NetBSD: shlib_version,v 1.50 2012/04/07 04:04:45 christos Exp $
 #	Remember to update distrib/sets/lists/base/shl.* when changing
 #
 major=7
-minor=19
+minor=20

Added files:

Index: src/lib/libutil/getfsspecname.3
diff -u /dev/null src/lib/libutil/getfsspecname.3:1.1
--- /dev/null	Sat Apr  7 00:04:46 2012
+++ src/lib/libutil/getfsspecname.3	Sat Apr  7 00:04:45 2012
@@ -0,0 +1,84 @@
+.\"	$NetBSD: getfsspecname.3,v 1.1 2012/04/07 04:04:45 christos Exp $
+.\"
+.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Christos Zoulas.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\"
+.Dd March 6, 2012
+.Dt GETFSSPECNAME 3
+.Os
+.Sh NAME
+.Nm getfsspecname
+.Nd get the underlying wedge name from a label
+.Sh LIBRARY
+.Lb libutl
+.Sh SYNOPSIS
+.In util.h
+.Ft const char *
+.Fn getfsspecname "char *buf" "size_t buflen" "const char *spec"
+.Sh DESCRIPTION
+The
+.Fn getfsspecname
+function translates an
+.Ft fs_spec
+field in the
+.Fa spec
+argument of the form
+.Dq NAME=wedgename
+to the underlying 
+.Xr dk 4 
+device node, and places the resulting pathname in
+.Fa buf 
+up to len
+.Fa bufsiz .
+.Pp
+If the
+.Fa spec
+argument is not of the form
+.Dq NAME=wedgename ,
+.Fa spec
+is copied
+to
+.Fa buf
+and returned.
+.Sh RETURN VALUES
+On success the absolute pathname of the underlying wedge device is returned,
+or the original
+.Fa spec
+argument.
+On failure
+.Dv NULL
+is return and
+.Fa buf
+contains the reason for the error.
+.Sh SEE ALSO
+.Xr fstab 5
+.Sh HISTORY
+The
+.Fn getfsspecname
+function appeared in
+.Nx 7.0
Index: src/lib/libutil/getfsspecname.c
diff -u /dev/null src/lib/libutil/getfsspecname.c:1.1
--- /dev/null	Sat Apr  7 00:04:46 2012
+++ src/lib/libutil/getfsspecname.c	Sat Apr  7 00:04:45 2012
@@ -0,0 +1,124 @@
+/*	$NetBSD: getfsspecname.c,v 1.1 2012/04/07 04:04:45 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: getfsspecname.c,v 1.1 2012/04/07 04:04:45 christos Exp $");
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/disk.h>
+
+#include <stdio.h>
+#include <vis.h>
+#include <string.h>
+#include <fstab.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <util.h>
+
+const char *
+getfsspecname(char *buf, size_t bufsiz, const char *name)
+{
+	static const int mib[] = { CTL_HW, HW_DISKNAMES };
+	static const unsigned int miblen = __arraycount(mib);
+	char *drives, *dk;
+	size_t len;
+	int fd, savee;
+	char *vname;
+
+	drives = NULL;
+	vname = NULL;
+	if (strncasecmp(name, "NAME=", 5) != 0) {
+		strlcpy(buf, name, bufsiz);
+		return buf;
+	}
+
+	name += 5;
+	vname = malloc(strlen(name) * 4 + 1);
+	if (vname == NULL) {
+		savee = errno;
+		strlcpy(buf, "malloc failed", bufsiz);
+		goto out;
+	}
+
+	strunvis(vname, name);
+
+	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
+		savee = errno;
+		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
+		goto out;
+	}
+
+	drives = malloc(len);
+	if (drives == NULL) {
+		savee = errno;
+		strlcpy(buf, "malloc failed", bufsiz);
+		goto out;
+	}
+	if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
+		savee = errno;
+		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
+		goto out;
+	}
+
+	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) {
+			savee = errno;
+			snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
+			(void)close(fd);
+			goto out;
+		}
+		(void)close(fd);
+		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
+			char *p = strstr(buf, "/rdk");
+			if (p++ == NULL) 
+				return buf;
+			strcpy(p, p + 1);
+			free(drives);
+			free(vname);
+			return buf;
+		}
+	}
+	savee = ESRCH;
+	snprintf(buf, bufsiz, "no match for `%s'", vname);
+out:
+	free(drives);
+	free(vname);
+	errno = savee;
+	return NULL;
+}

Reply via email to