From 254d1313ae5a69c08c9b93032aaaf3d6083cfc07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 19 Dec 2022 13:07:42 +0100 Subject: tree-wide: use -EBADF for fd initialization -1 was used everywhere, but -EBADF or -EBADFD started being used in various places. Let's make things consistent in the new style. Note that there are two candidates: EBADF 9 Bad file descriptor EBADFD 77 File descriptor in bad state Since we're initializating the fd, we're just assigning a value that means "no fd yet", so it's just a bad file descriptor, and the first errno fits better. If instead we had a valid file descriptor that became invalid because of some operation or state change, the other errno would fit better. In some places, initialization is dropped if unnecessary. --- src/home/homectl.c | 2 +- src/home/homed-home-bus.c | 4 ++-- src/home/homed-home.c | 14 +++++++------- src/home/homed-manager.c | 8 ++++---- src/home/homed-operation.c | 2 +- src/home/homework-directory.c | 2 +- src/home/homework-fscrypt.c | 2 +- src/home/homework-luks.c | 18 +++++++++--------- src/home/homework-mount.c | 8 ++++---- src/home/homework.c | 6 +++--- src/home/homework.h | 4 ++-- src/home/pam_systemd_home.c | 2 +- 12 files changed, 36 insertions(+), 36 deletions(-) (limited to 'src/home') diff --git a/src/home/homectl.c b/src/home/homectl.c index a6d25c84fc..0a4c01834a 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -1964,7 +1964,7 @@ static int with_home(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(user_record_unrefp) UserRecord *secret = NULL; - _cleanup_close_ int acquired_fd = -1; + _cleanup_close_ int acquired_fd = -EBADF; _cleanup_strv_free_ char **cmdline = NULL; const char *home; int r, ret; diff --git a/src/home/homed-home-bus.c b/src/home/homed-home-bus.c index 4e3fd5f0ae..a47f4d8a84 100644 --- a/src/home/homed-home-bus.c +++ b/src/home/homed-home-bus.c @@ -597,7 +597,7 @@ int bus_home_method_acquire( _cleanup_(user_record_unrefp) UserRecord *secret = NULL; _cleanup_(operation_unrefp) Operation *o = NULL; - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; int r, please_suspend; Home *h = ASSERT_PTR(userdata); @@ -635,7 +635,7 @@ int bus_home_method_ref( void *userdata, sd_bus_error *error) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; Home *h = ASSERT_PTR(userdata); HomeState state; int please_suspend, r; diff --git a/src/home/homed-home.c b/src/home/homed-home.c index 19be186400..c2514aafc9 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -135,11 +135,11 @@ int home_new(Manager *m, UserRecord *hr, const char *sysfs, Home **ret) { .user_name = TAKE_PTR(nm), .uid = hr->uid, .state = _HOME_STATE_INVALID, - .worker_stdout_fd = -1, + .worker_stdout_fd = -EBADF, .sysfs = TAKE_PTR(ns), .signed_locally = -1, - .pin_fd = -1, - .luks_lock_fd = -1, + .pin_fd = -EBADF, + .luks_lock_fd = -EBADF, }; r = hashmap_put(m->homes_by_name, home->user_name, home); @@ -1137,7 +1137,7 @@ static int home_on_worker_process(sd_event_source *s, const siginfo_t *si, void static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord *secret) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; _cleanup_(erase_and_freep) char *formatted = NULL; - _cleanup_close_ int stdin_fd = -1, stdout_fd = -1; + _cleanup_close_ int stdin_fd = -EBADF, stdout_fd = -EBADF; pid_t pid = 0; int r; @@ -1496,7 +1496,7 @@ int home_create(Home *h, UserRecord *secret, sd_bus_error *error) { if (IN_SET(t, USER_TEST_MAYBE, USER_TEST_UNDEFINED)) break; /* And if the image path test isn't conclusive, let's also go on */ - if (IN_SET(t, -EBADFD, -ENOTDIR)) + if (IN_SET(t, -EBADF, -ENOTDIR)) return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Selected home image of user %s already exists or has wrong inode type.", h->user_name); return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Selected home image of user %s already exists.", h->user_name); @@ -2630,7 +2630,7 @@ static int on_home_ref_eof(sd_event_source *s, int fd, uint32_t revents, void *u } int home_create_fifo(Home *h, bool please_suspend) { - _cleanup_close_ int ret_fd = -1; + _cleanup_close_ int ret_fd = -EBADF; sd_event_source **ss; const char *fn, *suffix; int r; @@ -2648,7 +2648,7 @@ int home_create_fifo(Home *h, bool please_suspend) { fn = strjoina("/run/systemd/home/", h->user_name, suffix); if (!*ss) { - _cleanup_close_ int ref_fd = -1; + _cleanup_close_ int ref_fd = -EBADF; (void) mkdir("/run/systemd/home/", 0755); if (mkfifo(fn, 0600) < 0 && errno != EEXIST) diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c index 61ef979049..2fd1d67eff 100644 --- a/src/home/homed-manager.c +++ b/src/home/homed-manager.c @@ -860,7 +860,7 @@ static int manager_assess_image( if (S_ISDIR(st.st_mode)) { _cleanup_free_ char *n = NULL, *user_name = NULL, *realm = NULL; - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; UserStorage storage; if (!directory_suffix) @@ -1041,7 +1041,7 @@ static ssize_t read_datagram( CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int))) control; _cleanup_free_ void *buffer = NULL; - _cleanup_close_ int passed_fd = -1; + _cleanup_close_ int passed_fd = -EBADF; struct ucred *sender = NULL; struct cmsghdr *cmsg; struct msghdr mh; @@ -1119,7 +1119,7 @@ static ssize_t read_datagram( static int on_notify_socket(sd_event_source *s, int fd, uint32_t revents, void *userdata) { _cleanup_strv_free_ char **l = NULL; _cleanup_free_ void *datagram = NULL; - _cleanup_close_ int passed_fd = -1; + _cleanup_close_ int passed_fd = -EBADF; struct ucred sender = UCRED_INVALID; Manager *m = ASSERT_PTR(userdata); ssize_t n; @@ -1154,7 +1154,7 @@ static int on_notify_socket(sd_event_source *s, int fd, uint32_t revents, void * } static int manager_listen_notify(Manager *m) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; union sockaddr_union sa = { .un.sun_family = AF_UNIX, .un.sun_path = "/run/systemd/home/notify", diff --git a/src/home/homed-operation.c b/src/home/homed-operation.c index 3847fc5d68..618e920a59 100644 --- a/src/home/homed-operation.c +++ b/src/home/homed-operation.c @@ -17,7 +17,7 @@ Operation *operation_new(OperationType type, sd_bus_message *m) { .type = type, .n_ref = 1, .message = sd_bus_message_ref(m), - .send_fd = -1, + .send_fd = -EBADF, .result = -1, }; diff --git a/src/home/homework-directory.c b/src/home/homework-directory.c index d3bc547429..6668edb749 100644 --- a/src/home/homework-directory.c +++ b/src/home/homework-directory.c @@ -108,7 +108,7 @@ int home_activate_directory( int home_create_directory_or_subvolume(UserRecord *h, HomeSetup *setup, UserRecord **ret_home) { _cleanup_(rm_rf_subvolume_and_freep) char *temporary = NULL; _cleanup_(user_record_unrefp) UserRecord *new_home = NULL; - _cleanup_close_ int mount_fd = -1; + _cleanup_close_ int mount_fd = -EBADF; _cleanup_free_ char *d = NULL; bool is_subvolume = false; const char *ip; diff --git a/src/home/homework-fscrypt.c b/src/home/homework-fscrypt.c index f29c55c895..f8f9f462bb 100644 --- a/src/home/homework-fscrypt.c +++ b/src/home/homework-fscrypt.c @@ -491,7 +491,7 @@ int home_create_fscrypt( _cleanup_(rm_rf_physical_and_freep) char *temporary = NULL; _cleanup_(user_record_unrefp) UserRecord *new_home = NULL; _cleanup_(erase_and_freep) void *volume_key = NULL; - _cleanup_close_ int mount_fd = -1; + _cleanup_close_ int mount_fd = -EBADF; struct fscrypt_policy policy = {}; size_t volume_key_size = 512 / 8; _cleanup_free_ char *d = NULL; diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 2b803bc0bd..f215558ae0 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -108,7 +108,7 @@ int run_mark_dirty(int fd, bool b) { } int run_mark_dirty_by_path(const char *path, bool b) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; assert(path); @@ -178,7 +178,7 @@ static int probe_file_system_by_fd( } static int probe_file_system_by_path(const char *path, char **ret_fstype, sd_id128_t *ret_uuid) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (fd < 0) @@ -203,7 +203,7 @@ static int block_get_size_by_fd(int fd, uint64_t *ret) { } static int block_get_size_by_path(const char *path, uint64_t *ret) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (fd < 0) @@ -1135,7 +1135,7 @@ int run_fallocate(int backing_fd, const struct stat *st) { } int run_fallocate_by_path(const char *backing_path) { - _cleanup_close_ int backing_fd = -1; + _cleanup_close_ int backing_fd = -EBADF; backing_fd = open(backing_path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); if (backing_fd < 0) @@ -1187,7 +1187,7 @@ static int open_image_file( const char *force_image_path, struct stat *ret_stat) { - _cleanup_close_ int image_fd = -1; + _cleanup_close_ int image_fd = -EBADF; struct stat st; const char *ip; int r; @@ -1959,7 +1959,7 @@ static bool supported_fs_size(const char *fstype, uint64_t host_size) { } static int wait_for_devlink(const char *path) { - _cleanup_close_ int inotify_fd = -1; + _cleanup_close_ int inotify_fd = -EBADF; usec_t until; int r; @@ -2141,7 +2141,7 @@ int home_create_luks( host_size = 0, partition_offset = 0, partition_size = 0; /* Unnecessary initialization to appease gcc */ _cleanup_(user_record_unrefp) UserRecord *new_home = NULL; sd_id128_t partition_uuid, fs_uuid, luks_uuid, disk_uuid; - _cleanup_close_ int mount_fd = -1; + _cleanup_close_ int mount_fd = -EBADF; const char *fstype, *ip; struct statfs sfs; int r; @@ -3079,9 +3079,9 @@ int home_resize_luks( _cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL; _cleanup_(fdisk_unref_tablep) struct fdisk_table *table = NULL; struct fdisk_partition *partition = NULL; - _cleanup_close_ int opened_image_fd = -1; + _cleanup_close_ int opened_image_fd = -EBADF; _cleanup_free_ char *whole_disk = NULL; - int r, resize_type, image_fd = -1; + int r, resize_type, image_fd = -EBADF; sd_id128_t disk_uuid; const char *ip, *ipo; struct statfs sfs; diff --git a/src/home/homework-mount.c b/src/home/homework-mount.c index cd03f79a73..cf42a0b94d 100644 --- a/src/home/homework-mount.c +++ b/src/home/homework-mount.c @@ -186,7 +186,7 @@ static int append_identity_range(char **text, uid_t start, uid_t next_start, uid static int make_userns(uid_t stored_uid, uid_t exposed_uid) { _cleanup_free_ char *text = NULL; - _cleanup_close_ int userns_fd = -1; + _cleanup_close_ int userns_fd = -EBADF; int r; assert(uid_is_valid(stored_uid)); @@ -238,7 +238,7 @@ static int make_userns(uid_t stored_uid, uid_t exposed_uid) { } int home_shift_uid(int dir_fd, const char *target, uid_t stored_uid, uid_t exposed_uid, int *ret_mount_fd) { - _cleanup_close_ int mount_fd = -1, userns_fd = -1; + _cleanup_close_ int mount_fd = -EBADF, userns_fd = -EBADF; int r; assert(dir_fd >= 0); @@ -261,7 +261,7 @@ int home_shift_uid(int dir_fd, const char *target, uid_t stored_uid, uid_t expos log_debug_errno(errno, "The open_tree() syscall is not supported, not setting up UID shift mount: %m"); if (ret_mount_fd) - *ret_mount_fd = -1; + *ret_mount_fd = -EBADF; return 0; } @@ -284,7 +284,7 @@ int home_shift_uid(int dir_fd, const char *target, uid_t stored_uid, uid_t expos log_debug_errno(errno, "UID/GID mapping for shifted mount not available, not setting it up: %m"); if (ret_mount_fd) - *ret_mount_fd = -1; + *ret_mount_fd = -EBADF; return 0; } diff --git a/src/home/homework.c b/src/home/homework.c index bc38437f2c..28907386a4 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -512,7 +512,7 @@ int home_sync_and_statfs(int root_fd, struct statfs *ret) { static int read_identity_file(int root_fd, JsonVariant **ret) { _cleanup_(fclosep) FILE *identity_file = NULL; - _cleanup_close_ int identity_fd = -1; + _cleanup_close_ int identity_fd = -EBADF; unsigned line, column; int r; @@ -543,7 +543,7 @@ static int read_identity_file(int root_fd, JsonVariant **ret) { static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) { _cleanup_(json_variant_unrefp) JsonVariant *normalized = NULL; _cleanup_(fclosep) FILE *identity_file = NULL; - _cleanup_close_ int identity_fd = -1; + _cleanup_close_ int identity_fd = -EBADF; _cleanup_free_ char *fn = NULL; int r; @@ -785,7 +785,7 @@ int home_maybe_shift_uid( HomeSetupFlags flags, HomeSetup *setup) { - _cleanup_close_ int mount_fd = -1; + _cleanup_close_ int mount_fd = -EBADF; struct stat st; assert(h); diff --git a/src/home/homework.h b/src/home/homework.h index b27c31d56b..cef3f4eb98 100644 --- a/src/home/homework.h +++ b/src/home/homework.h @@ -50,8 +50,8 @@ typedef struct HomeSetup { #define HOME_SETUP_INIT \ { \ - .root_fd = -1, \ - .image_fd = -1, \ + .root_fd = -EBADF, \ + .image_fd = -EBADF, \ .partition_offset = UINT64_MAX, \ .partition_size = UINT64_MAX, \ .key_serial = -1, \ diff --git a/src/home/pam_systemd_home.c b/src/home/pam_systemd_home.c index 8b41416578..8a7bfc5837 100644 --- a/src/home/pam_systemd_home.c +++ b/src/home/pam_systemd_home.c @@ -477,7 +477,7 @@ static int acquire_home( _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *secret = NULL; bool do_auth = please_authenticate, home_not_active = false, home_locked = false; _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; - _cleanup_close_ int acquired_fd = -1; + _cleanup_close_ int acquired_fd = -EBADF; _cleanup_free_ char *fd_field = NULL; const void *home_fd_ptr = NULL; const char *username = NULL; -- cgit v1.2.1