Version 1.6.2.
[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  * Copyright (C) 2009-2013, Milan Broz
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include "internal.h"
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) - 1);
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 static int _read_uint64(const char *sysfs_path, uint64_t *value)
171 {
172         char tmp[64] = {0};
173         int fd, r;
174
175         if ((fd = open(sysfs_path, O_RDONLY)) < 0)
176                 return 0;
177         r = read(fd, tmp, sizeof(tmp));
178         close(fd);
179
180         if (r <= 0)
181                 return 0;
182
183         if (sscanf(tmp, "%" PRIu64, value) != 1)
184                 return 0;
185
186         return 1;
187 }
188
189 static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
190 {
191         char path[PATH_MAX];
192
193         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
194                      major, minor, attr) < 0)
195                 return 0;
196
197         return _read_uint64(path, value);
198 }
199
200 static int _path_get_uint64(const char *sysfs_path, uint64_t *value, const char *attr)
201 {
202         char path[PATH_MAX];
203
204         if (snprintf(path, sizeof(path), "%s/%s",
205                      sysfs_path, attr) < 0)
206                 return 0;
207
208         return _read_uint64(path, value);
209 }
210
211 int crypt_dev_is_rotational(int major, int minor)
212 {
213         uint64_t val;
214
215         if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
216                 return 1; /* if failed, expect rotational disk */
217
218         return val ? 1 : 0;
219 }
220
221 int crypt_dev_is_partition(const char *dev_path)
222 {
223         uint64_t val;
224         struct stat st;
225
226         if (stat(dev_path, &st) < 0)
227                 return 0;
228
229         if (!S_ISBLK(st.st_mode))
230                 return 0;
231
232         if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
233                               &val, "partition"))
234                 return 0;
235
236         return val ? 1 : 0;
237 }
238
239 /* Try to find partition which match offset and size on top level device */
240 char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size)
241 {
242         char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
243         char *result = NULL;
244         struct stat st;
245         size_t devname_len;
246         ssize_t len;
247         struct dirent *entry;
248         DIR *dir;
249         uint64_t part_offset, part_size;
250
251         if (stat(dev_path, &st) < 0)
252                 return NULL;
253
254         if (!S_ISBLK(st.st_mode))
255                 return NULL;
256
257         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
258                 major(st.st_rdev), minor(st.st_rdev)) < 0)
259                 return NULL;
260
261         len = readlink(path, link, sizeof(link) - 1);
262         if (len < 0)
263                 return NULL;
264
265         /* Get top level disk name for sysfs search */
266         link[len] = '\0';
267         devname = strrchr(link, '/');
268         if (!devname)
269                 return NULL;
270         devname++;
271
272         /* DM devices do not use kernel partitions. */
273         if (dm_is_dm_kernel_name(devname))
274                 return NULL;
275
276         dir = opendir(path);
277         if (!dir)
278                 return NULL;
279
280         devname_len = strlen(devname);
281         while((entry = readdir(dir))) {
282                 if (strncmp(entry->d_name, devname, devname_len))
283                         continue;
284
285                 if (snprintf(part_path, sizeof(part_path), "%s/%s",
286                     path, entry->d_name) < 0)
287                         continue;
288
289                 if (stat(part_path, &st) < 0)
290                         continue;
291
292                 if (S_ISDIR(st.st_mode)) {
293                         if (!_path_get_uint64(part_path, &part_offset, "start") ||
294                             !_path_get_uint64(part_path, &part_size, "size"))
295                                 continue;
296                         if (part_offset == offset && part_size == size &&
297                             snprintf(part_path, sizeof(part_path), "/dev/%s",
298                                      entry->d_name) > 0) {
299                                 result = strdup(part_path);
300                                 break;
301                         }
302                 }
303         }
304         closedir(dir);
305
306         return result;
307 }