* 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;
}
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);
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;