Skip to content

Commit b25a930

Browse files
committed
basic,shared: move a bunch of files to src/shared/
The goal is to move everything that requires selinux or smack away from src/basic/. This means that src/basic/label.[ch] must move, which implies btrfs-util.[ch], copy.[ch], and a bunch of other files which form a cluster of internal use. This is just moving text around, so there should be no functional difference. test-blockdev-util is new, because path_is_encrypted() is moved to blockdev-util.c, and so far we didn't have any tests for code there.
1 parent 2d32453 commit b25a930

28 files changed

+152
-140
lines changed

src/basic/fs-util.c

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <unistd.h>
99

1010
#include "alloc-util.h"
11-
#include "blockdev-util.h"
1211
#include "dirent-util.h"
1312
#include "fd-util.h"
1413
#include "fileio.h"
@@ -1504,91 +1503,6 @@ int open_parent(const char *path, int flags, mode_t mode) {
15041503
return fd;
15051504
}
15061505

1507-
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
1508-
_cleanup_free_ char *p = NULL, *uuids = NULL;
1509-
_cleanup_closedir_ DIR *d = NULL;
1510-
int r, found_encrypted = false;
1511-
1512-
assert(sysfs_path);
1513-
1514-
if (depth_left == 0)
1515-
return -EINVAL;
1516-
1517-
p = path_join(sysfs_path, "dm/uuid");
1518-
if (!p)
1519-
return -ENOMEM;
1520-
1521-
r = read_one_line_file(p, &uuids);
1522-
if (r != -ENOENT) {
1523-
if (r < 0)
1524-
return r;
1525-
1526-
/* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
1527-
if (startswith(uuids, "CRYPT-"))
1528-
return true;
1529-
}
1530-
1531-
/* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
1532-
* subdir. */
1533-
1534-
p = mfree(p);
1535-
p = path_join(sysfs_path, "slaves");
1536-
if (!p)
1537-
return -ENOMEM;
1538-
1539-
d = opendir(p);
1540-
if (!d) {
1541-
if (errno == ENOENT) /* Doesn't have underlying devices */
1542-
return false;
1543-
1544-
return -errno;
1545-
}
1546-
1547-
for (;;) {
1548-
_cleanup_free_ char *q = NULL;
1549-
struct dirent *de;
1550-
1551-
errno = 0;
1552-
de = readdir_no_dot(d);
1553-
if (!de) {
1554-
if (errno != 0)
1555-
return -errno;
1556-
1557-
break; /* No more underlying devices */
1558-
}
1559-
1560-
q = path_join(p, de->d_name);
1561-
if (!q)
1562-
return -ENOMEM;
1563-
1564-
r = blockdev_is_encrypted(q, depth_left - 1);
1565-
if (r < 0)
1566-
return r;
1567-
if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
1568-
return false;
1569-
1570-
found_encrypted = true;
1571-
}
1572-
1573-
return found_encrypted;
1574-
}
1575-
1576-
int path_is_encrypted(const char *path) {
1577-
char p[SYS_BLOCK_PATH_MAX(NULL)];
1578-
dev_t devt;
1579-
int r;
1580-
1581-
r = get_block_device(path, &devt);
1582-
if (r < 0)
1583-
return r;
1584-
if (r == 0) /* doesn't have a block device */
1585-
return false;
1586-
1587-
xsprintf_sys_block_path(p, NULL, devt);
1588-
1589-
return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
1590-
}
1591-
15921506
int conservative_renameat(
15931507
int olddirfd, const char *oldpath,
15941508
int newdirfd, const char *newpath) {

src/basic/fs-util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ int syncfs_path(int atfd, const char *path);
145145

146146
int open_parent(const char *path, int flags, mode_t mode);
147147

148-
int path_is_encrypted(const char *path);
149-
150148
int conservative_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
151149
static inline int conservative_rename(const char *oldpath, const char *newpath) {
152150
return conservative_renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath);

src/basic/meson.build

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ basic_sources = files('''
1515
async.h
1616
audit-util.c
1717
audit-util.h
18-
blockdev-util.c
19-
blockdev-util.h
20-
btrfs-util.c
21-
btrfs-util.h
2218
build.c
2319
build.h
2420
bus-label.c
@@ -33,12 +29,8 @@ basic_sources = files('''
3329
chattr-util.h
3430
conf-files.c
3531
conf-files.h
36-
copy.c
37-
copy.h
3832
creds-util.c
3933
creds-util.h
40-
data-fd-util.c
41-
data-fd-util.h
4234
def.h
4335
dirent-util.c
4436
dirent-util.h
@@ -85,8 +77,6 @@ basic_sources = files('''
8577
ioprio.h
8678
khash.c
8779
khash.h
88-
label.c
89-
label.h
9080
limits-util.c
9181
limits-util.h
9282
linux/btrfs.h
@@ -157,7 +147,6 @@ basic_sources = files('''
157147
missing_syscall.h
158148
missing_timerfd.h
159149
missing_type.h
160-
mkdir-label.c
161150
mkdir.c
162151
mkdir.h
163152
mountpoint-util.c
@@ -200,20 +189,13 @@ basic_sources = files('''
200189
replace-var.h
201190
rlimit-util.c
202191
rlimit-util.h
203-
rm-rf.c
204-
rm-rf.h
205-
selinux-util.c
206-
selinux-util.h
207192
set.h
208193
sigbus.c
209194
sigbus.h
210195
signal-util.c
211196
signal-util.h
212197
siphash24.c
213198
siphash24.h
214-
smack-util.c
215-
smack-util.h
216-
socket-label.c
217199
socket-util.c
218200
socket-util.h
219201
sort-util.c

src/home/homework.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stddef.h>
44
#include <sys/mount.h>
55

6+
#include "blockdev-util.h"
67
#include "chown-recursive.h"
78
#include "copy.h"
89
#include "fd-util.h"

src/libsystemd/sd-bus/bus-socket.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "path-util.h"
2323
#include "process-util.h"
2424
#include "rlimit-util.h"
25-
#include "selinux-util.h"
2625
#include "signal-util.h"
2726
#include "stdio-util.h"
2827
#include "string-util.h"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,88 @@ int blockdev_partscan_enabled(int fd) {
256256

257257
return !FLAGS_SET(ull, GENHD_FL_NO_PART_SCAN);
258258
}
259+
260+
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
261+
_cleanup_free_ char *p = NULL, *uuids = NULL;
262+
_cleanup_closedir_ DIR *d = NULL;
263+
int r, found_encrypted = false;
264+
265+
assert(sysfs_path);
266+
267+
if (depth_left == 0)
268+
return -EINVAL;
269+
270+
p = path_join(sysfs_path, "dm/uuid");
271+
if (!p)
272+
return -ENOMEM;
273+
274+
r = read_one_line_file(p, &uuids);
275+
if (r != -ENOENT) {
276+
if (r < 0)
277+
return r;
278+
279+
/* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
280+
if (startswith(uuids, "CRYPT-"))
281+
return true;
282+
}
283+
284+
/* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
285+
* subdir. */
286+
287+
p = mfree(p);
288+
p = path_join(sysfs_path, "slaves");
289+
if (!p)
290+
return -ENOMEM;
291+
292+
d = opendir(p);
293+
if (!d) {
294+
if (errno == ENOENT) /* Doesn't have underlying devices */
295+
return false;
296+
297+
return -errno;
298+
}
299+
300+
for (;;) {
301+
_cleanup_free_ char *q = NULL;
302+
struct dirent *de;
303+
304+
errno = 0;
305+
de = readdir_no_dot(d);
306+
if (!de) {
307+
if (errno != 0)
308+
return -errno;
309+
310+
break; /* No more underlying devices */
311+
}
312+
313+
q = path_join(p, de->d_name);
314+
if (!q)
315+
return -ENOMEM;
316+
317+
r = blockdev_is_encrypted(q, depth_left - 1);
318+
if (r < 0)
319+
return r;
320+
if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
321+
return false;
322+
323+
found_encrypted = true;
324+
}
325+
326+
return found_encrypted;
327+
}
328+
329+
int path_is_encrypted(const char *path) {
330+
char p[SYS_BLOCK_PATH_MAX(NULL)];
331+
dev_t devt;
332+
int r;
333+
334+
r = get_block_device(path, &devt);
335+
if (r < 0)
336+
return r;
337+
if (r == 0) /* doesn't have a block device */
338+
return false;
339+
340+
xsprintf_sys_block_path(p, NULL, devt);
341+
342+
return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
343+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ int get_block_device_harder(const char *path, dev_t *dev);
2222
int lock_whole_block_device(dev_t devt, int operation);
2323

2424
int blockdev_partscan_enabled(int fd);
25+
26+
int path_is_encrypted(const char *path);

src/shared/condition.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "apparmor-util.h"
1818
#include "architecture.h"
1919
#include "audit-util.h"
20+
#include "blockdev-util.h"
2021
#include "cap-list.h"
2122
#include "cgroup-util.h"
2223
#include "condition.h"

0 commit comments

Comments
 (0)