journal: hash_ops related cleanups (#37115)

This commit is contained in:
Yu Watanabe
2025-04-15 22:32:15 +09:00
committed by GitHub
10 changed files with 56 additions and 49 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;

View File

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

View File

@@ -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;
@@ -2494,7 +2494,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();
@@ -2502,7 +2502,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();
@@ -2699,7 +2699,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);
@@ -2709,7 +2709,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);

View File

@@ -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:

View File

@@ -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,

View File

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

View File

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

View File

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