From: Yu Watanabe Date: Sun, 13 Mar 2022 07:17:08 +0000 (+0900) Subject: home: shorten code a bit and add missing assertions X-Git-Tag: v251-rc1~142^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e60c3c72f7bdacfc95e3168902a306eced09f13f;p=thirdparty%2Fsystemd.git home: shorten code a bit and add missing assertions This drops redundant call of fstat(), and reduces indentation. --- diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 54d91e06995..a7e53013ed3 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -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;