From 83d3aed907e5051c52a2eae1e7b648d3a3acc0f4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 11 Apr 2025 10:44:29 +0900 Subject: [PATCH 1/4] journal: replace unnecessary use of set_free_free() with set_free() These uses pcre2_code_hash_ops_free, hence set_free() is enough. --- src/journal/journald-context.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/journal/journald-context.c b/src/journal/journald-context.c index 50dc8867e1..1d5aca1d87 100644 --- a/src/journal/journald-context.c +++ b/src/journal/journald-context.c @@ -182,8 +182,8 @@ static void client_context_reset(Server *s, ClientContext *c) { c->log_ratelimit_interval = s->ratelimit_interval; c->log_ratelimit_burst = s->ratelimit_burst; - c->log_filter_allowed_patterns = set_free_free(c->log_filter_allowed_patterns); - c->log_filter_denied_patterns = set_free_free(c->log_filter_denied_patterns); + c->log_filter_allowed_patterns = set_free(c->log_filter_allowed_patterns); + c->log_filter_denied_patterns = set_free(c->log_filter_denied_patterns); c->capability_quintet = CAPABILITY_QUINTET_NULL; } From c5ed1935164c80e9179fc83fd12e8c278a8c4417 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 12 Apr 2025 23:49:45 +0900 Subject: [PATCH 2/4] journal: use hash_ops with destructor that make JournalFile offline and close --- src/journal/journald-server.c | 14 +++++++------- src/shared/journal-file-util.c | 7 ++++++- src/shared/journal-file-util.h | 2 ++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index a33c4b2824..9c57f98e03 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -286,7 +286,7 @@ static int server_open_journal( (seal ? JOURNAL_SEAL : 0) | JOURNAL_STRICT_ORDER; - set_clear_with_destructor(s->deferred_closes, journal_file_offline_close); + set_clear(s->deferred_closes); if (reliably) r = journal_file_open_reliably( @@ -1477,8 +1477,8 @@ int server_relinquish_var(Server *s) { (void) server_system_journal_open(s, /* flush_requested */ false, /* relinquish_requested=*/ true); s->system_journal = journal_file_offline_close(s->system_journal); - ordered_hashmap_clear_with_destructor(s->user_journals, journal_file_offline_close); - set_clear_with_destructor(s->deferred_closes, journal_file_offline_close); + ordered_hashmap_clear(s->user_journals); + set_clear(s->deferred_closes); server_refresh_idle_timer(s); return 0; @@ -2503,7 +2503,7 @@ int server_init(Server *s, const char *namespace) { (void) mkdir_p(s->runtime_directory, 0755); - s->user_journals = ordered_hashmap_new(NULL); + s->user_journals = ordered_hashmap_new(&journal_file_hash_ops_offline_close); if (!s->user_journals) return log_oom(); @@ -2511,7 +2511,7 @@ int server_init(Server *s, const char *namespace) { if (!s->mmap) return log_oom(); - s->deferred_closes = set_new(NULL); + s->deferred_closes = set_new(&journal_file_hash_ops_offline_close); if (!s->deferred_closes) return log_oom(); @@ -2708,7 +2708,7 @@ Server* server_free(Server *s) { free(s->namespace); free(s->namespace_field); - set_free_with_destructor(s->deferred_closes, journal_file_offline_close); + set_free(s->deferred_closes); while (s->stdout_streams) stdout_stream_free(s->stdout_streams); @@ -2718,7 +2718,7 @@ Server* server_free(Server *s) { (void) journal_file_offline_close(s->system_journal); (void) journal_file_offline_close(s->runtime_journal); - ordered_hashmap_free_with_destructor(s->user_journals, journal_file_offline_close); + ordered_hashmap_free(s->user_journals); sd_varlink_server_unref(s->varlink_server); diff --git a/src/shared/journal-file-util.c b/src/shared/journal-file-util.c index 383c1354f3..b352ede275 100644 --- a/src/shared/journal-file-util.c +++ b/src/shared/journal-file-util.c @@ -448,7 +448,7 @@ int journal_file_rotate( if (r < 0) return r; - set_clear_with_destructor(deferred_closes, journal_file_offline_close); + set_clear(deferred_closes); r = journal_file_open( /* fd= */ -EBADF, @@ -532,3 +532,8 @@ int journal_file_open_reliably( return journal_file_open(-EBADF, fname, open_flags, file_flags, mode, compress_threshold_bytes, metrics, mmap_cache, /* template = */ old_file, ret); } + +DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + journal_file_hash_ops_offline_close, + void, trivial_hash_func, trivial_compare_func, + JournalFile, journal_file_offline_close); diff --git a/src/shared/journal-file-util.h b/src/shared/journal-file-util.h index 8df10a7927..780fe5368e 100644 --- a/src/shared/journal-file-util.h +++ b/src/shared/journal-file-util.h @@ -26,3 +26,5 @@ int journal_file_rotate( JournalFileFlags file_flags, uint64_t compress_threshold_bytes, Set *deferred_closes); + +extern const struct hash_ops journal_file_hash_ops_offline_close; From 8eb4ce4118a2cbc80a52531b9fb0a6042774e45e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 12 Apr 2025 23:57:21 +0900 Subject: [PATCH 3/4] sd-journal: use hash_ops with destructor that closes JournalFile --- src/libsystemd/sd-journal/journal-file.c | 5 +++++ src/libsystemd/sd-journal/journal-file.h | 2 ++ src/libsystemd/sd-journal/sd-journal.c | 4 ++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 9d953793a0..30a5c716a8 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -92,6 +92,11 @@ # pragma GCC diagnostic ignored "-Waddress-of-packed-member" #endif +DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + journal_file_hash_ops_by_path, + char, path_hash_func, path_compare, + JournalFile, journal_file_close); + static int mmap_prot_from_open_flags(int flags) { switch (flags & O_ACCMODE) { case O_RDONLY: diff --git a/src/libsystemd/sd-journal/journal-file.h b/src/libsystemd/sd-journal/journal-file.h index 4096f05783..669c066156 100644 --- a/src/libsystemd/sd-journal/journal-file.h +++ b/src/libsystemd/sd-journal/journal-file.h @@ -145,6 +145,8 @@ typedef struct { uint64_t hash; } EntryItem; +extern const struct hash_ops journal_file_hash_ops_by_path; + int journal_file_open( int fd, const char *fname, diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index 3b1f060565..b6d3a7f627 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -2296,7 +2296,7 @@ static sd_journal *journal_new(int flags, const char *path, const char *namespac return NULL; } - j->files = ordered_hashmap_new(&path_hash_ops); + j->files = ordered_hashmap_new(&journal_file_hash_ops_by_path); if (!j->files) return NULL; @@ -2548,7 +2548,7 @@ _public_ void sd_journal_close(sd_journal *j) { if (j->mmap) mmap_cache_stats_log_debug(j->mmap); - ordered_hashmap_free_with_destructor(j->files, journal_file_close); + ordered_hashmap_free(j->files); iterated_cache_free(j->files_cache); hashmap_free(j->directories_by_path); From d9856d812bc950d344d89e5a2c0c6dc560f0da3b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 12 Apr 2025 11:32:47 +0900 Subject: [PATCH 4/4] journal-remote: introduce custom hash_ops with destructor for MHDDaemonWrapper Then, we can move declaration of the type from journal-remote.c to journal-remote-main.c, and drop several #if ... #endif. --- src/journal-remote/journal-remote-main.c | 30 +++++++++++++++++++++++- src/journal-remote/journal-remote.c | 18 +------------- src/journal-remote/journal-remote.h | 19 --------------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/journal-remote/journal-remote-main.c b/src/journal-remote/journal-remote-main.c index 6a47c99493..bf71a9dc94 100644 --- a/src/journal-remote/journal-remote-main.c +++ b/src/journal-remote/journal-remote-main.c @@ -17,6 +17,7 @@ #include "logs-show.h" #include "main-func.h" #include "memory-util.h" +#include "microhttpd-util.h" #include "parse-argument.h" #include "parse-helpers.h" #include "pretty-print.h" @@ -84,6 +85,33 @@ static const char* const journal_write_split_mode_table[_JOURNAL_WRITE_SPLIT_MAX DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode, JournalWriteSplitMode); static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode, journal_write_split_mode, JournalWriteSplitMode); +typedef struct MHDDaemonWrapper { + uint64_t fd; + struct MHD_Daemon *daemon; + + sd_event_source *io_event; + sd_event_source *timer_event; +} MHDDaemonWrapper; + +static MHDDaemonWrapper* MHDDaemonWrapper_free(MHDDaemonWrapper *d) { + if (!d) + return NULL; + + if (d->daemon) + MHD_stop_daemon(d->daemon); + sd_event_source_unref(d->io_event); + sd_event_source_unref(d->timer_event); + + return mfree(d); +} + +DEFINE_TRIVIAL_CLEANUP_FUNC(MHDDaemonWrapper*, MHDDaemonWrapper_free); + +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( + mhd_daemon_hash_ops, + uint64_t, uint64_hash_func, uint64_compare_func, + MHDDaemonWrapper, MHDDaemonWrapper_free); + /********************************************************************** ********************************************************************** **********************************************************************/ @@ -525,7 +553,7 @@ static int setup_microhttpd_server(RemoteServer *s, if (r < 0) return log_error_errno(r, "Failed to set source name: %m"); - r = hashmap_ensure_put(&s->daemons, &uint64_hash_ops, &d->fd, d); + r = hashmap_ensure_put(&s->daemons, &mhd_daemon_hash_ops, &d->fd, d); if (r == -ENOMEM) return log_oom(); if (r < 0) diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 4ad60deecb..77e9702bf6 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -30,20 +30,6 @@ #define filename_escape(s) xescape((s), "/ ") -#if HAVE_MICROHTTPD -MHDDaemonWrapper* MHDDaemonWrapper_free(MHDDaemonWrapper *d) { - if (!d) - return NULL; - - if (d->daemon) - MHD_stop_daemon(d->daemon); - sd_event_source_unref(d->io_event); - sd_event_source_unref(d->timer_event); - - return mfree(d); -} -#endif - static int open_output(RemoteServer *s, Writer *w, const char *host) { _cleanup_free_ char *_filename = NULL; const char *filename; @@ -364,9 +350,7 @@ void journal_remote_server_destroy(RemoteServer *s) { if (!s) return; -#if HAVE_MICROHTTPD - hashmap_free_with_destructor(s->daemons, MHDDaemonWrapper_free); -#endif + hashmap_free(s->daemons); for (i = 0; i < MALLOC_ELEMENTSOF(s->sources); i++) remove_source(s, i); diff --git a/src/journal-remote/journal-remote.h b/src/journal-remote/journal-remote.h index 5fe52c1b91..6fb82bb46a 100644 --- a/src/journal-remote/journal-remote.h +++ b/src/journal-remote/journal-remote.h @@ -8,23 +8,6 @@ #include "journal-remote-write.h" #include "journal-vacuum.h" -#if HAVE_MICROHTTPD -#include "microhttpd-util.h" - -typedef struct MHDDaemonWrapper MHDDaemonWrapper; - -struct MHDDaemonWrapper { - uint64_t fd; - struct MHD_Daemon *daemon; - - sd_event_source *io_event; - sd_event_source *timer_event; -}; - -MHDDaemonWrapper* MHDDaemonWrapper_free(MHDDaemonWrapper *d); -DEFINE_TRIVIAL_CLEANUP_FUNC(MHDDaemonWrapper*, MHDDaemonWrapper_free); -#endif - struct RemoteServer { RemoteSource **sources; size_t active; @@ -36,9 +19,7 @@ struct RemoteServer { Writer *_single_writer; uint64_t event_count; -#if HAVE_MICROHTTPD Hashmap *daemons; -#endif const char *output; /* either the output file or directory */ JournalWriteSplitMode split_mode;