2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of the device-mapper userspace tools.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "libdm-targets.h"
18 #include "libdm-common.h"
23 #include <sys/param.h>
24 #include <sys/ioctl.h>
28 #ifdef UDEV_SYNC_SUPPORT
29 # include <sys/types.h>
32 # define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
37 # include <linux/fs.h>
41 # include <selinux/selinux.h>
43 #ifdef HAVE_SELINUX_LABEL_H
44 # include <selinux/label.h>
47 #define DEV_DIR "/dev/"
49 #ifdef UDEV_SYNC_SUPPORT
50 #ifdef _SEM_SEMUN_UNDEFINED
53 int val; /* value for SETVAL */
54 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
55 unsigned short int *array; /* array for GETALL & SETALL */
56 struct seminfo *__buf; /* buffer for IPC_INFO */
61 static char _dm_dir[PATH_MAX] = DEV_DIR DM_DIR;
63 static int _verbose = 0;
65 #ifdef HAVE_SELINUX_LABEL_H
66 static struct selabel_handle *_selabel_handle = NULL;
69 #ifdef UDEV_SYNC_SUPPORT
70 static int _semaphore_supported = -1;
71 static int _udev_running = -1;
72 static int _sync_with_udev = 1;
73 static int _udev_checking = 1;
77 * Library users can provide their own logging
81 static void _default_log_line(int level,
82 const char *file __attribute__((unused)),
83 int line __attribute__((unused)), int dm_errno,
84 const char *f, va_list ap)
86 int use_stderr = level & _LOG_STDERR;
88 level &= ~_LOG_STDERR;
90 if (level > _LOG_WARN && !_verbose)
93 if (level < _LOG_WARN)
94 vfprintf(stderr, f, ap);
96 vfprintf(use_stderr ? stderr : stdout, f, ap);
98 if (level < _LOG_WARN)
99 fprintf(stderr, "\n");
101 fprintf(use_stderr ? stderr : stdout, "\n");
104 static void _default_log_with_errno(int level,
105 const char *file __attribute__((unused)),
106 int line __attribute__((unused)), int dm_errno,
112 _default_log_line(level, file, line, dm_errno, f, ap);
116 static void _default_log(int level, const char *file,
117 int line, const char *f, ...)
122 _default_log_line(level, file, line, 0, f, ap);
126 dm_log_fn dm_log = _default_log;
127 dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno;
129 void dm_log_init(dm_log_fn fn)
134 dm_log = _default_log;
136 dm_log_with_errno = _default_log_with_errno;
139 int dm_log_is_non_default(void)
141 return (dm_log == _default_log) ? 0 : 1;
144 void dm_log_with_errno_init(dm_log_with_errno_fn fn)
147 dm_log_with_errno = fn;
149 dm_log_with_errno = _default_log_with_errno;
151 dm_log = _default_log;
154 void dm_log_init_verbose(int level)
159 static void _build_dev_path(char *buffer, size_t len, const char *dev_name)
161 /* If there's a /, assume caller knows what they're doing */
162 if (strchr(dev_name, '/'))
163 snprintf(buffer, len, "%s", dev_name);
165 snprintf(buffer, len, "%s/%s", _dm_dir, dev_name);
168 int dm_get_library_version(char *version, size_t size)
170 strncpy(version, DM_LIB_VERSION, size);
174 struct dm_task *dm_task_create(int type)
176 struct dm_task *dmt = dm_zalloc(sizeof(*dmt));
179 log_error("dm_task_create: malloc(%" PRIsize_t ") failed",
184 if (!dm_check_version()) {
192 dmt->allow_default_major_fallback = 1;
193 dmt->uid = DM_DEVICE_UID;
194 dmt->gid = DM_DEVICE_GID;
195 dmt->mode = DM_DEVICE_MODE;
196 dmt->no_open_count = 0;
197 dmt->read_ahead = DM_READ_AHEAD_AUTO;
198 dmt->read_ahead_flags = 0;
201 dmt->query_inactive_table = 0;
208 * Find the name associated with a given device number by scanning _dm_dir.
210 static char *_find_dm_name_of_device(dev_t st_rdev)
214 struct dirent *dirent;
217 char *new_name = NULL;
219 if (!(d = opendir(_dm_dir))) {
220 log_sys_error("opendir", _dm_dir);
224 while ((dirent = readdir(d))) {
225 name = dirent->d_name;
227 if (!strcmp(name, ".") || !strcmp(name, ".."))
230 if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
232 log_error("Couldn't create path for %s", name);
236 if (stat(path, &buf))
239 if (buf.st_rdev == st_rdev) {
240 if (!(new_name = dm_strdup(name)))
241 log_error("dm_task_set_name: strdup(%s) failed",
248 log_sys_error("closedir", _dm_dir);
253 int dm_task_set_name(struct dm_task *dmt, const char *name)
256 char *new_name = NULL;
258 struct stat st1, st2;
260 dm_free(dmt->dev_name);
261 dmt->dev_name = NULL;
264 * Path supplied for existing device?
266 if ((pos = strrchr(name, '/'))) {
267 if (dmt->type == DM_DEVICE_CREATE) {
268 log_error("Name \"%s\" invalid. It contains \"/\".", name);
272 if (stat(name, &st1)) {
273 log_error("Device %s not found", name);
278 * If supplied path points to same device as last component
279 * under /dev/mapper, use that name directly. Otherwise call
280 * _find_dm_name_of_device() to scan _dm_dir for a match.
282 if (dm_snprintf(path, sizeof(path), "%s/%s", _dm_dir,
284 log_error("Couldn't create path for %s", pos + 1);
288 if (!stat(path, &st2) && (st1.st_rdev == st2.st_rdev))
290 else if ((new_name = _find_dm_name_of_device(st1.st_rdev)))
293 log_error("Device %s not found", name);
298 if (strlen(name) >= DM_NAME_LEN) {
299 log_error("Name \"%s\" too long", name);
305 dmt->dev_name = new_name;
306 else if (!(dmt->dev_name = dm_strdup(name))) {
307 log_error("dm_task_set_name: strdup(%s) failed", name);
314 int dm_task_set_uuid(struct dm_task *dmt, const char *uuid)
318 if (!(dmt->uuid = dm_strdup(uuid))) {
319 log_error("dm_task_set_uuid: strdup(%s) failed", uuid);
326 int dm_task_set_major(struct dm_task *dmt, int major)
329 dmt->allow_default_major_fallback = 0;
334 int dm_task_set_minor(struct dm_task *dmt, int minor)
341 int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor,
342 int allow_default_major_fallback)
346 dmt->allow_default_major_fallback = allow_default_major_fallback;
351 int dm_task_set_uid(struct dm_task *dmt, uid_t uid)
358 int dm_task_set_gid(struct dm_task *dmt, gid_t gid)
365 int dm_task_set_mode(struct dm_task *dmt, mode_t mode)
372 int dm_task_add_target(struct dm_task *dmt, uint64_t start, uint64_t size,
373 const char *ttype, const char *params)
375 struct target *t = create_target(start, size, ttype, params);
381 dmt->head = dmt->tail = t;
391 static int _selabel_lookup(const char *path, mode_t mode,
392 security_context_t *scontext)
394 #ifdef HAVE_SELINUX_LABEL_H
395 if (!_selabel_handle &&
396 !(_selabel_handle = selabel_open(SELABEL_CTX_FILE, NULL, 0))) {
397 log_error("selabel_open failed: %s", strerror(errno));
401 if (selabel_lookup(_selabel_handle, scontext, path, mode)) {
402 log_error("selabel_lookup failed: %s", strerror(errno));
406 if (matchpathcon(path, mode, scontext)) {
407 log_error("matchpathcon failed: %s", strerror(errno));
415 int dm_prepare_selinux_context(const char *path, mode_t mode)
418 security_context_t scontext = NULL;
420 if (is_selinux_enabled() <= 0)
424 if (!_selabel_lookup(path, mode, &scontext))
427 log_debug("Preparing SELinux context for %s to %s.", path, scontext);
430 log_debug("Resetting SELinux context to default value.");
432 if (setfscreatecon(scontext) < 0) {
433 log_sys_error("setfscreatecon", path);
443 int dm_set_selinux_context(const char *path, mode_t mode)
446 security_context_t scontext;
448 if (is_selinux_enabled() <= 0)
451 if (!_selabel_lookup(path, mode, &scontext))
454 log_debug("Setting SELinux context for %s to %s.", path, scontext);
456 if ((lsetfilecon(path, scontext) < 0) && (errno != ENOTSUP)) {
457 log_sys_error("lsetfilecon", path);
467 void selinux_release(void)
469 #ifdef HAVE_SELINUX_LABEL_H
471 selabel_close(_selabel_handle);
472 _selabel_handle = NULL;
476 static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
477 uid_t uid, gid_t gid, mode_t mode, int check_udev)
481 dev_t dev = MKDEV(major, minor);
484 _build_dev_path(path, sizeof(path), dev_name);
486 if (stat(path, &info) >= 0) {
487 if (!S_ISBLK(info.st_mode)) {
488 log_error("A non-block device file at '%s' "
489 "is already present", path);
493 /* If right inode already exists we don't touch uid etc. */
494 if (info.st_rdev == dev)
497 if (unlink(path) < 0) {
498 log_error("Unable to unlink device node for '%s'",
502 } else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
504 log_warn("%s not set up by udev: Falling back to direct "
505 "node creation.", path);
507 (void) dm_prepare_selinux_context(path, S_IFBLK);
509 if (mknod(path, S_IFBLK | mode, dev) < 0) {
510 log_error("Unable to make device node for '%s'", dev_name);
512 (void) dm_prepare_selinux_context(NULL, 0);
516 (void) dm_prepare_selinux_context(NULL, 0);
518 if (chown(path, uid, gid) < 0) {
519 log_sys_error("chown", path);
523 log_debug("Created %s", path);
528 static int _rm_dev_node(const char *dev_name, int check_udev)
533 _build_dev_path(path, sizeof(path), dev_name);
535 if (stat(path, &info) < 0)
537 else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
539 log_warn("Node %s was not removed by udev. "
540 "Falling back to direct node removal.", path);
542 if (unlink(path) < 0) {
543 log_error("Unable to unlink device node for '%s'", dev_name);
547 log_debug("Removed %s", path);
552 static int _rename_dev_node(const char *old_name, const char *new_name,
555 char oldpath[PATH_MAX];
556 char newpath[PATH_MAX];
559 _build_dev_path(oldpath, sizeof(oldpath), old_name);
560 _build_dev_path(newpath, sizeof(newpath), new_name);
562 if (stat(newpath, &info) == 0) {
563 if (!S_ISBLK(info.st_mode)) {
564 log_error("A non-block device file at '%s' "
565 "is already present", newpath);
568 else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
570 if (stat(oldpath, &info) < 0 &&
572 /* assume udev already deleted this */
575 log_warn("The node %s should have been renamed to %s "
576 "by udev but old node is still present. "
577 "Falling back to direct old node removal.",
579 return _rm_dev_node(old_name, 0);
583 if (unlink(newpath) < 0) {
584 if (errno == EPERM) {
585 /* devfs, entry has already been renamed */
588 log_error("Unable to unlink device node for '%s'",
593 else if (dm_udev_get_sync_support() && dm_udev_get_checking() &&
595 log_warn("The node %s should have been renamed to %s "
596 "by udev but new node is not present. "
597 "Falling back to direct node rename.",
600 if (rename(oldpath, newpath) < 0) {
601 log_error("Unable to rename device node from '%s' to '%s'",
606 log_debug("Renamed %s to %s", oldpath, newpath);
612 static int _open_dev_node(const char *dev_name)
617 _build_dev_path(path, sizeof(path), dev_name);
619 if ((fd = open(path, O_RDONLY, 0)) < 0)
620 log_sys_error("open", path);
625 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
629 long read_ahead_long;
632 log_error("Empty device name passed to BLKRAGET");
636 if ((fd = _open_dev_node(dev_name)) < 0)
639 if (ioctl(fd, BLKRAGET, &read_ahead_long)) {
640 log_sys_error("BLKRAGET", dev_name);
644 *read_ahead = (uint32_t) read_ahead_long;
645 log_debug("%s: read ahead is %" PRIu32, dev_name, *read_ahead);
654 static int _set_read_ahead(const char *dev_name, uint32_t read_ahead)
658 long read_ahead_long = (long) read_ahead;
661 log_error("Empty device name passed to BLKRAGET");
665 if ((fd = _open_dev_node(dev_name)) < 0)
668 log_debug("%s: Setting read ahead to %" PRIu32, dev_name, read_ahead);
670 if (ioctl(fd, BLKRASET, read_ahead_long)) {
671 log_sys_error("BLKRASET", dev_name);
681 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
682 uint32_t read_ahead_flags)
684 uint32_t current_read_ahead;
686 if (read_ahead == DM_READ_AHEAD_AUTO)
689 if (read_ahead == DM_READ_AHEAD_NONE)
692 if (read_ahead_flags & DM_READ_AHEAD_MINIMUM_FLAG) {
693 if (!get_dev_node_read_ahead(dev_name, ¤t_read_ahead))
696 if (current_read_ahead > read_ahead) {
697 log_debug("%s: retaining kernel read ahead of %" PRIu32
698 " (requested %" PRIu32 ")",
699 dev_name, current_read_ahead, read_ahead);
704 return _set_read_ahead(dev_name, read_ahead);
709 int get_dev_node_read_ahead(const char *dev_name, uint32_t *read_ahead)
716 static int _set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
717 uint32_t read_ahead_flags)
730 static int _do_node_op(node_op_t type, const char *dev_name, uint32_t major,
731 uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
732 const char *old_name, uint32_t read_ahead,
733 uint32_t read_ahead_flags, int check_udev)
737 return _add_dev_node(dev_name, major, minor, uid, gid,
740 return _rm_dev_node(dev_name, check_udev);
742 return _rename_dev_node(old_name, dev_name, check_udev);
743 case NODE_READ_AHEAD:
744 return _set_dev_node_read_ahead(dev_name, read_ahead,
751 static DM_LIST_INIT(_node_ops);
753 struct node_op_parms {
763 uint32_t read_ahead_flags;
769 static void _store_str(char **pos, char **ptr, const char *str)
773 *pos += strlen(*ptr) + 1;
776 static int _stack_node_op(node_op_t type, const char *dev_name, uint32_t major,
777 uint32_t minor, uid_t uid, gid_t gid, mode_t mode,
778 const char *old_name, uint32_t read_ahead,
779 uint32_t read_ahead_flags, int check_udev)
781 struct node_op_parms *nop;
782 struct dm_list *noph, *nopht;
783 size_t len = strlen(dev_name) + strlen(old_name) + 2;
787 * Ignore any outstanding operations on the node if deleting it
789 if (type == NODE_DEL) {
790 dm_list_iterate_safe(noph, nopht, &_node_ops) {
791 nop = dm_list_item(noph, struct node_op_parms);
792 if (!strcmp(dev_name, nop->dev_name)) {
793 dm_list_del(&nop->list);
799 if (!(nop = dm_malloc(sizeof(*nop) + len))) {
800 log_error("Insufficient memory to stack mknod operation");
811 nop->read_ahead = read_ahead;
812 nop->read_ahead_flags = read_ahead_flags;
813 nop->check_udev = check_udev;
815 _store_str(&pos, &nop->dev_name, dev_name);
816 _store_str(&pos, &nop->old_name, old_name);
818 dm_list_add(&_node_ops, &nop->list);
823 static void _pop_node_ops(void)
825 struct dm_list *noph, *nopht;
826 struct node_op_parms *nop;
828 dm_list_iterate_safe(noph, nopht, &_node_ops) {
829 nop = dm_list_item(noph, struct node_op_parms);
830 _do_node_op(nop->type, nop->dev_name, nop->major, nop->minor,
831 nop->uid, nop->gid, nop->mode, nop->old_name,
832 nop->read_ahead, nop->read_ahead_flags,
834 dm_list_del(&nop->list);
839 int add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
840 uid_t uid, gid_t gid, mode_t mode, int check_udev)
842 log_debug("%s: Stacking NODE_ADD (%" PRIu32 ",%" PRIu32 ") %u:%u 0%o",
843 dev_name, major, minor, uid, gid, mode);
845 return _stack_node_op(NODE_ADD, dev_name, major, minor, uid,
846 gid, mode, "", 0, 0, check_udev);
849 int rename_dev_node(const char *old_name, const char *new_name, int check_udev)
851 log_debug("%s: Stacking NODE_RENAME to %s", old_name, new_name);
853 return _stack_node_op(NODE_RENAME, new_name, 0, 0, 0,
854 0, 0, old_name, 0, 0, check_udev);
857 int rm_dev_node(const char *dev_name, int check_udev)
859 log_debug("%s: Stacking NODE_DEL (replaces other stacked ops)", dev_name);
861 return _stack_node_op(NODE_DEL, dev_name, 0, 0, 0,
862 0, 0, "", 0, 0, check_udev);
865 int set_dev_node_read_ahead(const char *dev_name, uint32_t read_ahead,
866 uint32_t read_ahead_flags)
868 if (read_ahead == DM_READ_AHEAD_AUTO)
871 log_debug("%s: Stacking NODE_READ_AHEAD %" PRIu32 " (flags=%" PRIu32
872 ")", dev_name, read_ahead, read_ahead_flags);
874 return _stack_node_op(NODE_READ_AHEAD, dev_name, 0, 0, 0, 0,
875 0, "", read_ahead, read_ahead_flags, 0);
878 void update_devs(void)
883 int dm_set_dev_dir(const char *dev_dir)
887 if (*dev_dir != '/') {
888 log_debug("Invalid dev_dir value, %s: "
889 "not an absolute name.", dev_dir);
893 len = strlen(dev_dir);
894 slash = dev_dir[len-1] == '/' ? "" : "/";
896 if (snprintf(_dm_dir, sizeof _dm_dir, "%s%s%s", dev_dir, slash, DM_DIR)
898 log_debug("Invalid dev_dir value, %s: name too long.", dev_dir);
905 const char *dm_dir(void)
910 int dm_mknodes(const char *name)
915 if (!(dmt = dm_task_create(DM_DEVICE_MKNODES)))
918 if (name && !dm_task_set_name(dmt, name))
921 if (!dm_task_no_open_count(dmt))
924 r = dm_task_run(dmt);
927 dm_task_destroy(dmt);
931 int dm_driver_version(char *version, size_t size)
936 if (!(dmt = dm_task_create(DM_DEVICE_VERSION)))
939 if (!dm_task_run(dmt))
940 log_error("Failed to get driver version");
942 if (!dm_task_get_driver_version(dmt, version, size))
948 dm_task_destroy(dmt);
952 #ifndef UDEV_SYNC_SUPPORT
953 void dm_udev_set_sync_support(int sync_with_udev)
957 int dm_udev_get_sync_support(void)
962 void dm_udev_set_checking(int checking)
966 int dm_udev_get_checking(void)
971 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
973 if (dm_cookie_supported())
974 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
980 int dm_udev_complete(uint32_t cookie)
985 int dm_udev_wait(uint32_t cookie)
990 #else /* UDEV_SYNC_SUPPORT */
992 static int _check_semaphore_is_supported(void)
996 struct seminfo seminfo;
998 arg.__buf = &seminfo;
999 maxid = semctl(0, 0, SEM_INFO, arg);
1002 log_warn("Kernel not configured for semaphores (System V IPC). "
1003 "Not using udev synchronisation code.");
1010 static int _check_udev_is_running(void)
1013 struct udev_queue *udev_queue;
1016 if (!(udev = udev_new()))
1019 if (!(udev_queue = udev_queue_new(udev))) {
1024 if (!(r = udev_queue_get_udev_is_active(udev_queue)))
1025 log_debug("Udev is not running. "
1026 "Not using udev synchronisation code.");
1028 udev_queue_unref(udev_queue);
1034 log_error("Could not get udev state. Assuming udev is not running.");
1038 static void _check_udev_sync_requirements_once(void)
1040 if (_semaphore_supported < 0)
1041 _semaphore_supported = _check_semaphore_is_supported();
1043 if (_udev_running < 0)
1044 _udev_running = _check_udev_is_running();
1047 void dm_udev_set_sync_support(int sync_with_udev)
1049 _check_udev_sync_requirements_once();
1050 _sync_with_udev = sync_with_udev;
1053 int dm_udev_get_sync_support(void)
1055 _check_udev_sync_requirements_once();
1057 return _semaphore_supported && dm_cookie_supported() &&
1058 _udev_running && _sync_with_udev;
1061 void dm_udev_set_checking(int checking)
1063 if ((_udev_checking = checking))
1064 log_debug("DM udev checking enabled");
1066 log_debug("DM udev checking disabled");
1069 int dm_udev_get_checking(void)
1071 return _udev_checking;
1074 static int _get_cookie_sem(uint32_t cookie, int *semid)
1076 if (cookie >> 16 != DM_COOKIE_MAGIC) {
1077 log_error("Could not continue to access notification "
1078 "semaphore identified by cookie value %"
1079 PRIu32 " (0x%x). Incorrect cookie prefix.",
1084 if ((*semid = semget((key_t) cookie, 1, 0)) >= 0)
1089 log_error("Could not find notification "
1090 "semaphore identified by cookie "
1091 "value %" PRIu32 " (0x%x)",
1095 log_error("No permission to access "
1096 "notificaton semaphore identified "
1097 "by cookie value %" PRIu32 " (0x%x)",
1101 log_error("Failed to access notification "
1102 "semaphore identified by cookie "
1103 "value %" PRIu32 " (0x%x): %s",
1104 cookie, cookie, strerror(errno));
1111 static int _udev_notify_sem_inc(uint32_t cookie, int semid)
1113 struct sembuf sb = {0, 1, 0};
1115 if (semop(semid, &sb, 1) < 0) {
1116 log_error("semid %d: semop failed for cookie 0x%" PRIx32 ": %s",
1117 semid, cookie, strerror(errno));
1121 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1127 static int _udev_notify_sem_dec(uint32_t cookie, int semid)
1129 struct sembuf sb = {0, -1, IPC_NOWAIT};
1131 if (semop(semid, &sb, 1) < 0) {
1134 log_error("semid %d: semop failed for cookie "
1136 "incorrect semaphore state",
1140 log_error("semid %d: semop failed for cookie "
1141 "0x%" PRIx32 ": %s",
1142 semid, cookie, strerror(errno));
1148 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) decremented",
1154 static int _udev_notify_sem_destroy(uint32_t cookie, int semid)
1156 if (semctl(semid, 0, IPC_RMID, 0) < 0) {
1157 log_error("Could not cleanup notification semaphore "
1158 "identified by cookie value %" PRIu32 " (0x%x): %s",
1159 cookie, cookie, strerror(errno));
1163 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) destroyed", cookie,
1169 static int _udev_notify_sem_create(uint32_t *cookie, int *semid)
1173 uint16_t base_cookie;
1174 uint32_t gen_cookie;
1175 union semun sem_arg;
1177 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
1178 log_error("Failed to open /dev/urandom "
1179 "to create random cookie value");
1184 /* Generate random cookie value. Be sure it is unique and non-zero. */
1186 /* FIXME Handle non-error returns from read(). Move _io() into libdm? */
1187 if (read(fd, &base_cookie, sizeof(base_cookie)) != sizeof(base_cookie)) {
1188 log_error("Failed to initialize notification cookie");
1192 gen_cookie = DM_COOKIE_MAGIC << 16 | base_cookie;
1194 if (base_cookie && (gen_semid = semget((key_t) gen_cookie,
1195 1, 0600 | IPC_CREAT | IPC_EXCL)) < 0) {
1198 /* if the semaphore key exists, we
1199 * simply generate another random one */
1203 log_error("Not enough memory to create "
1204 "notification semaphore");
1207 log_error("Limit for the maximum number "
1208 "of semaphores reached. You can "
1209 "check and set the limits in "
1210 "/proc/sys/kernel/sem.");
1213 log_error("Failed to create notification "
1214 "semaphore: %s", strerror(errno));
1218 } while (!base_cookie);
1220 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) created",
1221 gen_cookie, gen_semid);
1225 if (semctl(gen_semid, 0, SETVAL, sem_arg) < 0) {
1226 log_error("semid %d: semctl failed: %s", gen_semid, strerror(errno));
1227 /* We have to destroy just created semaphore
1228 * so it won't stay in the system. */
1229 (void) _udev_notify_sem_destroy(gen_cookie, gen_semid);
1233 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) incremented",
1234 gen_cookie, gen_semid);
1240 *cookie = gen_cookie;
1253 int dm_udev_create_cookie(uint32_t *cookie)
1257 if (!dm_udev_get_sync_support()) {
1262 return _udev_notify_sem_create(cookie, &semid);
1265 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags)
1269 if (dm_cookie_supported())
1270 dmt->event_nr = flags << DM_UDEV_FLAGS_SHIFT;
1272 if (!dm_udev_get_sync_support()) {
1278 if (!_get_cookie_sem(*cookie, &semid))
1280 } else if (!_udev_notify_sem_create(cookie, &semid))
1283 if (!_udev_notify_sem_inc(*cookie, semid)) {
1284 log_error("Could not set notification semaphore "
1285 "identified by cookie value %" PRIu32 " (0x%x)",
1290 dmt->event_nr |= ~DM_UDEV_FLAGS_MASK & *cookie;
1291 dmt->cookie_set = 1;
1293 log_debug("Udev cookie 0x%" PRIx32 " (semid %d) assigned to dm_task "
1294 "type %d with flags 0x%" PRIx16, *cookie, semid, dmt->type, flags);
1303 int dm_udev_complete(uint32_t cookie)
1307 if (!cookie || !dm_udev_get_sync_support())
1310 if (!_get_cookie_sem(cookie, &semid))
1313 if (!_udev_notify_sem_dec(cookie, semid)) {
1314 log_error("Could not signal waiting process using notification "
1315 "semaphore identified by cookie value %" PRIu32 " (0x%x)",
1323 int dm_udev_wait(uint32_t cookie)
1326 struct sembuf sb = {0, 0, 0};
1328 if (!cookie || !dm_udev_get_sync_support())
1331 if (!_get_cookie_sem(cookie, &semid))
1334 if (!_udev_notify_sem_dec(cookie, semid)) {
1335 log_error("Failed to set a proper state for notification "
1336 "semaphore identified by cookie value %" PRIu32 " (0x%x) "
1337 "to initialize waiting for incoming notifications.",
1339 (void) _udev_notify_sem_destroy(cookie, semid);
1343 log_debug("Udev cookie 0x%" PRIx32 " (semid %d): Waiting for zero",
1347 if (semop(semid, &sb, 1) < 0) {
1350 else if (errno == EIDRM)
1353 log_error("Could not set wait state for notification semaphore "
1354 "identified by cookie value %" PRIu32 " (0x%x): %s",
1355 cookie, cookie, strerror(errno));
1356 (void) _udev_notify_sem_destroy(cookie, semid);
1360 return _udev_notify_sem_destroy(cookie, semid);
1363 #endif /* UDEV_SYNC_SUPPORT */