New device access backend.
[platform/upstream/cryptsetup.git] / lib / utils_devpath.c
1 /*
2  * devname - search for device name
3  *
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  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "utils_dm.h"
32
33 char *crypt_lookup_dev(const char *dev_id);
34 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
35
36 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
37 {
38         struct dirent *entry;
39         struct stat st;
40         char *ptr;
41         char *result = NULL;
42         DIR *dir;
43         int space;
44
45         /* Ignore strange nested directories */
46         if (dir_level > max_level)
47                 return NULL;
48
49         path[PATH_MAX - 1] = '\0';
50         ptr = path + strlen(path);
51         *ptr++ = '/';
52         *ptr = '\0';
53         space = PATH_MAX - (ptr - path);
54
55         dir = opendir(path);
56         if (!dir)
57                 return NULL;
58
59         while((entry = readdir(dir))) {
60                 if (entry->d_name[0] == '.' ||
61                     !strncmp(entry->d_name, "..", 2))
62                         continue;
63
64                 if (dir_level == 0 &&
65                     (!strcmp(entry->d_name, "shm") ||
66                      !strcmp(entry->d_name, "fd") ||
67                      !strcmp(entry->d_name, "char") ||
68                      !strcmp(entry->d_name, "pts")))
69                         continue;
70
71                 strncpy(ptr, entry->d_name, space);
72                 if (stat(path, &st) < 0)
73                         continue;
74
75                 if (S_ISDIR(st.st_mode)) {
76                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
77                         if (result)
78                                 break;
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))
82                                 continue;
83                         if (st.st_rdev == dev) {
84                                 result = strdup(path);
85                                 break;
86                         }
87                 }
88         }
89
90         closedir(dir);
91         return result;
92 }
93
94 /*
95  * Non-udev systemd need to scan for device here.
96  */
97 static char *lookup_dev_old(int major, int minor)
98 {
99         dev_t dev;
100         char *result = NULL, buf[PATH_MAX + 1];
101
102         dev = makedev(major, minor);
103         strncpy(buf, "/dev", PATH_MAX);
104         buf[PATH_MAX] = '\0';
105
106         /* First try low level device */
107         if ((result = __lookup_dev(buf, dev, 0, 0)))
108                 return result;
109
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)))
114                         return result;
115         }
116
117         strncpy(buf, "/dev", PATH_MAX);
118         return  __lookup_dev(buf, dev, 0, 4);
119 }
120
121 /*
122  * Returns string pointing to device in /dev according to "major:minor" dev_id
123  */
124 char *crypt_lookup_dev(const char *dev_id)
125 {
126         int major, minor;
127         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
128         struct stat st;
129         ssize_t len;
130
131         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
132                 return NULL;
133
134         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
135                 return NULL;
136
137         len = readlink(path, link, sizeof(link));
138         if (len < 0) {
139                 /* Without /sys use old scan */
140                 if (stat("/sys/dev/block", &st) < 0)
141                         return lookup_dev_old(major, minor);
142                 return NULL;
143         }
144
145         link[len] = '\0';
146         devname = strrchr(link, '/');
147         if (!devname)
148                 return NULL;
149         devname++;
150
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);
155
156         /*
157          * Check that path is correct.
158          */
159         if (devpath && ((stat(devpath, &st) < 0) ||
160             !S_ISBLK(st.st_mode) ||
161             (st.st_rdev != makedev(major, minor)))) {
162                 free(devpath);
163                 /* Should never happen unless user mangles with dev nodes. */
164                 return lookup_dev_old(major, minor);
165         }
166
167         return devpath;
168 }
169
170 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
171 {
172         char path[PATH_MAX], tmp[64] = {0};
173         int fd, r;
174
175         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
176                      major, minor) < 0)
177                 return 0;
178
179         if ((fd = open(path, O_RDONLY)) < 0)
180                 return 0;
181         r = read(fd, tmp, sizeof(tmp));
182         close(fd);
183
184         if (r <= 0)
185                 return 0;
186
187         if (sscanf(tmp, "%d", rotational) != 1)
188                 return 0;
189
190         return 1;
191 }