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 /* These are DM helpers used only by this file */
36 int dm_is_dm_device(int major, int minor);
37 int dm_is_dm_kernel_name(const char *name);
38 char *dm_device_path(const char *prefix, int major, int minor);
40 char *crypt_lookup_dev(const char *dev_id);
41 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
43 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
52 /* Ignore strange nested directories */
53 if (dir_level > max_level)
56 path[PATH_MAX - 1] = '\0';
57 ptr = path + strlen(path);
60 space = PATH_MAX - (ptr - path);
66 while((entry = readdir(dir))) {
67 if (entry->d_name[0] == '.' ||
68 !strncmp(entry->d_name, "..", 2))
72 (!strcmp(entry->d_name, "shm") ||
73 !strcmp(entry->d_name, "fd") ||
74 !strcmp(entry->d_name, "char") ||
75 !strcmp(entry->d_name, "pts")))
78 strncpy(ptr, entry->d_name, space);
79 if (stat(path, &st) < 0)
82 if (S_ISDIR(st.st_mode)) {
83 result = __lookup_dev(path, dev, dir_level + 1, max_level);
86 } else if (S_ISBLK(st.st_mode)) {
87 /* workaround: ignore dm-X devices, these are internal kernel names */
88 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
90 if (st.st_rdev == dev) {
91 result = strdup(path);
102 * Non-udev systemd need to scan for device here.
104 static char *lookup_dev_old(int major, int minor)
107 char *result = NULL, buf[PATH_MAX + 1];
109 dev = makedev(major, minor);
110 strncpy(buf, "/dev", PATH_MAX);
111 buf[PATH_MAX] = '\0';
113 /* First try low level device */
114 if ((result = __lookup_dev(buf, dev, 0, 0)))
117 /* If it is dm, try DM dir */
118 if (dm_is_dm_device(major, minor)) {
119 strncpy(buf, dm_get_dir(), PATH_MAX);
120 if ((result = __lookup_dev(buf, dev, 0, 0)))
124 strncpy(buf, "/dev", PATH_MAX);
125 return __lookup_dev(buf, dev, 0, 4);
129 * Returns string pointing to device in /dev according to "major:minor" dev_id
131 char *crypt_lookup_dev(const char *dev_id)
134 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
138 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
141 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
144 len = readlink(path, link, sizeof(link) - 1);
146 /* Without /sys use old scan */
147 if (stat("/sys/dev/block", &st) < 0)
148 return lookup_dev_old(major, minor);
153 devname = strrchr(link, '/');
158 if (dm_is_dm_kernel_name(devname))
159 devpath = dm_device_path("/dev/mapper/", major, minor);
160 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
161 devpath = strdup(path);
164 * Check that path is correct.
166 if (devpath && ((stat(devpath, &st) < 0) ||
167 !S_ISBLK(st.st_mode) ||
168 (st.st_rdev != makedev(major, minor)))) {
170 /* Should never happen unless user mangles with dev nodes. */
171 return lookup_dev_old(major, minor);
177 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
179 char path[PATH_MAX], tmp[64] = {0};
182 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
186 if ((fd = open(path, O_RDONLY)) < 0)
188 r = read(fd, tmp, sizeof(tmp));
194 if (sscanf(tmp, "%d", rotational) != 1)