summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2026-06-21 12:46:27 +0200
committerSander Vrijders <sander@ouroboros.rocks>2026-06-29 08:32:59 +0200
commite42a24f39afe15c5ac579fa519df98643d4fc6dd (patch)
tree87f145d0b756ff2dc1915077872167aa957054ee
parentbe419a9e4cac9428c432d8d7907778d31c12d409 (diff)
downloadouroboros-e42a24f39afe15c5ac579fa519df98643d4fc6dd.tar.gz
ouroboros-e42a24f39afe15c5ac579fa519df98643d4fc6dd.zip
lib: Guard logging against thread cancellation
POSIX defines printf() and syslog() as possible cancellation points. This hardens thread cancellation cleanup by wrapping the log macros in pthread_setcancelstate(PTHREAD_CANCEL_DISABLE) and restores the prior cancel state afterwards. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
-rw-r--r--include/ouroboros/logs.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/include/ouroboros/logs.h b/include/ouroboros/logs.h
index 58494531..1ae77673 100644
--- a/include/ouroboros/logs.h
+++ b/include/ouroboros/logs.h
@@ -29,6 +29,7 @@
#include <ouroboros/hash.h>
+#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
@@ -55,6 +56,8 @@ void log_fini(void);
#define __olog(CLR, LVL, SYSLVL, ...) \
do { \
+ int __cs; \
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &__cs); \
if (log_syslog) { \
syslog(SYSLVL, __VA_ARGS__); \
} else { \
@@ -64,10 +67,13 @@ void log_fini(void);
printf(CLR_RESET "\n"); \
fflush(stdout); \
} \
+ pthread_setcancelstate(__cs, NULL); \
} while (0)
#define __olog_id(CLR, LVL, SYSLVL, id, fmt, ...) \
do { \
+ int __cs; \
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &__cs); \
if (log_syslog) { \
syslog(SYSLVL, "[" HASH_FMT64 "] " fmt, \
HASH_VAL64(id), ## __VA_ARGS__); \
@@ -79,6 +85,7 @@ void log_fini(void);
printf(CLR_RESET "\n"); \
fflush(stdout); \
} \
+ pthread_setcancelstate(__cs, NULL); \
} while (0)
#ifndef OUROBOROS_DISABLE_LOGGING