X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=lib%2Futils_devpath.c;h=fc5175ce19ee028d4ad01e06ab48b6bd59d44246;hb=433758e4cbcffef5557692134afec35a9acd0e6a;hp=f8688f785fcb80c1fdf4e71d87551b77197abcaa;hpb=7835b365a730ebc721460166cce9a03cabde4677;p=platform%2Fupstream%2Fcryptsetup.git diff --git a/lib/utils_devpath.c b/lib/utils_devpath.c index f8688f7..fc5175c 100644 --- a/lib/utils_devpath.c +++ b/lib/utils_devpath.c @@ -3,11 +3,13 @@ * * Copyright (C) 2004, Christophe Saout * Copyright (C) 2004-2007, Clemens Fruhwirth - * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved. + * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved. + * Copyright (C) 2009-2012, Milan Broz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License - * version 2 as published by the Free Software Foundation. + * 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 @@ -26,12 +28,12 @@ #include #include #include +#include #include #include #include "utils_dm.h" char *crypt_lookup_dev(const char *dev_id); -int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size); 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) @@ -135,7 +137,7 @@ char *crypt_lookup_dev(const char *dev_id) if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0) return NULL; - len = readlink(path, link, sizeof(link)); + len = readlink(path, link, sizeof(link) - 1); if (len < 0) { /* Without /sys use old scan */ if (stat("/sys/dev/block", &st) < 0) @@ -168,82 +170,6 @@ char *crypt_lookup_dev(const char *dev_id) return devpath; } -static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor) -{ - char path[PATH_MAX], tmp[64] = {0}; - int fd, r = 0; - - if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0) - return 0; - - if ((fd = open(path, O_RDONLY)) < 0) - return 0; - r = read(fd, tmp, sizeof(tmp)); - close(fd); - - if (r <= 0) - return 0; - - tmp[63] = '\0'; - if (sscanf(tmp, "%d:%d", major, minor) != 2) - return 0; - - return 1; -} - -static int crypt_sysfs_get_holders_dir(const char *device, char *path, int size) -{ - struct stat st; - - if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode)) - return 0; - - if (snprintf(path, size, "/sys/dev/block/%d:%d/holders", - major(st.st_rdev), minor(st.st_rdev)) < 0) - return 0; - - return 1; -} - -int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size) -{ - DIR *dir; - struct dirent *d; - char path[PATH_MAX], *dmname; - int major, minor, r = 0; - - if (!crypt_sysfs_get_holders_dir(device, path, sizeof(path))) - return -EINVAL; - - if (!(dir = opendir(path))) - return -EINVAL; - - while (!r && (d = readdir(dir))) { - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) - continue; - - if (!dm_is_dm_kernel_name(d->d_name)) { - r = -EBUSY; - break; - } - - if (!crypt_sysfs_get_major_minor(d->d_name, &major, &minor)) { - r = -EINVAL; - break; - } - - if (!(dmname = dm_device_path(NULL, major, minor))) { - r = -EINVAL; - break; - } - r = dm_check_segment(dmname, offset, size); - free(dmname); - } - closedir(dir); - - return r; -} - int crypt_sysfs_get_rotational(int major, int minor, int *rotational) { char path[PATH_MAX], tmp[64] = {0};