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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
33 char *crypt_lookup_dev(const char *dev_id);
34 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
36 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
45 /* Ignore strange nested directories */
46 if (dir_level > max_level)
49 path[PATH_MAX - 1] = '\0';
50 ptr = path + strlen(path);
53 space = PATH_MAX - (ptr - path);
59 while((entry = readdir(dir))) {
60 if (entry->d_name[0] == '.' ||
61 !strncmp(entry->d_name, "..", 2))
65 (!strcmp(entry->d_name, "shm") ||
66 !strcmp(entry->d_name, "fd") ||
67 !strcmp(entry->d_name, "char") ||
68 !strcmp(entry->d_name, "pts")))
71 strncpy(ptr, entry->d_name, space);
72 if (stat(path, &st) < 0)
75 if (S_ISDIR(st.st_mode)) {
76 result = __lookup_dev(path, dev, dir_level + 1, max_level);
79 } else if (S_ISBLK(st.st_mode)) {
80 /* workaround: ignore dm-X devices, these are internal kernel names */
81 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
83 if (st.st_rdev == dev) {
84 result = strdup(path);
95 * Non-udev systemd need to scan for device here.
97 static char *lookup_dev_old(int major, int minor)
100 char *result = NULL, buf[PATH_MAX + 1];
102 dev = makedev(major, minor);
103 strncpy(buf, "/dev", PATH_MAX);
104 buf[PATH_MAX] = '\0';
106 /* First try low level device */
107 if ((result = __lookup_dev(buf, dev, 0, 0)))
110 /* If it is dm, try DM dir */
111 if (dm_is_dm_device(major, minor)) {
112 strncpy(buf, dm_get_dir(), PATH_MAX);
113 if ((result = __lookup_dev(buf, dev, 0, 0)))
117 strncpy(buf, "/dev", PATH_MAX);
118 return __lookup_dev(buf, dev, 0, 4);
122 * Returns string pointing to device in /dev according to "major:minor" dev_id
124 char *crypt_lookup_dev(const char *dev_id)
127 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
131 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
134 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
137 len = readlink(path, link, sizeof(link));
139 /* Without /sys use old scan */
140 if (stat("/sys/dev/block", &st) < 0)
141 return lookup_dev_old(major, minor);
146 devname = strrchr(link, '/');
151 if (dm_is_dm_kernel_name(devname))
152 devpath = dm_device_path("/dev/mapper/", major, minor);
153 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
154 devpath = strdup(path);
157 * Check that path is correct.
159 if (devpath && ((stat(devpath, &st) < 0) ||
160 !S_ISBLK(st.st_mode) ||
161 (st.st_rdev != makedev(major, minor)))) {
163 /* Should never happen unless user mangles with dev nodes. */
164 return lookup_dev_old(major, minor);
170 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
172 char path[PATH_MAX], tmp[64] = {0};
175 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
179 if ((fd = open(path, O_RDONLY)) < 0)
181 r = read(fd, tmp, sizeof(tmp));
187 if (sscanf(tmp, "%d", rotational) != 1)