diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-03-13 16:17:08 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-03-15 12:38:08 +0900 |
commit | e60c3c72f7bdacfc95e3168902a306eced09f13f (patch) | |
tree | 8811d59bc925df3b593b6a29cb29962bab5b1776 /src/home | |
parent | 629c1cdf03fdb032ca70a57786bd478c2372ccb4 (diff) | |
download | systemd-e60c3c72f7bdacfc95e3168902a306eced09f13f.tar.gz |
home: shorten code a bit and add missing assertions
This drops redundant call of fstat(), and reduces indentation.
Diffstat (limited to 'src/home')
-rw-r--r-- | src/home/homework-luks.c | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 54d91e0699..a7e53013ed 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; |