home: shorten code a bit and add missing assertions

This drops redundant call of fstat(), and reduces indentation.
This commit is contained in:
Yu Watanabe
2022-03-15 12:38:08 +09:00
parent 629c1cdf03
commit e60c3c72f7
+28 -32
View File
@@ -1151,45 +1151,34 @@ 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 (r == 0)
return 0;
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 (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 */
}
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");
return errno != EWOULDBLOCK ? -errno : -EADDRINUSE; /* Make error recognizable */
}
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");
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;