Home | History | Annotate | Line # | Download | only in libutil
getfsspecname.c revision 1.4
      1  1.4       dsl /*	$NetBSD: getfsspecname.c,v 1.4 2013/01/01 18:32:17 dsl 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.4       dsl __RCSID("$NetBSD: getfsspecname.c,v 1.4 2013/01/01 18:32:17 dsl Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/types.h>
     35  1.4       dsl #include <sys/ioctl.h>
     36  1.1  christos #include <sys/sysctl.h>
     37  1.1  christos #include <sys/disk.h>
     38  1.1  christos 
     39  1.1  christos #include <stdio.h>
     40  1.1  christos #include <vis.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos #include <fstab.h>
     43  1.1  christos #include <fcntl.h>
     44  1.1  christos #include <stdlib.h>
     45  1.1  christos #include <errno.h>
     46  1.1  christos #include <unistd.h>
     47  1.1  christos #include <util.h>
     48  1.1  christos 
     49  1.2  christos #define COMPAT_DKWEDGE	/* To be removed */
     50  1.2  christos 
     51  1.1  christos const char *
     52  1.1  christos getfsspecname(char *buf, size_t bufsiz, const char *name)
     53  1.1  christos {
     54  1.1  christos 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
     55  1.1  christos 	static const unsigned int miblen = __arraycount(mib);
     56  1.1  christos 	char *drives, *dk;
     57  1.1  christos 	size_t len;
     58  1.1  christos 	int fd, savee;
     59  1.1  christos 	char *vname;
     60  1.1  christos 
     61  1.1  christos 	drives = NULL;
     62  1.1  christos 	vname = NULL;
     63  1.1  christos 	if (strncasecmp(name, "NAME=", 5) != 0) {
     64  1.2  christos #ifdef COMPAT_DKWEDGE
     65  1.2  christos 		/*
     66  1.2  christos 		 * We try to open the disk name, and if we fail with EBUSY
     67  1.2  christos 		 * we use the name as the label to find the wedge.
     68  1.2  christos 		 */
     69  1.2  christos 		char rbuf[MAXPATHLEN];
     70  1.2  christos 		if (name[0] == '/') {
     71  1.3  christos 			if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) {
     72  1.3  christos 				if ((fd = open(rbuf, O_RDONLY)) == -1) {
     73  1.3  christos 					if (errno == EBUSY) {
     74  1.3  christos 						name = strrchr(name, '/') + 1;
     75  1.3  christos 						goto search;
     76  1.3  christos 					}
     77  1.3  christos 				} else
     78  1.3  christos 					close(fd);
     79  1.2  christos 			}
     80  1.2  christos 		}
     81  1.2  christos #endif
     82  1.1  christos 		strlcpy(buf, name, bufsiz);
     83  1.1  christos 		return buf;
     84  1.2  christos 	} else
     85  1.2  christos 		name += 5;
     86  1.1  christos 
     87  1.2  christos #ifdef COMPAT_DKWEDGE
     88  1.2  christos search:
     89  1.2  christos #endif
     90  1.1  christos 	vname = malloc(strlen(name) * 4 + 1);
     91  1.1  christos 	if (vname == NULL) {
     92  1.1  christos 		savee = errno;
     93  1.1  christos 		strlcpy(buf, "malloc failed", bufsiz);
     94  1.1  christos 		goto out;
     95  1.1  christos 	}
     96  1.1  christos 
     97  1.1  christos 	strunvis(vname, name);
     98  1.1  christos 
     99  1.1  christos 	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
    100  1.1  christos 		savee = errno;
    101  1.1  christos 		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
    102  1.1  christos 		goto out;
    103  1.1  christos 	}
    104  1.1  christos 
    105  1.1  christos 	drives = malloc(len);
    106  1.1  christos 	if (drives == NULL) {
    107  1.1  christos 		savee = errno;
    108  1.1  christos 		strlcpy(buf, "malloc failed", bufsiz);
    109  1.1  christos 		goto out;
    110  1.1  christos 	}
    111  1.1  christos 	if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
    112  1.1  christos 		savee = errno;
    113  1.1  christos 		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
    114  1.1  christos 		goto out;
    115  1.1  christos 	}
    116  1.1  christos 
    117  1.1  christos 	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
    118  1.1  christos 		struct dkwedge_info dkw;
    119  1.1  christos 		if (strncmp(dk, "dk", 2) != 0)
    120  1.1  christos 			continue;
    121  1.1  christos 		fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
    122  1.1  christos 		if (fd == -1)
    123  1.1  christos 			continue;
    124  1.1  christos 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
    125  1.1  christos 			savee = errno;
    126  1.1  christos 			snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
    127  1.1  christos 			(void)close(fd);
    128  1.1  christos 			goto out;
    129  1.1  christos 		}
    130  1.1  christos 		(void)close(fd);
    131  1.1  christos 		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
    132  1.1  christos 			char *p = strstr(buf, "/rdk");
    133  1.1  christos 			if (p++ == NULL)
    134  1.1  christos 				return buf;
    135  1.1  christos 			strcpy(p, p + 1);
    136  1.1  christos 			free(drives);
    137  1.1  christos 			free(vname);
    138  1.1  christos 			return buf;
    139  1.1  christos 		}
    140  1.1  christos 	}
    141  1.1  christos 	savee = ESRCH;
    142  1.1  christos 	snprintf(buf, bufsiz, "no match for `%s'", vname);
    143  1.1  christos out:
    144  1.1  christos 	free(drives);
    145  1.1  christos 	free(vname);
    146  1.1  christos 	errno = savee;
    147  1.1  christos 	return NULL;
    148  1.1  christos }
    149