From c7f0d9e5acef3f62db3640d5ab7446241c022b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 7 Dec 2021 15:02:55 +0100 Subject: [PATCH 1/6] tree-wide: make FOREACH_DIRENT_ALL define the iterator variable The variable is not useful outside of the loop (it'll always be null after the loop is finished), so we can declare it inline in the loop. This saves one variable declaration and reduces the chances that somebody tries to use the variable outside of the loop. For consistency, 'de' is used everywhere for the var name. --- src/basic/cgroup-util.c | 2 -- src/basic/dirent-util.h | 2 +- src/basic/fs-util.c | 1 - src/basic/unit-file.c | 1 - src/core/manager.c | 4 +-- src/delta/delta.c | 1 - src/import/import-common.c | 12 +++---- src/import/pull-common.c | 1 - src/journal/journald-server.c | 1 - src/libsystemd/sd-device/device-enumerator.c | 36 +++++++++----------- src/libsystemd/sd-device/sd-device.c | 17 +++++---- src/libsystemd/sd-journal/journal-vacuum.c | 1 - src/libsystemd/sd-journal/sd-journal.c | 4 --- src/libsystemd/sd-login/sd-login.c | 1 - src/login/logind-dbus.c | 5 +-- src/nspawn/nspawn-patch-uid.c | 2 -- src/resolve/resolved-manager.c | 1 - src/shared/blockdev-util.c | 1 - src/shared/chown-recursive.c | 1 - src/shared/clean-ipc.c | 2 -- src/shared/copy.c | 1 - src/shared/discover-image.c | 1 - src/shared/killall.c | 7 ++-- src/shared/rm-rf.c | 1 - src/tmpfiles/tmpfiles.c | 32 ++++++++--------- src/udev/udev-builtin-net_id.c | 17 ++++----- src/udev/udev-builtin-path_id.c | 11 +++--- src/udev/udev-node.c | 11 +++--- src/udev/udev-rules.c | 7 ++-- src/udev/udev-watch.c | 1 - src/udev/udevadm-info.c | 2 -- 31 files changed, 71 insertions(+), 116 deletions(-) diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index b8268bc65c..a626ecf2e2 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -199,8 +199,6 @@ int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) { } int cg_read_subgroup(DIR *d, char **fn) { - struct dirent *de; - assert(d); assert(fn); diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h index a6272f891f..2dc5dae1e1 100644 --- a/src/basic/dirent-util.h +++ b/src/basic/dirent-util.h @@ -26,7 +26,7 @@ struct dirent *readdir_no_dot(DIR *dirp); else #define FOREACH_DIRENT_ALL(de, d, on_error) \ - for (de = readdir_ensure_type(d);; de = readdir_ensure_type(d)) \ + for (struct dirent *(de) = readdir_ensure_type(d);; (de) = readdir_ensure_type(d)) \ if (!de) { \ if (errno > 0) { \ on_error; \ diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index a135632cee..552986f546 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -533,7 +533,6 @@ int mkfifoat_atomic(int dirfd, const char *path, mode_t mode) { int get_files_in_directory(const char *path, char ***list) { _cleanup_strv_free_ char **l = NULL; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; size_t n = 0; assert(path); diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c index 89bfeceae4..30c632dfce 100644 --- a/src/basic/unit-file.c +++ b/src/basic/unit-file.c @@ -276,7 +276,6 @@ int unit_file_build_name_map( STRV_FOREACH(dir, (char**) lp->search_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*dir); if (!d) { diff --git a/src/core/manager.c b/src/core/manager.c index c5c6e62868..9368a1dfa1 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -287,7 +287,6 @@ static void manager_print_jobs_in_progress(Manager *m) { static int have_ask_password(void) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *de; dir = opendir("/run/systemd/ask-password"); if (!dir) { @@ -297,10 +296,9 @@ static int have_ask_password(void) { return -errno; } - FOREACH_DIRENT_ALL(de, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) if (startswith(de->d_name, "ask.")) return true; - } return false; } diff --git a/src/delta/delta.c b/src/delta/delta.c index 8f154f7680..3f1b206e6c 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -290,7 +290,6 @@ static int enumerate_dir( const char *path, bool dropins) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; _cleanup_strv_free_ char **files = NULL, **dirs = NULL; size_t n_files = 0, n_dirs = 0; char **t; diff --git a/src/import/import-common.c b/src/import/import-common.c index c36105221f..4eda9087c5 100644 --- a/src/import/import-common.c +++ b/src/import/import-common.c @@ -159,7 +159,7 @@ int import_fork_tar_c(const char *path, pid_t *ret) { int import_mangle_os_tree(const char *path) { _cleanup_free_ char *child = NULL, *t = NULL, *joined = NULL; _cleanup_closedir_ DIR *d = NULL, *cd = NULL; - struct dirent *de; + struct dirent *dent; struct stat st; int r; @@ -183,8 +183,8 @@ int import_mangle_os_tree(const char *path) { return log_error_errno(r, "Failed to open directory '%s': %m", path); errno = 0; - de = readdir_no_dot(d); - if (!de) { + dent = readdir_no_dot(d); + if (!dent) { if (errno != 0) return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path); @@ -192,13 +192,13 @@ int import_mangle_os_tree(const char *path) { return 0; } - child = strdup(de->d_name); + child = strdup(dent->d_name); if (!child) return log_oom(); errno = 0; - de = readdir_no_dot(d); - if (de) { + dent = readdir_no_dot(d); + if (dent) { if (errno != 0) return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path); diff --git a/src/import/pull-common.c b/src/import/pull-common.c index d0d0c85adc..028bae82a8 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -59,7 +59,6 @@ int pull_find_old_etags( } _cleanup_strv_free_ char **ans = NULL; - struct dirent *de; FOREACH_DIRENT_ALL(de, d, return -errno) { _cleanup_free_ char *u = NULL; diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index c04801d9fc..a244d9c5e7 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -90,7 +90,6 @@ static int determine_path_usage( uint64_t *ret_free) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; struct statvfs ss; assert(s); diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index 3ae80ab942..4f1719b3ed 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -350,7 +350,6 @@ static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator, const char *basedir, const char *subdir1, const char *subdir2) { _cleanup_closedir_ DIR *dir = NULL; char *path; - struct dirent *dent; int r = 0; assert(enumerator); @@ -369,18 +368,18 @@ static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator, /* this is necessarily racey, so ignore missing directories */ return (errno == ENOENT && (subdir1 || subdir2)) ? 0 : -errno; - FOREACH_DIRENT_ALL(dent, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; - char syspath[strlen(path) + 1 + strlen(dent->d_name) + 1]; + char syspath[strlen(path) + 1 + strlen(de->d_name) + 1]; int initialized, k; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; - if (!match_sysname(enumerator, dent->d_name)) + if (!match_sysname(enumerator, de->d_name)) continue; - (void) sprintf(syspath, "%s%s", path, dent->d_name); + (void) sprintf(syspath, "%s%s", path, de->d_name); k = sd_device_new_from_syspath(&device, syspath); if (k < 0) { @@ -461,7 +460,6 @@ static bool match_subsystem(sd_device_enumerator *enumerator, const char *subsys static int enumerator_scan_dir(sd_device_enumerator *enumerator, const char *basedir, const char *subdir, const char *subsystem) { _cleanup_closedir_ DIR *dir = NULL; char *path; - struct dirent *dent; int r = 0; path = strjoina("/sys/", basedir); @@ -472,16 +470,16 @@ static int enumerator_scan_dir(sd_device_enumerator *enumerator, const char *bas log_debug("sd-device-enumerator: Scanning %s", path); - FOREACH_DIRENT_ALL(dent, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) { int k; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; - if (!match_subsystem(enumerator, subsystem ? : dent->d_name)) + if (!match_subsystem(enumerator, subsystem ? : de->d_name)) continue; - k = enumerator_scan_dir_and_add_devices(enumerator, basedir, dent->d_name, subdir); + k = enumerator_scan_dir_and_add_devices(enumerator, basedir, de->d_name, subdir); if (k < 0) r = k; } @@ -492,7 +490,6 @@ static int enumerator_scan_dir(sd_device_enumerator *enumerator, const char *bas static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const char *tag) { _cleanup_closedir_ DIR *dir = NULL; char *path; - struct dirent *dent; int r = 0; assert(enumerator); @@ -509,15 +506,15 @@ static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const c /* TODO: filter away subsystems? */ - FOREACH_DIRENT_ALL(dent, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *subsystem, *sysname; int k; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; - k = sd_device_new_from_device_id(&device, dent->d_name); + k = sd_device_new_from_device_id(&device, de->d_name); if (k < 0) { if (k != -ENODEV) /* this is necessarily racy, so ignore missing devices */ @@ -625,24 +622,23 @@ static int parent_add_child(sd_device_enumerator *enumerator, const char *path) static int parent_crawl_children(sd_device_enumerator *enumerator, const char *path, unsigned maxdepth) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; int r = 0; dir = opendir(path); if (!dir) return log_debug_errno(errno, "sd-device-enumerator: Failed to open parent directory %s: %m", path); - FOREACH_DIRENT_ALL(dent, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) { _cleanup_free_ char *child = NULL; int k; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; - if (dent->d_type != DT_DIR) + if (de->d_type != DT_DIR) continue; - child = path_join(path, dent->d_name); + child = path_join(path, de->d_name); if (!child) return -ENOMEM; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index e594d5fbe4..c348bd8f0f 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -1747,7 +1747,6 @@ _public_ const char *sd_device_get_property_next(sd_device *device, const char * static int device_sysattrs_read_all_internal(sd_device *device, const char *subdir) { _cleanup_free_ char *path_dir = NULL; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; const char *syspath; int r; @@ -1779,33 +1778,33 @@ static int device_sysattrs_read_all_internal(sd_device *device, const char *subd if (!dir) return -errno; - FOREACH_DIRENT_ALL(dent, dir, return -errno) { + FOREACH_DIRENT_ALL(de, dir, return -errno) { _cleanup_free_ char *path = NULL, *p = NULL; struct stat statbuf; - if (dot_or_dot_dot(dent->d_name)) + if (dot_or_dot_dot(de->d_name)) continue; /* only handle symlinks, regular files, and directories */ - if (!IN_SET(dent->d_type, DT_LNK, DT_REG, DT_DIR)) + if (!IN_SET(de->d_type, DT_LNK, DT_REG, DT_DIR)) continue; if (subdir) { - p = path_join(subdir, dent->d_name); + p = path_join(subdir, de->d_name); if (!p) return -ENOMEM; } - if (dent->d_type == DT_DIR) { + if (de->d_type == DT_DIR) { /* read subdirectory */ - r = device_sysattrs_read_all_internal(device, p ?: dent->d_name); + r = device_sysattrs_read_all_internal(device, p ?: de->d_name); if (r < 0) return r; continue; } - path = path_join(syspath, p ?: dent->d_name); + path = path_join(syspath, p ?: de->d_name); if (!path) return -ENOMEM; @@ -1815,7 +1814,7 @@ static int device_sysattrs_read_all_internal(sd_device *device, const char *subd if (!(statbuf.st_mode & S_IRUSR)) continue; - r = set_put_strdup(&device->sysattrs, p ?: dent->d_name); + r = set_put_strdup(&device->sysattrs, p ?: de->d_name); if (r < 0) return r; } diff --git a/src/libsystemd/sd-journal/journal-vacuum.c b/src/libsystemd/sd-journal/journal-vacuum.c index 889caa1905..8296448ed5 100644 --- a/src/libsystemd/sd-journal/journal-vacuum.c +++ b/src/libsystemd/sd-journal/journal-vacuum.c @@ -127,7 +127,6 @@ int journal_directory_vacuum( _cleanup_closedir_ DIR *d = NULL; struct vacuum_info *list = NULL; usec_t retention_limit = 0; - struct dirent *de; int r; assert(directory); diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index b3f14cc548..efaa2bf501 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -1560,14 +1560,11 @@ static int directory_open(sd_journal *j, const char *path, DIR **ret) { static int add_directory(sd_journal *j, const char *prefix, const char *dirname); static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) { - struct dirent *de; - assert(j); assert(m); assert(d); FOREACH_DIRENT_ALL(de, d, goto fail) { - if (dirent_is_journal_file(de)) (void) add_file_by_name(j, m->path, de->d_name); @@ -1576,7 +1573,6 @@ static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) { } return; - fail: log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path); } diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index 4a35e61425..00129ff77d 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -774,7 +774,6 @@ _public_ int sd_get_sessions(char ***sessions) { _public_ int sd_get_uids(uid_t **users) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; unsigned n = 0; _cleanup_free_ uid_t *l = NULL; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 331dcd2a05..cc5214c2eb 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1370,9 +1370,7 @@ static int flush_devices(Manager *m) { if (!d) { if (errno != ENOENT) log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m"); - } else { - struct dirent *de; - + } else FOREACH_DIRENT_ALL(de, d, break) { if (!dirent_is_file(de)) continue; @@ -1386,7 +1384,6 @@ static int flush_devices(Manager *m) { if (unlinkat(dirfd(d), de->d_name, 0) < 0) log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); } - } return trigger_device(m, NULL); } diff --git a/src/nspawn/nspawn-patch-uid.c b/src/nspawn/nspawn-patch-uid.c index f47f827361..1535d19bbb 100644 --- a/src/nspawn/nspawn-patch-uid.c +++ b/src/nspawn/nspawn-patch-uid.c @@ -313,8 +313,6 @@ static int recurse_fd(int fd, bool donate_fd, const struct stat *st, uid_t shift goto read_only; if (S_ISDIR(st->st_mode)) { - struct dirent *de; - if (!donate_fd) { int copy; diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 6b32ee4cf0..615431ebb7 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -1536,7 +1536,6 @@ void manager_reset_server_features(Manager *m) { void manager_cleanup_saved_user(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert(m); diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index db50505c9a..a0c60be26e 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -103,7 +103,6 @@ int block_get_originating(dev_t dt, dev_t *ret) { _cleanup_free_ char *t = NULL; char p[SYS_BLOCK_PATH_MAX("/slaves")]; _cleanup_free_ char *first_found = NULL; - struct dirent *de; const char *q; dev_t devt; int r; diff --git a/src/shared/chown-recursive.c b/src/shared/chown-recursive.c index 50848715e3..7c9a3050b4 100644 --- a/src/shared/chown-recursive.c +++ b/src/shared/chown-recursive.c @@ -52,7 +52,6 @@ static int chown_recursive_internal( _cleanup_closedir_ DIR *d = NULL; bool changed = false; - struct dirent *de; int r; assert(fd >= 0); diff --git a/src/shared/clean-ipc.c b/src/shared/clean-ipc.c index 497b0884d4..bbb343f3d3 100644 --- a/src/shared/clean-ipc.c +++ b/src/shared/clean-ipc.c @@ -219,7 +219,6 @@ static int clean_sysvipc_msg(uid_t delete_uid, gid_t delete_gid, bool rm) { } static int clean_posix_shm_internal(const char *dirname, DIR *dir, uid_t uid, gid_t gid, bool rm) { - struct dirent *de; int ret = 0, r; assert(dir); @@ -315,7 +314,6 @@ static int clean_posix_shm(uid_t uid, gid_t gid, bool rm) { static int clean_posix_mq(uid_t uid, gid_t gid, bool rm) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *de; int ret = 0; dir = opendir("/dev/mqueue"); diff --git a/src/shared/copy.c b/src/shared/copy.c index bb3ac8a3f8..1ace40424e 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -789,7 +789,6 @@ static int fd_copy_directory( _cleanup_close_ int fdf = -1, fdt = -1; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; bool exists, created; int r; diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index 9c07cda8ee..268d910214 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -543,7 +543,6 @@ int image_discover( NULSTR_FOREACH(path, image_search_path[class]) { _cleanup_free_ char *resolved = NULL; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; r = chase_symlinks_and_opendir(path, root, CHASE_PREFIX_ROOT, &resolved, &d); if (r == -ENOENT) diff --git a/src/shared/killall.c b/src/shared/killall.c index 869955a58f..343c2dc42c 100644 --- a/src/shared/killall.c +++ b/src/shared/killall.c @@ -188,7 +188,6 @@ static int wait_for_children(Set *pids, sigset_t *mask, usec_t timeout) { static int killall(int sig, Set *pids, bool send_sighup) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *d; int n_killed = 0; /* Send the specified signal to all remaining processes, if not excluded by ignore_proc(). @@ -198,14 +197,14 @@ static int killall(int sig, Set *pids, bool send_sighup) { if (!dir) return log_warning_errno(errno, "opendir(/proc) failed: %m"); - FOREACH_DIRENT_ALL(d, dir, break) { + FOREACH_DIRENT_ALL(de, dir, break) { pid_t pid; int r; - if (!IN_SET(d->d_type, DT_DIR, DT_UNKNOWN)) + if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN)) continue; - if (parse_pid(d->d_name, &pid) < 0) + if (parse_pid(de->d_name, &pid) < 0) continue; if (ignore_proc(pid, sig == SIGKILL && !in_initrd())) diff --git a/src/shared/rm-rf.c b/src/shared/rm-rf.c index 81ee5c37ba..fd54b2ccaf 100644 --- a/src/shared/rm-rf.c +++ b/src/shared/rm-rf.c @@ -203,7 +203,6 @@ int rm_rf_children( const struct stat *root_dev) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int ret = 0, r; assert(fd >= 0); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 4f1ce1f73f..fcab51c208 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -590,14 +590,13 @@ static int dir_cleanup( AgeBy age_by_dir) { bool deleted = false; - struct dirent *dent; int r = 0; - FOREACH_DIRENT_ALL(dent, d, break) { + FOREACH_DIRENT_ALL(de, d, break) { _cleanup_free_ char *sub_path = NULL; nsec_t atime_nsec, mtime_nsec, ctime_nsec, btime_nsec; - if (dot_or_dot_dot(dent->d_name)) + if (dot_or_dot_dot(de->d_name)) continue; /* If statx() is supported, use it. It's preferable over fstatat() since it tells us @@ -614,7 +613,7 @@ static int dir_cleanup( STRUCT_STATX_DEFINE(sx); r = statx_fallback( - dirfd(d), dent->d_name, + dirfd(d), de->d_name, AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, STATX_TYPE|STATX_MODE|STATX_UID|STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_BTIME, &sx); @@ -623,14 +622,14 @@ static int dir_cleanup( if (r < 0) { /* FUSE, NFS mounts, SELinux might return EACCES */ r = log_full_errno(errno == EACCES ? LOG_DEBUG : LOG_ERR, errno, - "statx(%s/%s) failed: %m", p, dent->d_name); + "statx(%s/%s) failed: %m", p, de->d_name); continue; } if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) { /* Yay, we have the mount point API, use it */ if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT)) { - log_debug("Ignoring \"%s/%s\": different mount points.", p, dent->d_name); + log_debug("Ignoring \"%s/%s\": different mount points.", p, de->d_name); continue; } } else { @@ -638,7 +637,7 @@ static int dir_cleanup( * back to traditional stx_dev checking. */ if (sx.stx_dev_major != rootdev_major || sx.stx_dev_minor != rootdev_minor) { - log_debug("Ignoring \"%s/%s\": different filesystem.", p, dent->d_name); + log_debug("Ignoring \"%s/%s\": different filesystem.", p, de->d_name); continue; } @@ -648,11 +647,11 @@ static int dir_cleanup( if (S_ISDIR(sx.stx_mode)) { int q; - q = fd_is_mount_point(dirfd(d), dent->d_name, 0); + q = fd_is_mount_point(dirfd(d), de->d_name, 0); if (q < 0) - log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, dent->d_name); + log_debug_errno(q, "Failed to determine whether \"%s/%s\" is a mount point, ignoring: %m", p, de->d_name); else if (q > 0) { - log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, dent->d_name); + log_debug("Ignoring \"%s/%s\": different mount of the same filesystem.", p, de->d_name); continue; } } @@ -663,7 +662,7 @@ static int dir_cleanup( ctime_nsec = FLAGS_SET(sx.stx_mask, STATX_CTIME) ? load_statx_timestamp_nsec(&sx.stx_ctime) : 0; btime_nsec = FLAGS_SET(sx.stx_mask, STATX_BTIME) ? load_statx_timestamp_nsec(&sx.stx_btime) : 0; - sub_path = path_join(p, dent->d_name); + sub_path = path_join(p, de->d_name); if (!sub_path) { r = log_oom(); goto finish; @@ -684,7 +683,7 @@ static int dir_cleanup( _cleanup_closedir_ DIR *sub_dir = NULL; if (mountpoint && - streq(dent->d_name, "lost+found") && + streq(de->d_name, "lost+found") && sx.stx_uid == 0) { log_debug("Ignoring directory \"%s\".", sub_path); continue; @@ -695,7 +694,7 @@ static int dir_cleanup( else { int q; - sub_dir = xopendirat_nomod(dirfd(d), dent->d_name); + sub_dir = xopendirat_nomod(dirfd(d), de->d_name); if (!sub_dir) { if (errno != ENOENT) r = log_warning_errno(errno, "Opening directory \"%s\" failed, ignoring: %m", sub_path); @@ -737,7 +736,7 @@ static int dir_cleanup( continue; log_debug("Removing directory \"%s\".", sub_path); - if (unlinkat(dirfd(d), dent->d_name, AT_REMOVEDIR) < 0) + if (unlinkat(dirfd(d), de->d_name, AT_REMOVEDIR) < 0) if (!IN_SET(errno, ENOENT, ENOTEMPTY)) r = log_warning_errno(errno, "Failed to remove directory \"%s\", ignoring: %m", sub_path); @@ -752,7 +751,7 @@ static int dir_cleanup( if (mountpoint && S_ISREG(sx.stx_mode) && sx.stx_uid == 0 && - STR_IN_SET(dent->d_name, + STR_IN_SET(de->d_name, ".journal", "aquota.user", "aquota.group")) { @@ -783,7 +782,7 @@ static int dir_cleanup( continue; log_debug("Removing \"%s\".", sub_path); - if (unlinkat(dirfd(d), dent->d_name, 0) < 0) + if (unlinkat(dirfd(d), de->d_name, 0) < 0) if (errno != ENOENT) r = log_warning_errno(errno, "Failed to remove \"%s\", ignoring: %m", sub_path); @@ -1920,7 +1919,6 @@ static int item_do(Item *i, int fd, const char *path, fdaction_t action) { if (S_ISDIR(st.st_mode)) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; /* The passed 'fd' was opened with O_PATH. We need to convert it into a 'regular' fd before * reading the directory content. */ diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c index 4d16591750..c1f9f232ae 100644 --- a/src/udev/udev-builtin-net_id.c +++ b/src/udev/udev-builtin-net_id.c @@ -97,7 +97,6 @@ static int get_virtfn_info(sd_device *pcidev, sd_device **ret_physfn_pcidev, cha _cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL; const char *physfn_syspath, *syspath; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; int r; assert(pcidev); @@ -123,15 +122,15 @@ static int get_virtfn_info(sd_device *pcidev, sd_device **ret_physfn_pcidev, cha if (!dir) return -errno; - FOREACH_DIRENT_ALL(dent, dir, break) { + FOREACH_DIRENT_ALL(de, dir, break) { _cleanup_free_ char *virtfn_link_file = NULL, *virtfn_pci_syspath = NULL; const char *n; - n = startswith(dent->d_name, "virtfn"); + n = startswith(de->d_name, "virtfn"); if (!n) continue; - virtfn_link_file = path_join(physfn_syspath, dent->d_name); + virtfn_link_file = path_join(physfn_syspath, de->d_name); if (!virtfn_link_file) return -ENOMEM; @@ -390,8 +389,6 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) { hotplug_slot_dev = names->pcidev; while (hotplug_slot_dev) { - struct dirent *dent; - r = parse_hotplug_slot_from_function_id(hotplug_slot_dev, slots, &hotplug_slot); if (r < 0) return 0; @@ -404,20 +401,20 @@ static int dev_pci_slot(sd_device *dev, const LinkInfo *info, NetNames *names) { if (r < 0) return log_device_debug_errno(hotplug_slot_dev, r, "Failed to get sysname: %m"); - FOREACH_DIRENT_ALL(dent, dir, break) { + FOREACH_DIRENT_ALL(de, dir, break) { _cleanup_free_ char *address = NULL; char str[PATH_MAX]; uint32_t i; - if (dot_or_dot_dot(dent->d_name)) + if (dot_or_dot_dot(de->d_name)) continue; - r = safe_atou32(dent->d_name, &i); + r = safe_atou32(de->d_name, &i); if (r < 0 || i <= 0) continue; /* match slot address with device by stripping the function */ - if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) && + if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, de->d_name) && read_one_line_file(str, &address) >= 0 && startswith(sysname, address)) { hotplug_slot = i; diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 6fdd3eceea..ae92e45205 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -309,7 +309,6 @@ static sd_device *handle_scsi_default(sd_device *parent, char **path) { int host, bus, target, lun; const char *name, *base, *pos; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; int basenum = -1; assert(parent); @@ -352,16 +351,16 @@ static sd_device *handle_scsi_default(sd_device *parent, char **path) { if (!dir) return NULL; - FOREACH_DIRENT_ALL(dent, dir, break) { + FOREACH_DIRENT_ALL(de, dir, break) { unsigned i; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; - if (!IN_SET(dent->d_type, DT_DIR, DT_LNK)) + if (!IN_SET(de->d_type, DT_DIR, DT_LNK)) continue; - if (!startswith(dent->d_name, "host")) + if (!startswith(de->d_name, "host")) continue; - if (safe_atou_full(&dent->d_name[4], 10, &i) < 0) + if (safe_atou_full(&de->d_name[4], 10, &i) < 0) continue; /* * find the smallest number; the host really needs to export its diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index 760c3a4448..1824ccef6a 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -111,7 +111,6 @@ static int node_symlink(sd_device *dev, const char *node, const char *slink) { static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir, char **ret) { _cleanup_closedir_ DIR *dir = NULL; _cleanup_free_ char *target = NULL; - struct dirent *dent; int r, priority = 0; const char *id; @@ -153,18 +152,18 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir, if (r < 0) return r; - FOREACH_DIRENT_ALL(dent, dir, break) { + FOREACH_DIRENT_ALL(de, dir, break) { _cleanup_free_ char *path = NULL, *buf = NULL; int tmp_prio; - if (dent->d_name[0] == '.') + if (de->d_name[0] == '.') continue; /* skip ourself */ - if (streq(dent->d_name, id)) + if (streq(de->d_name, id)) continue; - path = path_join(stackdir, dent->d_name); + path = path_join(stackdir, de->d_name); if (!path) return -ENOMEM; @@ -197,7 +196,7 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir, /* Old format. The devnode and priority must be obtained from uevent and * udev database files. */ - if (sd_device_new_from_device_id(&tmp_dev, dent->d_name) < 0) + if (sd_device_new_from_device_id(&tmp_dev, de->d_name) < 0) continue; if (device_get_devlink_priority(tmp_dev, &tmp_prio) < 0) diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 693c743c57..1a384d6b38 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1494,7 +1494,6 @@ static int import_parent_into_properties(sd_device *dev, const char *filter) { static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; char buf[UDEV_PATH_SIZE], *p; const char *tail; size_t len, size; @@ -1516,11 +1515,11 @@ static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { if (!dir) return -errno; - FOREACH_DIRENT_ALL(dent, dir, break) { - if (dent->d_name[0] == '.') + FOREACH_DIRENT_ALL(de, dir, break) { + if (de->d_name[0] == '.') continue; - strscpyl(p, size, dent->d_name, tail, NULL); + strscpyl(p, size, de->d_name, tail, NULL); if (faccessat(dirfd(dir), p, F_OK, 0) < 0) continue; diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index 87c4f09928..a8b7d2d2d1 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -20,7 +20,6 @@ #define MIN_RANDOM_DELAY ( 10 * USEC_PER_MSEC) int udev_watch_restore(int inotify_fd) { - struct dirent *ent; DIR *dir; int r; diff --git a/src/udev/udevadm-info.c b/src/udev/udevadm-info.c index 84f7794e86..133be80b70 100644 --- a/src/udev/udevadm-info.c +++ b/src/udev/udevadm-info.c @@ -224,8 +224,6 @@ static int export_devices(void) { } static void cleanup_dir(DIR *dir, mode_t mask, int depth) { - struct dirent *dent; - if (depth <= 0) return; From af3b864d76cf916a026b7877099791811b348fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 7 Dec 2021 12:52:58 +0100 Subject: [PATCH 2/6] Define FOREACH_DIRENT through FOREACH_DIRENT_ALL As in the previous commit, 'de' is used as the iterator variable name. --- src/basic/conf-files.c | 1 - src/basic/dirent-util.h | 17 ++++++----------- src/basic/fd-util.c | 1 - src/basic/locale-util.c | 7 +++---- src/basic/os-util.c | 1 - src/basic/util.c | 1 - src/basic/virt.c | 7 +++---- src/boot/bootctl.c | 3 --- src/core/smack-setup.c | 3 --- src/coredump/coredump-vacuum.c | 1 - src/coredump/coredump.c | 9 ++++----- src/home/homed-manager.c | 2 -- src/journal/journald-stream.c | 1 - src/login/logind.c | 5 ----- src/machine/machined.c | 1 - src/mount/mount-tool.c | 1 - src/portable/portable.c | 3 --- src/pstore/pstore.c | 1 - src/shared/bootspec.c | 1 - src/shared/devnode-acl.c | 5 ++--- src/shared/efi-loader.c | 1 - src/shared/fdset.c | 1 - src/shared/install.c | 5 ----- src/shared/numa-util.c | 1 - src/shared/userdb.c | 1 - src/sysv-generator/sysv-generator.c | 2 -- src/test/test-cgroup-util.c | 1 - src/test/test-process-util.c | 1 - .../tty-ask-password-agent.c | 1 - src/userdb/userdbctl.c | 1 - .../xdg-autostart-generator.c | 1 - 31 files changed, 18 insertions(+), 69 deletions(-) diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c index 9f0c97b691..287428b561 100644 --- a/src/basic/conf-files.c +++ b/src/basic/conf-files.c @@ -30,7 +30,6 @@ static int files_add( _cleanup_closedir_ DIR *dir = NULL; const char *dirpath; - struct dirent *de; int r; assert(h); diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h index 2dc5dae1e1..5fde9043a3 100644 --- a/src/basic/dirent-util.h +++ b/src/basic/dirent-util.h @@ -14,17 +14,6 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pu struct dirent *readdir_ensure_type(DIR *d); struct dirent *readdir_no_dot(DIR *dirp); -#define FOREACH_DIRENT(de, d, on_error) \ - for (de = readdir_ensure_type(d);; de = readdir_ensure_type(d)) \ - if (!de) { \ - if (errno > 0) { \ - on_error; \ - } \ - break; \ - } else if (hidden_or_backup_file((de)->d_name)) \ - continue; \ - else - #define FOREACH_DIRENT_ALL(de, d, on_error) \ for (struct dirent *(de) = readdir_ensure_type(d);; (de) = readdir_ensure_type(d)) \ if (!de) { \ @@ -34,6 +23,12 @@ struct dirent *readdir_no_dot(DIR *dirp); break; \ } else +#define FOREACH_DIRENT(de, d, on_error) \ + FOREACH_DIRENT_ALL(de, d, on_error) \ + if (hidden_or_backup_file((de)->d_name)) \ + continue; \ + else + /* Maximum space one dirent structure might require at most */ #define DIRENT_SIZE_MAX CONST_MAX(sizeof(struct dirent), offsetof(struct dirent, d_name) + NAME_MAX + 1) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 2334a30e3d..3866e87675 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -300,7 +300,6 @@ int close_all_fds_without_malloc(const int except[], size_t n_except) { int close_all_fds(const int except[], size_t n_except) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(n_except == 0 || except); diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 9a7c7795e7..7f1a2f15f7 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -158,20 +158,19 @@ static int add_locales_from_archive(Set *locales) { static int add_locales_from_libdir (Set *locales) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int r; dir = opendir("/usr/lib/locale"); if (!dir) return errno == ENOENT ? 0 : -errno; - FOREACH_DIRENT(entry, dir, return -errno) { + FOREACH_DIRENT(de, dir, return -errno) { char *z; - if (entry->d_type != DT_DIR) + if (de->d_type != DT_DIR) continue; - z = normalize_locale(entry->d_name); + z = normalize_locale(de->d_name); if (!z) return -ENOMEM; diff --git a/src/basic/os-util.c b/src/basic/os-util.c index 69ea39345a..75c8500e51 100644 --- a/src/basic/os-util.c +++ b/src/basic/os-util.c @@ -89,7 +89,6 @@ int open_extension_release(const char *root, const char *extension, char **ret_p return log_debug_errno(r, "Cannot open %s/usr/lib/extension-release.d/, ignoring: %m", root); r = -ENOENT; - struct dirent *de; FOREACH_DIRENT(de, extension_release_dir, return -errno) { int k; diff --git a/src/basic/util.c b/src/basic/util.c index 43e74b773f..3aecb22fc4 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -118,7 +118,6 @@ void in_initrd_force(bool value) { int on_ac_power(void) { bool found_offline = false, found_online = false; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; d = opendir("/sys/class/power_supply"); diff --git a/src/basic/virt.c b/src/basic/virt.c index 9b7dbdc205..617439708d 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -98,7 +98,6 @@ static int detect_vm_device_tree(void) { r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype); if (r == -ENOENT) { _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; if (access("/proc/device-tree/ibm,partition-name", F_OK) == 0 && access("/proc/device-tree/hmc-managed?", F_OK) == 0 && @@ -114,9 +113,9 @@ static int detect_vm_device_tree(void) { return -errno; } - FOREACH_DIRENT(dent, dir, return -errno) - if (strstr(dent->d_name, "fw-cfg")) { - log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name); + FOREACH_DIRENT(de, dir, return -errno) + if (strstr(de->d_name, "fw-cfg")) { + log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", de->d_name); return VIRTUALIZATION_QEMU; } diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 51d304ea04..1a940f9462 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -184,7 +184,6 @@ finish: static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *p; int c = 0, r; @@ -658,7 +657,6 @@ static int copy_one_file(const char *esp_path, const char *name, bool force) { } static int install_binaries(const char *esp_path, bool force) { - struct dirent *de; _cleanup_closedir_ DIR *d = NULL; int r = 0; @@ -850,7 +848,6 @@ static int install_variables(const char *esp_path, static int remove_boot_efi(const char *esp_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *p; int r, c = 0; diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 5b2671ff40..f88cb80834 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -51,7 +51,6 @@ static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *s static int write_access2_rules(const char *srcdir) { _cleanup_close_ int load2_fd = -1, change_fd = -1; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); @@ -124,7 +123,6 @@ static int write_access2_rules(const char *srcdir) { static int write_cipso2_rules(const char *srcdir) { _cleanup_close_ int cipso2_fd = -1; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); @@ -184,7 +182,6 @@ static int write_cipso2_rules(const char *srcdir) { static int write_netlabel_rules(const char *srcdir) { _cleanup_fclose_ FILE *dst = NULL; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *entry; int dfd = -1, r = 0; dst = fopen("/sys/fs/smackfs/netlabel", "we"); diff --git a/src/coredump/coredump-vacuum.c b/src/coredump/coredump-vacuum.c index 0c08ab21dc..dcf9cc03cd 100644 --- a/src/coredump/coredump-vacuum.c +++ b/src/coredump/coredump-vacuum.c @@ -142,7 +142,6 @@ int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) { for (;;) { _cleanup_(vacuum_candidate_hashmap_freep) Hashmap *h = NULL; VacuumCandidate *worst = NULL; - struct dirent *de; uint64_t sum = 0; rewinddir(d); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index cb74b34aff..9e0dafb1ce 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -580,7 +580,6 @@ static int compose_open_fds(pid_t pid, char **open_fds) { _cleanup_free_ char *buffer = NULL; _cleanup_fclose_ FILE *stream = NULL; const char *fddelim = "", *path; - struct dirent *dent = NULL; size_t size = 0; int r; @@ -600,20 +599,20 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (!stream) return -ENOMEM; - FOREACH_DIRENT(dent, proc_fd_dir, return -errno) { + FOREACH_DIRENT(de, proc_fd_dir, return -errno) { _cleanup_fclose_ FILE *fdinfo = NULL; _cleanup_free_ char *fdname = NULL; _cleanup_close_ int fd = -1; - r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname); + r = readlinkat_malloc(dirfd(proc_fd_dir), de->d_name, &fdname); if (r < 0) return r; - fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname); + fprintf(stream, "%s%s:%s\n", fddelim, de->d_name, fdname); fddelim = "\n"; /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */ - fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); + fd = openat(proc_fdinfo_fd, de->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY); if (fd < 0) continue; diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c index d49fcedb57..215b4567a9 100644 --- a/src/home/homed-manager.c +++ b/src/home/homed-manager.c @@ -447,7 +447,6 @@ unlink_this_file: static int manager_enumerate_records(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert(m); @@ -919,7 +918,6 @@ static int manager_assess_image( int manager_enumerate_images(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert(m); diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index d94b1df02d..19f0799914 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -848,7 +848,6 @@ static int stdout_stream_restore(Server *s, const char *fname, int fd) { int server_restore_streams(Server *s, FDSet *fds) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; const char *path; int r; diff --git a/src/login/logind.c b/src/login/logind.c index 57a8604b7f..52b1d95034 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -244,7 +244,6 @@ static int manager_enumerate_buttons(Manager *m) { static int manager_enumerate_seats(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); @@ -286,7 +285,6 @@ static int manager_enumerate_seats(Manager *m) { static int manager_enumerate_linger_users(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); @@ -315,7 +313,6 @@ static int manager_enumerate_linger_users(Manager *m) { static int manager_enumerate_users(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r, k; assert(m); @@ -474,7 +471,6 @@ static int manager_attach_fds(Manager *m) { static int manager_enumerate_sessions(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0, k; assert(m); @@ -516,7 +512,6 @@ static int manager_enumerate_sessions(Manager *m) { static int manager_enumerate_inhibitors(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(m); diff --git a/src/machine/machined.c b/src/machine/machined.c index 6ffa4191ec..a433afb1a6 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -136,7 +136,6 @@ static int manager_add_host_machine(Manager *m) { static int manager_enumerate_machines(Manager *m) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; assert(m); diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index 412ef7813a..b0ea45debc 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -777,7 +777,6 @@ static int find_mount_points(const char *what, char ***list) { static int find_loop_device(const char *backing_file, char **loop_dev) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; _cleanup_free_ char *l = NULL; assert(backing_file); diff --git a/src/portable/portable.c b/src/portable/portable.c index 8b088f3e32..be311f94c4 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -244,7 +244,6 @@ static int extract_now( STRV_FOREACH(i, paths.search_path) { _cleanup_free_ char *resolved = NULL; _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; r = chase_symlinks_and_opendir(*i, where, 0, &resolved, &d); if (r < 0) { @@ -1492,7 +1491,6 @@ int portable_detach( _cleanup_set_free_ Set *unit_files = NULL, *markers = NULL; _cleanup_closedir_ DIR *d = NULL; const char *where, *item; - struct dirent *de; int ret = 0; int r; @@ -1662,7 +1660,6 @@ static int portable_get_state_internal( _cleanup_set_free_ Set *unit_files = NULL; _cleanup_closedir_ DIR *d = NULL; const char *where; - struct dirent *de; int r; assert(name_or_path); diff --git a/src/pstore/pstore.c b/src/pstore/pstore.c index 81538d9cb3..d820ceb9f6 100644 --- a/src/pstore/pstore.c +++ b/src/pstore/pstore.c @@ -323,7 +323,6 @@ static void process_dmesg_files(PStoreList *list) { static int list_files(PStoreList *list, const char *sourcepath) { _cleanup_(closedirp) DIR *dirp = NULL; - struct dirent *de; int r; dirp = opendir(sourcepath); diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 298f3df3c7..0076092c2a 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -488,7 +488,6 @@ static int boot_entries_find_unified( size_t *n_entries) { _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; int r; assert(root); diff --git a/src/shared/devnode-acl.c b/src/shared/devnode-acl.c index 9ea89ee920..89ff566832 100644 --- a/src/shared/devnode-acl.c +++ b/src/shared/devnode-acl.c @@ -166,7 +166,6 @@ int devnode_acl_all(const char *seat, _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; _cleanup_set_free_free_ Set *nodes = NULL; _cleanup_closedir_ DIR *dir = NULL; - struct dirent *dent; sd_device *d; char *n; int r; @@ -218,11 +217,11 @@ int devnode_acl_all(const char *seat, * these devices are not known to the kernel at this moment */ dir = opendir("/run/udev/static_node-tags/uaccess"); if (dir) { - FOREACH_DIRENT(dent, dir, return -errno) { + FOREACH_DIRENT(de, dir, return -errno) { _cleanup_free_ char *unescaped_devname = NULL; ssize_t l; - l = cunescape(dent->d_name, UNESCAPE_RELAX, &unescaped_devname); + l = cunescape(de->d_name, UNESCAPE_RELAX, &unescaped_devname); if (l < 0) return l; diff --git a/src/shared/efi-loader.c b/src/shared/efi-loader.c index f5076fc02c..b14a2c32aa 100644 --- a/src/shared/efi-loader.c +++ b/src/shared/efi-loader.c @@ -485,7 +485,6 @@ static int cmp_uint16(const uint16_t *a, const uint16_t *b) { int efi_get_boot_options(uint16_t **options) { _cleanup_closedir_ DIR *dir = NULL; _cleanup_free_ uint16_t *list = NULL; - struct dirent *de; int count = 0; assert(options); diff --git a/src/shared/fdset.c b/src/shared/fdset.c index 679e4aad22..183fa239b6 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -112,7 +112,6 @@ int fdset_remove(FDSet *s, int fd) { int fdset_new_fill(FDSet **_s) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; FDSet *s; diff --git a/src/shared/install.c b/src/shared/install.c index 6e77a72bde..8f1af755fa 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -549,7 +549,6 @@ static int remove_marked_symlinks_fd( size_t *n_changes) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; assert(remove_symlinks_to); @@ -729,7 +728,6 @@ static int find_symlinks_in_directory( const char *config_path, bool *same_name_link) { - struct dirent *de; int r = 0; FOREACH_DIRENT(de, dir, return -errno) { @@ -814,7 +812,6 @@ static int find_symlinks( bool *same_name_link) { _cleanup_closedir_ DIR *config_dir = NULL; - struct dirent *de; int r = 0; assert(i); @@ -3369,7 +3366,6 @@ int unit_file_preset_all( STRV_FOREACH(i, paths.search_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*i); if (!d) { @@ -3434,7 +3430,6 @@ int unit_file_get_list( STRV_FOREACH(dirname, paths.search_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*dirname); if (!d) { diff --git a/src/shared/numa-util.c b/src/shared/numa-util.c index 644b0bde5b..4f757f0b55 100644 --- a/src/shared/numa-util.c +++ b/src/shared/numa-util.c @@ -128,7 +128,6 @@ int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) { static int numa_max_node(void) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r, max_node = 0; d = opendir("/sys/devices/system/node"); diff --git a/src/shared/userdb.c b/src/shared/userdb.c index 5c2b3ddb8f..0eddd382e6 100644 --- a/src/shared/userdb.c +++ b/src/shared/userdb.c @@ -405,7 +405,6 @@ static int userdb_start_query( _cleanup_(strv_freep) char **except = NULL, **only = NULL; _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; const char *e; int r, ret = 0; diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 1947664457..e9976540b5 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -718,7 +718,6 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) { STRV_FOREACH(path, sysvinit_path) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*path); if (!d) { @@ -805,7 +804,6 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic for (unsigned i = 0; i < ELEMENTSOF(rcnd_table); i ++) { _cleanup_closedir_ DIR *d = NULL; _cleanup_free_ char *path = NULL; - struct dirent *de; path = path_join(*p, rcnd_table[i].path); if (!path) { diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 621e68829e..93b114b3fd 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -167,7 +167,6 @@ TEST(get_paths, .sd_booted = true) { TEST(proc) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r; d = opendir("/proc"); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index ab093a7457..06a640b1cc 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -135,7 +135,6 @@ static void test_get_process_cmdline_one(pid_t pid) { TEST(get_process_cmdline) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; assert_se(d = opendir("/proc")); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 56110ba375..54a03af082 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -295,7 +295,6 @@ static int wall_tty_block(void) { static int process_password_files(void) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; int r = 0; d = opendir("/run/systemd/ask-password"); diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 4dac6b0e44..c2acd85742 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -518,7 +518,6 @@ static int display_memberships(int argc, char *argv[], void *userdata) { static int display_services(int argc, char *argv[], void *userdata) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_(closedirp) DIR *d = NULL; - struct dirent *de; int r; d = opendir("/run/systemd/userdb/"); diff --git a/src/xdg-autostart-generator/xdg-autostart-generator.c b/src/xdg-autostart-generator/xdg-autostart-generator.c index 7b441a9a12..c5c6b54fdc 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -43,7 +43,6 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { STRV_FOREACH(path, autostart_dirs) { _cleanup_closedir_ DIR *d = NULL; - struct dirent *de; d = opendir(*path); if (!d) { From b93f018f574a92c680669c35fea37be8d8f73c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 7 Dec 2021 15:39:43 +0100 Subject: [PATCH 3/6] meson: don't compile import sources four times Use a 'convenience library' to do the compilation once and then link the objects into all the files that need it. Those files are small, so this probably doesn't matter too much for speed, but has the advantage that we don't get the same error four times if something goes wrong. The library is conditionalized in the same way importd itself, because we cannot build it without the deps. --- meson.build | 12 ++++-- src/import/meson.build | 92 +++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/meson.build b/meson.build index 94bc49ed63..bdf192cfc1 100644 --- a/meson.build +++ b/meson.build @@ -2764,7 +2764,8 @@ if conf.get('ENABLE_IMPORTD') == 1 'systemd-pull', systemd_pull_sources, include_directories : includes, - link_with : [libshared], + link_with : [libshared, + lib_import_common], dependencies : [versiondep, libcurl, lib_openssl_or_gcrypt, @@ -2779,7 +2780,8 @@ if conf.get('ENABLE_IMPORTD') == 1 'systemd-import', systemd_import_sources, include_directories : includes, - link_with : [libshared], + link_with : [libshared, + lib_import_common], dependencies : [libcurl, libz, libbzip2, @@ -2792,7 +2794,8 @@ if conf.get('ENABLE_IMPORTD') == 1 'systemd-import-fs', systemd_import_fs_sources, include_directories : includes, - link_with : [libshared], + link_with : [libshared, + lib_import_common], install_rpath : rootlibexecdir, install : true, install_dir : rootlibexecdir) @@ -2801,7 +2804,8 @@ if conf.get('ENABLE_IMPORTD') == 1 'systemd-export', systemd_export_sources, include_directories : includes, - link_with : [libshared], + link_with : [libshared, + lib_import_common], dependencies : [libcurl, libz, libbzip2, diff --git a/src/import/meson.build b/src/import/meson.build index b3bc682b0b..e48e49293e 100644 --- a/src/import/meson.build +++ b/src/import/meson.build @@ -1,62 +1,52 @@ # SPDX-License-Identifier: LGPL-2.1-or-later -systemd_importd_sources = files(''' - importd.c -'''.split()) +systemd_importd_sources = files( + 'importd.c') -systemd_pull_sources = files(''' - pull.c - pull-raw.c - pull-raw.h - pull-tar.c - pull-tar.h - pull-job.c - pull-job.h - pull-common.c - pull-common.h - import-common.c - import-common.h - import-compress.c - import-compress.h - curl-util.c - curl-util.h - qcow2-util.c - qcow2-util.h -'''.split()) +systemd_pull_sources = files( + 'pull.c', + 'pull-raw.c', + 'pull-raw.h', + 'pull-tar.c', + 'pull-tar.h', + 'pull-job.c', + 'pull-job.h', + 'pull-common.c', + 'pull-common.h', + 'curl-util.c', + 'curl-util.h') -systemd_import_sources = files(''' - import.c - import-raw.c - import-raw.h - import-tar.c - import-tar.h - import-common.c - import-common.h - import-compress.c - import-compress.h - qcow2-util.c - qcow2-util.h -'''.split()) +systemd_import_sources = files( + 'import.c', + 'import-raw.c', + 'import-raw.h', + 'import-tar.c', + 'import-tar.h') -systemd_import_fs_sources = files(''' - import-fs.c - import-common.c - import-common.h -'''.split()) +systemd_import_fs_sources = files( + 'import-fs.c') -systemd_export_sources = files(''' - export.c - export-tar.c - export-tar.h - export-raw.c - export-raw.h - import-common.c - import-common.h - import-compress.c - import-compress.h -'''.split()) +systemd_export_sources = files( + 'export.c', + 'export-tar.c', + 'export-tar.h', + 'export-raw.c', + 'export-raw.h') if conf.get('ENABLE_IMPORTD') == 1 + lib_import_common = static_library( + 'import-common', + 'import-common.c', + 'import-common.h', + 'import-compress.c', + 'import-compress.h', + 'qcow2-util.c', + 'qcow2-util.h', + include_directories : includes, + dependencies : [libbzip2, + libxz, + libz]) + install_data('org.freedesktop.import1.conf', install_dir : dbuspolicydir) install_data('org.freedesktop.import1.service', From 03f7ddf0bf170c00e6d53bf039b6566113f6c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 16 Dec 2021 10:58:44 +0100 Subject: [PATCH 4/6] meson: stop building out convenience libraries by default The meson default for static_library() are: build_by_default=true, install=false. We never interact with the static libraries, and we only care about them as a stepping-stone towards the installable executables or libraries. Thus let's only build them if they are a dependency of something else we are building. While at it, let's drop install:false, since this appears to be the default. This change would have fixed the issue with lib_import_common failing to build too: we wouldn't attempt to build it. In practice this changes very little, because we generally only declare static libraries where there's something in the default target that will make use of them. But it seems to be a better pattern to set build_by_default to false. --- src/basic/meson.build | 5 +++-- src/core/meson.build | 3 ++- src/import/meson.build | 3 ++- src/journal-remote/meson.build | 2 +- src/journal/meson.build | 2 +- src/libsystemd-network/meson.build | 3 ++- src/libsystemd/meson.build | 4 ++-- src/libudev/meson.build | 3 ++- src/login/meson.build | 3 ++- src/machine/meson.build | 3 ++- src/network/meson.build | 3 ++- src/nspawn/meson.build | 3 ++- src/resolve/meson.build | 3 ++- src/shared/meson.build | 3 ++- src/timesync/meson.build | 3 ++- src/udev/meson.build | 4 +++- 16 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/basic/meson.build b/src/basic/meson.build index 891235ff42..229ac97c69 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -428,7 +428,7 @@ libbasic = static_library( libcap, libm], c_args : ['-fvisibility=default'], - install : false) + build_by_default : false) ############################################################ @@ -443,4 +443,5 @@ libbasic_gcrypt = static_library( basic_gcrypt_sources, include_directories : basic_includes, dependencies : [libgcrypt], - c_args : ['-fvisibility=default']) + c_args : ['-fvisibility=default'], + build_by_default : false) diff --git a/src/core/meson.build b/src/core/meson.build index 92a811f2f0..d229d46779 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -184,7 +184,8 @@ libcore = static_library( libapparmor, libselinux, libmount, - libacl]) + libacl], + build_by_default : false) core_includes = [includes, include_directories('.')] diff --git a/src/import/meson.build b/src/import/meson.build index e48e49293e..23f0858817 100644 --- a/src/import/meson.build +++ b/src/import/meson.build @@ -45,7 +45,8 @@ if conf.get('ENABLE_IMPORTD') == 1 include_directories : includes, dependencies : [libbzip2, libxz, - libz]) + libz], + build_by_default : false) install_data('org.freedesktop.import1.conf', install_dir : dbuspolicydir) diff --git a/src/journal-remote/meson.build b/src/journal-remote/meson.build index b9f8b472cb..168d0ed6a3 100644 --- a/src/journal-remote/meson.build +++ b/src/journal-remote/meson.build @@ -31,7 +31,7 @@ libsystemd_journal_remote = static_library( libgnutls, libxz, liblz4], - install : false) + build_by_default : false) systemd_journal_remote_sources = files(''' journal-remote-main.c diff --git a/src/journal/meson.build b/src/journal/meson.build index 383ed355fd..eb66bfd584 100644 --- a/src/journal/meson.build +++ b/src/journal/meson.build @@ -36,7 +36,7 @@ libjournal_core = static_library( sources, include_directories : includes, dependencies: threads, - install : false) + build_by_default : false) journal_includes = [includes, include_directories('.')] diff --git a/src/libsystemd-network/meson.build b/src/libsystemd-network/meson.build index 54248a1e6e..a7838cdaa1 100644 --- a/src/libsystemd-network/meson.build +++ b/src/libsystemd-network/meson.build @@ -49,7 +49,8 @@ sources = files(''' libsystemd_network = static_library( 'systemd-network', sources, - include_directories : includes) + include_directories : includes, + build_by_default : false) libsystemd_network_includes = [includes, include_directories('.')] diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index 73c0228248..124393d3e6 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -166,12 +166,12 @@ libsystemd_c_args = ['-fvisibility=default'] libsystemd_static = static_library( 'systemd_static', libsystemd_sources, - install : false, include_directories : libsystemd_includes, + c_args : libsystemd_c_args, link_with : libbasic, dependencies : [threads, librt], - c_args : libsystemd_c_args) + build_by_default : false) libsystemd_sym = files('libsystemd.sym') libsystemd_sym_path = meson.current_source_dir() / 'libsystemd.sym' diff --git a/src/libudev/meson.build b/src/libudev/meson.build index 488ae62480..e94b43d9b7 100644 --- a/src/libudev/meson.build +++ b/src/libudev/meson.build @@ -28,7 +28,8 @@ libudev_basic = static_library( 'udev-basic', libudev_sources, include_directories : includes, - c_args : ['-fvisibility=default']) + c_args : ['-fvisibility=default'], + build_by_default : false) libudev_static = static_library( 'udev_static', diff --git a/src/login/meson.build b/src/login/meson.build index a583025a1b..a78c2bc2dd 100644 --- a/src/login/meson.build +++ b/src/login/meson.build @@ -50,7 +50,8 @@ liblogind_core = static_library( 'logind-core', liblogind_core_sources, include_directories : includes, - dependencies : [libacl]) + dependencies : libacl, + build_by_default : false) loginctl_sources = files(''' loginctl.c diff --git a/src/machine/meson.build b/src/machine/meson.build index 9b9ad1d8a9..ef858a5988 100644 --- a/src/machine/meson.build +++ b/src/machine/meson.build @@ -24,7 +24,8 @@ libmachine_core = static_library( 'machine-core', libmachine_core_sources, include_directories : includes, - dependencies : [threads]) + dependencies : threads, + build_by_default : false) if conf.get('ENABLE_MACHINED') == 1 install_data('org.freedesktop.machine1.conf', diff --git a/src/network/meson.build b/src/network/meson.build index 04a5f966bb..5857439c5a 100644 --- a/src/network/meson.build +++ b/src/network/meson.build @@ -235,7 +235,8 @@ libnetworkd_core = static_library( 'networkd-core', sources, include_directories : network_includes, - link_with : [networkd_link_with]) + link_with : networkd_link_with, + build_by_default : false) if conf.get('ENABLE_NETWORKD') == 1 install_data('org.freedesktop.network1.conf', diff --git a/src/nspawn/meson.build b/src/nspawn/meson.build index d465b3d804..11ac404e99 100644 --- a/src/nspawn/meson.build +++ b/src/nspawn/meson.build @@ -45,7 +45,8 @@ libnspawn_core = static_library( include_directories : includes, dependencies : [libacl, libseccomp, - libselinux]) + libselinux], + build_by_default : false) systemd_nspawn_sources = files('nspawn.c') diff --git a/src/resolve/meson.build b/src/resolve/meson.build index 4d717f1964..0580fbeec6 100644 --- a/src/resolve/meson.build +++ b/src/resolve/meson.build @@ -120,7 +120,8 @@ basic_dns_sources += custom_target( libsystemd_resolve_core = static_library( 'systemd-resolve-core', basic_dns_sources, - include_directories : includes) + include_directories : includes, + build_by_default : false) systemd_resolved_sources += custom_target( 'resolved_gperf.c', diff --git a/src/shared/meson.build b/src/shared/meson.build index 562d984fc3..5dc58a863d 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -459,7 +459,8 @@ libshared_static = static_library( shared_sources, include_directories : includes, dependencies : libshared_deps, - c_args : ['-fvisibility=default']) + c_args : ['-fvisibility=default'], + build_by_default : false) libshared = shared_library( libshared_name, diff --git a/src/timesync/meson.build b/src/timesync/meson.build index 6d6ddeb285..83dd7c8f67 100644 --- a/src/timesync/meson.build +++ b/src/timesync/meson.build @@ -32,7 +32,8 @@ libtimesyncd_core = static_library( 'timesyncd-core', sources, include_directories : includes, - link_with : [timesyncd_link_with]) + link_with : timesyncd_link_with, + build_by_default : false) custom_target( 'timesyncd.conf', diff --git a/src/udev/meson.build b/src/udev/meson.build index 0b692df3d8..cd0bb6653f 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -115,7 +115,9 @@ libudevd_core = static_library( keyboard_keys_from_name_h, include_directories : udev_includes, link_with : udev_link_with, - dependencies : [libblkid, libkmod]) + dependencies : [libblkid, + libkmod], + build_by_default : false) udev_progs = [['ata_id/ata_id.c'], ['cdrom_id/cdrom_id.c'], From 3a4524116a130fb728bc52fa7ebf70c45fba79c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 16 Dec 2021 11:09:51 +0100 Subject: [PATCH 5/6] meson: drop convenience library that was only used in one place It doesn't make much sense to do this, the result is very similar to including to objects directly in the output binary without going through an intermediate target. The linkage of test-libudev was rather strange too: udev_link_with is used to allow udev to be linked to a static version of libshared, so that udev is not linked to libshared.so. But here we were using both, defeating the purpose of udev_link_with. I don't think it matters what the test is linked to, so let's use the non-static linkage to save space. --- src/libudev/meson.build | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/libudev/meson.build b/src/libudev/meson.build index e94b43d9b7..77b31a9492 100644 --- a/src/libudev/meson.build +++ b/src/libudev/meson.build @@ -31,12 +31,6 @@ libudev_basic = static_library( c_args : ['-fvisibility=default'], build_by_default : false) -libudev_static = static_library( - 'udev_static', - include_directories : includes, - link_with : udev_link_with, - link_whole : libudev_basic) - static_libudev = get_option('static-libudev') static_libudev_pic = static_libudev == 'true' or static_libudev == 'pic' @@ -53,5 +47,5 @@ custom_target( tests += [ [['src/libudev/test-libudev.c'], [libshared, - libudev_static]], + libudev_basic]], ] From c01543fdd53ea452566ed80643c96eae9fec3726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 16 Dec 2021 11:51:08 +0100 Subject: [PATCH 6/6] meson: drop three more single-use convenience libraries The way that the cryptsetup plugins were built was unnecessarilly complicated. We would build three static libraries that would then be linked into dynamic libraries. No need to do this. While at it, let's use a convenience library to avoid compiling the shared code more than once. We want the output .so files to be located in the main build directory, like with all consumable build artifacts, so we need to maintain the split between src/cryptsetup/cryptsetup-token/meson.build and the main meson.build file. AFAICT, the build artifacts are the same: exported and undefined symbols are identical. There is a tiny difference in size, but I think it might be caused by a different build directory name. --- meson.build | 30 +++++--- src/cryptsetup/cryptsetup-tokens/meson.build | 76 ++++++-------------- 2 files changed, 41 insertions(+), 65 deletions(-) diff --git a/meson.build b/meson.build index bdf192cfc1..b6170eecf9 100644 --- a/meson.build +++ b/meson.build @@ -1836,11 +1836,15 @@ if conf.get('HAVE_LIBCRYPTSETUP_PLUGINS') == 1 if conf.get('HAVE_TPM2') == 1 cryptsetup_token_systemd_tpm2 = shared_library( 'cryptsetup-token-systemd-tpm2', + cryptsetup_token_systemd_tpm2_sources, + include_directories : includes, link_args : ['-shared', '-Wl,--version-script=' + cryptsetup_token_sym_path], - dependencies : libshared_deps + [libcryptsetup, versiondep], - link_with : [libshared], - link_whole : [cryptsetup_token_systemd_tpm2_static], + link_with : [lib_cryptsetup_token_common, + libshared], + dependencies : [libcryptsetup, + tpm2, + versiondep], link_depends : cryptsetup_token_sym, install_rpath : rootlibexecdir, install : true, @@ -1850,11 +1854,15 @@ if conf.get('HAVE_LIBCRYPTSETUP_PLUGINS') == 1 if conf.get('HAVE_LIBFIDO2') == 1 cryptsetup_token_systemd_fido2 = shared_library( 'cryptsetup-token-systemd-fido2', + cryptsetup_token_systemd_fido2_sources, + include_directories : includes, link_args : ['-shared', '-Wl,--version-script=' + cryptsetup_token_sym_path], - dependencies : libshared_deps + [libcryptsetup, versiondep], - link_with : [libshared], - link_whole : [cryptsetup_token_systemd_fido2_static], + link_with : [lib_cryptsetup_token_common, + libshared], + dependencies : [libcryptsetup, + libfido2, + versiondep], link_depends : cryptsetup_token_sym, install_rpath : rootlibexecdir, install : true, @@ -1864,11 +1872,15 @@ if conf.get('HAVE_LIBCRYPTSETUP_PLUGINS') == 1 if conf.get('HAVE_P11KIT') == 1 cryptsetup_token_systemd_pkcs11 = shared_library( 'cryptsetup-token-systemd-pkcs11', + cryptsetup_token_systemd_pkcs11_sources, + include_directories : includes, link_args : ['-shared', '-Wl,--version-script=' + cryptsetup_token_sym_path], - dependencies : libshared_deps + [libcryptsetup, versiondep], - link_with : [libshared], - link_whole : [cryptsetup_token_systemd_pkcs11_static], + link_with : [lib_cryptsetup_token_common, + libshared], + dependencies : [libcryptsetup, + libp11kit, + versiondep], link_depends : cryptsetup_token_sym, install_rpath : rootlibexecdir, install : true, diff --git a/src/cryptsetup/cryptsetup-tokens/meson.build b/src/cryptsetup/cryptsetup-tokens/meson.build index d174fcb714..42c29f100e 100644 --- a/src/cryptsetup/cryptsetup-tokens/meson.build +++ b/src/cryptsetup/cryptsetup-tokens/meson.build @@ -1,64 +1,28 @@ # SPDX-License-Identifier: LGPL-2.1-or-later -if conf.get('HAVE_LIBCRYPTSETUP_PLUGINS') == 1 - -cryptsetup_token_c_args = ['-fvisibility=hidden'] - cryptsetup_token_sym = files('cryptsetup-token.sym') cryptsetup_token_sym_path = meson.current_source_dir() / 'cryptsetup-token.sym' -if conf.get('HAVE_TPM2') == 1 - cryptsetup_token_systemd_tpm2_sources = files(''' - cryptsetup-token-systemd-tpm2.c - cryptsetup-token.h - cryptsetup-token-util.h - cryptsetup-token-util.c - luks2-tpm2.c - luks2-tpm2.h - '''.split()) +lib_cryptsetup_token_common = static_library( + 'cryptsetup-token-common', + 'cryptsetup-token.h', + 'cryptsetup-token-util.h', + 'cryptsetup-token-util.c', + include_directories : includes, + link_with : libshared, + build_by_default : false) - cryptsetup_token_systemd_tpm2_static = static_library( - 'cryptsetup-token-systemd-tpm2_static', - cryptsetup_token_systemd_tpm2_sources, - include_directories : includes, - dependencies : libshared_deps + [libcryptsetup, versiondep], - c_args : cryptsetup_token_c_args) -endif +cryptsetup_token_systemd_tpm2_sources = files( + 'cryptsetup-token-systemd-tpm2.c', + 'luks2-tpm2.c', + 'luks2-tpm2.h') -if conf.get('HAVE_LIBFIDO2') == 1 - cryptsetup_token_systemd_fido2_sources = files(''' - cryptsetup-token-systemd-fido2.c - cryptsetup-token.h - cryptsetup-token-util.h - cryptsetup-token-util.c - luks2-fido2.c - luks2-fido2.h - '''.split()) +cryptsetup_token_systemd_fido2_sources = files( + 'cryptsetup-token-systemd-fido2.c', + 'luks2-fido2.c', + 'luks2-fido2.h') - cryptsetup_token_systemd_fido2_static = static_library( - 'cryptsetup-token-systemd-fido2_static', - cryptsetup_token_systemd_fido2_sources, - include_directories : includes, - dependencies : libshared_deps + [libcryptsetup, versiondep], - c_args : cryptsetup_token_c_args) -endif - -if conf.get('HAVE_P11KIT') == 1 - cryptsetup_token_systemd_pkcs11_sources = files(''' - cryptsetup-token-systemd-pkcs11.c - cryptsetup-token.h - cryptsetup-token-util.h - cryptsetup-token-util.c - luks2-pkcs11.c - luks2-pkcs11.h - '''.split()) - - cryptsetup_token_systemd_pkcs11_static = static_library( - 'cryptsetup-token-systemd-pkcs11_static', - cryptsetup_token_systemd_pkcs11_sources, - include_directories : includes, - dependencies : libshared_deps + [libcryptsetup, versiondep], - c_args : cryptsetup_token_c_args) -endif - -endif +cryptsetup_token_systemd_pkcs11_sources = files( + 'cryptsetup-token-systemd-pkcs11.c', + 'luks2-pkcs11.c', + 'luks2-pkcs11.h')