2 * devname - search for device name
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
31 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
40 /* Ignore strange nested directories */
41 if (dir_level > max_level)
44 path[PATH_MAX - 1] = '\0';
45 ptr = path + strlen(path);
48 space = PATH_MAX - (ptr - path);
54 while((entry = readdir(dir))) {
55 if (entry->d_name[0] == '.' ||
56 !strncmp(entry->d_name, "..", 2))
60 (!strcmp(entry->d_name, "shm") ||
61 !strcmp(entry->d_name, "fd") ||
62 !strcmp(entry->d_name, "char") ||
63 !strcmp(entry->d_name, "pts")))
66 strncpy(ptr, entry->d_name, space);
67 if (stat(path, &st) < 0)
70 if (S_ISDIR(st.st_mode)) {
71 result = __lookup_dev(path, dev, dir_level + 1, max_level);
74 } else if (S_ISBLK(st.st_mode)) {
75 /* workaround: ignore dm-X devices, these are internal kernel names */
76 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
78 if (st.st_rdev == dev) {
79 result = strdup(path);
90 * Non-udev systemd need to scan for device here.
92 static char *lookup_dev_old(int major, int minor)
95 char *result = NULL, buf[PATH_MAX + 1];
97 dev = makedev(major, minor);
98 strncpy(buf, "/dev", PATH_MAX);
101 /* First try low level device */
102 if ((result = __lookup_dev(buf, dev, 0, 0)))
105 /* If it is dm, try DM dir */
106 if (dm_is_dm_device(major, minor)) {
107 strncpy(buf, dm_get_dir(), PATH_MAX);
108 if ((result = __lookup_dev(buf, dev, 0, 0)))
112 strncpy(buf, "/dev", PATH_MAX);
113 return __lookup_dev(buf, dev, 0, 4);
117 * Returns string pointing to device in /dev according to "major:minor" dev_id
119 char *crypt_lookup_dev(const char *dev_id)
122 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
126 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
129 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
132 len = readlink(path, link, sizeof(link));
134 /* Without /sys use old scan */
135 if (stat("/sys/dev/block", &st) < 0)
136 return lookup_dev_old(major, minor);
141 devname = strrchr(link, '/');
146 if (dm_is_dm_kernel_name(devname))
147 devpath = dm_device_path(major, minor);
148 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
149 devpath = strdup(path);
152 * Check that path is correct.
154 if (devpath && ((stat(devpath, &st) < 0) ||
155 !S_ISBLK(st.st_mode) ||
156 (st.st_rdev != makedev(major, minor)))) {
158 /* Should never happen unless user mangles with dev nodes. */
159 return lookup_dev_old(major, minor);