Home | History | Annotate | Line # | Download | only in libutil
getfsspecname.c revision 1.1
      1  1.1  christos /*	$NetBSD: getfsspecname.c,v 1.1 2012/04/07 04:04:45 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos __RCSID("$NetBSD: getfsspecname.c,v 1.1 2012/04/07 04:04:45 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/types.h>
     35  1.1  christos #include <sys/sysctl.h>
     36  1.1  christos #include <sys/disk.h>
     37  1.1  christos 
     38  1.1  christos #include <stdio.h>
     39  1.1  christos #include <vis.h>
     40  1.1  christos #include <string.h>
     41  1.1  christos #include <fstab.h>
     42  1.1  christos #include <fcntl.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <errno.h>
     45  1.1  christos #include <unistd.h>
     46  1.1  christos #include <util.h>
     47  1.1  christos 
     48  1.1  christos const char *
     49  1.1  christos getfsspecname(char *buf, size_t bufsiz, const char *name)
     50  1.1  christos {
     51  1.1  christos 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
     52  1.1  christos 	static const unsigned int miblen = __arraycount(mib);
     53  1.1  christos 	char *drives, *dk;
     54  1.1  christos 	size_t len;
     55  1.1  christos 	int fd, savee;
     56  1.1  christos 	char *vname;
     57  1.1  christos 
     58  1.1  christos 	drives = NULL;
     59  1.1  christos 	vname = NULL;
     60  1.1  christos 	if (strncasecmp(name, "NAME=", 5) != 0) {
     61  1.1  christos 		strlcpy(buf, name, bufsiz);
     62  1.1  christos 		return buf;
     63  1.1  christos 	}
     64  1.1  christos 
     65  1.1  christos 	name += 5;
     66  1.1  christos 	vname = malloc(strlen(name) * 4 + 1);
     67  1.1  christos 	if (vname == NULL) {
     68  1.1  christos 		savee = errno;
     69  1.1  christos 		strlcpy(buf, "malloc failed", bufsiz);
     70  1.1  christos 		goto out;
     71  1.1  christos 	}
     72  1.1  christos 
     73  1.1  christos 	strunvis(vname, name);
     74  1.1  christos 
     75  1.1  christos 	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
     76  1.1  christos 		savee = errno;
     77  1.1  christos 		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
     78  1.1  christos 		goto out;
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	drives = malloc(len);
     82  1.1  christos 	if (drives == NULL) {
     83  1.1  christos 		savee = errno;
     84  1.1  christos 		strlcpy(buf, "malloc failed", bufsiz);
     85  1.1  christos 		goto out;
     86  1.1  christos 	}
     87  1.1  christos 	if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
     88  1.1  christos 		savee = errno;
     89  1.1  christos 		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
     90  1.1  christos 		goto out;
     91  1.1  christos 	}
     92  1.1  christos 
     93  1.1  christos 	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
     94  1.1  christos 		struct dkwedge_info dkw;
     95  1.1  christos 		if (strncmp(dk, "dk", 2) != 0)
     96  1.1  christos 			continue;
     97  1.1  christos 		fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
     98  1.1  christos 		if (fd == -1)
     99  1.1  christos 			continue;
    100  1.1  christos 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
    101  1.1  christos 			savee = errno;
    102  1.1  christos 			snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
    103  1.1  christos 			(void)close(fd);
    104  1.1  christos 			goto out;
    105  1.1  christos 		}
    106  1.1  christos 		(void)close(fd);
    107  1.1  christos 		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
    108  1.1  christos 			char *p = strstr(buf, "/rdk");
    109  1.1  christos 			if (p++ == NULL)
    110  1.1  christos 				return buf;
    111  1.1  christos 			strcpy(p, p + 1);
    112  1.1  christos 			free(drives);
    113  1.1  christos 			free(vname);
    114  1.1  christos 			return buf;
    115  1.1  christos 		}
    116  1.1  christos 	}
    117  1.1  christos 	savee = ESRCH;
    118  1.1  christos 	snprintf(buf, bufsiz, "no match for `%s'", vname);
    119  1.1  christos out:
    120  1.1  christos 	free(drives);
    121  1.1  christos 	free(vname);
    122  1.1  christos 	errno = savee;
    123  1.1  christos 	return NULL;
    124  1.1  christos }
    125