Skip to content

Commit a709a31

Browse files
poetteringyuwata
authored andcommitted
dissect: split out DM deferred remove into src/shared/dm-util.c
The function is generally useful, let's split it out so that we can make use of it later on in systemd-homed.
1 parent a9a50bd commit a709a31

File tree

4 files changed

+49
-35
lines changed

4 files changed

+49
-35
lines changed

src/shared/dissect-image.c

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "device-nodes.h"
2020
#include "device-util.h"
2121
#include "dissect-image.h"
22+
#include "dm-util.h"
2223
#include "env-file.h"
2324
#include "fd-util.h"
2425
#include "fileio.h"
@@ -1225,40 +1226,6 @@ int dissected_image_decrypt_interactively(
12251226
}
12261227
}
12271228

1228-
#if HAVE_LIBCRYPTSETUP
1229-
static int deferred_remove(DecryptedPartition *p) {
1230-
struct dm_ioctl dm = {
1231-
.version = {
1232-
DM_VERSION_MAJOR,
1233-
DM_VERSION_MINOR,
1234-
DM_VERSION_PATCHLEVEL
1235-
},
1236-
.data_size = sizeof(dm),
1237-
.flags = DM_DEFERRED_REMOVE,
1238-
};
1239-
1240-
_cleanup_close_ int fd = -1;
1241-
1242-
assert(p);
1243-
1244-
/* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1245-
1246-
fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
1247-
if (fd < 0)
1248-
return -errno;
1249-
1250-
if (strlen(p->name) > sizeof(dm.name))
1251-
return -ENAMETOOLONG;
1252-
1253-
strncpy(dm.name, p->name, sizeof(dm.name));
1254-
1255-
if (ioctl(fd, DM_DEV_REMOVE, &dm))
1256-
return -errno;
1257-
1258-
return 0;
1259-
}
1260-
#endif
1261-
12621229
int decrypted_image_relinquish(DecryptedImage *d) {
12631230

12641231
#if HAVE_LIBCRYPTSETUP
@@ -1278,7 +1245,7 @@ int decrypted_image_relinquish(DecryptedImage *d) {
12781245
if (p->relinquished)
12791246
continue;
12801247

1281-
r = deferred_remove(p);
1248+
r = dm_deferred_remove(p->name);
12821249
if (r < 0)
12831250
return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name);
12841251

src/shared/dm-util.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <fcntl.h>
2+
#include <linux/dm-ioctl.h>
3+
#include <string.h>
4+
#include <sys/ioctl.h>
5+
6+
#include "dm-util.h"
7+
#include "fd-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)-1)
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(dm.name, name, sizeof(dm.name));
36+
37+
if (ioctl(fd, DM_DEV_REMOVE, &dm))
38+
return -errno;
39+
40+
return 0;
41+
}

src/shared/dm-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
int dm_deferred_remove(const char *name);

src/shared/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ shared_sources = files('''
5050
dev-setup.h
5151
dissect-image.c
5252
dissect-image.h
53+
dm-util.c
54+
dm-util.h
5355
dns-domain.c
5456
dns-domain.h
5557
dropin.c

0 commit comments

Comments
 (0)