]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homework: move destruction of temporary image file into HomeSetup
authorLennart Poettering <lennart@poettering.net>
Tue, 26 Oct 2021 15:36:36 +0000 (17:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Oct 2021 06:17:46 +0000 (08:17 +0200)
Let's simplify things further a bit and move the destruction of the
temporary image file we operate on when creating a LUKS home into
HomeSetup, like all our other resources.

src/home/homework-luks.c
src/home/homework.c
src/home/homework.h

index 1ff4d504177cda12a148ec46380527b1384dbe01..8333f896b170ccb46974e39d790608be9b91b297 100644 (file)
@@ -1984,13 +1984,12 @@ int home_create_luks(
                 char **effective_passwords,
                 UserRecord **ret_home) {
 
-        _cleanup_free_ char *subdir = NULL, *disk_uuid_path = NULL, *temporary_image_path = NULL;
+        _cleanup_free_ char *subdir = NULL, *disk_uuid_path = NULL;
         uint64_t encrypted_size,
                 host_size = 0, partition_offset = 0, partition_size = 0; /* Unnecessary initialization to appease gcc */
         _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
         sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid;
         _cleanup_close_ int image_fd = -1;
-        bool image_created = false;
         const char *fstype, *ip;
         struct statfs sfs;
         int r;
@@ -1998,6 +1997,7 @@ int home_create_luks(
         assert(h);
         assert(h->storage < 0 || h->storage == USER_LUKS);
         assert(setup);
+        assert(!setup->temporary_image_path);
         assert(ret_home);
 
         r = dlopen_cryptsetup();
@@ -2123,7 +2123,7 @@ int home_create_luks(
                                 log_info("Full device discard completed.");
                 }
         } else {
-                _cleanup_free_ char *parent = NULL;
+                _cleanup_free_ char *parent = NULL, *t = NULL;
 
                 parent = dirname_malloc(ip);
                 if (!parent)
@@ -2140,22 +2140,22 @@ int home_create_luks(
                 if (!supported_fs_size(fstype, host_size))
                         return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Selected file system size too small for %s.", fstype);
 
-                r = tempfn_random(ip, "homework", &temporary_image_path);
+                r = tempfn_random(ip, "homework", &t);
                 if (r < 0)
                         return log_error_errno(r, "Failed to derive temporary file name for %s: %m", ip);
 
-                image_fd = open(temporary_image_path, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
+                image_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
                 if (image_fd < 0)
-                        return log_error_errno(errno, "Failed to create home image %s: %m", temporary_image_path);
+                        return log_error_errno(errno, "Failed to create home image %s: %m", t);
 
-                image_created = true;
+                setup->temporary_image_path = TAKE_PTR(t);
 
                 r = chattr_fd(image_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
                 if (r < 0)
                         log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r,
-                                       "Failed to set file attributes on %s, ignoring: %m", temporary_image_path);
+                                       "Failed to set file attributes on %s, ignoring: %m", setup->temporary_image_path);
 
-                r = home_truncate(h, image_fd, temporary_image_path, host_size);
+                r = home_truncate(h, image_fd, setup->temporary_image_path, host_size);
                 if (r < 0)
                         goto fail;
 
@@ -2184,7 +2184,7 @@ int home_create_luks(
                         goto fail;
                 }
 
-                log_error_errno(r, "Failed to set up loopback device for %s: %m", temporary_image_path);
+                log_error_errno(r, "Failed to set up loopback device for %s: %m", setup->temporary_image_path);
                 goto fail;
         }
 
@@ -2327,12 +2327,13 @@ int home_create_luks(
          * lock that ensures udev doesn't interfere with what we are doing */
         image_fd = safe_close(image_fd);
 
-        if (temporary_image_path) {
-                if (rename(temporary_image_path, ip) < 0) {
+        if (setup->temporary_image_path) {
+                if (rename(setup->temporary_image_path, ip) < 0) {
                         log_error_errno(errno, "Failed to rename image file: %m");
                         goto fail;
                 }
 
+                setup->temporary_image_path = mfree(setup->temporary_image_path);
                 log_info("Moved image file into place.");
         }
 
@@ -2354,8 +2355,8 @@ fail:
 
         setup->loop = loop_device_unref(setup->loop);
 
-        if (image_created)
-                (void) unlink(temporary_image_path);
+        if (setup->temporary_image_path)
+                (void) unlink(setup->temporary_image_path);
 
         return r;
 }
index 22a960efd5fe3aa3fc61de589f787fadf3992413..7a2d816e59b48ceff6b7629d887f4df8a81b1102 100644 (file)
@@ -382,6 +382,14 @@ int home_setup_done(HomeSetup *setup) {
                 setup->image_fd = safe_close(setup->image_fd);
         }
 
+        if (setup->temporary_image_path) {
+                if (unlink(setup->temporary_image_path) < 0)
+                        log_debug_errno(errno, "Failed to remove temporary image file '%s', ignoring: %m",
+                                        setup->temporary_image_path);
+
+                setup->temporary_image_path = mfree(setup->temporary_image_path);
+        }
+
         setup->undo_mount = false;
         setup->undo_dm = false;
         setup->do_offline_fitrim = false;
index 076033526c7bb8d0f3dbdf5e2aed1f6f9268357e..d7ad6fbdfaa88c81d0f723c24d45c371c8d12ed6 100644 (file)
@@ -39,6 +39,8 @@ typedef struct HomeSetup {
         uint64_t partition_size;
 
         char *mount_suffix;           /* The directory to use as home dir is this path below /run/systemd/user-home-mount */
+
+        char *temporary_image_path;
 } HomeSetup;
 
 typedef struct PasswordCache {