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 <lkn@darkstar.example.net>
This commit is contained in:
parent
48b34a47a6
commit
18e9c8231c
|
|
@ -31,6 +31,7 @@
|
|||
#include <string>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
#define _PSYNC_FS_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
|
||||
#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
|
||||
|
|
|
|||
Loading…
Reference in New Issue