1 1.8 alnsn /* $NetBSD: getfsspecname.c,v 1.8 2018/12/27 21:35:48 alnsn 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.8 alnsn __RCSID("$NetBSD: getfsspecname.c,v 1.8 2018/12/27 21:35:48 alnsn 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.6 jmcneill 63 1.6 jmcneill /* 64 1.6 jmcneill * If the name starts with "ROOT.", replace it with "/dev/<root_device>", 65 1.6 jmcneill * where <root_device> is the value of the kern.root_device sysctl. Any 66 1.6 jmcneill * characters after the special "ROOT." token are appended to the end 67 1.6 jmcneill * of this path. 68 1.6 jmcneill */ 69 1.6 jmcneill if (strncasecmp(name, "ROOT.", 5) == 0 && strchr(name, ':') == NULL) { 70 1.6 jmcneill static const int mib_root[] = { CTL_KERN, KERN_ROOT_DEVICE }; 71 1.7 christos static const unsigned int mib_rootlen = __arraycount(mib_root); 72 1.6 jmcneill 73 1.6 jmcneill strlcpy(buf, "/dev/", bufsiz); 74 1.6 jmcneill len = bufsiz - 5; 75 1.6 jmcneill if (sysctl(mib_root, mib_rootlen, buf + 5, &len, NULL, 0) == -1) { 76 1.6 jmcneill savee = errno; 77 1.6 jmcneill strlcpy(buf, "sysctl kern.root_device failed", bufsiz); 78 1.6 jmcneill goto out; 79 1.6 jmcneill } 80 1.6 jmcneill strlcat(buf, name + 5, bufsiz); 81 1.6 jmcneill return buf; 82 1.6 jmcneill } 83 1.6 jmcneill 84 1.1 christos if (strncasecmp(name, "NAME=", 5) != 0) { 85 1.2 christos #ifdef COMPAT_DKWEDGE 86 1.2 christos /* 87 1.2 christos * We try to open the disk name, and if we fail with EBUSY 88 1.2 christos * we use the name as the label to find the wedge. 89 1.2 christos */ 90 1.2 christos char rbuf[MAXPATHLEN]; 91 1.2 christos if (name[0] == '/') { 92 1.3 christos if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) { 93 1.3 christos if ((fd = open(rbuf, O_RDONLY)) == -1) { 94 1.3 christos if (errno == EBUSY) { 95 1.3 christos name = strrchr(name, '/') + 1; 96 1.3 christos goto search; 97 1.3 christos } 98 1.3 christos } else 99 1.3 christos close(fd); 100 1.2 christos } 101 1.2 christos } 102 1.2 christos #endif 103 1.1 christos strlcpy(buf, name, bufsiz); 104 1.1 christos return buf; 105 1.2 christos } else 106 1.2 christos name += 5; 107 1.1 christos 108 1.2 christos #ifdef COMPAT_DKWEDGE 109 1.2 christos search: 110 1.2 christos #endif 111 1.8 alnsn vname = malloc(strlen(name) + 1); 112 1.1 christos if (vname == NULL) { 113 1.1 christos savee = errno; 114 1.1 christos strlcpy(buf, "malloc failed", bufsiz); 115 1.1 christos goto out; 116 1.1 christos } 117 1.1 christos 118 1.1 christos strunvis(vname, name); 119 1.1 christos 120 1.1 christos if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) { 121 1.1 christos savee = errno; 122 1.1 christos strlcpy(buf, "sysctl hw.disknames failed", bufsiz); 123 1.1 christos goto out; 124 1.1 christos } 125 1.1 christos 126 1.1 christos drives = malloc(len); 127 1.1 christos if (drives == NULL) { 128 1.1 christos savee = errno; 129 1.1 christos strlcpy(buf, "malloc failed", bufsiz); 130 1.1 christos goto out; 131 1.1 christos } 132 1.1 christos if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) { 133 1.1 christos savee = errno; 134 1.1 christos strlcpy(buf, "sysctl hw.disknames failed", bufsiz); 135 1.1 christos goto out; 136 1.1 christos } 137 1.1 christos 138 1.1 christos for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) { 139 1.1 christos struct dkwedge_info dkw; 140 1.1 christos if (strncmp(dk, "dk", 2) != 0) 141 1.1 christos continue; 142 1.1 christos fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0); 143 1.1 christos if (fd == -1) 144 1.1 christos continue; 145 1.1 christos if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) { 146 1.1 christos savee = errno; 147 1.1 christos snprintf(buf, bufsiz, "%s: getwedgeinfo", dk); 148 1.1 christos (void)close(fd); 149 1.1 christos goto out; 150 1.1 christos } 151 1.1 christos (void)close(fd); 152 1.1 christos if (strcmp(vname, (char *)dkw.dkw_wname) == 0) { 153 1.5 christos p = strstr(buf, "/rdk"); 154 1.5 christos goto good; 155 1.1 christos } 156 1.1 christos } 157 1.5 christos #ifdef COMPAT_DKWEDGE 158 1.5 christos /* Last ditch effort assuming NAME=label, and label is a disk name */ 159 1.5 christos fd = opendisk(name, O_RDONLY, buf, bufsiz, 0); 160 1.5 christos if (fd != -1) { 161 1.5 christos close(fd); 162 1.5 christos p = strstr(buf, "/r"); 163 1.5 christos goto good; 164 1.5 christos } 165 1.5 christos #endif 166 1.1 christos savee = ESRCH; 167 1.1 christos snprintf(buf, bufsiz, "no match for `%s'", vname); 168 1.1 christos out: 169 1.5 christos buf = NULL; 170 1.5 christos good: 171 1.5 christos if (p++ != NULL) 172 1.5 christos strcpy(p, p + 1); 173 1.1 christos free(drives); 174 1.1 christos free(vname); 175 1.1 christos errno = savee; 176 1.5 christos return buf; 177 1.1 christos } 178