signal.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Implementation of the SIGNAL and ALARM g77 intrinsics
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran 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. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <signal.h>
  25. #ifdef HAVE_INTTYPES_H
  26. #include <inttypes.h>
  27. #endif
  28. #include <errno.h>
  29. /* SIGNAL subroutine with PROCEDURE as handler */
  30. extern void signal_sub (int *, void (*)(int), int *);
  31. iexport_proto(signal_sub);
  32. void
  33. signal_sub (int *number, void (*handler)(int), int *status)
  34. {
  35. intptr_t ret;
  36. if (status != NULL)
  37. {
  38. ret = (intptr_t) signal (*number, handler);
  39. *status = (int) ret;
  40. }
  41. else
  42. signal (*number, handler);
  43. }
  44. iexport(signal_sub);
  45. /* SIGNAL subroutine with INTEGER as handler */
  46. extern void signal_sub_int (int *, int *, int *);
  47. iexport_proto(signal_sub_int);
  48. void
  49. signal_sub_int (int *number, int *handler, int *status)
  50. {
  51. intptr_t ptr = *handler, ret;
  52. if (status != NULL)
  53. {
  54. ret = (intptr_t) signal (*number, (void (*)(int)) ptr);
  55. *status = (int) ret;
  56. }
  57. else
  58. signal (*number, (void (*)(int)) ptr);
  59. }
  60. iexport(signal_sub_int);
  61. /* SIGNAL function with PROCEDURE as handler */
  62. extern int signal_func (int *, void (*)(int));
  63. iexport_proto(signal_func);
  64. int
  65. signal_func (int *number, void (*handler)(int))
  66. {
  67. int status;
  68. signal_sub (number, handler, &status);
  69. return status;
  70. }
  71. iexport(signal_func);
  72. /* SIGNAL function with INTEGER as handler */
  73. extern int signal_func_int (int *, int *);
  74. iexport_proto(signal_func_int);
  75. int
  76. signal_func_int (int *number, int *handler)
  77. {
  78. int status;
  79. signal_sub_int (number, handler, &status);
  80. return status;
  81. }
  82. iexport(signal_func_int);
  83. /* ALARM intrinsic with PROCEDURE as handler */
  84. extern void alarm_sub_i4 (int *, void (*)(int), GFC_INTEGER_4 *);
  85. iexport_proto(alarm_sub_i4);
  86. void
  87. alarm_sub_i4 (int * seconds __attribute__ ((unused)),
  88. void (*handler)(int) __attribute__ ((unused)),
  89. GFC_INTEGER_4 *status)
  90. {
  91. #if defined (SIGALRM) && defined (HAVE_ALARM)
  92. if (status != NULL)
  93. {
  94. if (signal (SIGALRM, handler) == SIG_ERR)
  95. *status = -1;
  96. else
  97. *status = alarm (*seconds);
  98. }
  99. else
  100. {
  101. signal (SIGALRM, handler);
  102. alarm (*seconds);
  103. }
  104. #else
  105. errno = ENOSYS;
  106. if (status != NULL)
  107. *status = -1;
  108. #endif
  109. }
  110. iexport(alarm_sub_i4);
  111. extern void alarm_sub_i8 (int *, void (*)(int), GFC_INTEGER_8 *);
  112. iexport_proto(alarm_sub_i8);
  113. void
  114. alarm_sub_i8 (int *seconds __attribute__ ((unused)),
  115. void (*handler)(int) __attribute__ ((unused)),
  116. GFC_INTEGER_8 *status)
  117. {
  118. #if defined (SIGALRM) && defined (HAVE_ALARM)
  119. if (status != NULL)
  120. {
  121. if (signal (SIGALRM, handler) == SIG_ERR)
  122. *status = -1;
  123. else
  124. *status = alarm (*seconds);
  125. }
  126. else
  127. {
  128. signal (SIGALRM, handler);
  129. alarm (*seconds);
  130. }
  131. #else
  132. errno = ENOSYS;
  133. if (status != NULL)
  134. *status = -1;
  135. #endif
  136. }
  137. iexport(alarm_sub_i8);
  138. /* ALARM intrinsic with INTEGER as handler */
  139. extern void alarm_sub_int_i4 (int *, int *, GFC_INTEGER_4 *);
  140. iexport_proto(alarm_sub_int_i4);
  141. void
  142. alarm_sub_int_i4 (int *seconds __attribute__ ((unused)),
  143. int *handler __attribute__ ((unused)),
  144. GFC_INTEGER_4 *status)
  145. {
  146. #if defined (SIGALRM) && defined (HAVE_ALARM)
  147. if (status != NULL)
  148. {
  149. if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
  150. *status = -1;
  151. else
  152. *status = alarm (*seconds);
  153. }
  154. else
  155. {
  156. signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
  157. alarm (*seconds);
  158. }
  159. #else
  160. errno = ENOSYS;
  161. if (status != NULL)
  162. *status = -1;
  163. #endif
  164. }
  165. iexport(alarm_sub_int_i4);
  166. extern void alarm_sub_int_i8 (int *, int *, GFC_INTEGER_8 *);
  167. iexport_proto(alarm_sub_int_i8);
  168. void
  169. alarm_sub_int_i8 (int *seconds __attribute__ ((unused)),
  170. int *handler __attribute__ ((unused)),
  171. GFC_INTEGER_8 *status)
  172. {
  173. #if defined (SIGALRM) && defined (HAVE_ALARM)
  174. if (status != NULL)
  175. {
  176. if (signal (SIGALRM, (void (*)(int)) (intptr_t) *handler) == SIG_ERR)
  177. *status = -1;
  178. else
  179. *status = alarm (*seconds);
  180. }
  181. else
  182. {
  183. signal (SIGALRM, (void (*)(int)) (intptr_t) *handler);
  184. alarm (*seconds);
  185. }
  186. #else
  187. errno = ENOSYS;
  188. if (status != NULL)
  189. *status = -1;
  190. #endif
  191. }
  192. iexport(alarm_sub_int_i8);