summaryrefslogtreecommitdiff
path: root/src/shared/dm-util.c
blob: f73bf93520a3001d78b1370fcd59ccbb721920b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <fcntl.h>
#include <linux/dm-ioctl.h>
#include <string.h>
#include <sys/ioctl.h>

#include "dm-util.h"
#include "fd-util.h"
#include "string-util.h"

int dm_deferred_remove(const char *name) {

        struct dm_ioctl dm = {
                .version = {
                        DM_VERSION_MAJOR,
                        DM_VERSION_MINOR,
                        DM_VERSION_PATCHLEVEL
                },
                .data_size = sizeof(dm),
                .flags = DM_DEFERRED_REMOVE,
        };

        _cleanup_close_ int fd = -1;

        assert(name);

        /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl()
         * directly. */

        if (strlen(name) >= sizeof(dm.name))
                return -ENODEV; /* A device with a name longer than this cannot possibly exist */

        fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
        if (fd < 0)
                return -errno;

        strncpy_exact(dm.name, name, sizeof(dm.name));

        if (ioctl(fd, DM_DEV_REMOVE, &dm))
                return -errno;

        return 0;
}