Broadcast forwarder for ip-bound gameservers
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.

signals.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /***************************************************************************
  2. * Copyright (C) 05/2009 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include "event.h"
  24. #include "list.h"
  25. #include "logging.h"
  26. #include "signals.h"
  27. struct signal_entry {
  28. struct list_head list;
  29. int signum;
  30. int deleted;
  31. void (*callback)(void *privdata);
  32. void *privdata;
  33. };
  34. static LIST_HEAD(callback_list);
  35. static int sig_pipe[2];
  36. static void sig_handler(int signal)
  37. {
  38. unsigned char signum = (signal & 0xFF);
  39. write(sig_pipe[1], &signum, 1);
  40. }
  41. int signal_remove_callback(int signum, int type)
  42. {
  43. /* mark all old handler for deletion */
  44. struct signal_entry *search;
  45. list_for_each_entry(search, &callback_list, list) {
  46. if (search->signum == signum)
  47. search->deleted = 1;
  48. }
  49. struct sigaction sig_action;
  50. switch (type) {
  51. case SIG_IGNORE:
  52. sig_action.sa_handler = SIG_IGN;
  53. break;
  54. default:
  55. case SIG_DEFAULT:
  56. sig_action.sa_handler = SIG_DFL;
  57. break;
  58. }
  59. if (sigaction(signum, &sig_action, NULL) < 0) {
  60. log_print(LOG_WARN, "signal_remove_callback(): sigaction()");
  61. return -1;
  62. }
  63. /* trigger remove */
  64. sig_handler(0);
  65. return 0;
  66. }
  67. int signal_add_callback(int signum, void (*callback)(void *privdata), void *privdata)
  68. {
  69. struct signal_entry *entry = malloc(sizeof(struct signal_entry));
  70. if (entry == NULL) {
  71. log_print(LOG_WARN, "signal_add_callback(): out of memory");
  72. return -1;
  73. }
  74. entry->signum = signum;
  75. entry->deleted = 0;
  76. entry->callback = callback;
  77. entry->privdata = privdata;
  78. list_add_tail(&entry->list, &callback_list);
  79. struct sigaction sig_action = {
  80. .sa_handler = sig_handler,
  81. };
  82. if (sigaction(signum, &sig_action, NULL) < 0) {
  83. log_print(LOG_WARN, "signal_add_callback(): sigaction()");
  84. list_del(&entry->list);
  85. free(entry);
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. static int sig_event(int fd, void *privdata)
  91. {
  92. unsigned char signum;
  93. int len = read(fd, &signum, 1);
  94. if (len <= 0) {
  95. log_print(LOG_WARN, "sig_event(): read()");
  96. return -1;
  97. }
  98. struct signal_entry *search, *tmp;
  99. list_for_each_entry_safe(search, tmp, &callback_list, list) {
  100. if (search->deleted) {
  101. list_del(&search->list);
  102. free(search);
  103. continue;
  104. }
  105. if (search->signum == signum)
  106. search->callback(search->privdata);
  107. }
  108. return 0;
  109. }
  110. int signal_init(void)
  111. {
  112. if (pipe(sig_pipe) < 0) {
  113. log_print(LOG_ERROR, "signal_init(): pipe()");
  114. return -1;
  115. }
  116. if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
  117. log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
  118. return -1;
  119. }
  120. if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) {
  121. log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
  122. return -1;
  123. }
  124. event_add_readfd(NULL, sig_pipe[0], sig_event, NULL);
  125. return 0;
  126. }