1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
* devname - search for device name
*
* Copyright (C) 2004, Christophe Saout <christophe@saout.de>
* Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
* Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
* Copyright (C) 2009-2013, Milan Broz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "utils_dm.h"
char *crypt_lookup_dev(const char *dev_id);
int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
{
struct dirent *entry;
struct stat st;
char *ptr;
char *result = NULL;
DIR *dir;
int space;
/* Ignore strange nested directories */
if (dir_level > max_level)
return NULL;
path[PATH_MAX - 1] = '\0';
ptr = path + strlen(path);
*ptr++ = '/';
*ptr = '\0';
space = PATH_MAX - (ptr - path);
dir = opendir(path);
if (!dir)
return NULL;
while((entry = readdir(dir))) {
if (entry->d_name[0] == '.' ||
!strncmp(entry->d_name, "..", 2))
continue;
if (dir_level == 0 &&
(!strcmp(entry->d_name, "shm") ||
!strcmp(entry->d_name, "fd") ||
!strcmp(entry->d_name, "char") ||
!strcmp(entry->d_name, "pts")))
continue;
strncpy(ptr, entry->d_name, space);
if (stat(path, &st) < 0)
continue;
if (S_ISDIR(st.st_mode)) {
result = __lookup_dev(path, dev, dir_level + 1, max_level);
if (result)
break;
} else if (S_ISBLK(st.st_mode)) {
/* workaround: ignore dm-X devices, these are internal kernel names */
if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
continue;
if (st.st_rdev == dev) {
result = strdup(path);
break;
}
}
}
closedir(dir);
return result;
}
/*
* Non-udev systemd need to scan for device here.
*/
static char *lookup_dev_old(int major, int minor)
{
dev_t dev;
char *result = NULL, buf[PATH_MAX + 1];
dev = makedev(major, minor);
strncpy(buf, "/dev", PATH_MAX);
buf[PATH_MAX] = '\0';
/* First try low level device */
if ((result = __lookup_dev(buf, dev, 0, 0)))
return result;
/* If it is dm, try DM dir */
if (dm_is_dm_device(major, minor)) {
strncpy(buf, dm_get_dir(), PATH_MAX);
if ((result = __lookup_dev(buf, dev, 0, 0)))
return result;
}
strncpy(buf, "/dev", PATH_MAX);
return __lookup_dev(buf, dev, 0, 4);
}
/*
* Returns string pointing to device in /dev according to "major:minor" dev_id
*/
char *crypt_lookup_dev(const char *dev_id)
{
int major, minor;
char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
struct stat st;
ssize_t len;
if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
return NULL;
if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
return NULL;
len = readlink(path, link, sizeof(link) - 1);
if (len < 0) {
/* Without /sys use old scan */
if (stat("/sys/dev/block", &st) < 0)
return lookup_dev_old(major, minor);
return NULL;
}
link[len] = '\0';
devname = strrchr(link, '/');
if (!devname)
return NULL;
devname++;
if (dm_is_dm_kernel_name(devname))
devpath = dm_device_path("/dev/mapper/", major, minor);
else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
devpath = strdup(path);
/*
* Check that path is correct.
*/
if (devpath && ((stat(devpath, &st) < 0) ||
!S_ISBLK(st.st_mode) ||
(st.st_rdev != makedev(major, minor)))) {
free(devpath);
/* Should never happen unless user mangles with dev nodes. */
return lookup_dev_old(major, minor);
}
return devpath;
}
static int _read_uint64(const char *sysfs_path, uint64_t *value)
{
char tmp[64] = {0};
int fd, r;
if ((fd = open(sysfs_path, O_RDONLY)) < 0)
return 0;
r = read(fd, tmp, sizeof(tmp));
close(fd);
if (r <= 0)
return 0;
if (sscanf(tmp, "%" PRIu64, value) != 1)
return 0;
return 1;
}
static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
{
char path[PATH_MAX];
if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
major, minor, attr) < 0)
return 0;
return _read_uint64(path, value);
}
static int _path_get_uint64(const char *sysfs_path, uint64_t *value, const char *attr)
{
char path[PATH_MAX];
if (snprintf(path, sizeof(path), "%s/%s",
sysfs_path, attr) < 0)
return 0;
return _read_uint64(path, value);
}
int crypt_dev_is_rotational(int major, int minor)
{
uint64_t val;
if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
return 1; /* if failed, expect rotational disk */
return val ? 1 : 0;
}
int crypt_dev_is_partition(const char *dev_path)
{
uint64_t val;
struct stat st;
if (stat(dev_path, &st) < 0)
return 0;
if (!S_ISBLK(st.st_mode))
return 0;
if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
&val, "partition"))
return 0;
return val ? 1 : 0;
}
/* Try to find partition which match offset and size on top level device */
char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size)
{
char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
char *result = NULL;
struct stat st;
size_t devname_len;
ssize_t len;
struct dirent *entry;
DIR *dir;
uint64_t part_offset, part_size;
if (stat(dev_path, &st) < 0)
return NULL;
if (!S_ISBLK(st.st_mode))
return NULL;
if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d",
major(st.st_rdev), minor(st.st_rdev)) < 0)
return NULL;
len = readlink(path, link, sizeof(link) - 1);
if (len < 0)
return NULL;
/* Get top level disk name for sysfs search */
link[len] = '\0';
devname = strrchr(link, '/');
if (!devname)
return NULL;
devname++;
/* DM devices do not use kernel partitions. */
if (dm_is_dm_kernel_name(devname))
return NULL;
dir = opendir(path);
if (!dir)
return NULL;
devname_len = strlen(devname);
while((entry = readdir(dir))) {
if (strncmp(entry->d_name, devname, devname_len))
continue;
if (snprintf(part_path, sizeof(part_path), "%s/%s",
path, entry->d_name) < 0)
continue;
if (stat(part_path, &st) < 0)
continue;
if (S_ISDIR(st.st_mode)) {
if (!_path_get_uint64(part_path, &part_offset, "start") ||
!_path_get_uint64(part_path, &part_size, "size"))
continue;
if (part_offset == offset && part_size == size &&
snprintf(part_path, sizeof(part_path), "/dev/%s",
entry->d_name) > 0) {
result = strdup(part_path);
break;
}
}
}
closedir(dir);
return result;
}
|