2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 * Utility functions for setting signal handlers.
7 * This program can be distributed under the terms of the GNU LGPLv2.
8 * See the file COPYING.LIB
13 #include "fuse_lowlevel.h"
20 static struct fuse_session
*fuse_instance
;
22 static void exit_handler(int sig
)
25 fuse_session_exit(fuse_instance
);
27 fuse_log(FUSE_LOG_ERR
, "assertion error: signal value <= 0\n");
30 fuse_instance
->error
= sig
;
34 static void do_nothing(int sig
)
39 static int set_one_signal_handler(int sig
, void (*handler
)(int), int remove
)
42 struct sigaction old_sa
;
44 memset(&sa
, 0, sizeof(struct sigaction
));
45 sa
.sa_handler
= remove
? SIG_DFL
: handler
;
46 sigemptyset(&(sa
.sa_mask
));
49 if (sigaction(sig
, NULL
, &old_sa
) == -1) {
50 perror("fuse: cannot get old signal handler");
54 if (old_sa
.sa_handler
== (remove
? handler
: SIG_DFL
) &&
55 sigaction(sig
, &sa
, NULL
) == -1) {
56 perror("fuse: cannot set signal handler");
62 int fuse_set_signal_handlers(struct fuse_session
*se
)
65 * If we used SIG_IGN instead of the do_nothing function,
66 * then we would be unable to tell if we set SIG_IGN (and
67 * thus should reset to SIG_DFL in fuse_remove_signal_handlers)
68 * or if it was already set to SIG_IGN (and should be left
71 if (set_one_signal_handler(SIGHUP
, exit_handler
, 0) == -1 ||
72 set_one_signal_handler(SIGINT
, exit_handler
, 0) == -1 ||
73 set_one_signal_handler(SIGTERM
, exit_handler
, 0) == -1 ||
74 set_one_signal_handler(SIGPIPE
, do_nothing
, 0) == -1) {
82 void fuse_remove_signal_handlers(struct fuse_session
*se
)
84 if (fuse_instance
!= se
) {
85 fuse_log(FUSE_LOG_ERR
,
86 "fuse: fuse_remove_signal_handlers: unknown session\n");
91 set_one_signal_handler(SIGHUP
, exit_handler
, 1);
92 set_one_signal_handler(SIGINT
, exit_handler
, 1);
93 set_one_signal_handler(SIGTERM
, exit_handler
, 1);
94 set_one_signal_handler(SIGPIPE
, do_nothing
, 1);