2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
47 static void finddevlabel(char **pathp, const char *devname);
48 static int xlatedevpath(char **pathp, struct stat *st);
49 static char *dodequote(char *base);
52 * Acquire device path.
56 getdevpath(const char *devname, int flags)
62 if (devname[0] == '/' || devname[0] == '.') {
63 asprintf(&path, "%s", devname);
65 asprintf(&path, "/dev/%s", devname);
66 if (lstat(path, &st) < 0) {
69 finddevlabel(&path, devname);
71 asprintf(&path, "%s", devname);
78 * Translate softlinks if requested. If the lstat() of the
79 * pre-translated path fails NULL is expected to be returned.
80 * lstat() is not called on the post-translated path.
82 if ((flags & GETDEVPATH_RAWDEV) && path) {
83 if (stgood == 0 && lstat(path, &st) == 0)
86 stgood = xlatedevpath(&path, &st);
99 finddevlabel(char **pathp, const char *devname)
101 const char *prefix = _PATH_DEVTAB_PATHS;
111 size_t len; /* directory prefix length */
112 size_t tlen; /* devname length without trailer */
114 if ((trailer = strrchr(devname, '.')) != NULL)
115 tlen = trailer - devname;
119 while (*prefix && *pathp == NULL) {
121 * Directory search path
123 ptr1 = strchr(prefix, ':');
124 len = (ptr1) ? (size_t)(ptr1 - prefix) : strlen(prefix);
125 asprintf(&dtpath, "%*.*s/devtab", (int)len, (int)len, prefix);
130 if ((fp = fopen(dtpath, "r")) != NULL) {
131 while (fgets(buf, sizeof(buf), fp) != NULL) {
133 * Extract label field, check degenerate
136 label = strtok_r(buf, " \t\r\n", &bufp);
137 if (label == NULL || *label == 0 ||
143 * Match label, with or without the
144 * trailer (aka ".s1a"). The trailer
145 * is tacked on if the match is without
148 if (strcmp(devname, label) == 0) {
150 } else if (tlen && strlen(label) == tlen &&
151 strncmp(devname, label, tlen) == 0) {
152 trailer = devname + tlen;
158 * Match, extract and process remaining fields.
160 ptr2 = strtok_r(NULL, " \t\r\n", &bufp);
161 ptr3 = strtok_r(NULL, " \t\r\n", &bufp);
162 if (ptr2 == NULL || ptr3 == NULL)
164 if (*ptr2 == 0 || *ptr3 == 0)
166 ptr3 = dodequote(ptr3);
167 if (strcmp(ptr2, "path") == 0) {
168 asprintf(pathp, "%s%s", ptr3, trailer);
170 asprintf(pathp, "/dev/%s/%s%s",
171 ptr2, ptr3, trailer);
185 xlatedevpath(char **pathp, struct stat *st)
192 * If not a softlink return unchanged.
194 if (!S_ISLNK(st->st_mode))
198 * If the softlink isn't reasonable return bad (0)
200 len = (int)st->st_size;
201 if (len < 0 || len > PATH_MAX)
205 * Read the link, return if the result is not what we expected.
207 path = malloc(len + 1);
208 n = readlink(*pathp, path, len);
209 if (n < 0 || n > len) {
215 * Success, replace (*pathp).
224 dodequote(char *base)
226 int len = strlen(base);
228 if (len && base[0] == '\"' && base[len-1] == '\"') {