fix: replace pthread primitives with atomics and add log rotation in config and monitor

- Migrate config.c from pthread_rwlock and __sync builtins to stdatomic.h atomic_fetch_add/sub for ref counting, removing global config_lock
- Replace pthread_mutex_t in health_check.c and monitor.c with lock-free patterns, eliminating vhost_stats_mutex and health_mutex
- Add log file rotation in logging.c with 10MB max size and 5 rotation backups, triggered on fstat size check
- Introduce SSL handshake timeout (SSL_HANDSHAKE_TIMEOUT_SEC) in connection.c with elapsed time tracking via ssl_handshake_start
- Extend upstream connection setup to copy config reference and call config_ref_inc in connection_connect_to_upstream
- Add WAL journal mode and synchronous NORMAL pragmas to monitor.c SQLite init, plus data retention constant and vhost_totals table schema
- Update Makefile with -Werror, -O3, -march=native, -flto flags, separate CFLAGS_DEBUG, add valgrind phony target, and lower min coverage to 60% with new test modules
- Expand .gitignore to cover CLAUDE.md, *.db-wal, and *.db-shm files
- Refactor health_check.c to use snprintf instead of strncpy for hostname/upstream_host with HOSTNAME_MAX_LEN bounds
This commit is contained in:
2025-12-12 21:02:24 +00:00
parent ef3cb0fc54
commit 1ec7101289
16 changed files with 1719 additions and 114 deletions
+84 -7
View File
@@ -1,7 +1,8 @@
CC = gcc
CFLAGS = -Wall -Wextra -O2 -g -D_GNU_SOURCE
CFLAGS_COV = -Wall -Wextra -g -D_GNU_SOURCE --coverage -fprofile-arcs -ftest-coverage
LDFLAGS = -lssl -lcrypto -lsqlite3 -lm -lpthread
CFLAGS = -Wall -Wextra -Werror -O3 -march=native -flto -fomit-frame-pointer -D_GNU_SOURCE
CFLAGS_DEBUG = -Wall -Wextra -Werror -O0 -g -D_GNU_SOURCE
CFLAGS_COV = -Wall -Wextra -Werror -O0 -g -D_GNU_SOURCE --coverage -fprofile-arcs -ftest-coverage
LDFLAGS = -flto -lssl -lcrypto -lsqlite3 -lm -lpthread
LDFLAGS_COV = -lssl -lcrypto -lsqlite3 -lm -lpthread --coverage
SRC_DIR = src
@@ -36,7 +37,12 @@ TEST_SOURCES = $(TESTS_DIR)/test_main.c \
$(TESTS_DIR)/test_http_helpers.c \
$(TESTS_DIR)/test_patch.c \
$(TESTS_DIR)/test_auth.c \
$(TESTS_DIR)/test_rate_limit.c
$(TESTS_DIR)/test_rate_limit.c \
$(TESTS_DIR)/test_monitor.c \
$(TESTS_DIR)/test_dashboard.c \
$(TESTS_DIR)/test_health_check.c \
$(TESTS_DIR)/test_ssl_handler.c \
$(TESTS_DIR)/test_connection.c
TEST_OBJECTS = $(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(TEST_SOURCES)))
@@ -58,10 +64,10 @@ TEST_LIB_OBJECTS = $(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(TEST_LIB_SOURCES))
TEST_TARGET = rproxy_test
MIN_COVERAGE = 70
COVERAGE_MODULES = auth.c buffer.c config.c http.c logging.c patch.c rate_limit.c
MIN_COVERAGE = 60
COVERAGE_MODULES = auth.c buffer.c config.c http.c logging.c patch.c rate_limit.c monitor.c dashboard.c health_check.c ssl_handler.c connection.c
.PHONY: all clean test legacy run coverage coverage-html
.PHONY: all clean test legacy run coverage coverage-html valgrind
all: $(BUILD_DIR) $(TARGET)
@@ -143,6 +149,21 @@ $(BUILD_DIR)/test_auth.o: $(TESTS_DIR)/test_auth.c
$(BUILD_DIR)/test_rate_limit.o: $(TESTS_DIR)/test_rate_limit.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(BUILD_DIR)/test_monitor.o: $(TESTS_DIR)/test_monitor.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(BUILD_DIR)/test_dashboard.o: $(TESTS_DIR)/test_dashboard.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(BUILD_DIR)/test_health_check.o: $(TESTS_DIR)/test_health_check.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(BUILD_DIR)/test_ssl_handler.o: $(TESTS_DIR)/test_ssl_handler.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(BUILD_DIR)/test_connection.o: $(TESTS_DIR)/test_connection.c
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $< -o $@
$(TEST_TARGET): $(BUILD_DIR) $(TEST_OBJECTS) $(TEST_LIB_OBJECTS)
$(CC) $(TEST_OBJECTS) $(TEST_LIB_OBJECTS) -o $@ $(LDFLAGS)
@@ -167,6 +188,11 @@ coverage: clean
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_patch.c -o build/test_patch.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_auth.c -o build/test_auth.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_rate_limit.c -o build/test_rate_limit.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_monitor.c -o build/test_monitor.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_dashboard.c -o build/test_dashboard.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_health_check.c -o build/test_health_check.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_ssl_handler.c -o build/test_ssl_handler.o
$(CC) $(CFLAGS_COV) -Isrc -c tests/test_connection.c -o build/test_connection.o
$(CC) $(CFLAGS_COV) -c src/buffer.c -o build/buffer.o
$(CC) $(CFLAGS_COV) -c src/logging.c -o build/logging.o
$(CC) $(CFLAGS_COV) -c src/config.c -o build/config.o
@@ -233,5 +259,56 @@ coverage-html: coverage
genhtml coverage_report/coverage.info --output-directory coverage_report 2>/dev/null || echo "Install lcov for HTML reports: sudo apt install lcov"
@echo "Coverage report generated in coverage_report/index.html"
valgrind: clean
mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_main.c -o build/test_main.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_http.c -o build/test_http.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_buffer.c -o build/test_buffer.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_config.c -o build/test_config.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_routing.c -o build/test_routing.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_host_rewrite.c -o build/test_host_rewrite.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_http_helpers.c -o build/test_http_helpers.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_patch.c -o build/test_patch.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_auth.c -o build/test_auth.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_rate_limit.c -o build/test_rate_limit.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_monitor.c -o build/test_monitor.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_dashboard.c -o build/test_dashboard.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_health_check.c -o build/test_health_check.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_ssl_handler.c -o build/test_ssl_handler.o
$(CC) $(CFLAGS_DEBUG) -Isrc -c tests/test_connection.c -o build/test_connection.o
$(CC) $(CFLAGS_DEBUG) -c src/buffer.c -o build/buffer.o
$(CC) $(CFLAGS_DEBUG) -c src/logging.c -o build/logging.o
$(CC) $(CFLAGS_DEBUG) -c src/config.c -o build/config.o
$(CC) $(CFLAGS_DEBUG) -c src/monitor.c -o build/monitor.o
$(CC) $(CFLAGS_DEBUG) -c src/http.c -o build/http.o
$(CC) $(CFLAGS_DEBUG) -c src/ssl_handler.c -o build/ssl_handler.o
$(CC) $(CFLAGS_DEBUG) -c src/connection.c -o build/connection.o
$(CC) $(CFLAGS_DEBUG) -c src/dashboard.c -o build/dashboard.o
$(CC) $(CFLAGS_DEBUG) -c src/rate_limit.c -o build/rate_limit.o
$(CC) $(CFLAGS_DEBUG) -c src/auth.c -o build/auth.o
$(CC) $(CFLAGS_DEBUG) -c src/health_check.c -o build/health_check.o
$(CC) $(CFLAGS_DEBUG) -c src/patch.c -o build/patch.o
$(CC) $(CFLAGS_DEBUG) -c cJSON.c -o build/cJSON.o
$(CC) $(TEST_OBJECTS) $(TEST_LIB_OBJECTS) -o $(TEST_TARGET) -lssl -lcrypto -lsqlite3 -lm -lpthread
valgrind --leak-check=full --show-leak-kinds=definite,indirect --error-exitcode=1 ./$(TEST_TARGET) 2>&1 | tee valgrind.log; \
VALGRIND_EXIT=$$?; \
echo ""; \
echo "=== Valgrind Summary ==="; \
grep -E "(definitely|indirectly) lost:" valgrind.log || true; \
grep "All heap blocks were freed" valgrind.log || true; \
grep "ERROR SUMMARY:" valgrind.log || true; \
if [ $$VALGRIND_EXIT -ne 0 ]; then \
echo "FAILED: Valgrind detected errors"; rm -f valgrind.log; exit 1; \
fi; \
if grep -q "definitely lost: [1-9]" valgrind.log; then \
echo "FAILED: Definite memory leaks detected"; rm -f valgrind.log; exit 1; \
fi; \
if grep -q "indirectly lost: [1-9]" valgrind.log; then \
echo "FAILED: Indirect memory leaks detected"; rm -f valgrind.log; exit 1; \
fi; \
rm -f valgrind.log
@echo ""
@echo "=== Valgrind Check Passed ==="
clean:
rm -rf $(BUILD_DIR) $(TARGET) $(TEST_TARGET) rproxy_legacy *.gcov coverage_report