A small USV for ALIX boards with i2c support
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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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);
  32. };
  33. static LIST_HEAD(callback_list);
  34. static int sig_pipe[2];
  35. static void sig_handler(int signal)
  36. {
  37. unsigned char signum = (signal & 0xFF);
  38. write(sig_pipe[1], &signum, 1);
  39. }
  40. int signal_remove_callback(int signum, int type)
  41. {
  42. /* mark all old handler for deletion */
  43. struct signal_entry *search;
  44. list_for_each_entry(search, &callback_list, list) {
  45. if (search->signum == signum)
  46. search->deleted = 1;
  47. }
  48. struct sigaction sig_action;
  49. switch (type) {
  50. case SIG_IGNORE:
  51. sig_action.sa_handler = SIG_IGN;
  52. break;
  53. default:
  54. case SIG_DEFAULT:
  55. sig_action.sa_handler = SIG_DFL;
  56. break;
  57. }
  58. if (sigaction(signum, &sig_action, NULL) < 0) {
  59. log_print(LOG_WARN, "signal_remove_callback(): sigaction()");
  60. return -1;
  61. }
  62. /* trigger remove */
  63. sig_handler(0);
  64. return 0;
  65. }
  66. int signal_set_callback(int signum, void (*callback)(void))
  67. {
  68. struct signal_entry *entry = malloc(sizeof(struct signal_entry));
  69. if (entry == NULL) {
  70. log_print(LOG_WARN, "signal_add_callback(): out of memory");
  71. return -1;
  72. }
  73. entry->signum = signum;
  74. entry->deleted = 0;
  75. entry->callback = callback;
  76. list_add_tail(&entry->list, &callback_list);
  77. struct sigaction sig_action = {
  78. .sa_handler = sig_handler,
  79. };
  80. if (sigaction(signum, &sig_action, NULL) < 0) {
  81. log_print(LOG_WARN, "signal_add_callback(): sigaction()");
  82. list_del(&entry->list);
  83. free(entry);
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static int sig_event(int fd, void *privdata)
  89. {
  90. unsigned char signum;
  91. int len = read(fd, &signum, 1);
  92. if (len <= 0) {
  93. log_print(LOG_WARN, "sig_event(): read()");
  94. return -1;
  95. }
  96. struct signal_entry *search, *tmp;
  97. list_for_each_entry_safe(search, tmp, &callback_list, list) {
  98. if (search->deleted) {
  99. list_del(&search->list);
  100. free(search);
  101. continue;
  102. }
  103. if (search->signum == signum)
  104. search->callback();
  105. }
  106. return 0;
  107. }
  108. int signal_init(void)
  109. {
  110. if (pipe(sig_pipe) < 0) {
  111. log_print(LOG_ERROR, "signal_init(): pipe()");
  112. return -1;
  113. }
  114. if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
  115. log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
  116. return -1;
  117. }
  118. if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) {
  119. log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)");
  120. return -1;
  121. }
  122. event_add_readfd(NULL, sig_pipe[0], sig_event, NULL);
  123. return 0;
  124. }