mergerfs/libfuse/lib/fuse_signals.c

97 lines
2 KiB
C
Raw Permalink Normal View History

/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU LGPLv2.
See the file COPYING.LIB
*/
2023-04-28 17:47:08 -04:00
#include "fuse.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
static struct fuse_session *fuse_instance;
2023-04-28 17:47:08 -04:00
static
void
exit_handler(int sig)
{
2020-07-31 23:50:47 -04:00
(void) sig;
if (fuse_instance)
fuse_session_exit(fuse_instance);
}
2023-04-28 17:47:08 -04:00
static
void
sigusr1_handler(int sig_)
{
int enabled;
enabled = fuse_log_metrics_get();
enabled = !enabled;
fuse_log_metrics_set(enabled);
}
static
int
set_one_signal_handler(int sig,
void (*handler)(int),
int remove)
{
2020-07-31 23:50:47 -04:00
struct sigaction sa;
struct sigaction old_sa;
2020-07-31 23:50:47 -04:00
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = remove ? SIG_DFL : handler;
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
2020-07-31 23:50:47 -04:00
if (sigaction(sig, NULL, &old_sa) == -1) {
perror("fuse: cannot get old signal handler");
2023-04-28 17:47:08 -04:00
return 1;
2020-07-31 23:50:47 -04:00
}
2023-04-28 17:47:08 -04:00
if(old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
2020-07-31 23:50:47 -04:00
sigaction(sig, &sa, NULL) == -1) {
perror("fuse: cannot set signal handler");
2023-04-28 17:47:08 -04:00
return 1;
2020-07-31 23:50:47 -04:00
}
2023-04-28 17:47:08 -04:00
2020-07-31 23:50:47 -04:00
return 0;
}
2023-04-28 17:47:08 -04:00
int
fuse_set_signal_handlers(struct fuse_session *se)
{
2023-04-28 17:47:08 -04:00
int rv;
2023-04-28 17:47:08 -04:00
rv = 0;
rv |= set_one_signal_handler(SIGUSR1,sigusr1_handler,0);
rv |= set_one_signal_handler(SIGHUP,exit_handler,0);
rv |= set_one_signal_handler(SIGINT,exit_handler,0);
rv |= set_one_signal_handler(SIGTERM,exit_handler,0);
rv |= set_one_signal_handler(SIGPIPE,SIG_IGN,0);
if(rv == 0)
fuse_instance = se;
return rv;
}
2023-04-28 17:47:08 -04:00
void
fuse_remove_signal_handlers(struct fuse_session *se)
{
2023-04-28 17:47:08 -04:00
if(fuse_instance != se)
fprintf(stderr,"fuse: fuse_remove_signal_handlers: unknown session\n");
2020-07-31 23:50:47 -04:00
else
fuse_instance = NULL;
2023-04-28 17:47:08 -04:00
set_one_signal_handler(SIGUSR1,sigusr1_handler,1);
set_one_signal_handler(SIGHUP,exit_handler,1);
set_one_signal_handler(SIGINT,exit_handler,1);
set_one_signal_handler(SIGTERM,exit_handler,1);
set_one_signal_handler(SIGPIPE,SIG_IGN,1);
}