add getfsspecname

This commit is contained in:
christos 2012-04-07 04:04:21 +00:00
parent 9dbf303bcb
commit 29484ab26f
5 changed files with 215 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: util.h,v 1.61 2012/01/07 18:41:14 christos Exp $ */
/* $NetBSD: util.h,v 1.62 2012/04/07 04:04:21 christos Exp $ */
/*-
* Copyright (c) 1995
@ -74,6 +74,7 @@ int getlabelusesmbr(void);
int getmaxpartitions(void);
int getrawpartition(void);
const char *getfstypename(int);
const char *getfsspecname(char *, size_t, const char *);
#ifndef __LIBC12_SOURCE__
void login(const struct utmp *) __RENAME(__login50);
void loginx(const struct utmpx *) __RENAME(__loginx50);

View File

@ -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 getlabelsector.c getmaxpartitions.c \
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 \

View File

@ -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

124
lib/libutil/getfsspecname.c Normal file
View File

@ -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;
}

View File

@ -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