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.

72 lines
1.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 void exit_handler(int sig)
  13. {
  14. (void) sig;
  15. if (fuse_instance)
  16. fuse_session_exit(fuse_instance);
  17. }
  18. static int set_one_signal_handler(int sig, void (*handler)(int), int remove)
  19. {
  20. struct sigaction sa;
  21. struct sigaction old_sa;
  22. memset(&sa, 0, sizeof(struct sigaction));
  23. sa.sa_handler = remove ? SIG_DFL : handler;
  24. sigemptyset(&(sa.sa_mask));
  25. sa.sa_flags = 0;
  26. if (sigaction(sig, NULL, &old_sa) == -1) {
  27. perror("fuse: cannot get old signal handler");
  28. return -1;
  29. }
  30. if (old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
  31. sigaction(sig, &sa, NULL) == -1) {
  32. perror("fuse: cannot set signal handler");
  33. return -1;
  34. }
  35. return 0;
  36. }
  37. int fuse_set_signal_handlers(struct fuse_session *se)
  38. {
  39. if (set_one_signal_handler(SIGHUP, exit_handler, 0) == -1 ||
  40. set_one_signal_handler(SIGINT, exit_handler, 0) == -1 ||
  41. set_one_signal_handler(SIGTERM, exit_handler, 0) == -1 ||
  42. set_one_signal_handler(SIGPIPE, SIG_IGN, 0) == -1)
  43. return -1;
  44. fuse_instance = se;
  45. return 0;
  46. }
  47. void fuse_remove_signal_handlers(struct fuse_session *se)
  48. {
  49. if (fuse_instance != se)
  50. fprintf(stderr,
  51. "fuse: fuse_remove_signal_handlers: unknown session\n");
  52. else
  53. fuse_instance = NULL;
  54. set_one_signal_handler(SIGHUP, exit_handler, 1);
  55. set_one_signal_handler(SIGINT, exit_handler, 1);
  56. set_one_signal_handler(SIGTERM, exit_handler, 1);
  57. set_one_signal_handler(SIGPIPE, SIG_IGN, 1);
  58. }