]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dm-util.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / shared / dm-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/dm-ioctl.h>
5 #include <sys/ioctl.h>
6
7 #include "dm-util.h"
8 #include "fd-util.h"
9 #include "string-util.h"
10
11 int dm_deferred_remove_cancel(const char *name) {
12 _cleanup_close_ int fd = -EBADF;
13 struct message {
14 struct dm_ioctl dm_ioctl;
15 struct dm_target_msg dm_target_msg;
16 char msg_text[STRLEN("@cancel_deferred_remove") + 1];
17 } _packed_ message = {
18 .dm_ioctl = {
19 .version = {
20 DM_VERSION_MAJOR,
21 DM_VERSION_MINOR,
22 DM_VERSION_PATCHLEVEL
23 },
24 .data_size = sizeof(struct message),
25 .data_start = sizeof(struct dm_ioctl),
26 },
27 .msg_text = "@cancel_deferred_remove",
28 };
29
30 assert(name);
31
32 if (strlen(name) >= sizeof(message.dm_ioctl.name))
33 return -ENODEV; /* A device with a name longer than this cannot possibly exist */
34
35 strncpy_exact(message.dm_ioctl.name, name, sizeof(message.dm_ioctl.name));
36
37 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
38 if (fd < 0)
39 return -errno;
40
41 if (ioctl(fd, DM_TARGET_MSG, &message))
42 return -errno;
43
44 return 0;
45 }