1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/magic.h>
6 #include <openssl/pem.h>
12 #include "btrfs-util.h"
13 #include "bus-common-errors.h"
14 #include "bus-error.h"
15 #include "bus-log-control-api.h"
16 #include "bus-polkit.h"
17 #include "clean-ipc.h"
18 #include "conf-files.h"
19 #include "device-util.h"
20 #include "dirent-util.h"
23 #include "format-util.h"
26 #include "home-util.h"
27 #include "homed-conf.h"
28 #include "homed-home-bus.h"
29 #include "homed-home.h"
30 #include "homed-manager-bus.h"
31 #include "homed-manager.h"
32 #include "homed-varlink.h"
35 #include "process-util.h"
36 #include "quota-util.h"
37 #include "random-util.h"
38 #include "socket-util.h"
39 #include "stat-util.h"
41 #include "tmpfile-util.h"
42 #include "udev-util.h"
43 #include "user-record-sign.h"
44 #include "user-record-util.h"
45 #include "user-record.h"
46 #include "user-util.h"
48 /* Where to look for private/public keys that are used to sign the user records. We are not using
49 * CONF_PATHS_NULSTR() here since we want to insert /var/lib/systemd/home/ in the middle. And we insert that
50 * since we want to auto-generate a persistent private/public key pair if we need to. */
51 #define KEY_PATHS_NULSTR \
52 "/etc/systemd/home/\0" \
53 "/run/systemd/home/\0" \
54 "/var/lib/systemd/home/\0" \
55 "/usr/local/lib/systemd/home/\0" \
56 "/usr/lib/systemd/home/\0"
58 static bool uid_is_home(uid_t uid
) {
59 return uid
>= HOME_UID_MIN
&& uid
<= HOME_UID_MAX
;
61 /* Takes a value generated randomly or by hashing and turns it into a UID in the right range */
63 #define UID_CLAMP_INTO_HOME_RANGE(rnd) (((uid_t) (rnd) % (HOME_UID_MAX - HOME_UID_MIN + 1)) + HOME_UID_MIN)
65 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(homes_by_uid_hash_ops
, void, trivial_hash_func
, trivial_compare_func
, Home
, home_free
);
66 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(homes_by_name_hash_ops
, char, string_hash_func
, string_compare_func
, Home
, home_free
);
67 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(homes_by_worker_pid_hash_ops
, void, trivial_hash_func
, trivial_compare_func
, Home
, home_free
);
68 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(homes_by_sysfs_hash_ops
, char, path_hash_func
, path_compare
, Home
, home_free
);
70 static int on_home_inotify(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
);
71 static int manager_gc_images(Manager
*m
);
72 static int manager_enumerate_images(Manager
*m
);
73 static int manager_assess_image(Manager
*m
, int dir_fd
, const char *dir_path
, const char *dentry_name
);
74 static void manager_revalidate_image(Manager
*m
, Home
*h
);
76 static void manager_watch_home(Manager
*m
) {
82 m
->inotify_event_source
= sd_event_source_unref(m
->inotify_event_source
);
83 m
->scan_slash_home
= false;
85 if (statfs("/home/", &sfs
) < 0) {
86 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
87 "Failed to statfs() /home/ directory, disabling automatic scanning.");
91 if (is_network_fs(&sfs
)) {
92 log_info("/home/ is a network file system, disabling automatic scanning.");
96 if (is_fs_type(&sfs
, AUTOFS_SUPER_MAGIC
)) {
97 log_info("/home/ is on autofs, disabling automatic scanning.");
101 m
->scan_slash_home
= true;
103 r
= sd_event_add_inotify(m
->event
, &m
->inotify_event_source
, "/home/", IN_CREATE
|IN_CLOSE_WRITE
|IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ONLYDIR
|IN_MOVED_TO
|IN_MOVED_FROM
|IN_DELETE
, on_home_inotify
, m
);
105 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
106 "Failed to create inotify watch on /home/, ignoring.");
108 (void) sd_event_source_set_description(m
->inotify_event_source
, "home-inotify");
111 static int on_home_inotify(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
112 Manager
*m
= userdata
;
118 if ((event
->mask
& (IN_Q_OVERFLOW
|IN_MOVE_SELF
|IN_DELETE_SELF
|IN_IGNORED
|IN_UNMOUNT
)) != 0) {
120 if (FLAGS_SET(event
->mask
, IN_Q_OVERFLOW
))
121 log_debug("/home/ inotify queue overflow, rescanning.");
122 else if (FLAGS_SET(event
->mask
, IN_MOVE_SELF
))
123 log_info("/home/ moved or renamed, recreating watch and rescanning.");
124 else if (FLAGS_SET(event
->mask
, IN_DELETE_SELF
))
125 log_info("/home/ deleted, recreating watch and rescanning.");
126 else if (FLAGS_SET(event
->mask
, IN_UNMOUNT
))
127 log_info("/home/ unmounted, recreating watch and rescanning.");
128 else if (FLAGS_SET(event
->mask
, IN_IGNORED
))
129 log_info("/home/ watch invalidated, recreating watch and rescanning.");
131 manager_watch_home(m
);
132 (void) manager_gc_images(m
);
133 (void) manager_enumerate_images(m
);
134 (void) bus_manager_emit_auto_login_changed(m
);
138 /* For the other inotify events, let's ignore all events for file names that don't match our
140 if (isempty(event
->name
))
142 e
= endswith(event
->name
, FLAGS_SET(event
->mask
, IN_ISDIR
) ? ".homedir" : ".home");
146 n
= strndupa(event
->name
, e
- event
->name
);
147 if (!suitable_user_name(n
))
150 if ((event
->mask
& (IN_CREATE
|IN_CLOSE_WRITE
|IN_MOVED_TO
)) != 0) {
151 if (FLAGS_SET(event
->mask
, IN_CREATE
))
152 log_debug("/home/%s has been created, having a look.", event
->name
);
153 else if (FLAGS_SET(event
->mask
, IN_CLOSE_WRITE
))
154 log_debug("/home/%s has been modified, having a look.", event
->name
);
155 else if (FLAGS_SET(event
->mask
, IN_MOVED_TO
))
156 log_debug("/home/%s has been moved in, having a look.", event
->name
);
158 (void) manager_assess_image(m
, -1, "/home/", event
->name
);
159 (void) bus_manager_emit_auto_login_changed(m
);
162 if ((event
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_DELETE
)) != 0) {
165 if (FLAGS_SET(event
->mask
, IN_DELETE
))
166 log_debug("/home/%s has been deleted, revalidating.", event
->name
);
167 else if (FLAGS_SET(event
->mask
, IN_CLOSE_WRITE
))
168 log_debug("/home/%s has been closed after writing, revalidating.", event
->name
);
169 else if (FLAGS_SET(event
->mask
, IN_MOVED_FROM
))
170 log_debug("/home/%s has been moved away, revalidating.", event
->name
);
172 h
= hashmap_get(m
->homes_by_name
, n
);
174 manager_revalidate_image(m
, h
);
175 (void) bus_manager_emit_auto_login_changed(m
);
182 int manager_new(Manager
**ret
) {
183 _cleanup_(manager_freep
) Manager
*m
= NULL
;
193 .default_storage
= _USER_STORAGE_INVALID
,
196 r
= manager_parse_config_file(m
);
200 r
= sd_event_default(&m
->event
);
204 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
208 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
212 (void) sd_event_set_watchdog(m
->event
, true);
214 m
->homes_by_uid
= hashmap_new(&homes_by_uid_hash_ops
);
215 if (!m
->homes_by_uid
)
218 m
->homes_by_name
= hashmap_new(&homes_by_name_hash_ops
);
219 if (!m
->homes_by_name
)
222 m
->homes_by_worker_pid
= hashmap_new(&homes_by_worker_pid_hash_ops
);
223 if (!m
->homes_by_worker_pid
)
226 m
->homes_by_sysfs
= hashmap_new(&homes_by_sysfs_hash_ops
);
227 if (!m
->homes_by_sysfs
)
234 Manager
* manager_free(Manager
*m
) {
237 hashmap_free(m
->homes_by_uid
);
238 hashmap_free(m
->homes_by_name
);
239 hashmap_free(m
->homes_by_worker_pid
);
240 hashmap_free(m
->homes_by_sysfs
);
242 m
->inotify_event_source
= sd_event_source_unref(m
->inotify_event_source
);
244 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
246 sd_bus_flush_close_unref(m
->bus
);
247 sd_event_unref(m
->event
);
249 m
->notify_socket_event_source
= sd_event_source_unref(m
->notify_socket_event_source
);
250 m
->device_monitor
= sd_device_monitor_unref(m
->device_monitor
);
252 m
->deferred_rescan_event_source
= sd_event_source_unref(m
->deferred_rescan_event_source
);
253 m
->deferred_gc_event_source
= sd_event_source_unref(m
->deferred_gc_event_source
);
254 m
->deferred_auto_login_event_source
= sd_event_source_unref(m
->deferred_auto_login_event_source
);
257 EVP_PKEY_free(m
->private_key
);
259 hashmap_free(m
->public_keys
);
261 varlink_server_unref(m
->varlink_server
);
263 free(m
->default_file_system_type
);
268 int manager_verify_user_record(Manager
*m
, UserRecord
*hr
) {
276 if (!m
->private_key
&& hashmap_isempty(m
->public_keys
)) {
277 r
= user_record_has_signature(hr
);
281 return r
? -ENOKEY
: USER_RECORD_UNSIGNED
;
285 if (m
->private_key
) {
286 r
= user_record_verify(hr
, m
->private_key
);
289 case USER_RECORD_FOREIGN
:
290 /* This record is not signed by this key, but let's see below */
293 case USER_RECORD_SIGNED
: /* Signed by us, but also by others, let's propagate that */
294 case USER_RECORD_SIGNED_EXCLUSIVE
: /* Signed by us, and nothing else, ditto */
295 case USER_RECORD_UNSIGNED
: /* Not signed at all, ditto */
301 HASHMAP_FOREACH(pkey
, m
->public_keys
, i
) {
302 r
= user_record_verify(hr
, pkey
);
305 case USER_RECORD_FOREIGN
:
306 /* This record is not signed by this key, but let's see our other keys */
309 case USER_RECORD_SIGNED
: /* It's signed by this key we are happy with, but which is not our own. */
310 case USER_RECORD_SIGNED_EXCLUSIVE
:
311 return USER_RECORD_FOREIGN
;
313 case USER_RECORD_UNSIGNED
: /* It's not signed at all */
322 static int manager_add_home_by_record(
328 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
329 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
330 unsigned line
, column
;
339 if (fstatat(dir_fd
, fname
, &st
, 0) < 0)
340 return log_error_errno(errno
, "Failed to stat identity record %s: %m", fname
);
342 if (!S_ISREG(st
.st_mode
)) {
343 log_debug("Identity record file %s is not a regular file, ignoring.", fname
);
348 goto unlink_this_file
;
350 r
= json_parse_file_at(NULL
, dir_fd
, fname
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
352 return log_error_errno(r
, "Failed to parse identity record at %s:%u%u: %m", fname
, line
, column
);
354 if (json_variant_is_blank_object(v
))
355 goto unlink_this_file
;
357 hr
= user_record_new();
361 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
);
365 if (!streq_ptr(hr
->user_name
, name
))
366 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Identity's user name %s does not match file name %s, refusing.", hr
->user_name
, name
);
368 is_signed
= manager_verify_user_record(m
, hr
);
372 return log_warning_errno(is_signed
, "User record %s is not signed by any accepted key, ignoring.", fname
);
373 case USER_RECORD_UNSIGNED
:
374 return log_warning_errno(SYNTHETIC_ERRNO(EPERM
), "User record %s is not signed at all, ignoring.", fname
);
375 case USER_RECORD_SIGNED
:
376 log_info("User record %s is signed by us (and others), accepting.", fname
);
378 case USER_RECORD_SIGNED_EXCLUSIVE
:
379 log_info("User record %s is signed only by us, accepting.", fname
);
381 case USER_RECORD_FOREIGN
:
382 log_info("User record %s is signed by registered key from others, accepting.", fname
);
385 assert(is_signed
< 0);
386 return log_error_errno(is_signed
, "Failed to verify signature of user record in %s: %m", fname
);
389 h
= hashmap_get(m
->homes_by_name
, name
);
391 r
= home_set_record(h
, hr
);
393 return log_error_errno(r
, "Failed to update home record for %s: %m", name
);
395 /* If we acquired a record now for a previously unallocated entry, then reset the state. This
396 * makes sure home_get_state() will check for the availability of the image file dynamically
397 * in order to detect to distinguish HOME_INACTIVE and HOME_ABSENT. */
398 if (h
->state
== HOME_UNFIXATED
)
399 h
->state
= _HOME_STATE_INVALID
;
401 r
= home_new(m
, hr
, NULL
, &h
);
403 return log_error_errno(r
, "Failed to allocate new home object: %m");
405 log_info("Added registered home for user %s.", hr
->user_name
);
408 /* Only entries we exclusively signed are writable to us, hence remember the result */
409 h
->signed_locally
= is_signed
== USER_RECORD_SIGNED_EXCLUSIVE
;
414 /* If this is an empty file, then let's just remove it. An empty file is not useful in any case, and
415 * apparently xfs likes to leave empty files around when not unmounted cleanly (see
416 * https://github.com/systemd/systemd/issues/15178 for example). Note that we don't delete non-empty
417 * files even if they are invalid, because that's just too risky, we might delete data the user still
418 * needs. But empty files are never useful, hence let's just remove them. */
420 if (unlinkat(dir_fd
, fname
, 0) < 0)
421 return log_error_errno(errno
, "Failed to remove empty user record file %s: %m", fname
);
423 log_notice("Discovered empty user record file /var/lib/systemd/home/%s, removed automatically.", fname
);
427 static int manager_enumerate_records(Manager
*m
) {
428 _cleanup_closedir_
DIR *d
= NULL
;
433 d
= opendir("/var/lib/systemd/home/");
435 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
436 "Failed to open /var/lib/systemd/home/: %m");
438 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read record directory: %m")) {
439 _cleanup_free_
char *n
= NULL
;
442 if (!dirent_is_file(de
))
445 e
= endswith(de
->d_name
, ".identity");
449 n
= strndup(de
->d_name
, e
- de
->d_name
);
453 if (!suitable_user_name(n
))
456 (void) manager_add_home_by_record(m
, n
, dirfd(d
), de
->d_name
);
462 static int search_quota(uid_t uid
, const char *exclude_quota_path
) {
463 struct stat exclude_st
= {};
464 dev_t previous_devno
= 0;
468 /* Checks whether the specified UID owns any files on the files system, but ignore any file system
469 * backing the specified file. The file is used when operating on home directories, where it's OK if
470 * the UID of them already owns files. */
472 if (exclude_quota_path
&& stat(exclude_quota_path
, &exclude_st
) < 0) {
474 return log_warning_errno(errno
, "Failed to stat %s, ignoring: %m", exclude_quota_path
);
477 /* Check a few usual suspects where regular users might own files. Note that this is by no means
478 * comprehensive, but should cover most cases. Note that in an ideal world every user would be
479 * registered in NSS and avoid our own UID range, but for all other cases, it's a good idea to be
480 * paranoid and check quota if we can. */
481 FOREACH_STRING(where
, "/home/", "/tmp/", "/var/", "/var/mail/", "/var/tmp/", "/var/spool/") {
485 if (stat(where
, &st
) < 0) {
486 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
487 "Failed to stat %s, ignoring: %m", where
);
491 if (major(st
.st_dev
) == 0) {
492 log_debug("Directory %s is not on a real block device, not checking quota for UID use.", where
);
496 if (st
.st_dev
== exclude_st
.st_dev
) { /* If an exclude path is specified, then ignore quota
497 * reported on the same block device as that path. */
498 log_debug("Directory %s is where the home directory is located, not checking quota for UID use.", where
);
502 if (st
.st_dev
== previous_devno
) { /* Does this directory have the same devno as the previous
503 * one we tested? If so, there's no point in testing this
505 log_debug("Directory %s is on same device as previous tested directory, not checking quota for UID use a second time.", where
);
509 previous_devno
= st
.st_dev
;
511 r
= quotactl_devno(QCMD_FIXED(Q_GETQUOTA
, USRQUOTA
), st
.st_dev
, uid
, &req
);
513 if (ERRNO_IS_NOT_SUPPORTED(r
))
514 log_debug_errno(r
, "No UID quota support on %s, ignoring.", where
);
516 log_warning_errno(r
, "Failed to query quota on %s, ignoring.", where
);
521 if ((FLAGS_SET(req
.dqb_valid
, QIF_SPACE
) && req
.dqb_curspace
> 0) ||
522 (FLAGS_SET(req
.dqb_valid
, QIF_INODES
) && req
.dqb_curinodes
> 0)) {
523 log_debug_errno(errno
, "Quota reports UID " UID_FMT
" occupies disk space on %s.", uid
, where
);
531 static int manager_acquire_uid(
534 const char *user_name
,
535 const char *exclude_quota_path
,
538 static const uint8_t hash_key
[] = {
539 0xa3, 0xb8, 0x82, 0x69, 0x9a, 0x71, 0xf7, 0xa9,
540 0xe0, 0x7c, 0xf6, 0xf1, 0x21, 0x69, 0xd2, 0x1e
547 } phase
= PHASE_SUGGESTED
;
549 unsigned n_tries
= 100;
566 case PHASE_SUGGESTED
:
567 phase
= PHASE_HASHED
;
569 if (!uid_is_home(start_uid
))
572 candidate
= start_uid
;
576 phase
= PHASE_RANDOM
;
581 candidate
= UID_CLAMP_INTO_HOME_RANGE(siphash24(user_name
, strlen(user_name
), hash_key
));
585 random_bytes(&candidate
, sizeof(candidate
));
586 candidate
= UID_CLAMP_INTO_HOME_RANGE(candidate
);
590 assert_not_reached("unknown phase");
593 other
= hashmap_get(m
->homes_by_uid
, UID_TO_PTR(candidate
));
595 log_debug("Candidate UID " UID_FMT
" already used by another home directory (%s), let's try another.", candidate
, other
->user_name
);
599 pw
= getpwuid(candidate
);
601 log_debug("Candidate UID " UID_FMT
" already registered by another user in NSS (%s), let's try another.", candidate
, pw
->pw_name
);
605 gr
= getgrgid((gid_t
) candidate
);
607 log_debug("Candidate UID " UID_FMT
" already registered by another group in NSS (%s), let's try another.", candidate
, gr
->gr_name
);
611 r
= search_ipc(candidate
, (gid_t
) candidate
);
615 log_debug_errno(r
, "Candidate UID " UID_FMT
" already owns IPC objects, let's try another: %m", candidate
);
619 r
= search_quota(candidate
, exclude_quota_path
);
628 static int manager_add_home_by_image(
630 const char *user_name
,
632 const char *image_path
,
637 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
647 assert(storage
>= 0);
648 assert(storage
< _USER_STORAGE_MAX
);
650 h
= hashmap_get(m
->homes_by_name
, user_name
);
654 if (h
->state
!= HOME_UNFIXATED
) {
655 log_debug("Found an image for user %s which already has a record, skipping.", user_name
);
656 return 0; /* ignore images that synthesize a user we already have a record for */
659 same
= user_record_storage(h
->record
) == storage
;
661 if (h
->sysfs
&& sysfs
)
662 same
= path_equal(h
->sysfs
, sysfs
);
663 else if (!!h
->sysfs
!= !!sysfs
)
668 p
= user_record_image_path(h
->record
);
669 same
= p
&& path_equal(p
, image_path
);
674 log_debug("Found multiple images for user '%s', ignoring image '%s'.", user_name
, image_path
);
678 /* Check NSS, in case there's another user or group by this name */
679 if (getpwnam(user_name
) || getgrnam(user_name
)) {
680 log_debug("Found an existing user or group by name '%s', ignoring image '%s'.", user_name
, image_path
);
685 if (h
&& uid_is_valid(h
->uid
))
688 r
= manager_acquire_uid(m
, start_uid
, user_name
, IN_SET(storage
, USER_SUBVOLUME
, USER_DIRECTORY
, USER_FSCRYPT
) ? image_path
: NULL
, &uid
);
690 return log_warning_errno(r
, "Failed to acquire unused UID for %s: %m", user_name
);
693 hr
= user_record_new();
697 r
= user_record_synthesize(hr
, user_name
, realm
, image_path
, storage
, uid
, (gid_t
) uid
);
699 return log_error_errno(r
, "Failed to synthesize home record for %s (image %s): %m", user_name
, image_path
);
702 r
= home_set_record(h
, hr
);
704 return log_error_errno(r
, "Failed to update home record for %s: %m", user_name
);
706 r
= home_new(m
, hr
, sysfs
, &h
);
708 return log_error_errno(r
, "Failed to allocate new home object: %m");
710 h
->state
= HOME_UNFIXATED
;
712 log_info("Discovered new home for user %s through image %s.", user_name
, image_path
);
718 int manager_augment_record_with_uid(
722 const char *exclude_quota_path
= NULL
;
723 uid_t start_uid
= UID_INVALID
, uid
;
729 if (uid_is_valid(hr
->uid
))
732 if (IN_SET(hr
->storage
, USER_CLASSIC
, USER_SUBVOLUME
, USER_DIRECTORY
, USER_FSCRYPT
)) {
735 ip
= user_record_image_path(hr
);
739 if (stat(ip
, &st
) < 0) {
741 log_warning_errno(errno
, "Failed to stat(%s): %m", ip
);
742 } else if (uid_is_home(st
.st_uid
)) {
743 start_uid
= st
.st_uid
;
744 exclude_quota_path
= ip
;
749 r
= manager_acquire_uid(m
, start_uid
, hr
->user_name
, exclude_quota_path
, &uid
);
753 log_debug("Acquired new UID " UID_FMT
" for %s.", uid
, hr
->user_name
);
755 r
= user_record_add_binding(
757 _USER_STORAGE_INVALID
,
775 static int manager_assess_image(
778 const char *dir_path
,
779 const char *dentry_name
) {
781 char *luks_suffix
, *directory_suffix
;
782 _cleanup_free_
char *path
= NULL
;
790 luks_suffix
= endswith(dentry_name
, ".home");
792 directory_suffix
= NULL
;
794 directory_suffix
= endswith(dentry_name
, ".homedir");
796 /* Early filter out: by name */
797 if (!luks_suffix
&& !directory_suffix
)
800 path
= path_join(dir_path
, dentry_name
);
804 /* Follow symlinks here, to allow people to link in stuff to make them available locally. */
806 r
= fstatat(dir_fd
, dentry_name
, &st
, 0);
810 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
811 "Failed to stat() directory entry '%s', ignoring: %m", dentry_name
);
813 if (S_ISREG(st
.st_mode
)) {
814 _cleanup_free_
char *n
= NULL
, *user_name
= NULL
, *realm
= NULL
;
819 n
= strndup(dentry_name
, luks_suffix
- dentry_name
);
823 r
= split_user_name_realm(n
, &user_name
, &realm
);
824 if (r
== -EINVAL
) /* Not the right format: ignore */
827 return log_error_errno(r
, "Failed to split image name into user name/realm: %m");
829 return manager_add_home_by_image(m
, user_name
, realm
, path
, NULL
, USER_LUKS
, UID_INVALID
);
832 if (S_ISDIR(st
.st_mode
)) {
833 _cleanup_free_
char *n
= NULL
, *user_name
= NULL
, *realm
= NULL
;
834 _cleanup_close_
int fd
= -1;
837 if (!directory_suffix
)
840 n
= strndup(dentry_name
, directory_suffix
- dentry_name
);
844 r
= split_user_name_realm(n
, &user_name
, &realm
);
845 if (r
== -EINVAL
) /* Not the right format: ignore */
848 return log_error_errno(r
, "Failed to split image name into user name/realm: %m");
851 fd
= openat(dir_fd
, dentry_name
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
853 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
855 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
856 "Failed to open directory '%s', ignoring: %m", path
);
858 if (fstat(fd
, &st
) < 0)
859 return log_warning_errno(errno
, "Failed to fstat() %s, ignoring: %m", path
);
861 assert(S_ISDIR(st
.st_mode
)); /* Must hold, we used O_DIRECTORY above */
863 r
= btrfs_is_subvol_fd(fd
);
865 return log_warning_errno(errno
, "Failed to determine whether %s is a btrfs subvolume: %m", path
);
867 storage
= USER_SUBVOLUME
;
869 struct fscrypt_policy policy
;
871 if (ioctl(fd
, FS_IOC_GET_ENCRYPTION_POLICY
, &policy
) < 0) {
873 if (errno
== ENODATA
)
874 log_debug_errno(errno
, "Determined %s is not fscrypt encrypted.", path
);
875 else if (ERRNO_IS_NOT_SUPPORTED(errno
))
876 log_debug_errno(errno
, "Determined %s is not fscrypt encrypted because kernel or file system doesn't support it.", path
);
878 log_debug_errno(errno
, "FS_IOC_GET_ENCRYPTION_POLICY failed with unexpected error code on %s, ignoring: %m", path
);
880 storage
= USER_DIRECTORY
;
882 storage
= USER_FSCRYPT
;
885 return manager_add_home_by_image(m
, user_name
, realm
, path
, NULL
, storage
, st
.st_uid
);
891 int manager_enumerate_images(Manager
*m
) {
892 _cleanup_closedir_
DIR *d
= NULL
;
897 if (!m
->scan_slash_home
)
900 d
= opendir("/home/");
902 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
,
903 "Failed to open /home/: %m");
905 FOREACH_DIRENT(de
, d
, return log_error_errno(errno
, "Failed to read /home/ directory: %m"))
906 (void) manager_assess_image(m
, dirfd(d
), "/home", de
->d_name
);
911 static int manager_connect_bus(Manager
*m
) {
917 r
= sd_bus_default_system(&m
->bus
);
919 return log_error_errno(r
, "Failed to connect to system bus: %m");
921 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
925 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.home1", 0, NULL
, NULL
);
927 return log_error_errno(r
, "Failed to request name: %m");
929 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
931 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
933 (void) sd_bus_set_exit_on_disconnect(m
->bus
, true);
938 static int manager_bind_varlink(Manager
*m
) {
942 assert(!m
->varlink_server
);
944 r
= varlink_server_new(&m
->varlink_server
, VARLINK_SERVER_ACCOUNT_UID
);
946 return log_error_errno(r
, "Failed to allocate varlink server object: %m");
948 varlink_server_set_userdata(m
->varlink_server
, m
);
950 r
= varlink_server_bind_method_many(
952 "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record
,
953 "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record
,
954 "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships
);
956 return log_error_errno(r
, "Failed to register varlink methods: %m");
958 (void) mkdir_p("/run/systemd/userdb", 0755);
960 r
= varlink_server_listen_address(m
->varlink_server
, "/run/systemd/userdb/io.systemd.Home", 0666);
962 return log_error_errno(r
, "Failed to bind to varlink socket: %m");
964 r
= varlink_server_attach_event(m
->varlink_server
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
966 return log_error_errno(r
, "Failed to attach varlink connection to event loop: %m");
971 static ssize_t
read_datagram(int fd
, struct ucred
*ret_sender
, void **ret
) {
972 _cleanup_free_
void *buffer
= NULL
;
979 n
= next_datagram_size_fd(fd
);
983 buffer
= malloc(n
+ 2);
988 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred
))) control
;
989 bool found_ucred
= false;
990 struct cmsghdr
*cmsg
;
994 /* Pass one extra byte, as a size check */
995 iov
= IOVEC_MAKE(buffer
, n
+ 1);
997 mh
= (struct msghdr
) {
1000 .msg_control
= &control
,
1001 .msg_controllen
= sizeof(control
),
1004 m
= recvmsg_safe(fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1008 cmsg_close_all(&mh
);
1010 /* Ensure the size matches what we determined before */
1014 CMSG_FOREACH(cmsg
, &mh
)
1015 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1016 cmsg
->cmsg_type
== SCM_CREDENTIALS
&&
1017 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
))) {
1019 memcpy(ret_sender
, CMSG_DATA(cmsg
), sizeof(struct ucred
));
1024 *ret_sender
= (struct ucred
) {
1030 m
= recv(fd
, buffer
, n
+ 1, MSG_DONTWAIT
);
1034 /* Ensure the size matches what we determined before */
1039 /* For safety reasons: let's always NUL terminate. */
1040 ((char*) buffer
)[n
] = 0;
1041 *ret
= TAKE_PTR(buffer
);
1046 static int on_notify_socket(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1047 _cleanup_strv_free_
char **l
= NULL
;
1048 _cleanup_free_
void *datagram
= NULL
;
1049 struct ucred sender
;
1050 Manager
*m
= userdata
;
1057 n
= read_datagram(fd
, &sender
, &datagram
);
1058 if (IN_SET(n
, -EAGAIN
, -EINTR
))
1061 return log_error_errno(n
, "Failed to read notify datagram: %m");
1063 if (sender
.pid
<= 0) {
1064 log_warning("Received notify datagram without valid sender PID, ignoring.");
1068 h
= hashmap_get(m
->homes_by_worker_pid
, PID_TO_PTR(sender
.pid
));
1070 log_warning("Received notify datagram of unknown process, ignoring.");
1074 l
= strv_split(datagram
, "\n");
1078 home_process_notify(h
, l
);
1082 static int manager_listen_notify(Manager
*m
) {
1083 _cleanup_close_
int fd
= -1;
1084 union sockaddr_union sa
= {
1085 .un
.sun_family
= AF_UNIX
,
1086 .un
.sun_path
= "/run/systemd/home/notify",
1091 assert(!m
->notify_socket_event_source
);
1093 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
1095 return log_error_errno(errno
, "Failed to create listening socket: %m");
1097 (void) mkdir_parents(sa
.un
.sun_path
, 0755);
1098 (void) sockaddr_un_unlink(&sa
.un
);
1100 if (bind(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
1101 return log_error_errno(errno
, "Failed to bind to socket: %m");
1103 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
1107 r
= sd_event_add_io(m
->event
, &m
->notify_socket_event_source
, fd
, EPOLLIN
, on_notify_socket
, m
);
1109 return log_error_errno(r
, "Failed to allocate event source for notify socket: %m");
1111 (void) sd_event_source_set_description(m
->notify_socket_event_source
, "notify-socket");
1113 /* Make sure we process sd_notify() before SIGCHLD for any worker, so that we always know the error
1114 * number of a client before it exits. */
1115 r
= sd_event_source_set_priority(m
->notify_socket_event_source
, SD_EVENT_PRIORITY_NORMAL
- 5);
1117 return log_error_errno(r
, "Failed to alter priority of NOTIFY_SOCKET event source: %m");
1119 r
= sd_event_source_set_io_fd_own(m
->notify_socket_event_source
, true);
1121 return log_error_errno(r
, "Failed to pass ownership of notify socket: %m");
1126 static int manager_add_device(Manager
*m
, sd_device
*d
) {
1127 _cleanup_free_
char *user_name
= NULL
, *realm
= NULL
, *node
= NULL
;
1128 const char *tabletype
, *parttype
, *partname
, *partuuid
, *sysfs
;
1135 r
= sd_device_get_syspath(d
, &sysfs
);
1137 return log_error_errno(r
, "Failed to acquire sysfs path of device: %m");
1139 r
= sd_device_get_property_value(d
, "ID_PART_TABLE_TYPE", &tabletype
);
1143 return log_error_errno(r
, "Failed to acquire ID_PART_TABLE_TYPE device property, ignoring: %m");
1145 if (!streq(tabletype
, "gpt")) {
1146 log_debug("Found partition (%s) on non-GPT table, ignoring.", sysfs
);
1150 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_TYPE", &parttype
);
1154 return log_error_errno(r
, "Failed to acquire ID_PART_ENTRY_TYPE device property, ignoring: %m");
1155 r
= sd_id128_from_string(parttype
, &id
);
1157 return log_debug_errno(r
, "Failed to parse ID_PART_ENTRY_TYPE field '%s', ignoring: %m", parttype
);
1158 if (!sd_id128_equal(id
, GPT_USER_HOME
)) {
1159 log_debug("Found partition (%s) we don't care about, ignoring.", sysfs
);
1163 r
= sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &partname
);
1165 return log_warning_errno(r
, "Failed to acquire ID_PART_ENTRY_NAME device property, ignoring: %m");
1167 r
= split_user_name_realm(partname
, &user_name
, &realm
);
1169 return log_warning_errno(r
, "Found partition with correct partition type but a non-parsable partition name '%s', ignoring.", partname
);
1171 return log_error_errno(r
, "Failed to validate partition name '%s': %m", partname
);
1173 r
= sd_device_get_property_value(d
, "ID_FS_UUID", &partuuid
);
1175 return log_warning_errno(r
, "Failed to acquire ID_FS_UUID device property, ignoring: %m");
1177 r
= sd_id128_from_string(partuuid
, &id
);
1179 return log_warning_errno(r
, "Failed to parse ID_FS_UUID field '%s', ignoring: %m", partuuid
);
1181 if (asprintf(&node
, "/dev/disk/by-uuid/" SD_ID128_UUID_FORMAT_STR
, SD_ID128_FORMAT_VAL(id
)) < 0)
1184 return manager_add_home_by_image(m
, user_name
, realm
, node
, sysfs
, USER_LUKS
, UID_INVALID
);
1187 static int manager_on_device(sd_device_monitor
*monitor
, sd_device
*d
, void *userdata
) {
1188 Manager
*m
= userdata
;
1194 if (device_for_action(d
, DEVICE_ACTION_REMOVE
)) {
1198 r
= sd_device_get_syspath(d
, &sysfs
);
1200 log_warning_errno(r
, "Failed to acquire sysfs path from device: %m");
1204 log_info("block device %s has been removed.", sysfs
);
1206 /* Let's see if we previously synthesized a home record from this device, if so, let's just
1207 * revalidate that. Otherwise let's revalidate them all, but asynchronously. */
1208 h
= hashmap_get(m
->homes_by_sysfs
, sysfs
);
1210 manager_revalidate_image(m
, h
);
1212 manager_enqueue_gc(m
, NULL
);
1214 (void) manager_add_device(m
, d
);
1216 (void) bus_manager_emit_auto_login_changed(m
);
1220 static int manager_watch_devices(Manager
*m
) {
1224 assert(!m
->device_monitor
);
1226 r
= sd_device_monitor_new(&m
->device_monitor
);
1228 return log_error_errno(r
, "Failed to allocate device monitor: %m");
1230 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "block", NULL
);
1232 return log_error_errno(r
, "Failed to configure device monitor match: %m");
1234 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
1236 return log_error_errno(r
, "Failed to attach device monitor to event loop: %m");
1238 r
= sd_device_monitor_start(m
->device_monitor
, manager_on_device
, m
);
1240 return log_error_errno(r
, "Failed to start device monitor: %m");
1245 static int manager_enumerate_devices(Manager
*m
) {
1246 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1252 r
= sd_device_enumerator_new(&e
);
1256 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1260 FOREACH_DEVICE(e
, d
)
1261 (void) manager_add_device(m
, d
);
1266 static int manager_load_key_pair(Manager
*m
) {
1267 _cleanup_(fclosep
) FILE *f
= NULL
;
1273 if (m
->private_key
) {
1274 EVP_PKEY_free(m
->private_key
);
1275 m
->private_key
= NULL
;
1278 r
= search_and_fopen_nulstr("local.private", "re", NULL
, KEY_PATHS_NULSTR
, &f
);
1282 return log_error_errno(r
, "Failed to read private key file: %m");
1284 if (fstat(fileno(f
), &st
) < 0)
1285 return log_error_errno(errno
, "Failed to stat private key file: %m");
1287 r
= stat_verify_regular(&st
);
1289 return log_error_errno(r
, "Private key file is not regular: %m");
1291 if (st
.st_uid
!= 0 || (st
.st_mode
& 0077) != 0)
1292 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Private key file is readable by more than the root user");
1294 m
->private_key
= PEM_read_PrivateKey(f
, NULL
, NULL
, NULL
);
1295 if (!m
->private_key
)
1296 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to load private key pair");
1298 log_info("Successfully loaded private key pair.");
1303 DEFINE_TRIVIAL_CLEANUP_FUNC(EVP_PKEY_CTX
*, EVP_PKEY_CTX_free
);
1305 static int manager_generate_key_pair(Manager
*m
) {
1306 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*ctx
= NULL
;
1307 _cleanup_(unlink_and_freep
) char *temp_public
= NULL
, *temp_private
= NULL
;
1308 _cleanup_fclose_
FILE *fpublic
= NULL
, *fprivate
= NULL
;
1311 if (m
->private_key
) {
1312 EVP_PKEY_free(m
->private_key
);
1313 m
->private_key
= NULL
;
1316 ctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519
, NULL
);
1318 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to allocate Ed25519 key generation context.");
1320 if (EVP_PKEY_keygen_init(ctx
) <= 0)
1321 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to initialize Ed25519 key generation context.");
1323 log_info("Generating key pair for signing local user identity records.");
1325 if (EVP_PKEY_keygen(ctx
, &m
->private_key
) <= 0)
1326 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to generate Ed25519 key pair");
1328 log_info("Successfully created Ed25519 key pair.");
1330 (void) mkdir_p("/var/lib/systemd/home", 0755);
1332 /* Write out public key (note that we only do that as a help to the user, we don't make use of this ever */
1333 r
= fopen_temporary("/var/lib/systemd/home/local.public", &fpublic
, &temp_public
);
1335 return log_error_errno(errno
, "Failed to open key file for writing: %m");
1337 if (PEM_write_PUBKEY(fpublic
, m
->private_key
) <= 0)
1338 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to write public key.");
1340 r
= fflush_and_check(fpublic
);
1342 return log_error_errno(r
, "Failed to write private key: %m");
1344 fpublic
= safe_fclose(fpublic
);
1346 /* Write out the private key (this actually writes out both private and public, OpenSSL is confusing) */
1347 r
= fopen_temporary("/var/lib/systemd/home/local.private", &fprivate
, &temp_private
);
1349 return log_error_errno(errno
, "Failed to open key file for writing: %m");
1351 if (PEM_write_PrivateKey(fprivate
, m
->private_key
, NULL
, NULL
, 0, NULL
, 0) <= 0)
1352 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to write private key pair.");
1354 r
= fflush_and_check(fprivate
);
1356 return log_error_errno(r
, "Failed to write private key: %m");
1358 fprivate
= safe_fclose(fprivate
);
1360 /* Both are written now, move them into place */
1362 if (rename(temp_public
, "/var/lib/systemd/home/local.public") < 0)
1363 return log_error_errno(errno
, "Failed to move public key file into place: %m");
1364 temp_public
= mfree(temp_public
);
1366 if (rename(temp_private
, "/var/lib/systemd/home/local.private") < 0) {
1367 (void) unlink_noerrno("/var/lib/systemd/home/local.public"); /* try to remove the file we already created */
1368 return log_error_errno(errno
, "Failed to move privtate key file into place: %m");
1370 temp_private
= mfree(temp_private
);
1375 int manager_acquire_key_pair(Manager
*m
) {
1380 /* Already there? */
1384 /* First try to load key off disk */
1385 r
= manager_load_key_pair(m
);
1389 /* Didn't work, generate a new one */
1390 return manager_generate_key_pair(m
);
1393 int manager_sign_user_record(Manager
*m
, UserRecord
*u
, UserRecord
**ret
, sd_bus_error
*error
) {
1400 r
= manager_acquire_key_pair(m
);
1404 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_KEY
, "Can't sign without local key.");
1406 return user_record_sign(u
, m
->private_key
, ret
);
1409 DEFINE_PRIVATE_HASH_OPS_FULL(public_key_hash_ops
, char, string_hash_func
, string_compare_func
, free
, EVP_PKEY
, EVP_PKEY_free
);
1410 DEFINE_TRIVIAL_CLEANUP_FUNC(EVP_PKEY
*, EVP_PKEY_free
);
1412 static int manager_load_public_key_one(Manager
*m
, const char *path
) {
1413 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*pkey
= NULL
;
1414 _cleanup_fclose_
FILE *f
= NULL
;
1415 _cleanup_free_
char *fn
= NULL
;
1421 if (streq(basename(path
), "local.public")) /* we already loaded the private key, which includes the public one */
1424 f
= fopen(path
, "re");
1426 if (errno
== ENOENT
)
1429 return log_error_errno(errno
, "Failed to open public key %s: %m", path
);
1432 if (fstat(fileno(f
), &st
) < 0)
1433 return log_error_errno(errno
, "Failed to stat public key %s: %m", path
);
1435 r
= stat_verify_regular(&st
);
1437 return log_error_errno(r
, "Public key file %s is not a regular file: %m", path
);
1439 if (st
.st_uid
!= 0 || (st
.st_mode
& 0022) != 0)
1440 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Public key file %s is writable by more than the root user, refusing.", path
);
1442 r
= hashmap_ensure_allocated(&m
->public_keys
, &public_key_hash_ops
);
1446 pkey
= PEM_read_PUBKEY(f
, &pkey
, NULL
, NULL
);
1448 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to parse public key file %s.", path
);
1450 fn
= strdup(basename(path
));
1454 r
= hashmap_put(m
->public_keys
, fn
, pkey
);
1456 return log_error_errno(r
, "Failed to add public key to set: %m");
1464 static int manager_load_public_keys(Manager
*m
) {
1465 _cleanup_strv_free_
char **files
= NULL
;
1471 m
->public_keys
= hashmap_free(m
->public_keys
);
1473 r
= conf_files_list_nulstr(
1477 CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
,
1480 return log_error_errno(r
, "Failed to assemble list of public key directories: %m");
1482 STRV_FOREACH(i
, files
)
1483 (void) manager_load_public_key_one(m
, *i
);
1488 int manager_startup(Manager
*m
) {
1493 r
= manager_listen_notify(m
);
1497 r
= manager_connect_bus(m
);
1501 r
= manager_bind_varlink(m
);
1505 r
= manager_load_key_pair(m
); /* only try to load it, don't generate any */
1509 r
= manager_load_public_keys(m
);
1513 manager_watch_home(m
);
1514 (void) manager_watch_devices(m
);
1516 (void) manager_enumerate_records(m
);
1517 (void) manager_enumerate_images(m
);
1518 (void) manager_enumerate_devices(m
);
1520 /* Let's clean up home directories whose devices got removed while we were not running */
1521 (void) manager_enqueue_gc(m
, NULL
);
1526 void manager_revalidate_image(Manager
*m
, Home
*h
) {
1532 /* Frees an automatically discovered image, if it's synthetic and its image disappeared. Unmounts any
1533 * image if it's mounted but it's image vanished. */
1535 if (h
->current_operation
|| !ordered_set_isempty(h
->pending_operations
))
1538 if (h
->state
== HOME_UNFIXATED
) {
1539 r
= user_record_test_image_path(h
->record
);
1541 log_warning_errno(r
, "Can't determine if image of %s exists, freeing unfixated user: %m", h
->user_name
);
1542 else if (r
== USER_TEST_ABSENT
)
1543 log_info("Image for %s disappeared, freeing unfixated user.", h
->user_name
);
1549 } else if (h
->state
< 0) {
1551 r
= user_record_test_home_directory(h
->record
);
1553 log_warning_errno(r
, "Unable to determine state of home directory, ignoring: %m");
1557 if (r
== USER_TEST_MOUNTED
) {
1558 r
= user_record_test_image_path(h
->record
);
1560 log_warning_errno(r
, "Unable to determine state of image path, ignoring: %m");
1564 if (r
== USER_TEST_ABSENT
) {
1565 _cleanup_(operation_unrefp
) Operation
*o
= NULL
;
1567 log_notice("Backing image disappeared while home directory %s was mounted, unmounting it forcibly.", h
->user_name
);
1568 /* Wowza, the thing is mounted, but the device is gone? Act on it. */
1570 r
= home_killall(h
);
1572 log_warning_errno(r
, "Failed to kill processes of user %s, ignoring: %m", h
->user_name
);
1574 /* We enqueue the operation here, after all the home directory might
1575 * currently already run some operation, and we can deactivate it only after
1576 * that's complete. */
1577 o
= operation_new(OPERATION_DEACTIVATE_FORCE
, NULL
);
1583 r
= home_schedule_operation(h
, o
, NULL
);
1585 log_warning_errno(r
, "Failed to enqueue forced home directory %s deactivation, ignoring: %m", h
->user_name
);
1591 int manager_gc_images(Manager
*m
) {
1597 /* Focus on a specific home */
1599 h
= TAKE_PTR(m
->gc_focus
);
1600 manager_revalidate_image(m
, h
);
1605 HASHMAP_FOREACH(h
, m
->homes_by_name
, i
)
1606 manager_revalidate_image(m
, h
);
1612 static int on_deferred_rescan(sd_event_source
*s
, void *userdata
) {
1613 Manager
*m
= userdata
;
1617 m
->deferred_rescan_event_source
= sd_event_source_unref(m
->deferred_rescan_event_source
);
1619 manager_enumerate_devices(m
);
1620 manager_enumerate_images(m
);
1624 int manager_enqueue_rescan(Manager
*m
) {
1629 if (m
->deferred_rescan_event_source
)
1635 if (IN_SET(sd_event_get_state(m
->event
), SD_EVENT_FINISHED
, SD_EVENT_EXITING
))
1638 r
= sd_event_add_defer(m
->event
, &m
->deferred_rescan_event_source
, on_deferred_rescan
, m
);
1640 return log_error_errno(r
, "Failed to allocate rescan event source: %m");
1642 r
= sd_event_source_set_priority(m
->deferred_rescan_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
1644 log_warning_errno(r
, "Failed to tweak priority of event source, ignoring: %m");
1646 (void) sd_event_source_set_description(m
->deferred_rescan_event_source
, "deferred-rescan");
1650 static int on_deferred_gc(sd_event_source
*s
, void *userdata
) {
1651 Manager
*m
= userdata
;
1655 m
->deferred_gc_event_source
= sd_event_source_unref(m
->deferred_gc_event_source
);
1657 manager_gc_images(m
);
1661 int manager_enqueue_gc(Manager
*m
, Home
*focus
) {
1666 /* This enqueues a request to GC dead homes. It may be called with focus=NULL in which case all homes
1667 * will be scanned, or with the parameter set, in which case only that home is checked. */
1672 if (IN_SET(sd_event_get_state(m
->event
), SD_EVENT_FINISHED
, SD_EVENT_EXITING
))
1675 /* If a focus home is specified, then remember to focus just on this home. Otherwise invalidate any
1676 * focus that might be set to look at all homes. */
1678 if (m
->deferred_gc_event_source
) {
1679 if (m
->gc_focus
!= focus
) /* not the same focus, then look at everything */
1684 m
->gc_focus
= focus
; /* start focused */
1686 r
= sd_event_add_defer(m
->event
, &m
->deferred_gc_event_source
, on_deferred_gc
, m
);
1688 return log_error_errno(r
, "Failed to allocate GC event source: %m");
1690 r
= sd_event_source_set_priority(m
->deferred_gc_event_source
, SD_EVENT_PRIORITY_IDLE
);
1692 log_warning_errno(r
, "Failed to tweak priority of event source, ignoring: %m");
1694 (void) sd_event_source_set_description(m
->deferred_gc_event_source
, "deferred-gc");