]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
home: shorten code a bit and add missing assertions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 13 Mar 2022 07:17:08 +0000 (16:17 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 15 Mar 2022 03:38:08 +0000 (12:38 +0900)
This drops redundant call of fstat(), and reduces indentation.

src/home/homework-luks.c

index 54d91e069952c1529406d98aabd327cd6404c021..a7e53013ed301c7575e6f0d68fcae7e4b9c27392 100644 (file)
@@ -1151,44 +1151,33 @@ static int lock_image_fd(int image_fd, const char *ip) {
          * image file, and send it to our parent. homed will keep it open to ensure no other instance of
          * homed (across the network or such) will also mount the file. */
 
+        assert(image_fd >= 0);
+        assert(ip);
+
         r = getenv_bool("SYSTEMD_LUKS_LOCK");
         if (r == -ENXIO)
                 return 0;
         if (r < 0)
                 return log_error_errno(r, "Failed to parse $SYSTEMD_LUKS_LOCK environment variable: %m");
-        if (r > 0) {
-                struct stat st;
-
-                if (fstat(image_fd, &st) < 0)
-                        return log_error_errno(errno, "Failed to stat image file: %m");
-                if (S_ISBLK(st.st_mode)) {
-                        /* Locking block devices doesn't really make sense, as this might interfere with
-                         * udev's workings, and these locks aren't network propagated anyway, hence not what
-                         * we are after here. */
-                        log_debug("Not locking image file '%s', since it's a block device.", ip);
-                        return 0;
-                }
-                r = stat_verify_regular(&st);
-                if (r < 0)
-                        return log_error_errno(r, "Image file to lock is not a regular file: %m");
+        if (r == 0)
+                return 0;
 
-                if (flock(image_fd, LOCK_EX|LOCK_NB) < 0) {
+        if (flock(image_fd, LOCK_EX|LOCK_NB) < 0) {
 
-                        if (errno == EWOULDBLOCK)
-                                log_error_errno(errno, "Image file '%s' already locked, can't use.", ip);
-                        else
-                                log_error_errno(errno, "Failed to lock image file '%s': %m", ip);
+                if (errno == EWOULDBLOCK)
+                        log_error_errno(errno, "Image file '%s' already locked, can't use.", ip);
+                else
+                        log_error_errno(errno, "Failed to lock image file '%s': %m", ip);
 
-                        return errno != EWOULDBLOCK ? -errno : -EADDRINUSE; /* Make error recognizable */
-                }
+                return errno != EWOULDBLOCK ? -errno : -EADDRINUSE; /* Make error recognizable */
+        }
 
-                log_info("Successfully locked image file '%s'.", ip);
+        log_info("Successfully locked image file '%s'.", ip);
 
-                /* Now send it to our parent to keep safe while the home dir is active */
-                r = sd_pid_notify_with_fds(0, false, "SYSTEMD_LUKS_LOCK_FD=1", &image_fd, 1);
-                if (r < 0)
-                        log_warning_errno(r, "Failed to send LUKS lock fd to parent, ignoring: %m");
-        }
+        /* Now send it to our parent to keep safe while the home dir is active */
+        r = sd_pid_notify_with_fds(0, false, "SYSTEMD_LUKS_LOCK_FD=1", &image_fd, 1);
+        if (r < 0)
+                log_warning_errno(r, "Failed to send LUKS lock fd to parent, ignoring: %m");
 
         return 0;
 }
@@ -1203,6 +1192,8 @@ static int open_image_file(
         const char *ip;
         int r;
 
+        assert(h || force_image_path);
+
         ip = force_image_path ?: user_record_image_path(h);
 
         image_fd = open(ip, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
@@ -1216,9 +1207,14 @@ static int open_image_file(
                                 S_ISDIR(st.st_mode) ? SYNTHETIC_ERRNO(EISDIR) : SYNTHETIC_ERRNO(EBADFD),
                                 "Image file %s is not a regular file or block device: %m", ip);
 
-        r = lock_image_fd(image_fd, ip);
-        if (r < 0)
-                return r;
+        /* Locking block devices doesn't really make sense, as this might interfere with
+         * udev's workings, and these locks aren't network propagated anyway, hence not what
+         * we are after here. */
+        if (S_ISREG(st.st_mode)) {
+                r = lock_image_fd(image_fd, ip);
+                if (r < 0)
+                        return r;
+        }
 
         if (ret_stat)
                 *ret_stat = st;