You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB
  6. */
  7. #include "fuse_lowlevel.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. static struct fuse_session *fuse_instance;
  12. static
  13. void
  14. exit_handler(int sig)
  15. {
  16. (void)sig;
  17. if (fuse_instance)
  18. fuse_session_exit(fuse_instance);
  19. }
  20. static int set_one_signal_handler(int sig, void (*handler)(int), int remove)
  21. {
  22. struct sigaction sa;
  23. struct sigaction old_sa;
  24. memset(&sa, 0, sizeof(struct sigaction));
  25. sa.sa_handler = remove ? SIG_DFL : handler;
  26. sigemptyset(&(sa.sa_mask));
  27. sa.sa_flags = 0;
  28. if (sigaction(sig, NULL, &old_sa) == -1) {
  29. perror("fuse: cannot get old signal handler");
  30. return -1;
  31. }
  32. if (old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
  33. sigaction(sig, &sa, NULL) == -1) {
  34. perror("fuse: cannot set signal handler");
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. int fuse_set_signal_handlers(struct fuse_session *se)
  40. {
  41. if((set_one_signal_handler(SIGINT, exit_handler, 0) == -1) ||
  42. (set_one_signal_handler(SIGTERM, exit_handler, 0) == -1) ||
  43. (set_one_signal_handler(SIGQUIT, exit_handler, 0) == -1) ||
  44. (set_one_signal_handler(SIGPIPE, SIG_IGN, 0) == -1))
  45. return -1;
  46. fuse_instance = se;
  47. return 0;
  48. }
  49. void fuse_remove_signal_handlers(struct fuse_session *se)
  50. {
  51. if (fuse_instance != se)
  52. fprintf(stderr,
  53. "fuse: fuse_remove_signal_handlers: unknown session\n");
  54. else
  55. fuse_instance = NULL;
  56. set_one_signal_handler(SIGINT, exit_handler, 1);
  57. set_one_signal_handler(SIGTERM, exit_handler, 1);
  58. set_one_signal_handler(SIGQUIT, exit_handler, 1);
  59. set_one_signal_handler(SIGPIPE, SIG_IGN, 1);
  60. }