1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-11-04 22:22:07 +03:00
lvm2/libdm/libdm-common.c

2348 lines
54 KiB
C
Raw Normal View History

/*
2004-03-30 23:08:57 +04:00
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
*
2004-03-30 23:08:57 +04:00
* This file is part of the device-mapper userspace tools.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "dmlib.h"
#include "libdm-targets.h"
#include "libdm-common.h"
#include "kdev_t.h"
2008-11-01 05:19:19 +03:00
#include "dm-ioctl.h"
#include <stdarg.h>
#include <sys/param.h>
2007-12-03 20:56:36 +03:00
#include <sys/ioctl.h>
2007-11-30 19:42:26 +03:00
#include <fcntl.h>
#include <dirent.h>
#ifdef UDEV_SYNC_SUPPORT
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
# include <libudev.h>
#endif
2007-11-30 19:42:26 +03:00
#ifdef linux
# include <linux/fs.h>
#endif
2004-04-06 22:54:00 +04:00
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
#endif
#ifdef HAVE_SELINUX_LABEL_H
# include <selinux/label.h>
#endif
2004-04-06 22:54:00 +04:00
#define DM_DEFAULT_NAME_MANGLING_MODE_ENV_VAR_NAME "DM_DEFAULT_NAME_MANGLING_MODE"
#define DEV_DIR "/dev/"
#ifdef UDEV_SYNC_SUPPORT
#ifdef _SEM_SEMUN_UNDEFINED
union semun
{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short int *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
#endif
static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
static char _sysfs_dir[PATH_MAX] = "/sys/";
static char _path0[PATH_MAX]; /* path buffer, safe 4kB on stack */
#define DM_MAX_UUID_PREFIX_LEN 15
static char _default_uuid_prefix[DM_MAX_UUID_PREFIX_LEN + 1] = "LVM-";
2003-01-22 00:25:11 +03:00
static int _verbose = 0;
static int _suspended_dev_counter = 0;
static dm_string_mangling_t _name_mangling_mode = DEFAULT_DM_NAME_MANGLING;
2003-01-22 00:25:11 +03:00
#ifdef HAVE_SELINUX_LABEL_H
static struct selabel_handle *_selabel_handle = NULL;
#endif
static int _udev_disabled = 0;
#ifdef UDEV_SYNC_SUPPORT
static int _semaphore_supported = -1;
static int _udev_running = -1;
static int _sync_with_udev = 1;
static int _udev_checking = 1;
#endif
void dm_lib_init(void)
{
const char *env;
if (getenv("DM_DISABLE_UDEV"))
_udev_disabled = 1;
env = getenv(DM_DEFAULT_NAME_MANGLING_MODE_ENV_VAR_NAME);
if (env && *env) {
if (!strcasecmp(env, "none"))
_name_mangling_mode = DM_STRING_MANGLING_NONE;
else if (!strcasecmp(env, "auto"))
_name_mangling_mode = DM_STRING_MANGLING_AUTO;
else if (!strcasecmp(env, "hex"))
_name_mangling_mode = DM_STRING_MANGLING_HEX;
} else
_name_mangling_mode = DEFAULT_DM_NAME_MANGLING;
}
/*
* Library users can provide their own logging
* function.
*/
__attribute__((format(printf, 5, 0)))
static void _default_log_line(int level,
const char *file __attribute__((unused)),
int line __attribute__((unused)), int dm_errno_or_class,
const char *f, va_list ap)
{
int use_stderr = level & _LOG_STDERR;
level &= ~_LOG_STDERR;
2003-01-22 00:25:11 +03:00
if (level > _LOG_WARN && !_verbose)
return;
2003-01-22 00:25:11 +03:00
if (level < _LOG_WARN)
vfprintf(stderr, f, ap);
2003-01-22 00:25:11 +03:00
else
vfprintf(use_stderr ? stderr : stdout, f, ap);
2003-01-22 00:25:11 +03:00
if (level < _LOG_WARN)
fprintf(stderr, "\n");
else
fprintf(use_stderr ? stderr : stdout, "\n");
}
__attribute__((format(printf, 5, 6)))
static void _default_log_with_errno(int level,
const char *file __attribute__((unused)),
int line __attribute__((unused)), int dm_errno_or_class,
const char *f, ...)
{
va_list ap;
va_start(ap, f);
_default_log_line(level, file, line, dm_errno_or_class, f, ap);
va_end(ap);
}
__attribute__((format(printf, 4, 5)))
static void _default_log(int level, const char *file,
int line, const char *f, ...)
{
va_list ap;
va_start(ap, f);
_default_log_line(level, file, line, 0, f, ap);
va_end(ap);
}
dm_log_fn dm_log = _default_log;
dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
void dm_log_init(dm_log_fn fn)
{
if (fn)
dm_log = fn;
else
dm_log = _default_log;
dm_log_with_errno = _default_log_with_errno;
}
int dm_log_is_non_default(void)
{
return (dm_log == _default_log) ? 0 : 1;
}
void dm_log_with_errno_init(dm_log_with_errno_fn fn)
{
if (fn)
dm_log_with_errno = fn;
else
dm_log_with_errno = _default_log_with_errno;
dm_log = _default_log;
}
2003-01-22 00:25:11 +03:00
void dm_log_init_verbose(int level)
{
_verbose = level;
}
static void _build_dev_path(char *buffer, size_t len, const char *dev_name)
{
/* If there's a /, assume caller knows what they're doing */
if (strchr(dev_name, '/'))
snprintf(buffer, len, "%s", dev_name);
else
snprintf(buffer, len, "%s/%s", _dm_dir, dev_name);
}
int dm_get_library_version(char *version, size_t size)
{
strncpy(version, DM_LIB_VERSION, size);
return 1;
}
void inc_suspended(void)
{
_suspended_dev_counter++;
log_debug_activation("Suspended device counter increased to %d", _suspended_dev_counter);
}
void dec_suspended(void)
{
if (!_suspended_dev_counter) {
log_error("Attempted to decrement suspended device counter below zero.");
return;
}
_suspended_dev_counter--;
log_debug_activation("Suspended device counter reduced to %d", _suspended_dev_counter);
}
int dm_get_suspended_counter(void)
{
return _suspended_dev_counter;
}
int dm_set_name_mangling_mode(dm_string_mangling_t name_mangling_mode)
{
_name_mangling_mode = name_mangling_mode;
return 1;
}
dm_string_mangling_t dm_get_name_mangling_mode(void)
{
return _name_mangling_mode;
}
struct dm_task *dm_task_create(int type)
{
struct dm_task *dmt = dm_zalloc(sizeof(*dmt));
if (!dmt) {
log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
sizeof(*dmt));
return NULL;
}
2006-05-10 20:23:41 +04:00
if (!dm_check_version()) {
dm_free(dmt);
return_NULL;
2006-05-10 20:23:41 +04:00
}
dmt->type = type;
dmt->minor = -1;
2003-04-02 23:03:00 +04:00
dmt->major = -1;
dmt->allow_default_major_fallback = 1;
2008-06-07 00:44:35 +04:00
dmt->uid = DM_DEVICE_UID;
dmt->gid = DM_DEVICE_GID;
dmt->mode = DM_DEVICE_MODE;
2005-09-20 20:39:12 +04:00
dmt->no_open_count = 0;
dmt->read_ahead = DM_READ_AHEAD_AUTO;
dmt->read_ahead_flags = 0;
dmt->event_nr = 0;
2009-08-03 22:01:45 +04:00
dmt->cookie_set = 0;
dmt->query_inactive_table = 0;
dmt->new_uuid = 0;
dmt->secure_data = 0;
2003-01-22 00:25:11 +03:00
return dmt;
}
/*
* Find the name associated with a given device number by scanning _dm_dir.
*/
static int _find_dm_name_of_device(dev_t st_rdev, char *buf, size_t buf_len)
{
const char *name;
char path[PATH_MAX];
struct dirent *dirent;
DIR *d;
struct stat st;
int r = 0;
if (!(d = opendir(_dm_dir))) {
log_sys_error("opendir", _dm_dir);
return 0;
}
while ((dirent = readdir(d))) {
name = dirent->d_name;
if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;
if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
name) == -1) {
log_error("Couldn't create path for %s", name);
continue;
}
if (stat(path, &st))
continue;
if (st.st_rdev == st_rdev) {
strncpy(buf, name, buf_len);
r = 1;
break;
}
}
if (closedir(d))
log_sys_error("closedir", _dm_dir);
return r;
}
static int _is_whitelisted_char(char c)
{
/*
* Actually, DM supports any character in a device name.
* This whitelist is just for proper integration with udev.
*/
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
strchr("#+-.:=@_", c) != NULL)
return 1;
return 0;
}
int check_multiple_mangled_string_allowed(const char *str, const char *str_name,
dm_string_mangling_t mode)
{
if (mode == DM_STRING_MANGLING_AUTO && strstr(str, "\\x5cx")) {
log_error("The %s \"%s\" seems to be mangled more than once. "
"This is not allowed in auto mode.", str_name, str);
return 0;
}
return 1;
}
/*
* Mangle all characters in the input string which are not on a whitelist
* with '\xNN' format where NN is the hex value of the character.
*/
int mangle_string(const char *str, const char *str_name, size_t len,
char *buf, size_t buf_len, dm_string_mangling_t mode)
{
int need_mangling = -1; /* -1 don't know yet, 0 no, 1 yes */
size_t i, j;
if (!str || !buf)
return -1;
/* Is there anything to do at all? */
2012-03-05 16:40:34 +04:00
if (!*str || !len)
return 0;
if (buf_len < DM_NAME_LEN) {
log_error(INTERNAL_ERROR "mangle_string: supplied buffer too small");
return -1;
}
2012-03-05 16:40:34 +04:00
if (mode == DM_STRING_MANGLING_NONE)
mode = DM_STRING_MANGLING_AUTO;
for (i = 0, j = 0; str[i]; i++) {
if (mode == DM_STRING_MANGLING_AUTO) {
/*
* Detect already mangled part of the string and keep it.
* Return error on mixture of mangled/not mangled!
*/
if (str[i] == '\\' && str[i+1] == 'x') {
if ((len - i < 4) || (need_mangling == 1))
goto bad1;
if (buf_len - j < 4)
goto bad2;
memcpy(&buf[j], &str[i], 4);
i+=3; j+=4;
need_mangling = 0;
continue;
}
2009-09-25 23:06:05 +04:00
}
if (_is_whitelisted_char(str[i])) {
/* whitelisted, keep it. */
if (buf_len - j < 1)
goto bad2;
buf[j] = str[i];
j++;
} else {
/*
* Not on a whitelist, mangle it.
* Return error on mixture of mangled/not mangled
* unless a DM_STRING_MANGLING_HEX is used!.
*/
if ((mode != DM_STRING_MANGLING_HEX) && (need_mangling == 0))
goto bad1;
if (buf_len - j < 4)
goto bad2;
sprintf(&buf[j], "\\x%02x", (unsigned char) str[i]);
j+=4;
need_mangling = 1;
}
}
if (buf_len - j < 1)
goto bad2;
buf[j] = '\0';
/* All chars in the string whitelisted? */
if (need_mangling == -1)
need_mangling = 0;
return need_mangling;
bad1:
log_error("The %s \"%s\" contains mixed mangled and unmangled "
"characters or it's already mangled improperly.", str_name, str);
return -1;
bad2:
log_error("Mangled form of the %s too long for \"%s\".", str_name, str);
return -1;
}
/*
* Try to unmangle supplied string.
* Return value: -1 on error, 0 when no unmangling needed, 1 when unmangling applied
*/
int unmangle_string(const char *str, const char *str_name, size_t len,
char *buf, size_t buf_len, dm_string_mangling_t mode)
{
int strict = mode != DM_STRING_MANGLING_NONE;
char str_rest[DM_NAME_LEN];
size_t i, j;
int code;
int r = 0;
if (!str || !buf)
return -1;
/* Is there anything to do at all? */
2012-03-05 16:40:34 +04:00
if (!*str || !len)
return 0;
if (buf_len < DM_NAME_LEN) {
log_error(INTERNAL_ERROR "unmangle_string: supplied buffer too small");
return -1;
}
for (i = 0, j = 0; str[i]; i++, j++) {
if (strict && !(_is_whitelisted_char(str[i]) || str[i]=='\\')) {
log_error("The %s \"%s\" should be mangled but "
"it contains blacklisted characters.", str_name, str);
j=0; r=-1;
goto out;
}
if (str[i] == '\\' && str[i+1] == 'x') {
if (!sscanf(&str[i+2], "%2x%s", &code, str_rest)) {
log_debug_activation("Hex encoding mismatch detected in %s \"%s\" "
"while trying to unmangle it.", str_name, str);
goto out;
}
buf[j] = (unsigned char) code;
/* skip the encoded part we've just decoded! */
i+= 3;
/* unmangling applied */
r = 1;
} else
buf[j] = str[i];
}
out:
buf[j] = '\0';
return r;
}
static int _dm_task_set_name(struct dm_task *dmt, const char *name,
dm_string_mangling_t mangling_mode)
{
char mangled_name[DM_NAME_LEN];
2012-03-05 16:40:34 +04:00
int r = 0;
dm_free(dmt->dev_name);
dmt->dev_name = NULL;
dm_free(dmt->mangled_dev_name);
dmt->mangled_dev_name = NULL;
if (strlen(name) >= DM_NAME_LEN) {
log_error("Name \"%s\" too long.", name);
return 0;
}
if (!check_multiple_mangled_string_allowed(name, "name", mangling_mode))
return_0;
2012-03-05 16:40:34 +04:00
if (mangling_mode != DM_STRING_MANGLING_NONE &&
(r = mangle_string(name, "name", strlen(name), mangled_name,
sizeof(mangled_name), mangling_mode)) < 0) {
log_error("Failed to mangle device name \"%s\".", name);
return 0;
}
/* Store mangled_dev_name only if it differs from dev_name! */
if (r) {
log_debug_activation("Device name mangled [%s]: %s --> %s",
mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
name, mangled_name);
if (!(dmt->mangled_dev_name = dm_strdup(mangled_name))) {
log_error("_dm_task_set_name: dm_strdup(%s) failed", mangled_name);
return 0;
}
}
if (!(dmt->dev_name = dm_strdup(name))) {
log_error("_dm_task_set_name: strdup(%s) failed", name);
return 0;
}
return 1;
}
static int _dm_task_set_name_from_path(struct dm_task *dmt, const char *path,
const char *name)
{
char buf[PATH_MAX];
struct stat st1, st2;
const char *final_name;
if (dmt->type == DM_DEVICE_CREATE) {
log_error("Name \"%s\" invalid. It contains \"/\".", path);
return 0;
}
if (stat(path, &st1)) {
log_error("Device %s not found", path);
return 0;
}
/*
* If supplied path points to same device as last component
* under /dev/mapper, use that name directly. Otherwise call
* _find_dm_name_of_device() to scan _dm_dir for a match.
*/
if (dm_snprintf(buf, sizeof(buf), "%s/%s", _dm_dir, name) == -1) {
log_error("Couldn't create path for %s", name);
return 0;
}
if (!stat(buf, &st2) && (st1.st_rdev == st2.st_rdev))
final_name = name;
else if (_find_dm_name_of_device(st1.st_rdev, buf, sizeof(buf)))
final_name = buf;
else {
log_error("Device %s not found", name);
return 0;
}
/* This is an already existing path - do not mangle! */
return _dm_task_set_name(dmt, final_name, DM_STRING_MANGLING_NONE);
}
int dm_task_set_name(struct dm_task *dmt, const char *name)
{
char *pos;
/* Path supplied for existing device? */
if ((pos = strrchr(name, '/')))
return _dm_task_set_name_from_path(dmt, name, pos + 1);
return _dm_task_set_name(dmt, name, dm_get_name_mangling_mode());
}
const char *dm_task_get_name(const struct dm_task *dmt)
{
return (dmt->dmi.v4->name);
}
static char *_task_get_string_mangled(const char *str, const char *str_name,
char *buf, size_t buf_size,
dm_string_mangling_t mode)
{
char *rs;
int r;
if ((r = mangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0)
return NULL;
if (!(rs = r ? dm_strdup(buf) : dm_strdup(str)))
log_error("_task_get_string_mangled: dm_strdup failed");
return rs;
}
static char *_task_get_string_unmangled(const char *str, const char *str_name,
char *buf, size_t buf_size,
dm_string_mangling_t mode)
{
char *rs;
int r = 0;
/*
* Unless the mode used is 'none', the string
* is *already* unmangled on ioctl return!
*/
if (mode == DM_STRING_MANGLING_NONE &&
(r = unmangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0)
return NULL;
if (!(rs = r ? dm_strdup(buf) : dm_strdup(str)))
log_error("_task_get_string_unmangled: dm_strdup failed");
return rs;
}
char *dm_task_get_name_mangled(const struct dm_task *dmt)
{
const char *s = dm_task_get_name(dmt);
char buf[DM_NAME_LEN];
char *rs;
if (!(rs = _task_get_string_mangled(s, "name", buf, sizeof(buf), dm_get_name_mangling_mode())))
log_error("Failed to mangle device name \"%s\".", s);
return rs;
}
char *dm_task_get_name_unmangled(const struct dm_task *dmt)
{
const char *s = dm_task_get_name(dmt);
char buf[DM_NAME_LEN];
char *rs;
if (!(rs = _task_get_string_unmangled(s, "name", buf, sizeof(buf), dm_get_name_mangling_mode())))
log_error("Failed to unmangle device name \"%s\".", s);
return rs;
}
const char *dm_task_get_uuid(const struct dm_task *dmt)
{
return (dmt->dmi.v4->uuid);
}
char *dm_task_get_uuid_mangled(const struct dm_task *dmt)
{
const char *s = dm_task_get_uuid(dmt);
char buf[DM_UUID_LEN];
char *rs;
if (!(rs = _task_get_string_mangled(s, "UUID", buf, sizeof(buf), dm_get_name_mangling_mode())))
log_error("Failed to mangle device uuid \"%s\".", s);
return rs;
}
char *dm_task_get_uuid_unmangled(const struct dm_task *dmt)
{
const char *s = dm_task_get_uuid(dmt);
char buf[DM_UUID_LEN];
char *rs;
if (!(rs = _task_get_string_unmangled(s, "UUID", buf, sizeof(buf), dm_get_name_mangling_mode())))
log_error("Failed to unmangle device uuid \"%s\".", s);
return rs;
}
int dm_task_set_newname(struct dm_task *dmt, const char *newname)
{
dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
char mangled_name[DM_NAME_LEN];
2012-03-05 16:40:34 +04:00
int r = 0;
if (strchr(newname, '/')) {
log_error("Name \"%s\" invalid. It contains \"/\".", newname);
return 0;
}
if (strlen(newname) >= DM_NAME_LEN) {
log_error("Name \"%s\" too long", newname);
return 0;
}
if (!check_multiple_mangled_string_allowed(newname, "new name", mangling_mode))
return_0;
2012-03-05 16:40:34 +04:00
if (mangling_mode != DM_STRING_MANGLING_NONE &&
(r = mangle_string(newname, "new name", strlen(newname), mangled_name,
sizeof(mangled_name), mangling_mode)) < 0) {
log_error("Failed to mangle new device name \"%s\"", newname);
return 0;
}
if (r) {
log_debug_activation("New device name mangled [%s]: %s --> %s",
mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
newname, mangled_name);
newname = mangled_name;
}
if (!(dmt->newname = dm_strdup(<