utils_devpath.c 9.99 KB
Newer Older
1 2 3
/*
 * devname - search for device name
 *
Milan Broz's avatar
Milan Broz committed
4 5 6 7
 * Copyright (C) 2004 Jana Saout <jana@saout.de>
 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
 * Copyright (C) 2009-2019 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2009-2019 Milan Broz
8 9 10
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
11 12
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 23 24 25
 */

#include <string.h>
#include <stdio.h>
26 27
#include <stdlib.h>
#include <unistd.h>
28
#include <dirent.h>
29 30
#include <fcntl.h>
#include <errno.h>
John Spencer's avatar
John Spencer committed
31
#include <limits.h>
32 33
#include <sys/stat.h>
#include <sys/types.h>
34 35 36
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>     /* for major, minor */
#endif
Milan Broz's avatar
Milan Broz committed
37
#include "internal.h"
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
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;

67 68 69 70 71 72 73
		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;

74 75 76 77 78 79 80 81 82 83
		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 */
84
			if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
85 86 87 88 89 90 91 92 93 94 95 96
				continue;
			if (st.st_rdev == dev) {
				result = strdup(path);
				break;
			}
		}
	}

	closedir(dir);
	return result;
}

97 98 99
/*
 * Non-udev systemd need to scan for device here.
 */
100
static char *lookup_dev_old(int major, int minor)
101 102 103 104 105
{
	dev_t dev;
	char *result = NULL, buf[PATH_MAX + 1];

	dev = makedev(major, minor);
106
	strncpy(buf, "/dev", PATH_MAX);
107 108 109 110 111 112 113
	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  */
114
	if (dm_is_dm_device(major, minor)) {
115 116 117 118 119
		strncpy(buf, dm_get_dir(), PATH_MAX);
		if ((result = __lookup_dev(buf, dev, 0, 0)))
			return result;
	}

120 121
	strncpy(buf, "/dev", PATH_MAX);
	return  __lookup_dev(buf, dev, 0, 4);
122 123
}

124 125 126
/*
 * Returns string pointing to device in /dev according to "major:minor" dev_id
 */
127 128
char *crypt_lookup_dev(const char *dev_id)
{
129 130
	int major, minor;
	char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
131 132 133
	struct stat st;
	ssize_t len;

134 135 136
	if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
		return NULL;

137 138 139
	if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
		return NULL;

140
	len = readlink(path, link, sizeof(link) - 1);
141
	if (len < 0) {
142
		/* Without /sys use old scan */
143
		if (stat("/sys/dev/block", &st) < 0)
144
			return lookup_dev_old(major, minor);
145 146 147 148 149 150 151 152 153
		return NULL;
	}

	link[len] = '\0';
	devname = strrchr(link, '/');
	if (!devname)
		return NULL;
	devname++;

154
	if (dm_is_dm_kernel_name(devname))
155
		devpath = dm_device_path("/dev/mapper/", major, minor);
156 157 158 159 160 161 162 163 164 165 166 167 168
	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);
	}
169

170
	return devpath;
171
}
172

173
static int _read_uint64(const char *sysfs_path, uint64_t *value)
174
{
175
	char tmp[64] = {0};
176 177
	int fd, r;

178
	if ((fd = open(sysfs_path, O_RDONLY)) < 0)
179 180 181 182 183 184 185
		return 0;
	r = read(fd, tmp, sizeof(tmp));
	close(fd);

	if (r <= 0)
		return 0;

186
        if (sscanf(tmp, "%" PRIu64, value) != 1)
187 188 189 190
		return 0;

	return 1;
}
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
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);
}

Milan Broz's avatar
Milan Broz committed
214
int crypt_dev_is_rotational(int major, int minor)
215
{
216 217 218
	uint64_t val;

	if (!_sysfs_get_uint64(major, minor, &val, "queue/rotational"))
Milan Broz's avatar
Milan Broz committed
219
		return 1; /* if failed, expect rotational disk */
220

Milan Broz's avatar
Milan Broz committed
221
	return val ? 1 : 0;
222 223
}

Milan Broz's avatar
Milan Broz committed
224
int crypt_dev_is_partition(const char *dev_path)
225
{
226
	uint64_t val;
227 228 229 230 231 232 233 234
	struct stat st;

	if (stat(dev_path, &st) < 0)
		return 0;

	if (!S_ISBLK(st.st_mode))
		return 0;

235 236 237 238
	if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
			      &val, "partition"))
		return 0;

Milan Broz's avatar
Milan Broz committed
239
	return val ? 1 : 0;
240
}
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
uint64_t crypt_dev_partition_offset(const char *dev_path)
{
	uint64_t val;
	struct stat st;

	if (!crypt_dev_is_partition(dev_path))
		return 0;

	if (stat(dev_path, &st) < 0)
		return 0;

	if (!_sysfs_get_uint64(major(st.st_rdev), minor(st.st_rdev),
			      &val, "start"))
		return 0;

	return val;
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/* 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;

282 283 284 285
	dir = opendir(path);
	if (!dir)
		return NULL;

286
	len = readlink(path, link, sizeof(link) - 1);
287 288
	if (len < 0) {
		closedir(dir);
289
		return NULL;
290
	}
291 292 293 294

	/* Get top level disk name for sysfs search */
	link[len] = '\0';
	devname = strrchr(link, '/');
295 296
	if (!devname) {
		closedir(dir);
297
		return NULL;
298
	}
299 300 301
	devname++;

	/* DM devices do not use kernel partitions. */
302 303
	if (dm_is_dm_kernel_name(devname)) {
		closedir(dir);
304
		return NULL;
305
	}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

	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;
}
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

/* Try to find base device from partition */
char *crypt_get_base_device(const char *dev_path)
{
	char link[PATH_MAX], path[PATH_MAX], part_path[PATH_MAX], *devname;
	struct stat st;
	ssize_t len;

	if (!crypt_dev_is_partition(dev_path))
		return NULL;

	if (stat(dev_path, &st) < 0)
		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 = '\0';
	devname = strrchr(link, '/');
	if (!devname)
		return NULL;
	devname++;

	if (dm_is_dm_kernel_name(devname))
		return NULL;

	snprintf(part_path, sizeof(part_path), "/dev/%s", devname);
	return strdup(part_path);
}
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

int lookup_by_disk_id(const char *dm_uuid)
{
	struct dirent *entry;
	struct stat st;
	int r = 0; /* not found */
	DIR *dir = opendir("/dev/disk/by-id");

	if (!dir)
		/* map ENOTDIR to ENOENT we'll handle both errors same */
		return errno == ENOTDIR ? -ENOENT : -errno;

	while ((entry = readdir(dir))) {
		if (entry->d_name[0] == '.' ||
		    !strncmp(entry->d_name, "..", 2))
			continue;

		if (fstatat(dirfd(dir), entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) {
			r = -EINVAL;
			break;
		}

		if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
			continue;

		if (!strncmp(entry->d_name, dm_uuid, strlen(dm_uuid))) {
			r = 1;
			break;
		}
	}

	closedir(dir);

	return r;
}

int lookup_by_sysfs_uuid_field(const char *dm_uuid, size_t max_len)
{
	struct dirent *entry;
	char subpath[PATH_MAX], uuid[max_len];
	ssize_t s;
	struct stat st;
	int fd, len, r = 0; /* not found */
	DIR *dir = opendir("/sys/block/");

	if (!dir)
		/* map ENOTDIR to ENOENT we'll handle both errors same */
		return errno == ENOTDIR ? -ENOENT : -errno;

	while (r != 1 && (entry = readdir(dir))) {
		if (entry->d_name[0] == '.' ||
		    !strncmp(entry->d_name, "..", 2))
			continue;

		len = snprintf(subpath, PATH_MAX, "%s/%s", entry->d_name, "dm/uuid");
		if (len < 0 || len >= PATH_MAX) {
			r = -EINVAL;
			break;
		}

		/* looking for dm-X/dm/uuid file, symlinks are fine */
		fd = openat(dirfd(dir), subpath, O_RDONLY | O_CLOEXEC);
		if (fd < 0)
			continue;

		if (fstat(fd, &st) || !S_ISREG(st.st_mode)) {
			close(fd);
			continue;
		}

		/* reads binary data */
		s = read_buffer(fd, uuid, max_len - 1);
		if (s > 0) {
			uuid[s] = '\0';
			if (!strncmp(uuid, dm_uuid, strlen(dm_uuid)))
				r = 1;
		}

		close(fd);
	}

	closedir(dir);

	return r;
}