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-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/types.h>
35 char *crypt_lookup_dev(const char *dev_id);
36 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
38 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
47 /* Ignore strange nested directories */
48 if (dir_level > max_level)
51 path[PATH_MAX - 1] = '\0';
52 ptr = path + strlen(path);
55 space = PATH_MAX - (ptr - path);
61 while((entry = readdir(dir))) {
62 if (entry->d_name[0] == '.' ||
63 !strncmp(entry->d_name, "..", 2))
67 (!strcmp(entry->d_name, "shm") ||
68 !strcmp(entry->d_name, "fd") ||
69 !strcmp(entry->d_name, "char") ||
70 !strcmp(entry->d_name, "pts")))
73 strncpy(ptr, entry->d_name, space);
74 if (stat(path, &st) < 0)
77 if (S_ISDIR(st.st_mode)) {
78 result = __lookup_dev(path, dev, dir_level + 1, max_level);
81 } else if (S_ISBLK(st.st_mode)) {
82 /* workaround: ignore dm-X devices, these are internal kernel names */
83 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
85 if (st.st_rdev == dev) {
86 result = strdup(path);
97 * Non-udev systemd need to scan for device here.
99 static char *lookup_dev_old(int major, int minor)
102 char *result = NULL, buf[PATH_MAX + 1];
104 dev = makedev(major, minor);
105 strncpy(buf, "/dev", PATH_MAX);
106 buf[PATH_MAX] = '\0';
108 /* First try low level device */
109 if ((result = __lookup_dev(buf, dev, 0, 0)))
112 /* If it is dm, try DM dir */
113 if (dm_is_dm_device(major, minor)) {
114 strncpy(buf, dm_get_dir(), PATH_MAX);
115 if ((result = __lookup_dev(buf, dev, 0, 0)))
119 strncpy(buf, "/dev", PATH_MAX);
120 return __lookup_dev(buf, dev, 0, 4);
124 * Returns string pointing to device in /dev according to "major:minor" dev_id
126 char *crypt_lookup_dev(const char *dev_id)
129 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
133 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
136 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
139 len = readlink(path, link, sizeof(link) - 1);
141 /* Without /sys use old scan */
142 if (stat("/sys/dev/block", &st) < 0)
143 return lookup_dev_old(major, minor);
148 devname = strrchr(link, '/');
153 if (dm_is_dm_kernel_name(devname))
154 devpath = dm_device_path("/dev/mapper/", major, minor);
155 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
156 devpath = strdup(path);
159 * Check that path is correct.
161 if (devpath && ((stat(devpath, &st) < 0) ||
162 !S_ISBLK(st.st_mode) ||
163 (st.st_rdev != makedev(major, minor)))) {
165 /* Should never happen unless user mangles with dev nodes. */
166 return lookup_dev_old(major, minor);
172 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
174 char path[PATH_MAX], tmp[64] = {0};
177 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
181 if ((fd = open(path, O_RDONLY)) < 0)
183 r = read(fd, tmp, sizeof(tmp));
189 if (sscanf(tmp, "%d", rotational) != 1)