Skip to content

Commit 49a377f

Browse files
committed
Replace pthread-based logging buffer with signal masking for thread safety
This commit replaces the pthread mutex-based synchronization in the async logging buffer with signal masking, eliminating the pthread dependency while maintaining thread safety. Fixes: #38 Signed-off-by: Jindrich Novy <jnovy@redhat.com>
1 parent 88628cc commit 49a377f

File tree

10 files changed

+612
-4
lines changed

10 files changed

+612
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIBEXECDIR ?= ${PREFIX}/libexec
55
PKG_CONFIG ?= pkg-config
66
HEADERS := $(wildcard src/*.h)
77

8-
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o
8+
OBJS := src/conmon.o src/cmsg.o src/ctr_logging.o src/ctr_logging_buffer.o src/utils.o src/cli.o src/globals.o src/cgroup.o src/conn_sock.o src/oom.o src/ctrl.o src/ctr_stdio.o src/parent_pipe_fd.o src/ctr_exit.o src/runtime_args.o src/close_fds.o src/seccomp_notify.o
99

1010
MAKEFILE_PATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
1111

nix/derivation.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ with pkgs; stdenv.mkDerivation rec {
2424
# Static builds will use PKG_CONFIG_PATH approach instead
2525
];
2626
prePatch = ''
27-
export CFLAGS='-static -pthread'
27+
export CFLAGS='-static'
2828
export LDFLAGS='-s -w -static-libgcc -static'
2929
export EXTRA_LDFLAGS='-s -w -linkmode external -extldflags "-static -lm"'
3030
${lib.optionalString (!enableSystemd) "export DISABLE_SYSTEMD=1"}

src/cli.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cli.h"
22
#include "globals.h"
33
#include "ctr_logging.h"
4+
#include "ctr_logging_buffer.h"
45
#include "config.h"
56
#include "utils.h"
67

@@ -224,6 +225,14 @@ void process_cli()
224225

225226
configure_log_drivers(opt_log_path, opt_log_size_max, opt_log_global_size_max, opt_cid, opt_name, opt_log_tag, opt_log_labels);
226227

228+
/* Initialize async logging for improved log performance */
229+
if (!init_async_logging()) {
230+
nwarn("Failed to initialize async logging, falling back to direct logging");
231+
} else {
232+
/* Register cleanup handler for async logging */
233+
atexit(shutdown_async_logging);
234+
}
235+
227236
/* Warn if --no-container-partial-message is used without journald logging */
228237
if (opt_no_container_partial_message && !logging_is_journald_enabled()) {
229238
nwarnf("--no-container-partial-message has no effect without journald log driver");

src/conmon.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "conn_sock.h"
1515
#include "ctrl.h"
1616
#include "ctr_stdio.h"
17+
#include "ctr_logging_buffer.h"
1718
#include "config.h"
1819
#include "parent_pipe_fd.h"
1920
#include "ctr_exit.h"
@@ -418,6 +419,8 @@ int main(int argc, char *argv[])
418419
if (mainfd_stderr >= 0) {
419420
g_unix_fd_add(mainfd_stderr, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDERR_PIPE));
420421
}
422+
/* Setup log buffer timer for async flushing */
423+
setup_log_timer_in_main_loop();
421424

422425
if (opt_timeout > 0) {
423426
g_timeout_add_seconds(opt_timeout, timeout_cb, NULL);

src/ctr_logging.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define _GNU_SOURCE
22
#include "ctr_logging.h"
3+
#include "ctr_logging_buffer.h"
34
#include "cli.h"
45
#include "config.h"
56
#include <ctype.h>
@@ -761,6 +762,9 @@ static void set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
761762
/* Force closing any open FD. */
762763
void close_logging_fds(void)
763764
{
765+
/* Shutdown async logging system */
766+
shutdown_async_logging();
767+
764768
if (k8s_log_fd >= 0)
765769
close(k8s_log_fd);
766770
k8s_log_fd = -1;
@@ -1243,6 +1247,9 @@ static void reopen_k8s_file(void)
12431247

12441248
void sync_logs(void)
12451249
{
1250+
/* Flush any buffered logs before syncing */
1251+
flush_log_buffer();
1252+
12461253
/* Sync the logs to disk */
12471254
if (k8s_log_fd > 0)
12481255
if (fsync(k8s_log_fd) < 0)

0 commit comments

Comments
 (0)