]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dm-util.c
Merge pull request #14592 from keszybz/simplifications
[thirdparty/systemd.git] / src / shared / dm-util.c
1 #include <fcntl.h>
2 #include <linux/dm-ioctl.h>
3 #include <sys/ioctl.h>
4
5 #include "dm-util.h"
6 #include "fd-util.h"
7 #include "string-util.h"
8
9 int dm_deferred_remove(const char *name) {
10
11 struct dm_ioctl dm = {
12 .version = {
13 DM_VERSION_MAJOR,
14 DM_VERSION_MINOR,
15 DM_VERSION_PATCHLEVEL
16 },
17 .data_size = sizeof(dm),
18 .flags = DM_DEFERRED_REMOVE,
19 };
20
21 _cleanup_close_ int fd = -1;
22
23 assert(name);
24
25 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl()
26 * directly. */
27
28 if (strlen(name) >= sizeof(dm.name))
29 return -ENODEV; /* A device with a name longer than this cannot possibly exist */
30
31 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
32 if (fd < 0)
33 return -errno;
34
35 strncpy_exact(dm.name, name, sizeof(dm.name));
36
37 if (ioctl(fd, DM_DEV_REMOVE, &dm))
38 return -errno;
39
40 return 0;
41 }