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