getfsspecname.c revision 1.5 1 1.5 christos /* $NetBSD: getfsspecname.c,v 1.5 2014/05/25 13:46:07 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.5 christos __RCSID("$NetBSD: getfsspecname.c,v 1.5 2014/05/25 13:46:07 christos 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.5 christos char *drives, *dk, *p;
57 1.1 christos size_t len;
58 1.5 christos int fd, savee = errno;
59 1.1 christos char *vname;
60 1.1 christos
61 1.5 christos p = drives = vname = NULL;
62 1.1 christos if (strncasecmp(name, "NAME=", 5) != 0) {
63 1.2 christos #ifdef COMPAT_DKWEDGE
64 1.2 christos /*
65 1.2 christos * We try to open the disk name, and if we fail with EBUSY
66 1.2 christos * we use the name as the label to find the wedge.
67 1.2 christos */
68 1.2 christos char rbuf[MAXPATHLEN];
69 1.2 christos if (name[0] == '/') {
70 1.3 christos if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) {
71 1.3 christos if ((fd = open(rbuf, O_RDONLY)) == -1) {
72 1.3 christos if (errno == EBUSY) {
73 1.3 christos name = strrchr(name, '/') + 1;
74 1.3 christos goto search;
75 1.3 christos }
76 1.3 christos } else
77 1.3 christos close(fd);
78 1.2 christos }
79 1.2 christos }
80 1.2 christos #endif
81 1.1 christos strlcpy(buf, name, bufsiz);
82 1.1 christos return buf;
83 1.2 christos } else
84 1.2 christos name += 5;
85 1.1 christos
86 1.2 christos #ifdef COMPAT_DKWEDGE
87 1.2 christos search:
88 1.2 christos #endif
89 1.1 christos vname = malloc(strlen(name) * 4 + 1);
90 1.1 christos if (vname == NULL) {
91 1.1 christos savee = errno;
92 1.1 christos strlcpy(buf, "malloc failed", bufsiz);
93 1.1 christos goto out;
94 1.1 christos }
95 1.1 christos
96 1.1 christos strunvis(vname, name);
97 1.1 christos
98 1.1 christos if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
99 1.1 christos savee = errno;
100 1.1 christos strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
101 1.1 christos goto out;
102 1.1 christos }
103 1.1 christos
104 1.1 christos drives = malloc(len);
105 1.1 christos if (drives == NULL) {
106 1.1 christos savee = errno;
107 1.1 christos strlcpy(buf, "malloc failed", bufsiz);
108 1.1 christos goto out;
109 1.1 christos }
110 1.1 christos if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
111 1.1 christos savee = errno;
112 1.1 christos strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
113 1.1 christos goto out;
114 1.1 christos }
115 1.1 christos
116 1.1 christos for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
117 1.1 christos struct dkwedge_info dkw;
118 1.1 christos if (strncmp(dk, "dk", 2) != 0)
119 1.1 christos continue;
120 1.1 christos fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
121 1.1 christos if (fd == -1)
122 1.1 christos continue;
123 1.1 christos if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
124 1.1 christos savee = errno;
125 1.1 christos snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
126 1.1 christos (void)close(fd);
127 1.1 christos goto out;
128 1.1 christos }
129 1.1 christos (void)close(fd);
130 1.1 christos if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
131 1.5 christos p = strstr(buf, "/rdk");
132 1.5 christos goto good;
133 1.1 christos }
134 1.1 christos }
135 1.5 christos #ifdef COMPAT_DKWEDGE
136 1.5 christos /* Last ditch effort assuming NAME=label, and label is a disk name */
137 1.5 christos fd = opendisk(name, O_RDONLY, buf, bufsiz, 0);
138 1.5 christos if (fd != -1) {
139 1.5 christos close(fd);
140 1.5 christos p = strstr(buf, "/r");
141 1.5 christos goto good;
142 1.5 christos }
143 1.5 christos #endif
144 1.1 christos savee = ESRCH;
145 1.1 christos snprintf(buf, bufsiz, "no match for `%s'", vname);
146 1.1 christos out:
147 1.5 christos buf = NULL;
148 1.5 christos good:
149 1.5 christos if (p++ != NULL)
150 1.5 christos strcpy(p, p + 1);
151 1.1 christos free(drives);
152 1.1 christos free(vname);
153 1.1 christos errno = savee;
154 1.5 christos return buf;
155 1.1 christos }
156