]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Allow acquire shared lock on --copy-from files/devices
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 11 Aug 2023 08:41:04 +0000 (10:41 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 11 Aug 2023 08:42:12 +0000 (10:42 +0200)
We're not going to modify these, so no need for an exclusive lock.

src/partition/repart.c

index 033f441af3c74549a7c3f01ac4dc1c1479433eff..d5a64bfa12b847309e131f242b62709b3cbcc2a1 100644 (file)
@@ -1814,7 +1814,7 @@ static int find_verity_sibling(Context *context, Partition *p, VerityMode mode,
         return 0;
 }
 
-static int context_open_and_lock_backing_fd(const char *node, int *backing_fd) {
+static int context_open_and_lock_backing_fd(const char *node, int operation, int *backing_fd) {
         _cleanup_close_ int fd = -EBADF;
 
         assert(node);
@@ -1828,7 +1828,7 @@ static int context_open_and_lock_backing_fd(const char *node, int *backing_fd) {
                 return log_error_errno(errno, "Failed to open device '%s': %m", node);
 
         /* Tell udev not to interfere while we are processing the device */
-        if (flock(fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
+        if (flock(fd, operation) < 0)
                 return log_error_errno(errno, "Failed to lock device '%s': %m", node);
 
         log_debug("Device %s opened and locked.", node);
@@ -1917,7 +1917,7 @@ static int context_copy_from(Context *context) {
         if (!arg_copy_from)
                 return 0;
 
-        r = context_open_and_lock_backing_fd(arg_copy_from, &fd);
+        r = context_open_and_lock_backing_fd(arg_copy_from, LOCK_SH, &fd);
         if (r < 0)
                 return r;
 
@@ -2223,7 +2223,8 @@ static int context_load_partition_table(Context *context) {
         else {
                 uint32_t ssz;
 
-                r = context_open_and_lock_backing_fd(context->node, &context->backing_fd);
+                r = context_open_and_lock_backing_fd(context->node, arg_dry_run ? LOCK_SH : LOCK_EX,
+                                                     &context->backing_fd);
                 if (r < 0)
                         return r;
 
@@ -2270,7 +2271,9 @@ static int context_load_partition_table(Context *context) {
 
         if (context->backing_fd < 0) {
                 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
-                r = context_open_and_lock_backing_fd(FORMAT_PROC_FD_PATH(fdisk_get_devfd(c)), &context->backing_fd);
+                r = context_open_and_lock_backing_fd(FORMAT_PROC_FD_PATH(fdisk_get_devfd(c)),
+                                                     arg_dry_run ? LOCK_SH : LOCK_EX,
+                                                     &context->backing_fd);
                 if (r < 0)
                         return r;
         }