2010-12-03 05:34:57 +01:00
|
|
|
/* 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>
|
2011-03-27 21:14:55 +02:00
|
|
|
#include <sys/time.h>
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
#include "go-assert.h"
|
|
|
|
#include "go-panic.h"
|
|
|
|
#include "go-signal.h"
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
|
2011-03-27 21:14:55 +02:00
|
|
|
#ifndef SA_RESTART
|
|
|
|
#define SA_RESTART 0
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* What to do for a signal. */
|
|
|
|
|
|
|
|
struct sigtab
|
|
|
|
{
|
|
|
|
/* Signal number. */
|
|
|
|
int sig;
|
|
|
|
/* Nonzero if the signal should be ignored. */
|
|
|
|
_Bool ignore;
|
2011-03-27 21:14:55 +02:00
|
|
|
/* Nonzero if we should restart system calls. */
|
|
|
|
_Bool restart;
|
2010-12-03 05:34:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* What to do for signals. */
|
|
|
|
|
|
|
|
static struct sigtab signals[] =
|
|
|
|
{
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGHUP, 0, 1 },
|
|
|
|
{ SIGINT, 0, 1 },
|
|
|
|
{ SIGALRM, 1, 1 },
|
|
|
|
{ SIGTERM, 0, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#ifdef SIGBUS
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGBUS, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGFPE, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGUSR1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGSEGV, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGUSR2, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGPIPE, 1, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGCHLD, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGTSTP, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGTTIN, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGTTOU, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGURG, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGXCPU, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGXFSZ, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGVTARLM
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGVTALRM, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGWINCH, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGIO, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
2011-03-27 21:14:55 +02:00
|
|
|
{ SIGPWR, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
2011-03-27 21:14:55 +02:00
|
|
|
{ -1, 0, 0 }
|
2010-12-03 05:34:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The Go signal handler. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
sighandler (int sig)
|
|
|
|
{
|
|
|
|
const char *msg;
|
|
|
|
int i;
|
|
|
|
|
2011-03-27 21:14:55 +02:00
|
|
|
if (sig == SIGPROF)
|
|
|
|
{
|
|
|
|
/* FIXME. */
|
|
|
|
runtime_sigprof (0, 0, nil);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
/* 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;
|
|
|
|
|
2010-12-14 06:19:51 +01:00
|
|
|
if (__sync_bool_compare_and_swap (&m->mallocing, 1, 1))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "caught signal while mallocing: %s\n", msg);
|
|
|
|
__go_assert (0);
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
/* 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)
|
2011-03-27 21:14:55 +02:00
|
|
|
{
|
|
|
|
sa.sa_flags = signals[i].restart ? SA_RESTART : 0;
|
|
|
|
if (sigaction (signals[i].sig, &sa, NULL) != 0)
|
|
|
|
__go_assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_resetcpuprofiler(int32 hz)
|
|
|
|
{
|
|
|
|
struct itimerval it;
|
|
|
|
struct sigaction sa;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset (&it, 0, sizeof it);
|
|
|
|
|
|
|
|
memset (&sa, 0, sizeof sa);
|
|
|
|
i = sigfillset (&sa.sa_mask);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
|
|
|
|
if (hz == 0)
|
|
|
|
{
|
|
|
|
i = setitimer (ITIMER_PROF, &it, NULL);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
i = sigaction (SIGPROF, &sa, NULL);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sa.sa_handler = sighandler;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
i = sigaction (SIGPROF, &sa, NULL);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
|
|
|
|
it.it_interval.tv_sec = 0;
|
|
|
|
it.it_interval.tv_usec = 1000000 / hz;
|
|
|
|
it.it_value = it.it_interval;
|
|
|
|
i = setitimer (ITIMER_PROF, &it, NULL);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
m->profilehz = hz;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|