From: Yu Watanabe Date: Sat, 12 Apr 2025 02:32:47 +0000 (+0900) Subject: journal-remote: introduce custom hash_ops with destructor for MHDDaemonWrapper X-Git-Tag: v258-rc1~813^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d9856d812bc950d344d89e5a2c0c6dc560f0da3b;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/journal-remote/journal-remote-main.c b/src/journal-remote/journal-remote-main.c index 6a47c994937..bf71a9dc94c 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 4ad60deecb9..77e9702bf6a 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 5fe52c1b91c..6fb82bb46ac 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;