etc/services - sync with NetBSD-8
[minix.git] / lib / libutil / getfsspecname.c
blob8523e8cf82b3d310be7108cb265775b11371a1e2
1 /* $NetBSD: getfsspecname.c,v 1.5 2014/05/25 13:46:07 christos Exp $ */
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
37 #include <sys/disk.h>
39 #include <stdio.h>
40 #include <vis.h>
41 #include <string.h>
42 #include <fstab.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <util.h>
49 #if !defined(__minix)
50 #define COMPAT_DKWEDGE /* To be removed */
51 #endif /* !defined(__minix) */
53 const char *
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;
59 size_t len;
60 int fd, savee = errno;
61 char *vname;
63 p = drives = vname = NULL;
64 if (strncasecmp(name, "NAME=", 5) != 0) {
65 #ifdef COMPAT_DKWEDGE
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];
71 if (name[0] == '/') {
72 if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) {
73 if ((fd = open(rbuf, O_RDONLY)) == -1) {
74 if (errno == EBUSY) {
75 name = strrchr(name, '/') + 1;
76 goto search;
78 } else
79 close(fd);
82 #endif
83 strlcpy(buf, name, bufsiz);
84 return buf;
85 } else
86 name += 5;
88 #ifdef COMPAT_DKWEDGE
89 search:
90 #endif
91 vname = malloc(strlen(name) * 4 + 1);
92 if (vname == NULL) {
93 savee = errno;
94 strlcpy(buf, "malloc failed", bufsiz);
95 goto out;
98 strunvis(vname, name);
100 if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
101 savee = errno;
102 strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
103 goto out;
106 drives = malloc(len);
107 if (drives == NULL) {
108 savee = errno;
109 strlcpy(buf, "malloc failed", bufsiz);
110 goto out;
112 if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
113 savee = errno;
114 strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
115 goto out;
118 for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
119 struct dkwedge_info dkw;
120 if (strncmp(dk, "dk", 2) != 0)
121 continue;
122 fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
123 if (fd == -1)
124 continue;
125 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
126 savee = errno;
127 snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
128 (void)close(fd);
129 goto out;
131 (void)close(fd);
132 if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
133 p = strstr(buf, "/rdk");
134 goto good;
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);
140 if (fd != -1) {
141 close(fd);
142 p = strstr(buf, "/r");
143 goto good;
145 #endif
146 savee = ESRCH;
147 snprintf(buf, bufsiz, "no match for `%s'", vname);
148 out:
149 buf = NULL;
150 good:
151 if (p++ != NULL)
152 strcpy(p, p + 1);
153 free(drives);
154 free(vname);
155 errno = savee;
156 return buf;