signal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. * Copyright (c) 2010 Romain Francoise <rfrancoise@debian.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  16. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include "tmux.h"
  22. struct event ev_sighup;
  23. struct event ev_sigchld;
  24. struct event ev_sigcont;
  25. struct event ev_sigterm;
  26. struct event ev_sigusr1;
  27. struct event ev_sigwinch;
  28. void
  29. set_signals(void (*handler)(int, short, void *), void *arg)
  30. {
  31. struct sigaction sigact;
  32. memset(&sigact, 0, sizeof sigact);
  33. sigemptyset(&sigact.sa_mask);
  34. sigact.sa_flags = SA_RESTART;
  35. sigact.sa_handler = SIG_IGN;
  36. if (sigaction(SIGINT, &sigact, NULL) != 0)
  37. fatal("sigaction failed");
  38. if (sigaction(SIGPIPE, &sigact, NULL) != 0)
  39. fatal("sigaction failed");
  40. if (sigaction(SIGUSR2, &sigact, NULL) != 0)
  41. fatal("sigaction failed");
  42. if (sigaction(SIGTSTP, &sigact, NULL) != 0)
  43. fatal("sigaction failed");
  44. signal_set(&ev_sighup, SIGHUP, handler, arg);
  45. signal_add(&ev_sighup, NULL);
  46. signal_set(&ev_sigchld, SIGCHLD, handler, arg);
  47. signal_add(&ev_sigchld, NULL);
  48. signal_set(&ev_sigcont, SIGCONT, handler, arg);
  49. signal_add(&ev_sigcont, NULL);
  50. signal_set(&ev_sigterm, SIGTERM, handler, arg);
  51. signal_add(&ev_sigterm, NULL);
  52. signal_set(&ev_sigusr1, SIGUSR1, handler, arg);
  53. signal_add(&ev_sigusr1, NULL);
  54. signal_set(&ev_sigwinch, SIGWINCH, handler, arg);
  55. signal_add(&ev_sigwinch, NULL);
  56. }
  57. void
  58. clear_signals(int after_fork)
  59. {
  60. struct sigaction sigact;
  61. memset(&sigact, 0, sizeof sigact);
  62. sigemptyset(&sigact.sa_mask);
  63. sigact.sa_flags = SA_RESTART;
  64. sigact.sa_handler = SIG_DFL;
  65. if (sigaction(SIGINT, &sigact, NULL) != 0)
  66. fatal("sigaction failed");
  67. if (sigaction(SIGPIPE, &sigact, NULL) != 0)
  68. fatal("sigaction failed");
  69. if (sigaction(SIGUSR2, &sigact, NULL) != 0)
  70. fatal("sigaction failed");
  71. if (sigaction(SIGTSTP, &sigact, NULL) != 0)
  72. fatal("sigaction failed");
  73. if (after_fork) {
  74. if (sigaction(SIGHUP, &sigact, NULL) != 0)
  75. fatal("sigaction failed");
  76. if (sigaction(SIGCHLD, &sigact, NULL) != 0)
  77. fatal("sigaction failed");
  78. if (sigaction(SIGCONT, &sigact, NULL) != 0)
  79. fatal("sigaction failed");
  80. if (sigaction(SIGTERM, &sigact, NULL) != 0)
  81. fatal("sigaction failed");
  82. if (sigaction(SIGUSR1, &sigact, NULL) != 0)
  83. fatal("sigaction failed");
  84. if (sigaction(SIGWINCH, &sigact, NULL) != 0)
  85. fatal("sigaction failed");
  86. } else {
  87. event_del(&ev_sighup);
  88. event_del(&ev_sigchld);
  89. event_del(&ev_sigcont);
  90. event_del(&ev_sigterm);
  91. event_del(&ev_sigusr1);
  92. event_del(&ev_sigwinch);
  93. }
  94. }