1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

homework: move allocation/destruction into outer/generic scope

Previously in most cases we'd allocate the HomeSetup context object
in generic code in homework.c. But for some cases we allocated them
instead inside the specific code in homework-{cifs,directory,luks}.c
Let's clean that up, and systematically allocate it in the outer
"entrypoint" calls in homework.c instead of the inner ones.

This doesn't change much in behaviour (i.e. it just means when something
fails we'll now clean it up one stack frame further up). But it will
allow is to more easily work with the context objects, since we'll have
them around in all stack frames.
This commit is contained in:
Lennart Poettering 2021-10-18 13:59:29 +02:00
parent 4e660eca45
commit a74e2e447f
7 changed files with 45 additions and 40 deletions

View File

@ -99,16 +99,17 @@ int home_setup_cifs(
int home_activate_cifs(
UserRecord *h,
HomeSetup *setup,
PasswordCache *cache,
UserRecord **ret_home) {
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
const char *hdo, *hd;
int r;
assert(h);
assert(user_record_storage(h) == USER_CIFS);
assert(setup);
assert(ret_home);
if (!h->cifs_service)
@ -117,21 +118,21 @@ int home_activate_cifs(
assert_se(hdo = user_record_home_directory(h));
hd = strdupa_safe(hdo); /* copy the string out, since it might change later in the home record object */
r = home_setup_cifs(h, 0, &setup);
r = home_setup_cifs(h, 0, setup);
if (r < 0)
return r;
r = home_refresh(h, &setup, NULL, cache, NULL, &new_home);
r = home_refresh(h, setup, NULL, cache, NULL, &new_home);
if (r < 0)
return r;
setup.root_fd = safe_close(setup.root_fd);
setup->root_fd = safe_close(setup->root_fd);
r = home_move_mount(NULL, hd);
if (r < 0)
return r;
setup.undo_mount = false;
setup->undo_mount = false;
log_info("Everything completed.");
@ -139,8 +140,7 @@ int home_activate_cifs(
return 1;
}
int home_create_cifs(UserRecord *h, UserRecord **ret_home) {
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
int home_create_cifs(UserRecord *h, HomeSetup *setup, UserRecord **ret_home) {
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
_cleanup_(closedirp) DIR *d = NULL;
_cleanup_close_ int copy = -1;
@ -148,6 +148,7 @@ int home_create_cifs(UserRecord *h, UserRecord **ret_home) {
assert(h);
assert(user_record_storage(h) == USER_CIFS);
assert(setup);
assert(ret_home);
if (!h->cifs_service)
@ -160,11 +161,11 @@ int home_create_cifs(UserRecord *h, UserRecord **ret_home) {
return log_error_errno(errno, "Unable to detect whether /sbin/mount.cifs exists: %m");
}
r = home_setup_cifs(h, 0, &setup);
r = home_setup_cifs(h, 0, setup);
if (r < 0)
return r;
copy = fcntl(setup.root_fd, F_DUPFD_CLOEXEC, 3);
copy = fcntl(setup->root_fd, F_DUPFD_CLOEXEC, 3);
if (copy < 0)
return -errno;
@ -178,11 +179,11 @@ int home_create_cifs(UserRecord *h, UserRecord **ret_home) {
if (errno != 0)
return log_error_errno(errno, "Failed to detect if CIFS directory is empty: %m");
r = home_populate(h, setup.root_fd);
r = home_populate(h, setup->root_fd);
if (r < 0)
return r;
r = home_sync_and_statfs(setup.root_fd, NULL);
r = home_sync_and_statfs(setup->root_fd, NULL);
if (r < 0)
return r;

View File

@ -6,6 +6,6 @@
int home_setup_cifs(UserRecord *h, HomeSetupFlags flags, HomeSetup *setup);
int home_activate_cifs(UserRecord *h, PasswordCache *cache, UserRecord **ret_home);
int home_activate_cifs(UserRecord *h, HomeSetup *setup, PasswordCache *cache, UserRecord **ret_home);
int home_create_cifs(UserRecord *h, UserRecord **ret_home);
int home_create_cifs(UserRecord *h, HomeSetup *setup, UserRecord **ret_home);

View File

@ -26,16 +26,17 @@ int home_setup_directory(UserRecord *h, HomeSetup *setup) {
int home_activate_directory(
UserRecord *h,
HomeSetup *setup,
PasswordCache *cache,
UserRecord **ret_home) {
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *header_home = NULL;
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
const char *hdo, *hd, *ipo, *ip;
int r;
assert(h);
assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
assert(setup);
assert(ret_home);
assert_se(ipo = user_record_image_path(h));
@ -44,15 +45,15 @@ int home_activate_directory(
assert_se(hdo = user_record_home_directory(h));
hd = strdupa_safe(hdo);
r = home_setup(h, 0, cache, &setup, &header_home);
r = home_setup(h, 0, cache, setup, &header_home);
if (r < 0)
return r;
r = home_refresh(h, &setup, header_home, cache, NULL, &new_home);
r = home_refresh(h, setup, header_home, cache, NULL, &new_home);
if (r < 0)
return r;
setup.root_fd = safe_close(setup.root_fd);
setup->root_fd = safe_close(setup->root_fd);
/* Create mount point to mount over if necessary */
if (!path_equal(ip, hd))

View File

@ -5,6 +5,6 @@
#include "user-record.h"
int home_setup_directory(UserRecord *h, HomeSetup *setup);
int home_activate_directory(UserRecord *h, PasswordCache *cache, UserRecord **ret_home);
int home_activate_directory(UserRecord *h, HomeSetup *setup, PasswordCache *cache, UserRecord **ret_home);
int home_create_directory_or_subvolume(UserRecord *h, UserRecord **ret_home);
int home_resize_directory(UserRecord *h, HomeSetupFlags flags, PasswordCache *cache, HomeSetup *setup, UserRecord **ret_home);

View File

@ -1391,11 +1391,11 @@ static void print_size_summary(uint64_t host_size, uint64_t encrypted_size, stru
int home_activate_luks(
UserRecord *h,
HomeSetup *setup,
PasswordCache *cache,
UserRecord **ret_home) {
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *luks_home_record = NULL;
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
uint64_t host_size, encrypted_size;
const char *hdo, *hd;
struct statfs sfs;
@ -1403,6 +1403,7 @@ int home_activate_luks(
assert(h);
assert(user_record_storage(h) == USER_LUKS);
assert(setup);
assert(ret_home);
r = dlopen_cryptsetup();
@ -1412,33 +1413,33 @@ int home_activate_luks(
assert_se(hdo = user_record_home_directory(h));
hd = strdupa_safe(hdo); /* copy the string out, since it might change later in the home record object */
r = home_get_state_luks(h, &setup);
r = home_get_state_luks(h, setup);
if (r < 0)
return r;
if (r > 0)
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", setup.dm_node);
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device mapper device %s already exists, refusing.", setup->dm_node);
r = home_setup_luks(
h,
0,
NULL,
cache,
&setup,
setup,
&luks_home_record);
if (r < 0)
return r;
r = block_get_size_by_fd(setup.loop->fd, &host_size);
r = block_get_size_by_fd(setup->loop->fd, &host_size);
if (r < 0)
return log_error_errno(r, "Failed to get loopback block device size: %m");
r = block_get_size_by_path(setup.dm_node, &encrypted_size);
r = block_get_size_by_path(setup->dm_node, &encrypted_size);
if (r < 0)
return log_error_errno(r, "Failed to get LUKS block device size: %m");
r = home_refresh(
h,
&setup,
setup,
luks_home_record,
cache,
&sfs,
@ -1446,28 +1447,28 @@ int home_activate_luks(
if (r < 0)
return r;
r = home_extend_embedded_identity(new_home, h, &setup);
r = home_extend_embedded_identity(new_home, h, setup);
if (r < 0)
return r;
setup.root_fd = safe_close(setup.root_fd);
setup->root_fd = safe_close(setup->root_fd);
r = home_move_mount(user_record_user_name_and_realm(h), hd);
if (r < 0)
return r;
setup.undo_mount = false;
setup.do_offline_fitrim = false;
setup->undo_mount = false;
setup->do_offline_fitrim = false;
loop_device_relinquish(setup.loop);
loop_device_relinquish(setup->loop);
r = sym_crypt_deactivate_by_name(NULL, setup.dm_name, CRYPT_DEACTIVATE_DEFERRED);
r = sym_crypt_deactivate_by_name(NULL, setup->dm_name, CRYPT_DEACTIVATE_DEFERRED);
if (r < 0)
log_warning_errno(r, "Failed to relinquish DM device, ignoring: %m");
setup.undo_dm = false;
setup.do_offline_fallocate = false;
setup.do_mark_clean = false;
setup->undo_dm = false;
setup->do_offline_fallocate = false;
setup->do_mark_clean = false;
log_info("Everything completed.");

View File

@ -7,7 +7,7 @@
int home_setup_luks(UserRecord *h, HomeSetupFlags flags, const char *force_image_path, PasswordCache *cache, HomeSetup *setup, UserRecord **ret_luks_home);
int home_activate_luks(UserRecord *h, PasswordCache *cache, UserRecord **ret_home);
int home_activate_luks(UserRecord *h, HomeSetup *setup, PasswordCache *cache, UserRecord **ret_home);
int home_deactivate_luks(UserRecord *h);
int home_trim_luks(UserRecord *h);

View File

@ -759,8 +759,9 @@ int home_refresh(
}
static int home_activate(UserRecord *h, UserRecord **ret_home) {
_cleanup_(password_cache_free) PasswordCache cache = {};
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
_cleanup_(password_cache_free) PasswordCache cache = {};
int r;
assert(h);
@ -791,7 +792,7 @@ static int home_activate(UserRecord *h, UserRecord **ret_home) {
switch (user_record_storage(h)) {
case USER_LUKS:
r = home_activate_luks(h, &cache, &new_home);
r = home_activate_luks(h, &setup, &cache, &new_home);
if (r < 0)
return r;
@ -800,14 +801,14 @@ static int home_activate(UserRecord *h, UserRecord **ret_home) {
case USER_SUBVOLUME:
case USER_DIRECTORY:
case USER_FSCRYPT:
r = home_activate_directory(h, &cache, &new_home);
r = home_activate_directory(h, &setup, &cache, &new_home);
if (r < 0)
return r;
break;
case USER_CIFS:
r = home_activate_cifs(h, &cache, &new_home);
r = home_activate_cifs(h, &setup, &cache, &new_home);
if (r < 0)
return r;
@ -1163,6 +1164,7 @@ static int determine_default_storage(UserStorage *ret) {
static int home_create(UserRecord *h, UserRecord **ret_home) {
_cleanup_(strv_free_erasep) char **effective_passwords = NULL;
_cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT;
_cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
_cleanup_(password_cache_free) PasswordCache cache = {};
UserStorage new_storage = _USER_STORAGE_INVALID;
@ -1238,7 +1240,7 @@ static int home_create(UserRecord *h, UserRecord **ret_home) {
break;
case USER_CIFS:
r = home_create_cifs(h, &new_home);
r = home_create_cifs(h, &setup, &new_home);
break;
default: