]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-reread-partition-table: Don't keep open fds around
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 24 Nov 2025 10:53:12 +0000 (11:53 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 24 Nov 2025 12:09:41 +0000 (13:09 +0100)
Avoids EBUSY from BLKRRPART when built without libblkid support.

src/test/test-reread-partition-table.c

index f6234f0f8a4be754eaf3ea5d22bc7f90781c3af3..f171d1737a86464413d93707aec570adb6b49f6d 100644 (file)
@@ -97,11 +97,12 @@ TEST(rereadpt) {
 
         ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
 
-        _cleanup_close_ int pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-        ASSERT_OK_ERRNO(pfd);
+        _cleanup_close_ int pfd = -EBADF;
+        ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
         uint64_t size;
         ASSERT_OK(blockdev_get_device_size(pfd, &size));
         ASSERT_EQ(size, 20U*1024U*1024U);
+        pfd = safe_close(pfd);
 
         /* No change */
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
@@ -122,11 +123,14 @@ TEST(rereadpt) {
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
         ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
 
+        ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
         ASSERT_OK(blockdev_get_device_size(pfd, &size));
         ASSERT_EQ(size, 30U*1024U*1024U);
+        pfd = safe_close(pfd);
 
         /* No change */
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
+        ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
 
         /* Move */
         log_notice("MOVING BY 50M");
@@ -137,19 +141,19 @@ TEST(rereadpt) {
 
         ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
         ASSERT_ERROR(reread_partition_table_fd(loop->fd, /* flags= */ 0), EBUSY);
-        ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
-
-        safe_close(pfd);
+        pfd = safe_close(pfd);
 
+        ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
 
-        pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-        ASSERT_OK_ERRNO(pfd);
+        pfd = ASSERT_OK_ERRNO(open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
         ASSERT_OK(blockdev_get_device_size(pfd, &size));
         ASSERT_EQ(size, 15U*1024U*1024U);
+        pfd = safe_close(pfd);
 
         /* No change */
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
+        pfd = ASSERT_OK_ERRNO(open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
 
         /* Remove */
         log_notice("REMOVING");
@@ -159,9 +163,9 @@ TEST(rereadpt) {
 
         ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
         ASSERT_ERROR(reread_partition_table_fd(loop->fd, /* flags= */ 0), EBUSY);
+        pfd = safe_close(pfd);
 
         ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
-        pfd = safe_close(pfd);
         ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
         ASSERT_ERROR_ERRNO(access(p, F_OK), ENOENT);
 }