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 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/types.h>
34 /* These are DM helpers used only by this file */
35 int dm_is_dm_device(int major, int minor);
36 int dm_is_dm_kernel_name(const char *name);
37 char *dm_device_path(const char *prefix, int major, int minor);
39 char *crypt_lookup_dev(const char *dev_id);
40 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
42 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
51 /* Ignore strange nested directories */
52 if (dir_level > max_level)
55 path[PATH_MAX - 1] = '\0';
56 ptr = path + strlen(path);
59 space = PATH_MAX - (ptr - path);
65 while((entry = readdir(dir))) {
66 if (entry->d_name[0] == '.' ||
67 !strncmp(entry->d_name, "..", 2))
71 (!strcmp(entry->d_name, "shm") ||
72 !strcmp(entry->d_name, "fd") ||
73 !strcmp(entry->d_name, "char") ||
74 !strcmp(entry->d_name, "pts")))
77 strncpy(ptr, entry->d_name, space);
78 if (stat(path, &st) < 0)
81 if (S_ISDIR(st.st_mode)) {
82 result = __lookup_dev(path, dev, dir_level + 1, max_level);
85 } else if (S_ISBLK(st.st_mode)) {
86 /* workaround: ignore dm-X devices, these are internal kernel names */
87 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
89 if (st.st_rdev == dev) {
90 result = strdup(path);
101 * Non-udev systemd need to scan for device here.
103 static char *lookup_dev_old(int major, int minor)
106 char *result = NULL, buf[PATH_MAX + 1];
108 dev = makedev(major, minor);
109 strncpy(buf, "/dev", PATH_MAX);
110 buf[PATH_MAX] = '\0';
112 /* First try low level device */
113 if ((result = __lookup_dev(buf, dev, 0, 0)))
116 /* If it is dm, try DM dir */
117 if (dm_is_dm_device(major, minor)) {
118 strncpy(buf, dm_get_dir(), PATH_MAX);
119 if ((result = __lookup_dev(buf, dev, 0, 0)))
123 strncpy(buf, "/dev", PATH_MAX);
124 return __lookup_dev(buf, dev, 0, 4);
128 * Returns string pointing to device in /dev according to "major:minor" dev_id
130 char *crypt_lookup_dev(const char *dev_id)
133 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
137 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
140 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
143 len = readlink(path, link, sizeof(link) - 1);
145 /* Without /sys use old scan */
146 if (stat("/sys/dev/block", &st) < 0)
147 return lookup_dev_old(major, minor);
152 devname = strrchr(link, '/');
157 if (dm_is_dm_kernel_name(devname))
158 devpath = dm_device_path("/dev/mapper/", major, minor);
159 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
160 devpath = strdup(path);
163 * Check that path is correct.
165 if (devpath && ((stat(devpath, &st) < 0) ||
166 !S_ISBLK(st.st_mode) ||
167 (st.st_rdev != makedev(major, minor)))) {
169 /* Should never happen unless user mangles with dev nodes. */
170 return lookup_dev_old(major, minor);
176 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
178 char path[PATH_MAX], tmp[64] = {0};
181 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
185 if ((fd = open(path, O_RDONLY)) < 0)
187 r = read(fd, tmp, sizeof(tmp));
193 if (sscanf(tmp, "%d", rotational) != 1)