1 /* $NetBSD: getfsspecname.c,v 1.5 2014/05/25 13:46:07 christos Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: getfsspecname.c,v 1.5 2014/05/25 13:46:07 christos Exp $");
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/sysctl.h>
50 #define COMPAT_DKWEDGE /* To be removed */
51 #endif /* !defined(__minix) */
54 getfsspecname(char *buf
, size_t bufsiz
, const char *name
)
56 static const int mib
[] = { CTL_HW
, HW_DISKNAMES
};
57 static const unsigned int miblen
= __arraycount(mib
);
58 char *drives
, *dk
, *p
;
60 int fd
, savee
= errno
;
63 p
= drives
= vname
= NULL
;
64 if (strncasecmp(name
, "NAME=", 5) != 0) {
67 * We try to open the disk name, and if we fail with EBUSY
68 * we use the name as the label to find the wedge.
70 char rbuf
[MAXPATHLEN
];
72 if (getdiskrawname(rbuf
, sizeof(rbuf
), name
) != NULL
) {
73 if ((fd
= open(rbuf
, O_RDONLY
)) == -1) {
75 name
= strrchr(name
, '/') + 1;
83 strlcpy(buf
, name
, bufsiz
);
91 vname
= malloc(strlen(name
) * 4 + 1);
94 strlcpy(buf
, "malloc failed", bufsiz
);
98 strunvis(vname
, name
);
100 if (sysctl(mib
, miblen
, NULL
, &len
, NULL
, 0) == -1) {
102 strlcpy(buf
, "sysctl hw.disknames failed", bufsiz
);
106 drives
= malloc(len
);
107 if (drives
== NULL
) {
109 strlcpy(buf
, "malloc failed", bufsiz
);
112 if (sysctl(mib
, miblen
, drives
, &len
, NULL
, 0) == -1) {
114 strlcpy(buf
, "sysctl hw.disknames failed", bufsiz
);
118 for (dk
= strtok(drives
, " "); dk
!= NULL
; dk
= strtok(NULL
, " ")) {
119 struct dkwedge_info dkw
;
120 if (strncmp(dk
, "dk", 2) != 0)
122 fd
= opendisk(dk
, O_RDONLY
, buf
, bufsiz
, 0);
125 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) == -1) {
127 snprintf(buf
, bufsiz
, "%s: getwedgeinfo", dk
);
132 if (strcmp(vname
, (char *)dkw
.dkw_wname
) == 0) {
133 p
= strstr(buf
, "/rdk");
137 #ifdef COMPAT_DKWEDGE
138 /* Last ditch effort assuming NAME=label, and label is a disk name */
139 fd
= opendisk(name
, O_RDONLY
, buf
, bufsiz
, 0);
142 p
= strstr(buf
, "/r");
147 snprintf(buf
, bufsiz
, "no match for `%s'", vname
);