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.

96 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 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.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
  21. void
  22. sigusr1_handler(int sig_)
  23. {
  24. int enabled;
  25. enabled = fuse_log_metrics_get();
  26. enabled = !enabled;
  27. fuse_log_metrics_set(enabled);
  28. }
  29. static
  30. int
  31. set_one_signal_handler(int sig,
  32. void (*handler)(int),
  33. int remove)
  34. {
  35. struct sigaction sa;
  36. struct sigaction old_sa;
  37. memset(&sa, 0, sizeof(struct sigaction));
  38. sa.sa_handler = remove ? SIG_DFL : handler;
  39. sigemptyset(&(sa.sa_mask));
  40. sa.sa_flags = 0;
  41. if (sigaction(sig, NULL, &old_sa) == -1) {
  42. perror("fuse: cannot get old signal handler");
  43. return 1;
  44. }
  45. if(old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
  46. sigaction(sig, &sa, NULL) == -1) {
  47. perror("fuse: cannot set signal handler");
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. int
  53. fuse_set_signal_handlers(struct fuse_session *se)
  54. {
  55. int rv;
  56. rv = 0;
  57. rv |= set_one_signal_handler(SIGUSR1,sigusr1_handler,0);
  58. rv |= set_one_signal_handler(SIGHUP,exit_handler,0);
  59. rv |= set_one_signal_handler(SIGINT,exit_handler,0);
  60. rv |= set_one_signal_handler(SIGTERM,exit_handler,0);
  61. rv |= set_one_signal_handler(SIGPIPE,SIG_IGN,0);
  62. if(rv == 0)
  63. fuse_instance = se;
  64. return rv;
  65. }
  66. void
  67. fuse_remove_signal_handlers(struct fuse_session *se)
  68. {
  69. if(fuse_instance != se)
  70. fprintf(stderr,"fuse: fuse_remove_signal_handlers: unknown session\n");
  71. else
  72. fuse_instance = NULL;
  73. set_one_signal_handler(SIGUSR1,sigusr1_handler,1);
  74. set_one_signal_handler(SIGHUP,exit_handler,1);
  75. set_one_signal_handler(SIGINT,exit_handler,1);
  76. set_one_signal_handler(SIGTERM,exit_handler,1);
  77. set_one_signal_handler(SIGPIPE,SIG_IGN,1);
  78. }