treewide: unify signal handling, error reporting, and string utils

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I8351ecccb03281e438dba666390021306a6a6964
This commit is contained in:
raf 2026-04-15 12:44:43 +03:00
commit 987f4fcc99
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
4 changed files with 13 additions and 102 deletions

View file

@ -1,3 +1,4 @@
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
@ -32,49 +33,11 @@ static void print_version(void) {
printf("Minimal Wayland Multi-Monitor Wallpaper Daemon\n");
}
static void signal_handler(int sig) {
switch (sig) {
case SIGTERM:
case SIGINT:
chroma_should_quit = 1;
break;
case SIGHUP:
// TODO: Implement config reload
break;
}
}
static int setup_signals(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGTERM, &sa, NULL) == -1) {
perror("sigaction(SIGTERM)");
return -1;
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction(SIGINT)");
return -1;
}
if (sigaction(SIGHUP, &sa, NULL) == -1) {
perror("sigaction(SIGHUP)");
return -1;
}
return 0;
}
static int daemonize(void) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
chroma_log("ERROR", "fork: %s", strerror(errno));
return -1;
}
@ -85,13 +48,13 @@ static int daemonize(void) {
// Child process continues
if (setsid() < 0) {
perror("setsid");
chroma_log("ERROR", "setsid: %s", strerror(errno));
return -1;
}
// Change working directory to root
if (chdir("/") < 0) {
perror("chdir");
chroma_log("ERROR", "chdir: %s", strerror(errno));
return -1;
}
@ -166,12 +129,6 @@ int main(int argc, char *argv[]) {
// 0: ERROR+WARN only, 1: +INFO, 2: +DEBUG, 3+: +TRACE
chroma_set_log_level(verbose_level);
// Set up signal handlers
if (setup_signals() != 0) {
fprintf(stderr, "Failed to set up signal handlers\n");
return 1;
}
// Load configuration
if (!config_file) {
config_file = get_default_config_path();
@ -200,11 +157,14 @@ int main(int argc, char *argv[]) {
}
chroma_log_memory_stats("post-init");
// Set up signal handlers after state is initialized
chroma_set_signal_state(&state, config_file);
chroma_handle_signals();
// Load configuration
chroma_log("INFO", "Loading configuration from: %s", config_file);
if (chroma_config_load(&state.config, config_file) != CHROMA_OK) {
chroma_log("WARN", "Failed to load config file, using defaults");
// Continue with default configuration
}
chroma_log_memory_stats("post-config-load");