summaryrefslogtreecommitdiffstats
path: root/src/portable
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-04-07 16:09:45 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-04-10 15:52:29 +0200
commit41bc484906e973f6c91f8398a3ba309b7ee16f5a (patch)
tree57fad1132c0a90f01e527ac948f898ddea9863a4 /src/portable
parenttest-loop-block: reenable test on CI (diff)
downloadsystemd-41bc484906e973f6c91f8398a3ba309b7ee16f5a.tar.xz
systemd-41bc484906e973f6c91f8398a3ba309b7ee16f5a.zip
tree-wide: take BSD lock on loopback devices we dissect/mount/operate on
So here's something we should always keep in mind: systemd-udevd actually does *two* things with BSD file locks on block devices: 1. While it probes a device it takes a LOCK_SH lock. Thus everyone else taking a LOCK_EX lock will temporarily block udev from probing devices, which is good when making changes to it. 2. Whenever a device is closed after write (detected via inotify), udevd will issue BLKRRPART (requesting the kernel to reread the partition table). It does this while holding a LOCK_EX lock on the block device. Thus anyone else taking LOCK_SH or LOCK_EX will temporarily block udevd from issuing that ioctl. And that's quite relevant, since the kernel will temporarily flush out all partitions while re-reading the partition table and then create them anew. Thus it is smart to take LOCK_SH when dissecting a block device to ensure that no BLKRRPART is issued in the background, until we mounted the devices.
Diffstat (limited to 'src/portable')
-rw-r--r--src/portable/portable.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/portable/portable.c b/src/portable/portable.c
index 0c10b161f0..7bba7b47e4 100644
--- a/src/portable/portable.c
+++ b/src/portable/portable.c
@@ -359,6 +359,10 @@ static int portable_extract_by_path(
/* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it
* there, and extract the metadata we need. The metadata is sent from the child back to us. */
+ r = loop_device_flock(d, LOCK_SH);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to acquire lock on loopback block device: %m");
+
BLOCK_SIGNALS(SIGCHLD);
r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir);