From cc4d38b14b3a5e32cbd77cca2be02799e766dc8a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 15:46:14 +0900 Subject: [PATCH 1/8] core/cgroup: fix setting SocketBindAllow=/SocketBindDeny= through DBus --- src/core/dbus-cgroup.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 682ad5edd5..c373f0bbe8 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -2118,12 +2118,14 @@ int bus_cgroup_set_property( if (!f) return -ENOMEM; - fprintf(f, "%s:", name); - - LIST_FOREACH(socket_bind_items, item, *list) - cgroup_context_dump_socket_bind_item(item, f); - - fputc('\n', f); + if (n == 0) + fprintf(f, "%s=\n", name); + else + LIST_FOREACH(socket_bind_items, item, *list) { + fprintf(f, "%s=", name); + cgroup_context_dump_socket_bind_item(item, f); + fputc('\n', f); + } r = fflush_and_check(f); if (r < 0) From 64377c6089bbeb1d87cbdbacbe38dd19d78c4cec Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 21:43:45 +0900 Subject: [PATCH 2/8] oomd: drop unused key --- src/oom/oomd-manager.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index 3f050cdbb2..c39e75562d 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -811,7 +811,6 @@ int manager_get_dump_string(Manager *m, char **ret) { _cleanup_fclose_ FILE *f = NULL; OomdCGroupContext *c; size_t size; - char *key; int r; assert(m); @@ -834,11 +833,11 @@ int manager_get_dump_string(Manager *m, char **ret) { oomd_dump_system_context(&m->system_context, f, "\t"); fprintf(f, "Swap Monitored CGroups:\n"); - HASHMAP_FOREACH_KEY(c, key, m->monitored_swap_cgroup_contexts) + HASHMAP_FOREACH(c, m->monitored_swap_cgroup_contexts) oomd_dump_swap_cgroup_context(c, f, "\t"); fprintf(f, "Memory Pressure Monitored CGroups:\n"); - HASHMAP_FOREACH_KEY(c, key, m->monitored_mem_pressure_cgroup_contexts) + HASHMAP_FOREACH(c, m->monitored_mem_pressure_cgroup_contexts) oomd_dump_memory_pressure_cgroup_context(c, f, "\t"); r = fflush_and_check(f); From 5d2a48da126e696dcd61baa75911d7f5fb2d076c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 22:33:44 +0900 Subject: [PATCH 3/8] tree-wide: use _cleanup_set_free_ and friends Instead of _cleanup_(set_freep) or so. --- src/analyze/analyze-security.c | 2 +- src/core/dbus-manager.c | 2 +- src/core/unit.c | 2 +- src/creds/creds.c | 4 +-- src/cryptenroll/cryptenroll-wipe.c | 4 +-- src/dissect/dissect.c | 2 +- src/home/homectl-pkcs11.c | 2 +- src/home/homectl-recovery-key.c | 2 +- src/home/homectl.c | 14 ++++---- src/home/homed-manager-bus.c | 2 +- src/home/homed-manager.c | 2 +- src/home/homework-cifs.c | 2 +- src/home/homework.c | 12 +++---- src/home/user-record-util.c | 4 +-- src/libsystemd/sd-device/test-sd-device.c | 2 +- src/libsystemd/sd-event/test-event.c | 2 +- src/notify/notify.c | 2 +- src/partition/repart.c | 2 +- src/portable/portable.c | 4 +-- src/pstore/pstore.c | 2 +- src/shared/bootspec.c | 2 +- src/shared/data-fd-util.c | 2 +- src/shared/elf-util.c | 4 +-- src/shared/kbd-util.c | 2 +- src/shared/libfido2-util.c | 2 +- src/shared/mkfs-util.c | 2 +- src/shared/sleep-config.c | 2 +- src/shared/tpm2-util.c | 4 +-- src/shared/userdb.c | 4 +-- src/shutdown/umount.c | 4 +-- src/sysctl/sysctl.c | 2 +- src/sysext/sysext.c | 8 ++--- src/systemctl/systemctl-edit.c | 4 +-- src/systemctl/systemctl-list-unit-files.c | 2 +- src/sysupdate/sysupdate-resource.c | 4 +-- src/sysupdate/sysupdate-transfer.c | 2 +- src/sysupdate/sysupdate.c | 2 +- src/test/test-data-fd-util.c | 2 +- src/test/test-hashmap-plain.c | 34 +++++++++---------- src/test/test-install-root.c | 2 +- src/test/test-install.c | 2 +- src/test/test-load-fragment.c | 2 +- src/test/test-loop-block.c | 2 +- src/test/test-set.c | 4 +-- src/test/test-strv.c | 2 +- src/test/test-varlink.c | 2 +- src/tmpfiles/offline-passwd.c | 4 +-- src/tmpfiles/test-offline-passwd.c | 2 +- src/tmpfiles/tmpfiles.c | 2 +- src/udev/udevadm-lock.c | 2 +- src/userdb/userdbctl.c | 2 +- .../xdg-autostart-generator.c | 2 +- 52 files changed, 94 insertions(+), 94 deletions(-) diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 56bc362776..1fcdbf9cd3 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -2229,7 +2229,7 @@ static int property_read_ip_filters( void *userdata) { SecurityInfo *info = userdata; - _cleanup_(strv_freep) char **l = NULL; + _cleanup_strv_free_ char **l = NULL; int r; assert(bus); diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index d6b2dcb114..c02ad09c07 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -2153,7 +2153,7 @@ static int list_unit_files_by_patterns(sd_bus_message *message, void *userdata, _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; Manager *m = ASSERT_PTR(userdata); UnitFileList *item; - _cleanup_(hashmap_freep) Hashmap *h = NULL; + _cleanup_hashmap_free_ Hashmap *h = NULL; int r; assert(message); diff --git a/src/core/unit.c b/src/core/unit.c index 8b4fb0fce4..7b6d50e0ea 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1082,7 +1082,7 @@ static void unit_merge_dependencies(Unit *u, Unit *other) { } for (;;) { - _cleanup_(hashmap_freep) Hashmap *other_deps = NULL; + _cleanup_hashmap_free_ Hashmap *other_deps = NULL; UnitDependencyInfo di_back; Unit *back; diff --git a/src/creds/creds.c b/src/creds/creds.c index 670e719d21..51efe27616 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -122,7 +122,7 @@ not_found: } static int add_credentials_to_table(Table *t, bool encrypted) { - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; const char *prefix; int r; @@ -382,7 +382,7 @@ static int verb_cat(int argc, char **argv, void *userdata) { /* Look both in regular and in encrypted credentials */ for (encrypted = 0; encrypted < 2; encrypted++) { - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; r = open_credential_directory(encrypted, &d, NULL); if (r < 0) diff --git a/src/cryptenroll/cryptenroll-wipe.c b/src/cryptenroll/cryptenroll-wipe.c index 8042a0fe83..314ebd3113 100644 --- a/src/cryptenroll/cryptenroll-wipe.c +++ b/src/cryptenroll/cryptenroll-wipe.c @@ -88,7 +88,7 @@ static int find_slots_by_mask( Set *keep_slots, unsigned by_mask) { - _cleanup_(set_freep) Set *listed_slots = NULL; + _cleanup_set_free_ Set *listed_slots = NULL; int r; assert(cd); @@ -301,7 +301,7 @@ int wipe_slots(struct crypt_device *cd, unsigned by_mask, int except_slot) { - _cleanup_(set_freep) Set *wipe_slots = NULL, *wipe_tokens = NULL, *keep_slots = NULL; + _cleanup_set_free_ Set *wipe_slots = NULL, *wipe_tokens = NULL, *keep_slots = NULL; _cleanup_free_ int *ordered_slots = NULL, *ordered_tokens = NULL; size_t n_ordered_slots = 0, n_ordered_tokens = 0; int r, slot_max, ret; diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 45e0abc63d..5c742497d5 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -1569,7 +1569,7 @@ static int action_with(DissectedImage *m, LoopDevice *d) { } static int action_discover(void) { - _cleanup_(hashmap_freep) Hashmap *images = NULL; + _cleanup_hashmap_free_ Hashmap *images = NULL; _cleanup_(table_unrefp) Table *t = NULL; Image *img; int r; diff --git a/src/home/homectl-pkcs11.c b/src/home/homectl-pkcs11.c index dc6ecf1665..2539af0631 100644 --- a/src/home/homectl-pkcs11.c +++ b/src/home/homectl-pkcs11.c @@ -100,7 +100,7 @@ static int add_pkcs11_token_uri(JsonVariant **v, const char *uri) { int identity_add_token_pin(JsonVariant **v, const char *pin) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL, *l = NULL; - _cleanup_(strv_free_erasep) char **pins = NULL; + _cleanup_strv_free_erase_ char **pins = NULL; int r; assert(v); diff --git a/src/home/homectl-recovery-key.c b/src/home/homectl-recovery-key.c index ff1ab6820d..bf18ae49e4 100644 --- a/src/home/homectl-recovery-key.c +++ b/src/home/homectl-recovery-key.c @@ -67,7 +67,7 @@ static int add_public(JsonVariant **v) { static int add_secret(JsonVariant **v, const char *password) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL, *l = NULL; - _cleanup_(strv_free_erasep) char **passwords = NULL; + _cleanup_strv_free_erase_ char **passwords = NULL; int r; assert(v); diff --git a/src/home/homectl.c b/src/home/homectl.c index b265f8741e..d6e6bd0855 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -210,7 +210,7 @@ static int acquire_existing_password( bool emphasize_current, AskPasswordFlags flags) { - _cleanup_(strv_free_erasep) char **password = NULL; + _cleanup_strv_free_erase_ char **password = NULL; _cleanup_(erase_and_freep) char *envpw = NULL; _cleanup_free_ char *question = NULL; int r; @@ -271,7 +271,7 @@ static int acquire_recovery_key( UserRecord *hr, AskPasswordFlags flags) { - _cleanup_(strv_free_erasep) char **recovery_key = NULL; + _cleanup_strv_free_erase_ char **recovery_key = NULL; _cleanup_(erase_and_freep) char *envpw = NULL; _cleanup_free_ char *question = NULL; int r; @@ -329,7 +329,7 @@ static int acquire_token_pin( UserRecord *hr, AskPasswordFlags flags) { - _cleanup_(strv_free_erasep) char **pin = NULL; + _cleanup_strv_free_erase_ char **pin = NULL; _cleanup_(erase_and_freep) char *envpin = NULL; _cleanup_free_ char *question = NULL; int r; @@ -699,7 +699,7 @@ static char **mangle_user_list(char **list, char ***ret_allocated) { static int inspect_home(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_(strv_freep) char **mangled_list = NULL; + _cleanup_strv_free_ char **mangled_list = NULL; int r, ret = 0; char **items; @@ -783,7 +783,7 @@ static int inspect_home(int argc, char *argv[], void *userdata) { static int authenticate_home(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_(strv_freep) char **mangled_list = NULL; + _cleanup_strv_free_ char **mangled_list = NULL; int r, ret = 0; char **items; @@ -1187,7 +1187,7 @@ static int acquire_new_password( (void) suggest_passwords(); for (;;) { - _cleanup_(strv_free_erasep) char **first = NULL, **second = NULL; + _cleanup_strv_free_erase_ char **first = NULL, **second = NULL; _cleanup_free_ char *question = NULL; if (--i == 0) @@ -3162,7 +3162,7 @@ static int parse_argv(int argc, char *argv[]) { case ARG_SSH_AUTHORIZED_KEYS: { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - _cleanup_(strv_freep) char **l = NULL, **add = NULL; + _cleanup_strv_free_ char **l = NULL, **add = NULL; if (isempty(optarg)) { r = drop_from_identity("sshAuthorizedKeys"); diff --git a/src/home/homed-manager-bus.c b/src/home/homed-manager-bus.c index ca639ae39e..7cf543922f 100644 --- a/src/home/homed-manager-bus.c +++ b/src/home/homed-manager-bus.c @@ -36,7 +36,7 @@ static int property_get_auto_login( return r; HASHMAP_FOREACH(h, m->homes_by_name) { - _cleanup_(strv_freep) char **seats = NULL; + _cleanup_strv_free_ char **seats = NULL; _cleanup_free_ char *home_path = NULL; r = home_auto_login(h, &seats); diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c index 92fe5a88b8..9db8b9ee06 100644 --- a/src/home/homed-manager.c +++ b/src/home/homed-manager.c @@ -1357,7 +1357,7 @@ static int manager_enumerate_devices(Manager *m) { } static int manager_load_key_pair(Manager *m) { - _cleanup_(fclosep) FILE *f = NULL; + _cleanup_fclose_ FILE *f = NULL; struct stat st; int r; diff --git a/src/home/homework-cifs.c b/src/home/homework-cifs.c index 8ad1da6303..dff99e5a86 100644 --- a/src/home/homework-cifs.c +++ b/src/home/homework-cifs.c @@ -60,7 +60,7 @@ int home_setup_cifs( STRV_FOREACH(pw, h->password) { _cleanup_(unlink_and_freep) char *p = NULL; _cleanup_free_ char *options = NULL; - _cleanup_(fclosep) FILE *f = NULL; + _cleanup_fclose_ FILE *f = NULL; pid_t mount_pid; int exit_status; diff --git a/src/home/homework.c b/src/home/homework.c index 28907386a4..f8e333bb97 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -511,7 +511,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_fclose_ FILE *identity_file = NULL; _cleanup_close_ int identity_fd = -EBADF; unsigned line, column; int r; @@ -542,7 +542,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_fclose_ FILE *identity_file = NULL; _cleanup_close_ int identity_fd = -EBADF; _cleanup_free_ char *fn = NULL; int r; @@ -1088,7 +1088,7 @@ static int user_record_compile_effective_passwords( PasswordCache *cache, char ***ret_effective_passwords) { - _cleanup_(strv_free_erasep) char **effective = NULL; + _cleanup_strv_free_erase_ char **effective = NULL; size_t n; int r; @@ -1307,7 +1307,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_strv_free_erase_ 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 = {}; @@ -1679,7 +1679,7 @@ static int home_resize(UserRecord *h, bool automatic, UserRecord **ret) { static int home_passwd(UserRecord *h, UserRecord **ret_home) { _cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL; - _cleanup_(strv_free_erasep) char **effective_passwords = NULL; + _cleanup_strv_free_erase_ char **effective_passwords = NULL; _cleanup_(home_setup_done) HomeSetup setup = HOME_SETUP_INIT; _cleanup_(password_cache_free) PasswordCache cache = {}; HomeSetupFlags flags = 0; @@ -1850,7 +1850,7 @@ static int home_unlock(UserRecord *h) { static int run(int argc, char *argv[]) { _cleanup_(user_record_unrefp) UserRecord *home = NULL, *new_home = NULL; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - _cleanup_(fclosep) FILE *opened_file = NULL; + _cleanup_fclose_ FILE *opened_file = NULL; unsigned line = 0, column = 0; const char *json_path = NULL; FILE *json_file; diff --git a/src/home/user-record-util.c b/src/home/user-record-util.c index 08f1764d11..58f8ecc6f0 100644 --- a/src/home/user-record-util.c +++ b/src/home/user-record-util.c @@ -871,7 +871,7 @@ int user_record_set_hashed_password(UserRecord *h, char **hashed_password) { int user_record_set_password(UserRecord *h, char **password, bool prepend) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - _cleanup_(strv_free_erasep) char **e = NULL; + _cleanup_strv_free_erase_ char **e = NULL; int r; assert(h); @@ -933,7 +933,7 @@ int user_record_set_password(UserRecord *h, char **password, bool prepend) { int user_record_set_token_pin(UserRecord *h, char **pin, bool prepend) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - _cleanup_(strv_free_erasep) char **e = NULL; + _cleanup_strv_free_erase_ char **e = NULL; int r; assert(h); diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index 8c6d59e040..fb3ae9f329 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -266,7 +266,7 @@ static void test_sd_device_enumerator_filter_subsystem_one( static bool test_sd_device_enumerator_filter_subsystem_trial(void) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - _cleanup_(hashmap_freep) Hashmap *subsystems = NULL; + _cleanup_hashmap_free_ Hashmap *subsystems = NULL; unsigned n_new_dev = 0, n_removed_dev = 0; sd_device *d; Hashmap *h; diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index c5497e5148..fe0d83116d 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -841,7 +841,7 @@ TEST(leave_ratelimit) { bool expect_ratelimit = false, manually_left_ratelimit = false; _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; - _cleanup_(close_pairp) int pfd[2] = PIPE_EBADF; + _cleanup_close_pair_ int pfd[2] = PIPE_EBADF; unsigned c = 0; int r; diff --git a/src/notify/notify.c b/src/notify/notify.c index c15df588ef..f30790bc70 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -132,7 +132,7 @@ static int parse_argv(int argc, char *argv[]) { {} }; - _cleanup_(fdset_freep) FDSet *passed = NULL; + _cleanup_fdset_free_ FDSet *passed = NULL; bool do_exec = false; int c, r, n_env; diff --git a/src/partition/repart.c b/src/partition/repart.c index 2e9badfd31..89f7211bfd 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -5261,7 +5261,7 @@ static int resolve_copy_blocks_auto( const char *try1 = NULL, *try2 = NULL; char p[SYS_BLOCK_PATH_MAX("/slaves")]; - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; sd_id128_t found_uuid = SD_ID128_NULL; dev_t devno, found = 0; int r; diff --git a/src/portable/portable.c b/src/portable/portable.c index 7ab3ac6e97..1c31377567 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -357,7 +357,7 @@ static int portable_extract_by_path( else { _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; _cleanup_(rmdir_and_freep) char *tmpdir = NULL; - _cleanup_(close_pairp) int seq[2] = PIPE_EBADF; + _cleanup_close_pair_ int seq[2] = PIPE_EBADF; _cleanup_(sigkill_waitp) pid_t child = 0; /* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it @@ -689,7 +689,7 @@ int portable_extract( _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL; _cleanup_hashmap_free_ Hashmap *unit_files = NULL; - _cleanup_(strv_freep) char **valid_prefixes = NULL; + _cleanup_strv_free_ char **valid_prefixes = NULL; _cleanup_(image_unrefp) Image *image = NULL; int r; diff --git a/src/pstore/pstore.c b/src/pstore/pstore.c index 0a1bf00366..4501b38cff 100644 --- a/src/pstore/pstore.c +++ b/src/pstore/pstore.c @@ -281,7 +281,7 @@ static int process_dmesg_files(PStoreList *list) { } static int list_files(PStoreList *list, const char *sourcepath) { - _cleanup_(closedirp) DIR *dirp = NULL; + _cleanup_closedir_ DIR *dirp = NULL; int r; dirp = opendir(sourcepath); diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index a5ae2478fe..6459f5d2f8 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -854,7 +854,7 @@ static int boot_entries_find_unified( const char *root, const char *dir) { - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; _cleanup_free_ char *full = NULL; int r; diff --git a/src/shared/data-fd-util.c b/src/shared/data-fd-util.c index 4831682a02..86baf9bfa1 100644 --- a/src/shared/data-fd-util.c +++ b/src/shared/data-fd-util.c @@ -206,7 +206,7 @@ int copy_data_fd(int fd) { /* Hmm, pity, this didn't fit. Let's fall back to /tmp then, see below */ } else { - _cleanup_(close_pairp) int pipefds[2] = PIPE_EBADF; + _cleanup_close_pair_ int pipefds[2] = PIPE_EBADF; int isz; /* If memfds aren't available, use a pipe. Set O_NONBLOCK so that we will get EAGAIN rather diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 5885215a1c..68d186117f 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -570,7 +570,7 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** }; _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; - _cleanup_(set_freep) Set *modules = NULL; + _cleanup_set_free_ Set *modules = NULL; _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_destroy) */ _cleanup_(stack_context_destroy) StackContext c = { .package_metadata = &package_metadata, @@ -636,7 +636,7 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **ret_package_metadata) { _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL, *elf_metadata = NULL; - _cleanup_(set_freep) Set *modules = NULL; + _cleanup_set_free_ Set *modules = NULL; _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_destroy) */ _cleanup_(stack_context_destroy) StackContext c = { .package_metadata = &package_metadata, diff --git a/src/shared/kbd-util.c b/src/shared/kbd-util.c index 6222ef4fc8..3100862548 100644 --- a/src/shared/kbd-util.c +++ b/src/shared/kbd-util.c @@ -64,7 +64,7 @@ static int keymap_recurse_dir_callback( } int get_keymaps(char ***ret) { - _cleanup_(set_free_freep) Set *keymaps = NULL; + _cleanup_set_free_free_ Set *keymaps = NULL; int r; keymaps = set_new(&string_hash_ops); diff --git a/src/shared/libfido2-util.c b/src/shared/libfido2-util.c index 6802ad0d3d..57c4058487 100644 --- a/src/shared/libfido2-util.c +++ b/src/shared/libfido2-util.c @@ -820,7 +820,7 @@ int fido2_generate_hmac_hash( "Token asks for PIN but doesn't advertise 'clientPin' feature."); for (;;) { - _cleanup_(strv_free_erasep) char **pin = NULL; + _cleanup_strv_free_erase_ char **pin = NULL; r = ask_password_auto("Please enter security token PIN:", askpw_icon_name, NULL, "fido2-pin", "fido2-pin", USEC_INFINITY, 0, &pin); if (r < 0) diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index 08e89dc228..6ee4307c68 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -548,7 +548,7 @@ int make_filesystem( } int mkfs_options_from_env(const char *component, const char *fstype, char ***ret) { - _cleanup_(strv_freep) char **l = NULL; + _cleanup_strv_free_ char **l = NULL; const char *e; char *n; diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 2e3b47ddca..d388c89192 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -222,7 +222,7 @@ int battery_is_discharging_and_low(void) { /* Store current capacity of each battery before suspension and timestamp */ int fetch_batteries_capacity_by_name(Hashmap **ret) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - _cleanup_(hashmap_freep) Hashmap *batteries_capacity_by_name = NULL; + _cleanup_hashmap_free_ Hashmap *batteries_capacity_by_name = NULL; sd_device *dev; int r; diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index c21f5b2274..680cc726c3 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -2847,7 +2847,7 @@ int tpm2_unseal(const char *device, int tpm2_list_devices(void) { #if HAVE_TPM2 _cleanup_(table_unrefp) Table *t = NULL; - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; int r; r = dlopen_tpm2(); @@ -2923,7 +2923,7 @@ int tpm2_find_device_auto( int log_level, /* log level when no device is found */ char **ret) { #if HAVE_TPM2 - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; int r; r = dlopen_tpm2(); diff --git a/src/shared/userdb.c b/src/shared/userdb.c index a7896297ae..dfff5aba72 100644 --- a/src/shared/userdb.c +++ b/src/shared/userdb.c @@ -395,8 +395,8 @@ static int userdb_start_query( JsonVariant *query, UserDBFlags flags) { - _cleanup_(strv_freep) char **except = NULL, **only = NULL; - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_strv_free_ char **except = NULL, **only = NULL; + _cleanup_closedir_ DIR *d = NULL; const char *e; int r, ret = 0; diff --git a/src/shutdown/umount.c b/src/shutdown/umount.c index 93e082d77e..221d29ba6c 100644 --- a/src/shutdown/umount.c +++ b/src/shutdown/umount.c @@ -605,7 +605,7 @@ static void log_umount_blockers(const char *mnt) { } static int remount_with_timeout(MountPoint *m, bool last_try) { - _cleanup_(close_pairp) int pfd[2] = PIPE_EBADF; + _cleanup_close_pair_ int pfd[2] = PIPE_EBADF; _cleanup_(sigkill_nowaitp) pid_t pid = 0; int r; @@ -661,7 +661,7 @@ static int remount_with_timeout(MountPoint *m, bool last_try) { } static int umount_with_timeout(MountPoint *m, bool last_try) { - _cleanup_(close_pairp) int pfd[2] = PIPE_EBADF; + _cleanup_close_pair_ int pfd[2] = PIPE_EBADF; _cleanup_(sigkill_nowaitp) pid_t pid = 0; int r; diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index d752ad2742..c9b277b124 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -443,7 +443,7 @@ static int parse_argv(int argc, char *argv[]) { } static int run(int argc, char *argv[]) { - _cleanup_(ordered_hashmap_freep) OrderedHashmap *sysctl_options = NULL; + _cleanup_ordered_hashmap_free_ OrderedHashmap *sysctl_options = NULL; int r, k; r = parse_argv(argc, argv); diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c index f5b2248040..ec231da7a8 100644 --- a/src/sysext/sysext.c +++ b/src/sysext/sysext.c @@ -788,7 +788,7 @@ static int merge(Hashmap *images) { } static int image_discover_and_read_metadata(Hashmap **ret_images) { - _cleanup_(hashmap_freep) Hashmap *images = NULL; + _cleanup_hashmap_free_ Hashmap *images = NULL; Image *img; int r; @@ -814,7 +814,7 @@ static int image_discover_and_read_metadata(Hashmap **ret_images) { } static int verb_merge(int argc, char **argv, void *userdata) { - _cleanup_(hashmap_freep) Hashmap *images = NULL; + _cleanup_hashmap_free_ Hashmap *images = NULL; int r; r = have_effective_cap(CAP_SYS_ADMIN); @@ -852,7 +852,7 @@ static int verb_merge(int argc, char **argv, void *userdata) { } static int verb_refresh(int argc, char **argv, void *userdata) { - _cleanup_(hashmap_freep) Hashmap *images = NULL; + _cleanup_hashmap_free_ Hashmap *images = NULL; int r; r = have_effective_cap(CAP_SYS_ADMIN); @@ -892,7 +892,7 @@ static int verb_refresh(int argc, char **argv, void *userdata) { } static int verb_list(int argc, char **argv, void *userdata) { - _cleanup_(hashmap_freep) Hashmap *images = NULL; + _cleanup_hashmap_free_ Hashmap *images = NULL; _cleanup_(table_unrefp) Table *t = NULL; Image *img; int r; diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c index aff823d773..e3f25d52d5 100644 --- a/src/systemctl/systemctl-edit.c +++ b/src/systemctl/systemctl-edit.c @@ -14,7 +14,7 @@ #include "terminal-util.h" int verb_cat(int argc, char *argv[], void *userdata) { - _cleanup_(hashmap_freep) Hashmap *cached_name_map = NULL, *cached_id_map = NULL; + _cleanup_hashmap_free_ Hashmap *cached_name_map = NULL, *cached_id_map = NULL; _cleanup_(lookup_paths_free) LookupPaths lp = {}; _cleanup_strv_free_ char **names = NULL; sd_bus *bus; @@ -197,7 +197,7 @@ static int find_paths_to_edit( EditFileContext *context, char **names) { - _cleanup_(hashmap_freep) Hashmap *cached_name_map = NULL, *cached_id_map = NULL; + _cleanup_hashmap_free_ Hashmap *cached_name_map = NULL, *cached_id_map = NULL; _cleanup_(lookup_paths_free) LookupPaths lp = {}; _cleanup_free_ char *drop_in_alloc = NULL, *suffix = NULL; const char *drop_in; diff --git a/src/systemctl/systemctl-list-unit-files.c b/src/systemctl/systemctl-list-unit-files.c index aad248fe1f..fc1ad9800a 100644 --- a/src/systemctl/systemctl-list-unit-files.c +++ b/src/systemctl/systemctl-list-unit-files.c @@ -143,7 +143,7 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) { int verb_list_unit_files(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_free_ UnitFileList *units = NULL; - _cleanup_(hashmap_freep) Hashmap *h = NULL; + _cleanup_hashmap_free_ Hashmap *h = NULL; unsigned c = 0; const char *state; char *path; diff --git a/src/sysupdate/sysupdate-resource.c b/src/sysupdate/sysupdate-resource.c index 5f06377ade..201b37528a 100644 --- a/src/sysupdate/sysupdate-resource.c +++ b/src/sysupdate/sysupdate-resource.c @@ -72,7 +72,7 @@ static int resource_load_from_directory( Resource *rr, mode_t m) { - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; int r; assert(rr); @@ -242,7 +242,7 @@ static int download_manifest( size_t *ret_size) { _cleanup_free_ char *buffer = NULL, *suffixed_url = NULL; - _cleanup_(close_pairp) int pfd[2] = PIPE_EBADF; + _cleanup_close_pair_ int pfd[2] = PIPE_EBADF; _cleanup_fclose_ FILE *manifest = NULL; size_t size = 0; pid_t pid; diff --git a/src/sysupdate/sysupdate-transfer.c b/src/sysupdate/sysupdate-transfer.c index 8ae58c2b1b..f7009315a2 100644 --- a/src/sysupdate/sysupdate-transfer.c +++ b/src/sysupdate/sysupdate-transfer.c @@ -570,7 +570,7 @@ int transfer_resolve_paths( } static void transfer_remove_temporary(Transfer *t) { - _cleanup_(closedirp) DIR *d = NULL; + _cleanup_closedir_ DIR *d = NULL; int r; assert(t); diff --git a/src/sysupdate/sysupdate.c b/src/sysupdate/sysupdate.c index 76777dc08e..023eaac586 100644 --- a/src/sysupdate/sysupdate.c +++ b/src/sysupdate/sysupdate.c @@ -1093,7 +1093,7 @@ static int component_name_valid(const char *c) { static int verb_components(int argc, char **argv, void *userdata) { _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL; - _cleanup_(set_freep) Set *names = NULL; + _cleanup_set_free_ Set *names = NULL; _cleanup_free_ char **z = NULL; /* We use simple free() rather than strv_free() here, since set_free() will free the strings for us */ char **l = CONF_PATHS_STRV(""); bool has_default_component = false; diff --git a/src/test/test-data-fd-util.c b/src/test/test-data-fd-util.c index c0febec208..c5a4b08469 100644 --- a/src/test/test-data-fd-util.c +++ b/src/test/test-data-fd-util.c @@ -81,7 +81,7 @@ static void assert_equal_fd(int fd1, int fd2) { TEST(copy_data_fd) { _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF; - _cleanup_(close_pairp) int sfd[2] = PIPE_EBADF; + _cleanup_close_pair_ int sfd[2] = PIPE_EBADF; _cleanup_(sigkill_waitp) pid_t pid = -1; int r; diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index d5e5dcaa68..560a64b959 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -11,7 +11,7 @@ #include "tests.h" TEST(hashmap_replace) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; _cleanup_free_ char *val1 = NULL, *val2 = NULL, *val3 = NULL, *val4 = NULL, *val5 = NULL; char *r; @@ -43,8 +43,8 @@ TEST(hashmap_replace) { } TEST(hashmap_copy) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; - _cleanup_(hashmap_free_freep) Hashmap *copy = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *copy = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -76,8 +76,8 @@ TEST(hashmap_copy) { } TEST(hashmap_get_strv) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; - _cleanup_(strv_freep) char **strv = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; + _cleanup_strv_free_ char **strv = NULL; char *val1, *val2, *val3, *val4; val1 = strdup("val1"); @@ -109,7 +109,7 @@ TEST(hashmap_get_strv) { } TEST(hashmap_move_one) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL, *n = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL, *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -145,7 +145,7 @@ TEST(hashmap_move_one) { } TEST(hashmap_move) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL, *n = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL, *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("val1"); @@ -184,7 +184,7 @@ TEST(hashmap_move) { } TEST(hashmap_update) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; _cleanup_free_ char *val1 = NULL, *val2 = NULL; char *r; @@ -208,7 +208,7 @@ TEST(hashmap_update) { } TEST(hashmap_put) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; int valid_hashmap_put; void *val1 = (void*) "val 1"; void *val2 = (void*) "val 2"; @@ -417,7 +417,7 @@ TEST(hashmap_ensure_allocated) { } TEST(hashmap_foreach_key) { - _cleanup_(hashmap_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_ Hashmap *m = NULL; bool key_found[] = { false, false, false, false }; const char *s; const char *key; @@ -449,7 +449,7 @@ TEST(hashmap_foreach_key) { } TEST(hashmap_foreach) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; bool value_found[] = { false, false, false, false }; char *val1, *val2, *val3, *val4, *s; unsigned count; @@ -496,8 +496,8 @@ TEST(hashmap_foreach) { } TEST(hashmap_merge) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; - _cleanup_(hashmap_freep) Hashmap *n = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; + _cleanup_hashmap_free_ Hashmap *n = NULL; char *val1, *val2, *val3, *val4, *r; val1 = strdup("my val1"); @@ -528,7 +528,7 @@ TEST(hashmap_merge) { } TEST(hashmap_contains) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; char *val1; val1 = strdup("my val"); @@ -547,7 +547,7 @@ TEST(hashmap_contains) { } TEST(hashmap_isempty) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; char *val1; val1 = strdup("my val"); @@ -563,7 +563,7 @@ TEST(hashmap_isempty) { } TEST(hashmap_size) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; char *val1, *val2, *val3, *val4; val1 = strdup("my val"); @@ -591,7 +591,7 @@ TEST(hashmap_size) { } TEST(hashmap_get) { - _cleanup_(hashmap_free_freep) Hashmap *m = NULL; + _cleanup_hashmap_free_free_ Hashmap *m = NULL; char *r; char *val; diff --git a/src/test/test-install-root.c b/src/test/test-install-root.c index 80166b17c6..efd75b2a67 100644 --- a/src/test/test-install-root.c +++ b/src/test/test-install-root.c @@ -578,7 +578,7 @@ TEST(preset_and_list) { UnitFileState state; bool got_yes = false, got_no = false; UnitFileList *fl; - _cleanup_(hashmap_freep) Hashmap *h = NULL; + _cleanup_hashmap_free_ Hashmap *h = NULL; CLEANUP_ARRAY(changes, n_changes, install_changes_free); diff --git a/src/test/test-install.c b/src/test/test-install.c index 9fba0a8ca8..b54252efc8 100644 --- a/src/test/test-install.c +++ b/src/test/test-install.c @@ -20,7 +20,7 @@ static void dump_changes(InstallChange *c, unsigned n) { } int main(int argc, char* argv[]) { - _cleanup_(hashmap_freep) Hashmap *h = NULL; + _cleanup_hashmap_free_ Hashmap *h = NULL; UnitFileList *p; int r; const char *const files[] = { "avahi-daemon.service", NULL }; diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c index 708abb7026..e4ee9f697d 100644 --- a/src/test/test-load-fragment.c +++ b/src/test/test-load-fragment.c @@ -41,7 +41,7 @@ STATIC_DESTRUCTOR_REGISTER(runtime_dir, rm_rf_physical_and_freep); TEST_RET(unit_file_get_set) { int r; - _cleanup_(hashmap_freep) Hashmap *h = NULL; + _cleanup_hashmap_free_ Hashmap *h = NULL; UnitFileList *p; h = hashmap_new(&unit_file_list_hash_ops_free); diff --git a/src/test/test-loop-block.c b/src/test/test-loop-block.c index fad5496052..f76d3543eb 100644 --- a/src/test/test-loop-block.c +++ b/src/test/test-loop-block.c @@ -143,7 +143,7 @@ static int run(int argc, char *argv[]) { sd_id128_t id; #endif _cleanup_free_ char *p = NULL, *cmd = NULL; - _cleanup_(pclosep) FILE *sfdisk = NULL; + _cleanup_pclose_ FILE *sfdisk = NULL; _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; _cleanup_close_ int fd = -EBADF; int r; diff --git a/src/test/test-set.c b/src/test/test-set.c index 259ffa7ce2..0d5a6a1856 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -145,8 +145,8 @@ TEST(set_ensure_allocated) { } TEST(set_copy) { - _cleanup_(set_freep) Set *s = NULL; - _cleanup_(set_free_freep) Set *copy = NULL; + _cleanup_set_free_ Set *s = NULL; + _cleanup_set_free_free_ Set *copy = NULL; char *key1, *key2, *key3, *key4; key1 = strdup("key1"); diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 5feafc95e0..cfd662b329 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -260,7 +260,7 @@ TEST(invalid_unquote) { } TEST(strv_split) { - _cleanup_(strv_free_erasep) char **l = NULL; + _cleanup_strv_free_erase_ char **l = NULL; const char str[] = "one,two,three"; l = strv_split(str, ","); diff --git a/src/test/test-varlink.c b/src/test/test-varlink.c index 679d235c78..631305e8f0 100644 --- a/src/test/test-varlink.c +++ b/src/test/test-varlink.c @@ -272,7 +272,7 @@ int main(int argc, char *argv[]) { _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; - _cleanup_(close_pairp) int block_fds[2] = PIPE_EBADF; + _cleanup_close_pair_ int block_fds[2] = PIPE_EBADF; pthread_t t; const char *sp; diff --git a/src/tmpfiles/offline-passwd.c b/src/tmpfiles/offline-passwd.c index edf0dfb73d..7b5592b9c2 100644 --- a/src/tmpfiles/offline-passwd.c +++ b/src/tmpfiles/offline-passwd.c @@ -35,7 +35,7 @@ static int open_passwd_file(const char *root, const char *fname, FILE **ret_file } static int populate_uid_cache(const char *root, Hashmap **ret) { - _cleanup_(hashmap_freep) Hashmap *cache = NULL; + _cleanup_hashmap_free_ Hashmap *cache = NULL; int r; cache = hashmap_new(&uid_gid_hash_ops); @@ -76,7 +76,7 @@ static int populate_uid_cache(const char *root, Hashmap **ret) { } static int populate_gid_cache(const char *root, Hashmap **ret) { - _cleanup_(hashmap_freep) Hashmap *cache = NULL; + _cleanup_hashmap_free_ Hashmap *cache = NULL; int r; cache = hashmap_new(&uid_gid_hash_ops); diff --git a/src/tmpfiles/test-offline-passwd.c b/src/tmpfiles/test-offline-passwd.c index b70cef4753..ef49582510 100644 --- a/src/tmpfiles/test-offline-passwd.c +++ b/src/tmpfiles/test-offline-passwd.c @@ -17,7 +17,7 @@ static void test_resolve_one(const char *name) { log_info("/* %s(\"%s\") */", __func__, name); - _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL; + _cleanup_hashmap_free_ Hashmap *uid_cache = NULL, *gid_cache = NULL; uid_t uid = UID_INVALID; gid_t gid = GID_INVALID; int r; diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 1cb8f06b3e..af1b904d32 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -4055,7 +4055,7 @@ static int read_config_file( bool ignore_enoent, bool *invalid_config) { - _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL; + _cleanup_hashmap_free_ Hashmap *uid_cache = NULL, *gid_cache = NULL; _cleanup_fclose_ FILE *_f = NULL; _cleanup_free_ char *pp = NULL; unsigned v = 0; diff --git a/src/udev/udevadm-lock.c b/src/udev/udevadm-lock.c index b9f18d1552..81b21f0ab0 100644 --- a/src/udev/udevadm-lock.c +++ b/src/udev/udevadm-lock.c @@ -287,7 +287,7 @@ static int lock_device( } int lock_main(int argc, char *argv[], void *userdata) { - _cleanup_(fdset_freep) FDSet *fds = NULL; + _cleanup_fdset_free_ FDSet *fds = NULL; _cleanup_free_ dev_t *devnos = NULL; size_t n_devnos = 0; usec_t deadline; diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 67675b4b7f..a7db3fb0d5 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -910,7 +910,7 @@ 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; + _cleanup_closedir_ DIR *d = NULL; 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 918f6599ce..616c017357 100644 --- a/src/xdg-autostart-generator/xdg-autostart-generator.c +++ b/src/xdg-autostart-generator/xdg-autostart-generator.c @@ -95,7 +95,7 @@ static int enumerate_xdg_autostart(Hashmap *all_services) { } static int run(const char *dest, const char *dest_early, const char *dest_late) { - _cleanup_(hashmap_freep) Hashmap *all_services = NULL; + _cleanup_hashmap_free_ Hashmap *all_services = NULL; XdgAutostartService *service; int r; From 3256e2027b9f2cba83354b03ea805c9047e1f574 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 22:53:23 +0900 Subject: [PATCH 4/8] sd-bus: rename introspect_free() -> introspect_done --- src/libsystemd/sd-bus/bus-introspect.c | 2 +- src/libsystemd/sd-bus/bus-introspect.h | 2 +- src/libsystemd/sd-bus/bus-objects.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c index 3d4c47c6dd..27149dfcc3 100644 --- a/src/libsystemd/sd-bus/bus-introspect.c +++ b/src/libsystemd/sd-bus/bus-introspect.c @@ -277,7 +277,7 @@ int introspect_finish(struct introspect *i, char **ret) { return 0; } -void introspect_free(struct introspect *i) { +void introspect_done(struct introspect *i) { assert(i); /* Normally introspect_finish() does all the work, this is just a backup for error paths */ diff --git a/src/libsystemd/sd-bus/bus-introspect.h b/src/libsystemd/sd-bus/bus-introspect.h index 19e3ef09e2..8bd23a50fd 100644 --- a/src/libsystemd/sd-bus/bus-introspect.h +++ b/src/libsystemd/sd-bus/bus-introspect.h @@ -23,4 +23,4 @@ int introspect_write_interface( const char *interface_name, const sd_bus_vtable *v); int introspect_finish(struct introspect *i, char **ret); -void introspect_free(struct introspect *i); +void introspect_done(struct introspect *i); diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 80dc86d01a..c25c40ff37 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -927,7 +927,7 @@ int introspect_path( sd_bus_error *error) { _cleanup_ordered_set_free_free_ OrderedSet *s = NULL; - _cleanup_(introspect_free) struct introspect intro = {}; + _cleanup_(introspect_done) struct introspect intro = {}; bool empty; int r; From a694747d45478336ad9b58d2050ada0fbc92443d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 22:56:02 +0900 Subject: [PATCH 5/8] elf-util: rename stack_context_destroy() -> stack_context_done() --- src/shared/elf-util.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 68d186117f..91f42de400 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -158,9 +158,8 @@ typedef struct StackContext { Set **modules; } StackContext; -static StackContext* stack_context_destroy(StackContext *c) { - if (!c) - return NULL; +static void stack_context_done(StackContext *c) { + assert(c); c->f = safe_fclose(c->f); @@ -173,8 +172,6 @@ static StackContext* stack_context_destroy(StackContext *c) { sym_elf_end(c->elf); c->elf = NULL; } - - return NULL; } DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(Elf *, sym_elf_end, NULL); @@ -571,8 +568,8 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; - _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_destroy) */ - _cleanup_(stack_context_destroy) StackContext c = { + _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_done) */ + _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, .modules = &modules, }; @@ -637,8 +634,8 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **ret_package_metadata) { _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL, *elf_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; - _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_destroy) */ - _cleanup_(stack_context_destroy) StackContext c = { + _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_done) */ + _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, .modules = &modules, }; From 24b0c6c2c9e0cf10e97aaa9b1bda7398857cd0fc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 May 2023 23:03:26 +0900 Subject: [PATCH 6/8] calendarspec: rename arguments --- src/shared/calendarspec.c | 12 ++++++------ src/shared/calendarspec.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c index fa19e4c95a..9e43a4689e 100644 --- a/src/shared/calendarspec.c +++ b/src/shared/calendarspec.c @@ -336,14 +336,14 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us _format_chain(f, space, c, /* start = */ true, usec); } -int calendar_spec_to_string(const CalendarSpec *c, char **p) { +int calendar_spec_to_string(const CalendarSpec *c, char **ret) { _cleanup_free_ char *buf = NULL; _cleanup_fclose_ FILE *f = NULL; size_t sz = 0; int r; assert(c); - assert(p); + assert(ret); f = open_memstream_unlocked(&buf, &sz); if (!f) @@ -392,7 +392,7 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) { if (!buf) return -ENOMEM; - *p = TAKE_PTR(buf); + *ret = TAKE_PTR(buf); return 0; } @@ -879,7 +879,7 @@ finish: return 0; } -int calendar_spec_from_string(const char *p, CalendarSpec **spec) { +int calendar_spec_from_string(const char *p, CalendarSpec **ret) { const char *utc; _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; _cleanup_free_ char *p_tmp = NULL; @@ -1099,8 +1099,8 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { if (!calendar_spec_valid(c)) return -EINVAL; - if (spec) - *spec = TAKE_PTR(c); + if (ret) + *ret = TAKE_PTR(c); return 0; } diff --git a/src/shared/calendarspec.h b/src/shared/calendarspec.h index 5a04ac018d..60c1c79267 100644 --- a/src/shared/calendarspec.h +++ b/src/shared/calendarspec.h @@ -36,8 +36,8 @@ CalendarSpec* calendar_spec_free(CalendarSpec *c); bool calendar_spec_valid(CalendarSpec *spec); -int calendar_spec_to_string(const CalendarSpec *spec, char **p); -int calendar_spec_from_string(const char *p, CalendarSpec **spec); +int calendar_spec_to_string(const CalendarSpec *spec, char **ret); +int calendar_spec_from_string(const char *p, CalendarSpec **ret); int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next); From abe72100cfc292093153d48a132a5ab1b5f61dd5 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 26 May 2023 15:22:03 +0900 Subject: [PATCH 7/8] util: introduce memstream-util There is many pitfalls in using memstream. Let's introduce a wrapper to make us safely use it. --- src/basic/memstream-util.c | 75 ++++++++++++++++++++++++++++++++++ src/basic/memstream-util.h | 27 ++++++++++++ src/basic/meson.build | 1 + src/test/meson.build | 1 + src/test/test-memstream-util.c | 60 +++++++++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 src/basic/memstream-util.c create mode 100644 src/basic/memstream-util.h create mode 100644 src/test/test-memstream-util.c diff --git a/src/basic/memstream-util.c b/src/basic/memstream-util.c new file mode 100644 index 0000000000..4e147fd78f --- /dev/null +++ b/src/basic/memstream-util.c @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "alloc-util.h" +#include "fd-util.h" +#include "fileio.h" +#include "log.h" +#include "memstream-util.h" + +void memstream_done(MemStream *m) { + assert(m); + + /* First, close file stream, as the buffer may be reallocated on close. */ + safe_fclose(m->f); + + /* Then, free buffer. */ + free(m->buf); +} + +FILE* memstream_init(MemStream *m) { + assert(m); + assert(!m->f); + + m->f = open_memstream_unlocked(&m->buf, &m->sz); + return m->f; +} + +int memstream_finalize(MemStream *m, char **ret_buf, size_t *ret_size) { + int r; + + assert(m); + assert(m->f); + assert(ret_buf); + + /* Add terminating NUL, so that the output buffer is a valid string. */ + fputc('\0', m->f); + + r = fflush_and_check(m->f); + if (r < 0) + return r; + + m->f = safe_fclose(m->f); + + /* On fclose(), the buffer may be reallocated, and may trigger OOM. */ + if (!m->buf) + return -ENOMEM; + + assert(m->sz > 0); + + *ret_buf = TAKE_PTR(m->buf); + if (ret_size) + *ret_size = m->sz - 1; + + m->sz = 0; /* For safety when the MemStream object will be reused later. */ + return 0; +} + +int memstream_dump_internal( + int level, + int error, + const char *file, + int line, + const char *func, + MemStream *m) { + + _cleanup_free_ char *buf = NULL; + int r; + + assert(m); + + r = memstream_finalize(m, &buf, NULL); + if (r < 0) + return log_full_errno(level, r, "Failed to flush memstream: %m: %m"); + + return log_dump_internal(level, error, file, line, func, buf); +} diff --git a/src/basic/memstream-util.h b/src/basic/memstream-util.h new file mode 100644 index 0000000000..1aa5651bcf --- /dev/null +++ b/src/basic/memstream-util.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include + +#include "macro.h" + +typedef struct MemStream { + FILE *f; + char *buf; + size_t sz; +} MemStream; + +void memstream_done(MemStream *m); +FILE* memstream_init(MemStream *m); +int memstream_finalize(MemStream *m, char **ret_buf, size_t *ret_size); + +/* This finalizes the passed memstream. */ +int memstream_dump_internal( + int level, + int error, + const char *file, + int line, + const char *func, + MemStream *m); +#define memstream_dump(level, m) \ + memstream_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, m) diff --git a/src/basic/meson.build b/src/basic/meson.build index 9358a40001..5577c6bc78 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -53,6 +53,7 @@ basic_sources = files( 'memfd-util.c', 'memory-util.c', 'mempool.c', + 'memstream-util.c', 'mkdir.c', 'mountpoint-util.c', 'namespace-util.c', diff --git a/src/test/meson.build b/src/test/meson.build index d7eb2a857d..84d642bf6e 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -115,6 +115,7 @@ simple_tests += files( 'test-memfd-util.c', 'test-memory-util.c', 'test-mempool.c', + 'test-memstream-util.c', 'test-mkdir.c', 'test-modhex.c', 'test-mountpoint-util.c', diff --git a/src/test/test-memstream-util.c b/src/test/test-memstream-util.c new file mode 100644 index 0000000000..254bdcaa15 --- /dev/null +++ b/src/test/test-memstream-util.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "memstream-util.h" +#include "string-util.h" +#include "tests.h" + +TEST(memstream_done) { + _cleanup_(memstream_done) MemStream m = {}; + + assert_se(memstream_init(&m)); +} + +TEST(memstream_empty) { + _cleanup_(memstream_done) MemStream m = {}; + _cleanup_free_ char *buf = NULL; + size_t sz; + + assert_se(memstream_init(&m)); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, "")); + assert_se(sz == 0); +} + +TEST(memstream) { + _cleanup_(memstream_done) MemStream m = {}; + _cleanup_free_ char *buf = NULL; + size_t sz; + FILE *f; + + assert_se(f = memstream_init(&m)); + fputs("hoge", f); + fputs("おはよう!", f); + fputs(u8"😀😀😀", f); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, u8"hogeおはよう!😀😀😀")); + assert_se(sz == strlen(u8"hogeおはよう!😀😀😀")); + + buf = mfree(buf); + + assert_se(f = memstream_init(&m)); + fputs("second", f); + assert_se(memstream_finalize(&m, &buf, &sz) >= 0); + assert_se(streq(buf, "second")); + assert_se(sz == strlen("second")); +} + +TEST(memstream_dump) { + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; + + assert_se(f = memstream_init(&m)); + fputs("first", f); + assert_se(memstream_dump(LOG_DEBUG, &m) >= 0); + + assert_se(f = memstream_init(&m)); + fputs("second", f); + assert_se(memstream_dump(LOG_DEBUG, &m) >= 0); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); From 2485b7e2b8a66504e0b6f3bfd03dd48ea5629497 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 26 May 2023 15:40:12 +0900 Subject: [PATCH 8/8] tree-wide: use memstream-util --- src/basic/string-util.c | 20 ++--- src/boot/measure.c | 8 +- src/busctl/busctl.c | 14 ++-- src/core/dbus-cgroup.c | 87 ++++++++++---------- src/core/dbus-execute.c | 18 ++--- src/core/fuzz-unit-file.c | 10 +-- src/core/manager-dump.c | 22 ++---- src/coredump/coredump.c | 25 ++---- src/fuzz/fuzz-json.c | 9 ++- src/home/user-record-sign.c | 14 ++-- src/journal/journalctl.c | 22 ++---- src/libsystemd/sd-bus/bus-introspect.c | 96 ++++++++++++----------- src/libsystemd/sd-bus/bus-introspect.h | 5 +- src/libsystemd/sd-bus/bus-match.c | 15 ++-- src/libsystemd/sd-bus/fuzz-bus-match.c | 8 +- src/libsystemd/sd-bus/fuzz-bus-message.c | 8 +- src/libsystemd/sd-bus/test-bus-marshal.c | 45 +++++------ src/network/generator/network-generator.c | 73 +++-------------- src/oom/oomd-manager.c | 23 ++---- src/oom/oomd-util.c | 25 ++---- src/resolve/fuzz-resource-record.c | 8 +- src/resolve/resolved-dns-dnssec.c | 21 ++--- src/resolve/resolved-manager.c | 19 ++--- src/shared/bus-util.c | 19 ++--- src/shared/calendarspec.c | 21 ++--- src/shared/common-signal.c | 19 ++--- src/shared/elf-util.c | 71 ++++++----------- src/shared/format-table.c | 33 ++++---- src/shared/json.c | 23 ++---- 29 files changed, 302 insertions(+), 479 deletions(-) diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 61737ffb41..5d259cbf81 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -15,6 +15,7 @@ #include "locale-util.h" #include "macro.h" #include "memory-util.h" +#include "memstream-util.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" @@ -604,9 +605,9 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { STATE_CSI, STATE_CSO, } state = STATE_OTHER; - _cleanup_free_ char *obuf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0; + _cleanup_(memstream_done) MemStream m = {}; + size_t isz, shift[2] = {}, n_carriage_returns = 0; + FILE *f; assert(ibuf); assert(*ibuf); @@ -629,7 +630,7 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we * created f here and it doesn't leave our scope. */ - f = open_memstream_unlocked(&obuf, &osz); + f = memstream_init(&m); if (!f) return NULL; @@ -714,19 +715,12 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { } } - if (fflush_and_check(f) < 0) - return NULL; - - f = safe_fclose(f); - - if (!obuf) + char *obuf; + if (memstream_finalize(&m, &obuf, _isz) < 0) return NULL; free_and_replace(*ibuf, obuf); - if (_isz) - *_isz = osz; - if (highlight) { highlight[0] += shift[0]; highlight[1] += shift[1]; diff --git a/src/boot/measure.c b/src/boot/measure.c index a3b6e4f9df..bd7cc78399 100644 --- a/src/boot/measure.c +++ b/src/boot/measure.c @@ -11,6 +11,7 @@ #include "hexdecoct.h" #include "json.h" #include "main-func.h" +#include "memstream-util.h" #include "openssl-util.h" #include "parse-argument.h" #include "parse-util.h" @@ -779,13 +780,12 @@ static int verb_sign(int argc, char *argv[], void *userdata) { if (!pubkey) return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to parse public key '%s'.", arg_public_key); } else { - _cleanup_free_ char *data = NULL; - _cleanup_fclose_ FILE *tf = NULL; - size_t sz; + _cleanup_(memstream_done) MemStream m = {}; + FILE *tf; /* No public key was specified, let's derive it automatically, if we can */ - tf = open_memstream_unlocked(&data, &sz); + tf = memstream_init(&m); if (!tf) return log_oom(); diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 4d69aee5eb..b1ea0dea56 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -21,6 +21,7 @@ #include "json.h" #include "log.h" #include "main-func.h" +#include "memstream-util.h" #include "os-util.h" #include "pager.h" #include "parse-argument.h" @@ -1035,12 +1036,12 @@ static int introspect(int argc, char **argv, void *userdata) { return bus_log_parse_error(r); for (;;) { - _cleanup_fclose_ FILE *mf = NULL; + _cleanup_(memstream_done) MemStream ms = {}; _cleanup_free_ char *buf = NULL; const char *name, *contents; - size_t sz = 0; Member *z; char type; + FILE *mf; r = sd_bus_message_enter_container(reply, 'e', "sv"); if (r < 0) @@ -1062,7 +1063,7 @@ static int introspect(int argc, char **argv, void *userdata) { if (r < 0) return bus_log_parse_error(r); - mf = open_memstream_unlocked(&buf, &sz); + mf = memstream_init(&ms); if (!mf) return log_oom(); @@ -1070,10 +1071,9 @@ static int introspect(int argc, char **argv, void *userdata) { if (r < 0) return bus_log_parse_error(r); - mf = safe_fclose(mf); - - if (!buf) - return bus_log_parse_error(ENOMEM); + r = memstream_finalize(&ms, &buf, NULL); + if (r < 0) + return log_error_errno(r, "Failed to flush and close memstream: %m"); z = set_get(members, &((Member) { .type = "property", diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index c373f0bbe8..726035b00a 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -18,6 +18,7 @@ #include "in-addr-prefix-util.h" #include "ip-protocol-list.h" #include "limits-util.h" +#include "memstream-util.h" #include "parse-util.h" #include "path-util.h" #include "percent-util.h" @@ -657,15 +658,16 @@ static int bus_cgroup_set_transient_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) *filters = strv_free(*filters); unit_invalidate_cgroup_bpf(u); - f = open_memstream_unlocked(&buf, &size); + + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -675,7 +677,7 @@ static int bus_cgroup_set_transient_property( STRV_FOREACH(entry, *filters) fprintf(f, "%s=%s\n", name, *entry); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -736,15 +738,15 @@ static int bus_cgroup_set_transient_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) while (c->bpf_foreign_programs) cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -756,7 +758,7 @@ static int bus_cgroup_set_transient_property( bpf_cgroup_attach_type_to_string(fp->attach_type), fp->bpffs_path); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -1403,9 +1405,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) LIST_FOREACH(device_limits, a, c->io_device_limits) @@ -1413,7 +1415,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_IO); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1422,9 +1424,10 @@ int bus_cgroup_set_property( if (a->limits[iol_type] != cgroup_io_limit_defaults[iol_type]) fprintf(f, "%s=%s %" PRIu64 "\n", name, a->path, a->limits[iol_type]); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; + unit_write_setting(u, flags, name, buf); } @@ -1480,9 +1483,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) while (c->io_device_weights) @@ -1490,7 +1493,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_IO); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1498,9 +1501,10 @@ int bus_cgroup_set_property( LIST_FOREACH(device_weights, a, c->io_device_weights) fprintf(f, "IODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; + unit_write_setting(u, flags, name, buf); } @@ -1553,9 +1557,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) while (c->io_device_latencies) @@ -1563,7 +1567,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_IO); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1572,9 +1576,10 @@ int bus_cgroup_set_property( fprintf(f, "IODeviceLatencyTargetSec=%s %s\n", a->path, FORMAT_TIMESPAN(a->target_usec, 1)); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; + unit_write_setting(u, flags, name, buf); } @@ -1638,9 +1643,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths) { @@ -1652,7 +1657,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1668,7 +1673,7 @@ int bus_cgroup_set_property( fprintf(f, "BlockIOWriteBandwidth=%s %" PRIu64 "\n", a->path, a->wbps); } - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -1727,9 +1732,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) while (c->blockio_device_weights) @@ -1737,7 +1742,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1745,7 +1750,7 @@ int bus_cgroup_set_property( LIST_FOREACH(device_weights, a, c->blockio_device_weights) fprintf(f, "BlockIODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -1830,9 +1835,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) while (c->device_allow) @@ -1840,7 +1845,7 @@ int bus_cgroup_set_property( unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1848,9 +1853,10 @@ int bus_cgroup_set_property( LIST_FOREACH(device_allow, a, c->device_allow) fprintf(f, "DeviceAllow=%s %s%s%s\n", a->path, a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : ""); - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; + unit_write_setting(u, flags, name, buf); } @@ -1939,14 +1945,15 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; Set **prefixes; bool *reduced; + FILE *f; unit_invalidate_cgroup_bpf(u); - f = open_memstream_unlocked(&buf, &size); + + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1971,7 +1978,7 @@ int bus_cgroup_set_property( IN_ADDR_PREFIX_TO_STRING(p->family, &p->address, p->prefixlen)); } - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -2100,9 +2107,9 @@ int bus_cgroup_set_property( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) cgroup_context_remove_socket_bind(list); @@ -2114,7 +2121,7 @@ int bus_cgroup_set_property( "Starting this unit will fail!", u->id); } - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -2127,7 +2134,7 @@ int bus_cgroup_set_property( fputc('\n', f); } - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 4c413f4d29..a8553c962c 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -26,6 +26,7 @@ #include "io-util.h" #include "ioprio-util.h" #include "journal-file.h" +#include "memstream-util.h" #include "missing_ioprio.h" #include "mountpoint-util.h" #include "namespace.h" @@ -1616,14 +1617,14 @@ int bus_set_transient_exec_command( return r; if (!UNIT_WRITE_FLAGS_NOOP(flags)) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + FILE *f; if (n == 0) *exec_command = exec_command_free_list(*exec_command); - f = open_memstream_unlocked(&buf, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1656,7 +1657,7 @@ int bus_set_transient_exec_command( } } - r = fflush_and_check(f); + r = memstream_finalize(&m, &buf, NULL); if (r < 0) return r; @@ -3220,17 +3221,16 @@ int bus_exec_context_set_transient_property( return 1; } else if (streq(name, "EnvironmentFiles")) { - + _cleanup_(memstream_done) MemStream m = {}; _cleanup_free_ char *joined = NULL; - _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **l = NULL; - size_t size = 0; + FILE *f; r = sd_bus_message_enter_container(message, 'a', "(sb)"); if (r < 0) return r; - f = open_memstream_unlocked(&joined, &size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -3286,7 +3286,7 @@ int bus_exec_context_set_transient_property( if (r < 0) return r; - r = fflush_and_check(f); + r = memstream_finalize(&m, &joined, NULL); if (r < 0) return r; diff --git a/src/core/fuzz-unit-file.c b/src/core/fuzz-unit-file.c index 8e0ace2540..a11d6b53b5 100644 --- a/src/core/fuzz-unit-file.c +++ b/src/core/fuzz-unit-file.c @@ -6,14 +6,13 @@ #include "install.h" #include "load-fragment.h" #include "manager-dump.h" +#include "memstream-util.h" #include "string-util.h" #include "unit-serialize.h" #include "utf8.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_free_ char *out = NULL; /* out should be freed after g */ - size_t out_size; - _cleanup_fclose_ FILE *f = NULL, *g = NULL; + _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *p = NULL; UnitType t; _cleanup_(manager_freep) Manager *m = NULL; @@ -79,9 +78,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { u, NULL); - g = open_memstream_unlocked(&out, &out_size); - assert_se(g); + _cleanup_(memstream_done) MemStream ms = {}; + FILE *g; + assert_se(g = memstream_init(&ms)); unit_dump(u, g, ""); manager_dump(m, g, /* patterns= */ NULL, ">>>"); diff --git a/src/core/manager-dump.c b/src/core/manager-dump.c index 35143ebddf..e39aff5d89 100644 --- a/src/core/manager-dump.c +++ b/src/core/manager-dump.c @@ -5,6 +5,7 @@ #include "fileio.h" #include "hashmap.h" #include "manager-dump.h" +#include "memstream-util.h" #include "unit-serialize.h" void manager_dump_jobs(Manager *s, FILE *f, char **patterns, const char *prefix) { @@ -75,32 +76,19 @@ void manager_dump(Manager *m, FILE *f, char **patterns, const char *prefix) { } int manager_get_dump_string(Manager *m, char **patterns, char **ret) { - _cleanup_free_ char *dump = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size; - int r; + _cleanup_(memstream_done) MemStream ms = {}; + FILE *f; assert(m); assert(ret); - f = open_memstream_unlocked(&dump, &size); + f = memstream_init(&ms); if (!f) return -errno; manager_dump(m, f, patterns, NULL); - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!dump) - return -ENOMEM; - - *ret = TAKE_PTR(dump); - - return 0; + return memstream_finalize(&ms, ret, NULL); } void manager_test_summary(Manager *m) { diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index a6b0d96488..b355970f0f 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -36,6 +36,7 @@ #include "macro.h" #include "main-func.h" #include "memory-util.h" +#include "memstream-util.h" #include "mkdir-label.h" #include "parse-util.h" #include "process-util.h" @@ -641,17 +642,16 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s * flags: 0100002 * EOF */ -static int compose_open_fds(pid_t pid, char **open_fds) { +static int compose_open_fds(pid_t pid, char **ret) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_closedir_ DIR *proc_fd_dir = NULL; _cleanup_close_ int proc_fdinfo_fd = -EBADF; - _cleanup_free_ char *buffer = NULL; - _cleanup_fclose_ FILE *stream = NULL; const char *fddelim = "", *path; - size_t size = 0; + FILE *stream; int r; assert(pid >= 0); - assert(open_fds != NULL); + assert(ret); path = procfs_file_alloca(pid, "fd"); proc_fd_dir = opendir(path); @@ -662,7 +662,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (proc_fdinfo_fd < 0) return -errno; - stream = open_memstream_unlocked(&buffer, &size); + stream = memstream_init(&m); if (!stream) return -ENOMEM; @@ -701,18 +701,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) { } } - errno = 0; - stream = safe_fclose(stream); - - if (errno > 0) - return -errno; - - if (!buffer) - return -ENOMEM; - - *open_fds = TAKE_PTR(buffer); - - return 0; + return memstream_finalize(&m, ret, NULL); } static int get_process_ns(pid_t pid, const char *namespace, ino_t *ns) { diff --git a/src/fuzz/fuzz-json.c b/src/fuzz/fuzz-json.c index c393fcf394..b2cca78501 100644 --- a/src/fuzz/fuzz-json.c +++ b/src/fuzz/fuzz-json.c @@ -5,12 +5,13 @@ #include "fd-util.h" #include "fuzz.h" #include "json.h" +#include "memstream-util.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_free_ char *out = NULL; /* out should be freed after g */ - size_t out_size; - _cleanup_fclose_ FILE *f = NULL, *g = NULL; + _cleanup_(memstream_done) MemStream m = {}; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_fclose_ FILE *f = NULL; + FILE *g = NULL; int r; /* Disable most logging if not running standalone */ @@ -27,7 +28,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { } if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) - assert_se(g = open_memstream_unlocked(&out, &out_size)); + assert_se(g = memstream_init(&m)); json_variant_dump(v, 0, g ?: stdout, NULL); json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g ?: stdout, NULL); diff --git a/src/home/user-record-sign.c b/src/home/user-record-sign.c index ab73fba93f..2cef9969b7 100644 --- a/src/home/user-record-sign.c +++ b/src/home/user-record-sign.c @@ -3,6 +3,7 @@ #include #include "fd-util.h" +#include "memstream-util.h" #include "user-record-sign.h" #include "fileio.h" @@ -30,13 +31,14 @@ static int user_record_signable_json(UserRecord *ur, char **ret) { DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(EVP_MD_CTX*, EVP_MD_CTX_free, NULL); int user_record_sign(UserRecord *ur, EVP_PKEY *private_key, UserRecord **ret) { + _cleanup_(memstream_done) MemStream m = {}; _cleanup_(json_variant_unrefp) JsonVariant *encoded = NULL, *v = NULL; _cleanup_(user_record_unrefp) UserRecord *signed_ur = NULL; _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *md_ctx = NULL; _cleanup_free_ char *text = NULL, *key = NULL; - size_t signature_size = 0, key_size = 0; _cleanup_free_ void *signature = NULL; - _cleanup_fclose_ FILE *mf = NULL; + size_t signature_size = 0; + FILE *f; int r; assert(ur); @@ -65,14 +67,14 @@ int user_record_sign(UserRecord *ur, EVP_PKEY *private_key, UserRecord **ret) { if (EVP_DigestSign(md_ctx, signature, &signature_size, (uint8_t*) text, strlen(text)) <= 0) return -EIO; - mf = open_memstream_unlocked(&key, &key_size); - if (!mf) + f = memstream_init(&m); + if (!f) return -ENOMEM; - if (PEM_write_PUBKEY(mf, private_key) <= 0) + if (PEM_write_PUBKEY(f, private_key) <= 0) return -EIO; - r = fflush_and_check(mf); + r = memstream_finalize(&m, &key, NULL); if (r < 0) return r; diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 4f9c1f4780..ab7760290a 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -49,6 +49,7 @@ #include "logs-show.h" #include "main-func.h" #include "memory-util.h" +#include "memstream-util.h" #include "missing_sched.h" #include "mkdir.h" #include "mount-util.h" @@ -1758,15 +1759,14 @@ static int format_journal_url( sd_id128_t machine, bool full, char **ret_url) { - _cleanup_free_ char *url = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t url_size = 0; - int r; + + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; assert(seed); assert(seed_size > 0); - f = open_memstream_unlocked(&url, &url_size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1787,17 +1787,7 @@ static int format_journal_url( fprintf(f, ";hostname=%s", hn); } - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!url) - return -ENOMEM; - - *ret_url = TAKE_PTR(url); - return 0; + return memstream_finalize(&m, ret_url, NULL); } #endif diff --git a/src/libsystemd/sd-bus/bus-introspect.c b/src/libsystemd/sd-bus/bus-introspect.c index 27149dfcc3..84c8774c6c 100644 --- a/src/libsystemd/sd-bus/bus-introspect.c +++ b/src/libsystemd/sd-bus/bus-introspect.c @@ -8,6 +8,7 @@ #include "fd-util.h" #include "fileio.h" #include "memory-util.h" +#include "memstream-util.h" #include "string-util.h" #define BUS_INTROSPECT_DOCTYPE \ @@ -68,52 +69,59 @@ " \n" int introspect_begin(struct introspect *i, bool trusted) { + FILE *f; + assert(i); *i = (struct introspect) { .trusted = trusted, }; - i->f = open_memstream_unlocked(&i->introspection, &i->size); - if (!i->f) + f = memstream_init(&i->m); + if (!f) return -ENOMEM; fputs(BUS_INTROSPECT_DOCTYPE - "\n", i->f); + "\n", f); return 0; } int introspect_write_default_interfaces(struct introspect *i, bool object_manager) { assert(i); + assert(i->m.f); fputs(BUS_INTROSPECT_INTERFACE_PEER BUS_INTROSPECT_INTERFACE_INTROSPECTABLE - BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f); + BUS_INTROSPECT_INTERFACE_PROPERTIES, i->m.f); if (object_manager) - fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f); + fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->m.f); return 0; } -static int set_interface_name(struct introspect *intro, const char *interface_name) { - if (streq_ptr(intro->interface_name, interface_name)) +static int set_interface_name(struct introspect *i, const char *interface_name) { + assert(i); + assert(i->m.f); + + if (streq_ptr(i->interface_name, interface_name)) return 0; - if (intro->interface_name) - fputs(" \n", intro->f); + if (i->interface_name) + fputs(" \n", i->m.f); if (interface_name) - fprintf(intro->f, " \n", interface_name); + fprintf(i->m.f, " \n", interface_name); - return free_and_strdup(&intro->interface_name, interface_name); + return free_and_strdup(&i->interface_name, interface_name); } int introspect_write_child_nodes(struct introspect *i, OrderedSet *s, const char *prefix) { char *node; assert(i); + assert(i->m.f); assert(prefix); assert_se(set_interface_name(i, NULL) >= 0); @@ -123,7 +131,7 @@ int introspect_write_child_nodes(struct introspect *i, OrderedSet *s, const char e = object_path_startswith(node, prefix); if (e && e[0]) - fprintf(i->f, " \n", e); + fprintf(i->m.f, " \n", e); free(node); } @@ -132,28 +140,31 @@ int introspect_write_child_nodes(struct introspect *i, OrderedSet *s, const char } static void introspect_write_flags(struct introspect *i, int type, uint64_t flags) { + assert(i); + assert(i->m.f); + if (flags & SD_BUS_VTABLE_DEPRECATED) - fputs(" \n", i->f); + fputs(" \n", i->m.f); if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY)) - fputs(" \n", i->f); + fputs(" \n", i->m.f); if (IN_SET(type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) { if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT) - fputs(" \n", i->f); + fputs(" \n", i->m.f); if (flags & SD_BUS_VTABLE_PROPERTY_CONST) - fputs(" \n", i->f); + fputs(" \n", i->m.f); else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) - fputs(" \n", i->f); + fputs(" \n", i->m.f); else if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) - fputs(" \n", i->f); + fputs(" \n", i->m.f); } if (!i->trusted && IN_SET(type, _SD_BUS_VTABLE_METHOD, _SD_BUS_VTABLE_WRITABLE_PROPERTY) && !(flags & SD_BUS_VTABLE_UNPRIVILEGED)) - fputs(" \n", i->f); + fputs(" \n", i->m.f); } /* Note that "names" is both an input and an output parameter. It initially points to the first argument name in a @@ -161,6 +172,9 @@ static void introspect_write_flags(struct introspect *i, int type, uint64_t flag static int introspect_write_arguments(struct introspect *i, const char *signature, const char **names, const char *direction) { int r; + assert(i); + assert(i->m.f); + for (;;) { size_t l; @@ -171,17 +185,17 @@ static int introspect_write_arguments(struct introspect *i, const char *signatur if (r < 0) return r; - fprintf(i->f, " m.f, " f, " name=\"%s\"", *names); + fprintf(i->m.f, " name=\"%s\"", *names); *names += strlen(*names) + 1; } if (direction) - fprintf(i->f, " direction=\"%s\"/>\n", direction); + fprintf(i->m.f, " direction=\"%s\"/>\n", direction); else - fputs("/>\n", i->f); + fputs("/>\n", i->m.f); signature += l; } @@ -197,6 +211,7 @@ int introspect_write_interface( int r; assert(i); + assert(i->m.f); assert(interface_name); r = set_interface_name(i, interface_name); @@ -216,36 +231,36 @@ int introspect_write_interface( case _SD_BUS_VTABLE_START: if (v->flags & SD_BUS_VTABLE_DEPRECATED) - fputs(" \n", i->f); + fputs(" \n", i->m.f); break; case _SD_BUS_VTABLE_METHOD: - fprintf(i->f, " \n", v->x.method.member); + fprintf(i->m.f, " \n", v->x.method.member); if (bus_vtable_has_names(vtable)) names = strempty(v->x.method.names); introspect_write_arguments(i, strempty(v->x.method.signature), &names, "in"); introspect_write_arguments(i, strempty(v->x.method.result), &names, "out"); introspect_write_flags(i, v->type, v->flags); - fputs(" \n", i->f); + fputs(" \n", i->m.f); break; case _SD_BUS_VTABLE_PROPERTY: case _SD_BUS_VTABLE_WRITABLE_PROPERTY: - fprintf(i->f, " \n", + fprintf(i->m.f, " \n", v->x.property.member, v->x.property.signature, v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read"); introspect_write_flags(i, v->type, v->flags); - fputs(" \n", i->f); + fputs(" \n", i->m.f); break; case _SD_BUS_VTABLE_SIGNAL: - fprintf(i->f, " \n", v->x.signal.member); + fprintf(i->m.f, " \n", v->x.signal.member); if (bus_vtable_has_names(vtable)) names = strempty(v->x.signal.names); introspect_write_arguments(i, strempty(v->x.signal.signature), &names, NULL); introspect_write_flags(i, v->type, v->flags); - fputs(" \n", i->f); + fputs(" \n", i->m.f); break; } @@ -255,26 +270,14 @@ int introspect_write_interface( } int introspect_finish(struct introspect *i, char **ret) { - int r; - assert(i); + assert(i->m.f); assert_se(set_interface_name(i, NULL) >= 0); - fputs("\n", i->f); + fputs("\n", i->m.f); - r = fflush_and_check(i->f); - if (r < 0) - return r; - - i->f = safe_fclose(i->f); - - if (!i->introspection) - return -ENOMEM; - - *ret = TAKE_PTR(i->introspection); - - return 0; + return memstream_finalize(&i->m, ret, NULL); } void introspect_done(struct introspect *i) { @@ -282,7 +285,6 @@ void introspect_done(struct introspect *i) { /* Normally introspect_finish() does all the work, this is just a backup for error paths */ - safe_fclose(i->f); + memstream_done(&i->m); free(i->interface_name); - free(i->introspection); } diff --git a/src/libsystemd/sd-bus/bus-introspect.h b/src/libsystemd/sd-bus/bus-introspect.h index 8bd23a50fd..83bcfb2dad 100644 --- a/src/libsystemd/sd-bus/bus-introspect.h +++ b/src/libsystemd/sd-bus/bus-introspect.h @@ -5,13 +5,12 @@ #include "sd-bus.h" +#include "memstream-util.h" #include "ordered-set.h" struct introspect { - FILE *f; + MemStream m; char *interface_name; - char *introspection; - size_t size; bool trusted; }; diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index cc043abfe3..606304d833 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -7,6 +7,7 @@ #include "fd-util.h" #include "fileio.h" #include "hexdecoct.h" +#include "memstream-util.h" #include "sort-util.h" #include "string-util.h" #include "strv.h" @@ -823,9 +824,8 @@ int bus_match_parse( } char *bus_match_to_string(struct bus_match_component *components, size_t n_components) { - _cleanup_free_ char *buffer = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t size = 0; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; int r; if (n_components <= 0) @@ -833,7 +833,7 @@ char *bus_match_to_string(struct bus_match_component *components, size_t n_compo assert(components); - f = open_memstream_unlocked(&buffer, &size); + f = memstream_init(&m); if (!f) return NULL; @@ -855,13 +855,12 @@ char *bus_match_to_string(struct bus_match_component *components, size_t n_compo fputc('\'', f); } - r = fflush_and_check(f); + char *buffer; + r = memstream_finalize(&m, &buffer, NULL); if (r < 0) return NULL; - f = safe_fclose(f); - - return TAKE_PTR(buffer); + return buffer; } int bus_match_add( diff --git a/src/libsystemd/sd-bus/fuzz-bus-match.c b/src/libsystemd/sd-bus/fuzz-bus-match.c index d183460ea7..b79494d888 100644 --- a/src/libsystemd/sd-bus/fuzz-bus-match.c +++ b/src/libsystemd/sd-bus/fuzz-bus-match.c @@ -7,14 +7,14 @@ #include "fd-util.h" #include "fileio.h" #include "fuzz.h" +#include "memstream-util.h" DEFINE_TRIVIAL_DESTRUCTOR(bus_match_donep, struct bus_match_node, bus_match_free); int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_free_ char *out = NULL; /* out should be freed after g */ - size_t out_size; - _cleanup_fclose_ FILE *g = NULL; + _cleanup_(memstream_done) MemStream m = {}; _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; + FILE *g = NULL; int r; if (outside_size_range(size, 0, 65536)) @@ -40,7 +40,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { }; if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) - assert_se(g = open_memstream_unlocked(&out, &out_size)); + assert_se(g = memstream_init(&m)); for (size_t offset = 0; offset < size; ) { _cleanup_free_ char *line = NULL; diff --git a/src/libsystemd/sd-bus/fuzz-bus-message.c b/src/libsystemd/sd-bus/fuzz-bus-message.c index af3dbf4d57..9b04b4422b 100644 --- a/src/libsystemd/sd-bus/fuzz-bus-message.c +++ b/src/libsystemd/sd-bus/fuzz-bus-message.c @@ -7,14 +7,14 @@ #include "fd-util.h" #include "fileio.h" #include "fuzz.h" +#include "memstream-util.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_free_ char *out = NULL; /* out should be freed after g */ - size_t out_size; - _cleanup_fclose_ FILE *g = NULL; + _cleanup_(memstream_done) MemStream ms = {}; _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_free_ void *buffer = NULL; + FILE *g = NULL; int r; /* We don't want to fill the logs with messages about parse errors. @@ -34,7 +34,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { TAKE_PTR(buffer); if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) - assert_se(g = open_memstream_unlocked(&out, &out_size)); + assert_se(g = memstream_init(&ms)); sd_bus_message_dump(m, g ?: stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index 3361b0675e..0044d33a59 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -22,6 +22,7 @@ #include "fd-util.h" #include "fileio.h" #include "log.h" +#include "memstream-util.h" #include "tests.h" static void test_bus_path_encode_unique(void) { @@ -108,20 +109,19 @@ static void test_bus_label_escape(void) { int main(int argc, char *argv[]) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *copy = NULL; - int r, boolean; - const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature; - uint8_t u, v; - void *buffer = NULL; - size_t sz; - _cleanup_free_ char *h = NULL; + _cleanup_free_ char *h = NULL, *first = NULL, *second = NULL, *third = NULL; const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array; - char *s; - _cleanup_free_ char *first = NULL, *second = NULL, *third = NULL; - _cleanup_fclose_ FILE *ms = NULL; - size_t first_size = 0, second_size = 0, third_size = 0; + const char *x, *x2, *y, *z, *a, *b, *c, *d, *a_signature; + size_t sz, first_size, second_size = 0, third_size = 0; _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; - double dbl; + _cleanup_(memstream_done) MemStream ms = {}; + void *buffer = NULL; + int r, boolean; uint64_t u64; + uint8_t u, v; + double dbl; + FILE *mf; + char *s; test_setup_logging(LOG_INFO); @@ -191,10 +191,9 @@ int main(int argc, char *argv[]) { sd_bus_message_dump(m, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); - ms = open_memstream_unlocked(&first, &first_size); - sd_bus_message_dump(m, ms, 0); - fflush(ms); - assert_se(!ferror(ms)); + assert_se(mf = memstream_init(&ms)); + sd_bus_message_dump(m, mf, 0); + assert_se(memstream_finalize(&ms, &first, &first_size) >= 0); r = bus_message_get_blob(m, &buffer, &sz); assert_se(r >= 0); @@ -247,11 +246,9 @@ int main(int argc, char *argv[]) { sd_bus_message_dump(m, stdout, SD_BUS_MESSAGE_DUMP_WITH_HEADER); - fclose(ms); - ms = open_memstream_unlocked(&second, &second_size); - sd_bus_message_dump(m, ms, 0); - fflush(ms); - assert_se(!ferror(ms)); + assert_se(mf = memstream_init(&ms)); + sd_bus_message_dump(m, mf, 0); + assert_se(memstream_finalize(&ms, &second, &second_size) >= 0); assert_se(first_size == second_size); assert_se(memcmp(first, second, first_size) == 0); @@ -353,11 +350,9 @@ int main(int argc, char *argv[]) { r = sd_bus_message_seal(copy, 4712, 0); assert_se(r >= 0); - fclose(ms); - ms = open_memstream_unlocked(&third, &third_size); - sd_bus_message_dump(copy, ms, 0); - fflush(ms); - assert_se(!ferror(ms)); + assert_se(mf = memstream_init(&ms)); + sd_bus_message_dump(copy, mf, 0); + assert_se(memstream_finalize(&ms, &third, &third_size) >= 0); printf("<%.*s>\n", (int) first_size, first); printf("<%.*s>\n", (int) third_size, third); diff --git a/src/network/generator/network-generator.c b/src/network/generator/network-generator.c index 569dcdf511..823cd41f7e 100644 --- a/src/network/generator/network-generator.c +++ b/src/network/generator/network-generator.c @@ -5,6 +5,7 @@ #include "hostname-util.h" #include "log.h" #include "macro.h" +#include "memstream-util.h" #include "netif-naming-scheme.h" #include "network-generator.h" #include "parse-util.h" @@ -1198,97 +1199,49 @@ void link_dump(Link *link, FILE *f) { } int network_format(Network *network, char **ret) { - _cleanup_free_ char *s = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; - int r; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; assert(network); assert(ret); - f = open_memstream_unlocked(&s, &sz); + f = memstream_init(&m); if (!f) return -ENOMEM; network_dump(network, f); - /* Add terminating 0, so that the output buffer is a valid string. */ - fputc('\0', f); - - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!s) - return -ENOMEM; - - *ret = TAKE_PTR(s); - assert(sz > 0); - return (int) sz - 1; + return memstream_finalize(&m, ret, NULL); } int netdev_format(NetDev *netdev, char **ret) { - _cleanup_free_ char *s = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; - int r; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; assert(netdev); assert(ret); - f = open_memstream_unlocked(&s, &sz); + f = memstream_init(&m); if (!f) return -ENOMEM; netdev_dump(netdev, f); - /* Add terminating 0, so that the output buffer is a valid string. */ - fputc('\0', f); - - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!s) - return -ENOMEM; - - *ret = TAKE_PTR(s); - assert(sz > 0); - return (int) sz - 1; + return memstream_finalize(&m, ret, NULL); } int link_format(Link *link, char **ret) { - _cleanup_free_ char *s = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; - int r; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; assert(link); assert(ret); - f = open_memstream_unlocked(&s, &sz); + f = memstream_init(&m); if (!f) return -ENOMEM; link_dump(link, f); - /* Add terminating 0, so that the output buffer is a valid string. */ - fputc('\0', f); - - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!s) - return -ENOMEM; - - *ret = TAKE_PTR(s); - assert(sz > 0); - return (int) sz - 1; + return memstream_finalize(&m, ret, NULL); } diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index c39e75562d..514a4750d2 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -10,6 +10,7 @@ #include "fileio.h" #include "format-util.h" #include "memory-util.h" +#include "memstream-util.h" #include "oomd-manager-bus.h" #include "oomd-manager.h" #include "path-util.h" @@ -807,18 +808,16 @@ int manager_start( } int manager_get_dump_string(Manager *m, char **ret) { - _cleanup_free_ char *dump = NULL; - _cleanup_fclose_ FILE *f = NULL; + _cleanup_(memstream_done) MemStream ms = {}; OomdCGroupContext *c; - size_t size; - int r; + FILE *f; assert(m); assert(ret); - f = open_memstream_unlocked(&dump, &size); + f = memstream_init(&ms); if (!f) - return -errno; + return -ENOMEM; fprintf(f, "Dry Run: %s\n" @@ -840,15 +839,5 @@ int manager_get_dump_string(Manager *m, char **ret) { HASHMAP_FOREACH(c, m->monitored_mem_pressure_cgroup_contexts) oomd_dump_memory_pressure_cgroup_context(c, f, "\t"); - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!dump) - return -ENOMEM; - - *ret = TAKE_PTR(dump); - return 0; + return memstream_finalize(&ms, ret, NULL); } diff --git a/src/oom/oomd-util.c b/src/oom/oomd-util.c index 6309d2c473..810ea7f98d 100644 --- a/src/oom/oomd-util.c +++ b/src/oom/oomd-util.c @@ -7,6 +7,7 @@ #include "fd-util.h" #include "fileio.h" #include "format-util.h" +#include "memstream-util.h" #include "oomd-util.h" #include "parse-util.h" #include "path-util.h" @@ -281,30 +282,20 @@ int oomd_cgroup_kill(const char *path, bool recurse, bool dry_run) { typedef void (*dump_candidate_func)(const OomdCGroupContext *ctx, FILE *f, const char *prefix); static int dump_kill_candidates(OomdCGroupContext **sorted, int n, int dump_until, dump_candidate_func dump_func) { - /* Try dumping top offendors, ignoring any errors that might happen. */ - _cleanup_free_ char *dump = NULL; - _cleanup_fclose_ FILE *f = NULL; - int r; - size_t size; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; - f = open_memstream_unlocked(&dump, &size); + /* Try dumping top offendors, ignoring any errors that might happen. */ + + f = memstream_init(&m); if (!f) - return -errno; + return -ENOMEM; fprintf(f, "Considered %d cgroups for killing, top candidates were:\n", n); for (int i = 0; i < dump_until; i++) dump_func(sorted[i], f, "\t"); - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!dump) - return -ENOMEM; - - return log_dump(LOG_INFO, dump); + return memstream_dump(LOG_INFO, &m); } int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char **ret_selected) { diff --git a/src/resolve/fuzz-resource-record.c b/src/resolve/fuzz-resource-record.c index 15c465933d..3472721356 100644 --- a/src/resolve/fuzz-resource-record.c +++ b/src/resolve/fuzz-resource-record.c @@ -3,14 +3,14 @@ #include "fd-util.h" #include "fuzz.h" #include "memory-util.h" +#include "memstream-util.h" #include "resolved-dns-packet.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_free_ char *out = NULL; /* out should be freed after f */ - size_t out_size; - _cleanup_fclose_ FILE *f = NULL; _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *copy = NULL; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; if (outside_size_range(size, 0, DNS_PACKET_SIZE_MAX)) return 0; @@ -21,7 +21,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { assert_se(copy = dns_resource_record_copy(rr)); assert_se(dns_resource_record_equal(copy, rr) > 0); - assert_se(f = open_memstream_unlocked(&out, &out_size)); + assert_se(f = memstream_init(&m)); (void) fprintf(f, "%s", strna(dns_resource_record_to_string(rr))); if (dns_resource_record_to_json(rr, &v) < 0) diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c index e7c18c29a0..01f79331b9 100644 --- a/src/resolve/resolved-dns-dnssec.c +++ b/src/resolve/resolved-dns-dnssec.c @@ -7,6 +7,7 @@ #include "gcrypt-util.h" #include "hexdecoct.h" #include "memory-util.h" +#include "memstream-util.h" #include "openssl-util.h" #include "resolved-dns-dnssec.h" #include "resolved-dns-packet.h" @@ -810,11 +811,10 @@ static int dnssec_rrset_serialize_sig( char **ret_sig_data, size_t *ret_sig_size) { - _cleanup_free_ char *sig_data = NULL; - size_t sig_size = 0; - _cleanup_fclose_ FILE *f = NULL; + _cleanup_(memstream_done) MemStream m = {}; uint8_t wire_format_name[DNS_WIRE_FORMAT_HOSTNAME_MAX]; DnsResourceRecord *rr; + FILE *f; int r; assert(rrsig); @@ -823,7 +823,7 @@ static int dnssec_rrset_serialize_sig( assert(ret_sig_data); assert(ret_sig_size); - f = open_memstream_unlocked(&sig_data, &sig_size); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -866,18 +866,7 @@ static int dnssec_rrset_serialize_sig( fwrite(DNS_RESOURCE_RECORD_RDATA(rr), 1, l, f); } - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); /* sig_data may be reallocated when f is closed. */ - - if (!sig_data) - return -ENOMEM; - - *ret_sig_data = TAKE_PTR(sig_data); - *ret_sig_size = sig_size; - return 0; + return memstream_finalize(&m, ret_sig_data, ret_sig_size); } static int dnssec_rrset_verify_sig( diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 67786d39fd..96899ed0ad 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -19,6 +19,7 @@ #include "hostname-util.h" #include "idn-util.h" #include "io-util.h" +#include "memstream-util.h" #include "missing_network.h" #include "missing_socket.h" #include "netlink-util.h" @@ -492,16 +493,15 @@ static int manager_watch_hostname(Manager *m) { } static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { - _cleanup_free_ char *buffer = NULL; - _cleanup_fclose_ FILE *f = NULL; + _cleanup_(memstream_done) MemStream ms = {}; Manager *m = ASSERT_PTR(userdata); - size_t size = 0; Link *l; + FILE *f; assert(s); assert(si); - f = open_memstream_unlocked(&buffer, &size); + f = memstream_init(&ms); if (!f) return log_oom(); @@ -516,16 +516,7 @@ static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si LIST_FOREACH(servers, server, l->dns_servers) dns_server_dump(server, f); - if (fflush_and_check(f) < 0) - return log_oom(); - - f = safe_fclose(f); - - if (!buffer) - return -ENOMEM; - - log_dump(LOG_INFO, buffer); - return 0; + return memstream_dump(LOG_INFO, &ms); } static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index b99883a088..4eb1b1c316 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -20,6 +20,7 @@ #include "bus-util.h" #include "data-fd-util.h" #include "fd-util.h" +#include "memstream-util.h" #include "path-util.h" #include "socket-util.h" #include "stdio-util.h" @@ -610,26 +611,26 @@ int bus_reply_pair_array(sd_bus_message *m, char **l) { } static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { - _cleanup_free_ char *dump = NULL; /* keep this above dump_file, so that it's freed after */ - _cleanup_fclose_ FILE *dump_file = NULL; + _cleanup_(memstream_done) MemStream m = {}; + _cleanup_free_ char *dump = NULL; _cleanup_close_ int fd = -EBADF; size_t dump_size; + FILE *f; int r; assert(message); - dump_file = open_memstream(&dump, &dump_size); - if (!dump_file) + f = memstream_init(&m); + if (!f) return -ENOMEM; - r = RET_NERRNO(malloc_info(/* options= */ 0, dump_file)); + r = RET_NERRNO(malloc_info(/* options= */ 0, f)); if (r < 0) return r; - dump_file = safe_fclose(dump_file); - - if (!dump) - return -ENOMEM; + r = memstream_finalize(&m, &dump, &dump_size); + if (r < 0) + return r; fd = acquire_data_fd(dump, dump_size, 0); if (fd < 0) diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c index 9e43a4689e..628c6202e1 100644 --- a/src/shared/calendarspec.c +++ b/src/shared/calendarspec.c @@ -15,6 +15,7 @@ #include "fd-util.h" #include "fileio.h" #include "macro.h" +#include "memstream-util.h" #include "parse-util.h" #include "process-util.h" #include "sort-util.h" @@ -337,15 +338,13 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us } int calendar_spec_to_string(const CalendarSpec *c, char **ret) { - _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; - int r; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; assert(c); assert(ret); - f = open_memstream_unlocked(&buf, &sz); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -383,17 +382,7 @@ int calendar_spec_to_string(const CalendarSpec *c, char **ret) { } } - r = fflush_and_check(f); - if (r < 0) - return r; - - f = safe_fclose(f); - - if (!buf) - return -ENOMEM; - - *ret = TAKE_PTR(buf); - return 0; + return memstream_finalize(&m, ret, NULL); } static int parse_weekdays(const char **p, CalendarSpec *c) { diff --git a/src/shared/common-signal.c b/src/shared/common-signal.c index 1896443632..e4edd180d8 100644 --- a/src/shared/common-signal.c +++ b/src/shared/common-signal.c @@ -3,13 +3,13 @@ #include "common-signal.h" #include "fd-util.h" #include "fileio.h" +#include "memstream-util.h" #include "process-util.h" #include "signal-util.h" int sigrtmin18_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { struct sigrtmin18_info *info = userdata; _cleanup_free_ char *comm = NULL; - int r; assert(s); assert(si); @@ -57,11 +57,10 @@ int sigrtmin18_handler(sd_event_source *s, const struct signalfd_siginfo *si, vo break; case COMMON_SIGNAL_COMMAND_MALLOC_INFO: { - _cleanup_free_ char *data = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; - f = open_memstream_unlocked(&data, &sz); + f = memstream_init(&m); if (!f) { log_oom(); break; @@ -72,15 +71,7 @@ int sigrtmin18_handler(sd_event_source *s, const struct signalfd_siginfo *si, vo break; } - fputc(0, f); - - r = fflush_and_check(f); - if (r < 0) { - log_error_errno(r, "Failed to flush malloc_info() buffer: %m"); - break; - } - - log_dump(LOG_INFO, data); + (void) memstream_dump(LOG_INFO, &m); break; } diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 91f42de400..5d652d7ac7 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -22,6 +22,7 @@ #include "hexdecoct.h" #include "io-util.h" #include "macro.h" +#include "memstream-util.h" #include "process-util.h" #include "rlimit-util.h" #include "string-util.h" @@ -149,7 +150,7 @@ int dlopen_elf(void) { } typedef struct StackContext { - FILE *f; + MemStream m; Dwfl *dwfl; Elf *elf; unsigned n_thread; @@ -161,7 +162,7 @@ typedef struct StackContext { static void stack_context_done(StackContext *c) { assert(c); - c->f = safe_fclose(c->f); + memstream_done(&c->m); if (c->dwfl) { sym_dwfl_end(c->dwfl); @@ -232,8 +233,8 @@ static int frame_callback(Dwfl_Frame *frame, void *userdata) { module_offset = pc - start; } - if (c->f) - fprintf(c->f, "#%-2u 0x%016" PRIx64 " %s (%s + 0x%" PRIx64 ")\n", c->n_frame, (uint64_t) pc, strna(symbol), strna(fname), module_offset); + if (c->m.f) + fprintf(c->m.f, "#%-2u 0x%016" PRIx64 " %s (%s + 0x%" PRIx64 ")\n", c->n_frame, (uint64_t) pc, strna(symbol), strna(fname), module_offset); c->n_frame++; return DWARF_CB_OK; @@ -248,14 +249,14 @@ static int thread_callback(Dwfl_Thread *thread, void *userdata) { if (c->n_thread >= THREADS_MAX) return DWARF_CB_ABORT; - if (c->n_thread != 0 && c->f) - fputc('\n', c->f); + if (c->n_thread != 0 && c->m.f) + fputc('\n', c->m.f); c->n_frame = 0; - if (c->f) { + if (c->m.f) { tid = sym_dwfl_thread_tid(thread); - fprintf(c->f, "Stack trace of thread " PID_FMT ":\n", tid); + fprintf(c->m.f, "Stack trace of thread " PID_FMT ":\n", tid); } if (sym_dwfl_thread_getframes(thread, frame_callback, c) < 0) @@ -294,10 +295,10 @@ static void report_module_metadata(StackContext *c, const char *name, JsonVarian assert(c); assert(name); - if (!c->f) + if (!c->m.f) return; - fprintf(c->f, "Module %s", name); + fprintf(c->m.f, "Module %s", name); if (metadata) { const char @@ -311,14 +312,14 @@ static void report_module_metadata(StackContext *c, const char *name, JsonVarian /* Version/architecture is only meaningful with a package name. * Skip the detailed fields if package is unknown. */ _cleanup_free_ char *id = build_package_reference(type, package, version, arch); - fprintf(c->f, " from %s", strnull(id)); + fprintf(c->m.f, " from %s", strnull(id)); } if (build_id && !(package && version)) - fprintf(c->f, ", build-id=%s", build_id); + fprintf(c->m.f, ", build-id=%s", build_id); } - fputs("\n", c->f); + fputs("\n", c->m.f); } static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *elf, bool *ret_interpreter_found, StackContext *c) { @@ -461,8 +462,8 @@ static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackCont /* If we don't find a build-id, note it in the journal message, and try * anyway to find the package metadata. It's unlikely to have the latter * without the former, but there's no hard rule. */ - if (c->f) - fprintf(c->f, "Module %s without build-id.\n", name); + if (c->m.f) + fprintf(c->m.f, "Module %s without build-id.\n", name); } else { /* We will later parse package metadata json and pass it to our caller. Prepare the * build-id in json format too, so that it can be appended and parsed cleanly. It @@ -568,12 +569,10 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; - _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_done) */ _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, .modules = &modules, }; - size_t sz = 0; int r; assert(fd >= 0); @@ -581,11 +580,8 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** if (lseek(fd, 0, SEEK_SET) == (off_t) -1) return log_warning_errno(errno, "Failed to seek to beginning of the core file: %m"); - if (ret) { - c.f = open_memstream_unlocked(&buf, &sz); - if (!c.f) - return log_oom(); - } + if (ret && !memstream_init(&c.m)) + return log_oom(); sym_elf_version(EV_CURRENT); @@ -613,16 +609,9 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Could not parse core file, dwfl_getthreads() failed: %s", sym_dwfl_errmsg(sym_dwfl_errno())); if (ret) { - r = fflush_and_check(c.f); + r = memstream_finalize(&c.m, ret, NULL); if (r < 0) return log_warning_errno(r, "Could not parse core file, flushing file buffer failed: %m"); - - c.f = safe_fclose(c.f); - - if (!buf) - return log_oom(); - - *ret = TAKE_PTR(buf); } if (ret_package_metadata) @@ -634,14 +623,12 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **ret_package_metadata) { _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL, *elf_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; - _cleanup_free_ char *buf = NULL; /* buf should be freed last, c.f closed first (via stack_context_done) */ _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, .modules = &modules, }; const char *elf_type; GElf_Ehdr elf_header; - size_t sz = 0; int r; assert(fd >= 0); @@ -649,11 +636,8 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r if (lseek(fd, 0, SEEK_SET) == (off_t) -1) return log_warning_errno(errno, "Failed to seek to beginning of the ELF file: %m"); - if (ret) { - c.f = open_memstream_unlocked(&buf, &sz); - if (!c.f) - return log_oom(); - } + if (ret && !memstream_init(&c.m)) + return log_oom(); sym_elf_version(EV_CURRENT); @@ -672,7 +656,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r return log_warning_errno(r, "Failed to inspect core file: %m"); if (out) - fprintf(c.f, "%s", out); + fprintf(c.m.f, "%s", out); elf_type = "coredump"; } else { @@ -722,7 +706,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r return log_warning_errno(r, "Failed to merge JSON objects: %m"); if (ret) - fprintf(c.f, "ELF object binary architecture: %s\n", elf_architecture); + fprintf(c.m.f, "ELF object binary architecture: %s\n", elf_architecture); } #endif @@ -732,16 +716,9 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r return log_warning_errno(r, "Failed to merge JSON objects: %m"); if (ret) { - r = fflush_and_check(c.f); + r = memstream_finalize(&c.m, ret, NULL); if (r < 0) return log_warning_errno(r, "Could not parse ELF file, flushing file buffer failed: %m"); - - c.f = safe_fclose(c.f); - - if (!buf) - return log_oom(); - - *ret = TAKE_PTR(buf); } if (ret_package_metadata) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 22efd042e2..df79a42450 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -18,6 +18,7 @@ #include "id128-util.h" #include "in-addr-util.h" #include "memory-util.h" +#include "memstream-util.h" #include "pager.h" #include "parse-util.h" #include "path-util.h" @@ -1446,11 +1447,10 @@ static int table_data_compare(const size_t *a, const size_t *b, Table *t) { } static char* format_strv_width(char **strv, size_t column_width) { - _cleanup_free_ char *buf = NULL; /* buf must be freed after f */ - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; - f = open_memstream_unlocked(&buf, &sz); + f = memstream_init(&m); if (!f) return NULL; @@ -1471,11 +1471,11 @@ static char* format_strv_width(char **strv, size_t column_width) { } } - if (fflush_and_check(f) < 0) + char *buf; + if (memstream_finalize(&m, &buf, NULL) < 0) return NULL; - f = safe_fclose(f); - return TAKE_PTR(buf); + return buf; } static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercasing, size_t column_width, bool *have_soft) { @@ -2530,12 +2530,14 @@ int table_print(Table *t, FILE *f) { } int table_format(Table *t, char **ret) { - _cleanup_free_ char *buf = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; + _cleanup_(memstream_done) MemStream m = {}; + FILE *f; int r; - f = open_memstream_unlocked(&buf, &sz); + assert(t); + assert(ret); + + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -2543,14 +2545,7 @@ int table_format(Table *t, char **ret) { if (r < 0) return r; - f = safe_fclose(f); - - if (!buf) - return -ENOMEM; - - *ret = TAKE_PTR(buf); - - return 0; + return memstream_finalize(&m, ret, NULL); } size_t table_get_rows(Table *t) { diff --git a/src/shared/json.c b/src/shared/json.c index 5bf00f009f..26d99a5fba 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -20,6 +20,7 @@ #include "macro.h" #include "math-util.h" #include "memory-util.h" +#include "memstream-util.h" #include "string-table.h" #include "string-util.h" #include "strv.h" @@ -1768,9 +1769,9 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha } int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { - _cleanup_free_ char *s = NULL; - _cleanup_fclose_ FILE *f = NULL; - size_t sz = 0; + _cleanup_(memstream_done) MemStream m = {}; + size_t sz; + FILE *f; int r; /* Returns the length of the generated string (without the terminating NUL), @@ -1782,7 +1783,7 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { if (flags & JSON_FORMAT_OFF) return -ENOEXEC; - f = open_memstream_unlocked(&s, &sz); + f = memstream_init(&m); if (!f) return -ENOMEM; @@ -1790,21 +1791,11 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { if (r < 0) return r; - /* Add terminating 0, so that the output buffer is a valid string. */ - fputc('\0', f); - - r = fflush_and_check(f); + r = memstream_finalize(&m, ret, &sz); if (r < 0) return r; - f = safe_fclose(f); - - if (!s) - return -ENOMEM; - - *ret = TAKE_PTR(s); - assert(sz > 0); - return (int) sz - 1; + return sz; } int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char *prefix) {