]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-remote: introduce custom hash_ops with destructor for MHDDaemonWrapper 37115/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 12 Apr 2025 02:32:47 +0000 (11:32 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 13 Apr 2025 00:57:19 +0000 (09:57 +0900)
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
src/journal-remote/journal-remote.c
src/journal-remote/journal-remote.h

index 6a47c994937a499e1f9413d2da69bcb9abe77671..bf71a9dc94c4c540cf4c1de093f8c31accb01b4b 100644 (file)
@@ -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)
index 4ad60deecb9f38d04ed57babebf6bb6687097b67..77e9702bf6ab3e51402c7002a88fd017b2afc31c 100644 (file)
 
 #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);
index 5fe52c1b91c0b054c60dd0a0dc4e5ecf60841698..6fb82bb46ac870c497796a01b95a7d75a4bb1599 100644 (file)
@@ -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;