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.
This commit is contained in:
Yu Watanabe
2025-04-12 11:32:47 +09:00
parent 8eb4ce4118
commit d9856d812b
3 changed files with 30 additions and 37 deletions

View 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)

View File

@@ -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);

View 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;