m_sigtrp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Dedicated treatment of the sigtrp system call, MON 48.
  3. */
  4. /* $Header$ */
  5. #include <signal.h>
  6. #include "global.h"
  7. #include "log.h"
  8. #include "warn.h"
  9. #include "trap.h"
  10. /*************************** SIGTRP *************************************
  11. * The monitor call "sigtrp()" is handled by "do_sigtrp()". The first *
  12. * argument is a EM-trap number (0<=tn<=252), the second a UNIX signal *
  13. * number. The user wants trap "tn" to be generated, in case signal *
  14. * "sn" occurs. The report about this interpreter has a section, *
  15. * giving all details about signal handling. Do_sigtrp() returns the *
  16. * previous trap-number "sn" was mapped onto. A return value of -1 *
  17. * indicates an error. *
  18. ************************************************************************/
  19. #define UNIX_trap(sn) (SIGILL <= sn && sn <= SIGSYS)
  20. PRIVATE int sig_map[NSIG+1]; /* maps signals onto trap numbers */
  21. PRIVATE int HndlIntSig(); /* handle signal to interpreter */
  22. PRIVATE int HndlEmSig(); /* handle signal to user program */
  23. init_signals() {
  24. int sn;
  25. for (sn = 0; sn < NSIG+1; sn++) {
  26. sig_map[sn] = -2; /* Default EM trap number */
  27. }
  28. for (sn = 0; sn < NSIG+1; sn++) {
  29. /* for all signals that would cause termination */
  30. if (!UNIX_trap(sn)) {
  31. if (signal(sn, SIG_IGN) != SIG_IGN) {
  32. /* we take our fate in our own hand */
  33. signal(sn, HndlIntSig);
  34. }
  35. }
  36. }
  37. }
  38. int do_sigtrp(tn, sn)
  39. int tn; /* EM trap number */
  40. int sn; /* UNIX signal number */
  41. {
  42. register int old_tn;
  43. if (sn <= 0 || sn > NSIG) {
  44. einval(WILLSN);
  45. return (-1);
  46. }
  47. if (UNIX_trap(sn)) {
  48. einval(WUNIXTR);
  49. return (-1);
  50. }
  51. old_tn = sig_map[sn];
  52. sig_map[sn] = tn;
  53. if (tn == -2) { /* reset default for signal sn */
  54. signal(sn, SIG_DFL);
  55. }
  56. else if (tn == -3) { /* ignore signal sn */
  57. signal(sn, SIG_IGN);
  58. }
  59. else if (tn >= 0 && tn <= 252) {/* legal tn */
  60. if ((int)signal(sn, HndlEmSig) == -1) {
  61. sig_map[sn] = old_tn;
  62. return (-1);
  63. }
  64. }
  65. else {
  66. /* illegal trap number */
  67. einval(WILLTN);
  68. sig_map[sn] = old_tn; /* restore sig_map */
  69. return (-1);
  70. }
  71. return (old_tn);
  72. }
  73. trap_signal()
  74. {
  75. /* execute the trap belonging to the signal that came in during
  76. the last instruction
  77. */
  78. register int old_sig = signalled;
  79. signalled = 0;
  80. trap(sig_map[old_sig]);
  81. }
  82. /* The handling functions for the UNIX signals */
  83. PRIVATE HndlIntSig(sn)
  84. int sn;
  85. {
  86. /* The interpreter got the signal */
  87. signal(sn, SIG_IGN); /* peace and quiet for close_down() */
  88. LOG(("@t1 signal %d caught by interpreter", sn));
  89. message("interpreter received signal %d, which was not caught by the interpreted program",
  90. sn);
  91. close_down(1);
  92. }
  93. PRIVATE HndlEmSig(sn)
  94. int sn;
  95. {
  96. /* The EM machine got the signal */
  97. signal(sn, HndlIntSig); /* Revert to old situation */
  98. signalled = sn;
  99. }