From 18e9c8231c447a6a8dc47eed8189ed02434b0b42 Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Fri, 6 Mar 2026 21:34:00 +0100 Subject: [PATCH] Fix signal handler safety issues (pcl-2tv) (#351) - Use sig_atomic_t flag instead of calling unsafe functions in signal handlers - Remove pdbg_logf() and exit() calls from psync_signal_handler - Check shutdown_requested flag in main loops - Export flag for cross-module access Co-authored-by: Levi Neely --- control_tools.cpp | 5 +++++ pclsync/pfs.c | 20 +++++++++++++++++--- pclsync/pfs.h | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/control_tools.cpp b/control_tools.cpp index 59d25f7..07a0129 100644 --- a/control_tools.cpp +++ b/control_tools.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,7 @@ #include "pclsync/putil.h" #include "pclsync/pfoldersync.h" #include "pclsync/pcommands.h" +#include "pclsync/pfs.h" #include "rpcclient.h" #include "CLI11.hpp" @@ -506,6 +508,9 @@ int daemonize(bool do_commands) { while (1) { sleep(10); + if (shutdown_requested) { + break; + } } } diff --git a/pclsync/pfs.c b/pclsync/pfs.c index c9ab4ce..178d409 100644 --- a/pclsync/pfs.c +++ b/pclsync/pfs.c @@ -90,6 +90,7 @@ typedef off_t fuse_off_t; #define PSYNC_FS_ERR_MOVE_ACROSS_CRYPTO EXDEV static int shutdown_in_progress = 0; +volatile sig_atomic_t shutdown_requested = 0; static struct fuse_chan *psync_fuse_channel = NULL; static struct fuse *psync_fuse = NULL; static char *psync_current_mountpoint = NULL; @@ -3592,8 +3593,7 @@ static void pfs_do_stop(void) { void pfs_stop() { pfs_do_stop(); } static void psync_signal_handler(int sig) { - pdbg_logf(D_NOTICE, "got signal %d", sig); - exit(1); // invoke psync_do_stop via atexit() + shutdown_requested = 1; } static void psync_usr2_handler(int sig) { @@ -3651,7 +3651,21 @@ static void psync_fuse_thread() { } pthread_mutex_unlock(&start_mutex); pdbg_logf(D_NOTICE, "running fuse_loop_mt"); - fr = fuse_loop_mt(psync_fuse); + + // Check shutdown flag periodically during FUSE loop + while (!shutdown_requested) { + fr = fuse_loop_mt(psync_fuse); + if (fr != 0 || shutdown_requested) { + break; + } + } + + if (shutdown_requested) { + pdbg_logf(D_NOTICE, "shutdown requested, exiting fuse loop"); + pfs_do_stop(); + exit(0); + } + pdbg_logf(D_NOTICE, "fuse_loop_mt exited with code %d, running fuse_destroy", fr); pthread_mutex_lock(&start_mutex); fuse_destroy(psync_fuse); diff --git a/pclsync/pfs.h b/pclsync/pfs.h index eedd027..db14a3b 100644 --- a/pclsync/pfs.h +++ b/pclsync/pfs.h @@ -33,6 +33,7 @@ #define _PSYNC_FS_H #include +#include #include "pcrc32c.h" #include "pcrypto.h" @@ -196,4 +197,6 @@ void pfs_debug_init_file_mutex(pthread_mutex_t *m); void pfs_debug_dump_internals(); void pfs_debug_register_signal_handlers(); +extern volatile sig_atomic_t shutdown_requested; + #endif