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

@ -239,6 +239,11 @@ void chroma_image_free(chroma_image_t *image) {
if (image->data) {
// Log memory deallocation before freeing
size_t image_size = (size_t)image->width * image->height * image->channels;
if (strlen(image->path) > 0) {
chroma_log("DEBUG", "Freed image: %s", image->path);
}
chroma_log_resource_deallocation("image_data", image_size, image->path);
// Always use stbi_image_free since we load directly with stbi_load

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

View file

@ -442,57 +442,6 @@ const char *chroma_get_file_extension(const char *path) {
return last_dot + 1;
}
// Case-insensitive string comparison
int chroma_strcasecmp(const char *s1, const char *s2) {
if (!s1 || !s2) {
return (s1 == s2) ? 0 : (s1 ? 1 : -1);
}
while (*s1 && *s2) {
int c1 = tolower((unsigned char)*s1);
int c2 = tolower((unsigned char)*s2);
if (c1 != c2) {
return c1 - c2;
}
s1++;
s2++;
}
return tolower((unsigned char)*s1) - tolower((unsigned char)*s2);
}
// Safe string copy
size_t chroma_strlcpy(char *dst, const char *src, size_t size) {
size_t src_len = strlen(src);
if (size > 0) {
size_t copy_len = (src_len < size - 1) ? src_len : size - 1;
memcpy(dst, src, copy_len);
dst[copy_len] = '\0';
}
return src_len;
}
// Safe string concatenation
size_t chroma_strlcat(char *dst, const char *src, size_t size) {
size_t dst_len = strnlen(dst, size);
size_t src_len = strlen(src);
if (dst_len < size) {
size_t copy_len = size - dst_len - 1;
if (src_len < copy_len) {
copy_len = src_len;
}
memcpy(dst + dst_len, src, copy_len);
dst[dst_len + copy_len] = '\0';
}
return dst_len + src_len;
}
// Get current time in milliseconds
long long chroma_get_time_ms(void) {
struct timeval tv;