aboutsummaryrefslogtreecommitdiff
path: root/lib/fuse_signals.c
diff options
context:
space:
mode:
authorGravatar Miklos Szeredi <miklos@szeredi.hu>2006-01-06 18:29:40 +0000
committerGravatar Miklos Szeredi <miklos@szeredi.hu>2006-01-06 18:29:40 +0000
commit95da860d29a54c6dc21683a34edefead597e3bbc (patch)
treeac1df7de0b985741e97169b2e3233ab1267cfdd5 /lib/fuse_signals.c
parent1f6a5346406a620071ae6a659d5a75cd5220a128 (diff)
fix
Diffstat (limited to 'lib/fuse_signals.c')
-rw-r--r--lib/fuse_signals.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/fuse_signals.c b/lib/fuse_signals.c
new file mode 100644
index 0000000..38f407d
--- /dev/null
+++ b/lib/fuse_signals.c
@@ -0,0 +1,72 @@
+/*
+ FUSE: Filesystem in Userspace
+ Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
+
+ This program can be distributed under the terms of the GNU LGPL.
+ See the file COPYING.LIB
+*/
+
+#include "fuse_lowlevel.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+
+static struct fuse_session *fuse_instance;
+
+static void exit_handler(int sig)
+{
+ (void) sig;
+ if (fuse_instance)
+ fuse_session_exit(fuse_instance);
+}
+
+static int set_one_signal_handler(int sig, void (*handler)(int))
+{
+ struct sigaction sa;
+ struct sigaction old_sa;
+
+ memset(&sa, 0, sizeof(struct sigaction));
+ sa.sa_handler = handler;
+ sigemptyset(&(sa.sa_mask));
+ sa.sa_flags = 0;
+
+ if (sigaction(sig, NULL, &old_sa) == -1) {
+ perror("FUSE: cannot get old signal handler");
+ return -1;
+ }
+
+ if (old_sa.sa_handler == SIG_DFL &&
+ sigaction(sig, &sa, NULL) == -1) {
+ perror("Cannot set signal handler");
+ return -1;
+ }
+ return 0;
+}
+
+int fuse_set_signal_handlers(struct fuse_session *se)
+{
+ if (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)
+ return -1;
+
+ fuse_instance = se;
+ return 0;
+}
+
+void fuse_remove_signal_handlers(struct fuse_session *se)
+{
+ if (fuse_instance != se)
+ fprintf(stderr,
+ "fuse: fuse_remove_signal_handlers: unknown session\n");
+ else
+ fuse_instance = NULL;
+
+ set_one_signal_handler(SIGHUP, SIG_DFL);
+ set_one_signal_handler(SIGINT, SIG_DFL);
+ set_one_signal_handler(SIGTERM, SIG_DFL);
+ set_one_signal_handler(SIGPIPE, SIG_DFL);
+}
+