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 /* These are DM helpers used only by this file */
34 int dm_is_dm_device(int major, int minor);
35 int dm_is_dm_kernel_name(const char *name);
36 char *dm_device_path(const char *prefix, int major, int minor);
38 char *crypt_lookup_dev(const char *dev_id);
39 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
41 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
50 /* Ignore strange nested directories */
51 if (dir_level > max_level)
54 path[PATH_MAX - 1] = '\0';
55 ptr = path + strlen(path);
58 space = PATH_MAX - (ptr - path);
64 while((entry = readdir(dir))) {
65 if (entry->d_name[0] == '.' ||
66 !strncmp(entry->d_name, "..", 2))
70 (!strcmp(entry->d_name, "shm") ||
71 !strcmp(entry->d_name, "fd") ||
72 !strcmp(entry->d_name, "char") ||
73 !strcmp(entry->d_name, "pts")))
76 strncpy(ptr, entry->d_name, space);
77 if (stat(path, &st) < 0)
80 if (S_ISDIR(st.st_mode)) {
81 result = __lookup_dev(path, dev, dir_level + 1, max_level);
84 } else if (S_ISBLK(st.st_mode)) {
85 /* workaround: ignore dm-X devices, these are internal kernel names */
86 if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
88 if (st.st_rdev == dev) {
89 result = strdup(path);
100 * Non-udev systemd need to scan for device here.
102 static char *lookup_dev_old(int major, int minor)
105 char *result = NULL, buf[PATH_MAX + 1];
107 dev = makedev(major, minor);
108 strncpy(buf, "/dev", PATH_MAX);
109 buf[PATH_MAX] = '\0';
111 /* First try low level device */
112 if ((result = __lookup_dev(buf, dev, 0, 0)))
115 /* If it is dm, try DM dir */
116 if (dm_is_dm_device(major, minor)) {
117 strncpy(buf, dm_get_dir(), PATH_MAX);
118 if ((result = __lookup_dev(buf, dev, 0, 0)))
122 strncpy(buf, "/dev", PATH_MAX);
123 return __lookup_dev(buf, dev, 0, 4);
127 * Returns string pointing to device in /dev according to "major:minor" dev_id
129 char *crypt_lookup_dev(const char *dev_id)
132 char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
136 if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
139 if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
142 len = readlink(path, link, sizeof(link) - 1);
144 /* Without /sys use old scan */
145 if (stat("/sys/dev/block", &st) < 0)
146 return lookup_dev_old(major, minor);
151 devname = strrchr(link, '/');
156 if (dm_is_dm_kernel_name(devname))
157 devpath = dm_device_path("/dev/mapper/", major, minor);
158 else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
159 devpath = strdup(path);
162 * Check that path is correct.
164 if (devpath && ((stat(devpath, &st) < 0) ||
165 !S_ISBLK(st.st_mode) ||
166 (st.st_rdev != makedev(major, minor)))) {
168 /* Should never happen unless user mangles with dev nodes. */
169 return lookup_dev_old(major, minor);
175 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
177 char path[PATH_MAX], tmp[64] = {0};
180 if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
184 if ((fd = open(path, O_RDONLY)) < 0)
186 r = read(fd, tmp, sizeof(tmp));
192 if (sscanf(tmp, "%d", rotational) != 1)