summaryrefslogtreecommitdiff
path: root/libgo/runtime/go-signal.c
blob: 3838ab98871ca3592d0723dd5acc86f5c5ca4f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* go-signal.c -- signal handling for Go.

   Copyright 2009 The Go Authors. All rights reserved.
   Use of this source code is governed by a BSD-style
   license that can be found in the LICENSE file.  */

#include <signal.h>
#include <stdlib.h>

#include "go-assert.h"
#include "go-panic.h"
#include "go-signal.h"

#include "runtime.h"

#undef int 

#ifndef SA_ONSTACK
#define SA_ONSTACK 0
#endif

/* What to do for a signal.  */

struct sigtab
{
  /* Signal number.  */
  int sig;
  /* Nonzero if the signal should be ignored.  */
  _Bool ignore;
};

/* What to do for signals.  */

static struct sigtab signals[] =
{
  { SIGHUP, 0 },
  { SIGINT, 0 },
  { SIGALRM, 1 },
  { SIGTERM, 0 },
#ifdef SIGBUS
  { SIGBUS, 0 },
#endif
#ifdef SIGFPE
  { SIGFPE, 0 },
#endif
#ifdef SIGUSR1
  { SIGUSR1, 1 },
#endif
#ifdef SIGSEGV
  { SIGSEGV, 0 },
#endif
#ifdef SIGUSR2
  { SIGUSR2, 1 },
#endif
#ifdef SIGPIPE
  { SIGPIPE, 1 },
#endif
#ifdef SIGCHLD
  { SIGCHLD, 1 },
#endif
#ifdef SIGTSTP
  { SIGTSTP, 1 },
#endif
#ifdef SIGTTIN
  { SIGTTIN, 1 },
#endif
#ifdef SIGTTOU
  { SIGTTOU, 1 },
#endif
#ifdef SIGURG
  { SIGURG, 1 },
#endif
#ifdef SIGXCPU
  { SIGXCPU, 1 },
#endif
#ifdef SIGXFSZ
  { SIGXFSZ, 1 },
#endif
#ifdef SIGVTARLM
  { SIGVTALRM, 1 },
#endif
#ifdef SIGPROF
  { SIGPROF, 1 },
#endif
#ifdef SIGWINCH
  { SIGWINCH, 1 },
#endif
#ifdef SIGIO
  { SIGIO, 1 },
#endif
#ifdef SIGPWR
  { SIGPWR, 1 },
#endif
  { -1, 0 }
};

/* The Go signal handler.  */

static void
sighandler (int sig)
{
  const char *msg;
  int i;

  /* FIXME: Should check siginfo for more information when
     available.  */
  msg = NULL;
  switch (sig)
    {
#ifdef SIGBUS
    case SIGBUS:
      msg = "invalid memory address or nil pointer dereference";
      break;
#endif

#ifdef SIGFPE
    case SIGFPE:
      msg = "integer divide by zero or floating point error";
      break;
#endif

#ifdef SIGSEGV
    case SIGSEGV:
      msg = "invalid memory address or nil pointer dereference";
      break;
#endif

    default:
      break;
    }

  if (msg != NULL)
    {
      sigset_t clear;

      if (__sync_bool_compare_and_swap (&m->mallocing, 1, 1))
	{
	  fprintf (stderr, "caught signal while mallocing: %s\n", msg);
	  __go_assert (0);
	}

      /* The signal handler blocked signals; unblock them.  */
      i = sigfillset (&clear);
      __go_assert (i == 0);
      i = sigprocmask (SIG_UNBLOCK, &clear, NULL);
      __go_assert (i == 0);

      __go_panic_msg (msg);
    }

  if (__go_sigsend (sig))
    return;
  for (i = 0; signals[i].sig != -1; ++i)
    {
      if (signals[i].sig == sig)
	{
	  struct sigaction sa;

	  if (signals[i].ignore)
	    return;

	  memset (&sa, 0, sizeof sa);

	  sa.sa_handler = SIG_DFL;

	  i = sigemptyset (&sa.sa_mask);
	  __go_assert (i == 0);

	  if (sigaction (sig, &sa, NULL) != 0)
	    abort ();

	  raise (sig);
	  exit (2);
	}
    }
  abort ();
}

/* Initialize signal handling for Go.  This is called when the program
   starts.  */

void
__initsig ()
{
  struct sigaction sa;
  int i;

  siginit ();

  memset (&sa, 0, sizeof sa);

  sa.sa_handler = sighandler;

  i = sigfillset (&sa.sa_mask);
  __go_assert (i == 0);

  for (i = 0; signals[i].sig != -1; ++i)
    if (sigaction (signals[i].sig, &sa, NULL) != 0)
      __go_assert (0);
}