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-11-28 06:45:49 +01:00
|
|
|
#include <unistd.h>
|
2011-03-27 21:14:55 +02:00
|
|
|
#include <sys/time.h>
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
#include "runtime.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "go-assert.h"
|
|
|
|
#include "go-panic.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;
|
2011-11-28 06:45:49 +01:00
|
|
|
/* Nonzero if the signal should be caught. */
|
|
|
|
_Bool catch;
|
|
|
|
/* Nonzero if the signal should be queued. */
|
|
|
|
_Bool queue;
|
2010-12-03 05:34:57 +01:00
|
|
|
/* 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-11-28 06:45:49 +01:00
|
|
|
{ SIGHUP, 0, 1, 0, 1 },
|
|
|
|
{ SIGINT, 0, 1, 0, 1 },
|
|
|
|
{ SIGQUIT, 0, 1, 0, 1 },
|
|
|
|
{ SIGALRM, 0, 1, 1, 1 },
|
|
|
|
{ SIGTERM, 0, 1, 0, 1 },
|
|
|
|
#ifdef SIGILL
|
|
|
|
{ SIGILL, 1, 0, 0, 0 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
{ SIGTRAP, 1, 0, 0, 0 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
|
|
|
{ SIGABRT, 1, 0, 0, 0 },
|
|
|
|
#endif
|
2010-12-03 05:34:57 +01:00
|
|
|
#ifdef SIGBUS
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGBUS, 1, 0, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGFPE, 1, 0, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGUSR1, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGSEGV, 1, 0, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGUSR2, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGPIPE, 0, 0, 1, 0 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
{ SIGSTKFLT, 1, 0, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGCHLD, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGTSTP, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGTTIN, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGTTOU, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGURG, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGXCPU, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGXFSZ, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGVTARLM
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGVTALRM, 0, 1, 1, 1 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
{ SIGPROF, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGWINCH, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGIO, 0, 1, 1, 1 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
2011-11-28 06:45:49 +01:00
|
|
|
{ SIGPWR, 0, 1, 1, 1 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
|
|
{ SIGSYS, 1, 0, 0, 0 },
|
2010-12-03 05:34:57 +01:00
|
|
|
#endif
|
2011-11-28 06:45:49 +01:00
|
|
|
{ -1, 0, 0, 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. */
|
2011-11-28 06:45:49 +01:00
|
|
|
runtime_sigprof (0, 0, nil, nil);
|
2011-03-27 21:14:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
/* FIXME: Should check siginfo for more information when
|
|
|
|
available. */
|
|
|
|
msg = NULL;
|
|
|
|
switch (sig)
|
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
#ifdef SIGILL
|
|
|
|
case SIGILL:
|
|
|
|
msg = "illegal instruction";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
#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;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
if (runtime_m()->mallocing)
|
2010-12-14 06:19:51 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_panicstring (msg);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; signals[i].sig != -1; ++i)
|
|
|
|
{
|
|
|
|
if (signals[i].sig == sig)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
if (signals[i].queue)
|
|
|
|
{
|
|
|
|
if (__go_sigsend (sig) || signals[i].ignore)
|
|
|
|
return;
|
|
|
|
runtime_exit (2); // SIGINT, SIGTERM, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runtime_panicking)
|
|
|
|
runtime_exit (2);
|
|
|
|
runtime_panicking = 1;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
/* Ignore a signal. */
|
|
|
|
|
|
|
|
static void
|
2011-12-01 18:08:12 +01:00
|
|
|
sig_ignore (int sig __attribute__ ((unused)))
|
2011-11-28 06:45:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
/* Initialize signal handling for Go. This is called when the program
|
|
|
|
starts. */
|
|
|
|
|
|
|
|
void
|
2011-11-28 06:45:49 +01:00
|
|
|
runtime_initsig (int32 queue)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-11-28 06:45:49 +01:00
|
|
|
if (signals[i].queue != (queue ? 1 : 0))
|
|
|
|
continue;
|
|
|
|
if (signals[i].catch || signals[i].queue)
|
|
|
|
sa.sa_handler = sighandler;
|
|
|
|
else
|
2011-12-01 18:08:12 +01:00
|
|
|
sa.sa_handler = sig_ignore;
|
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);
|
|
|
|
}
|
|
|
|
|
2011-11-28 06:45:49 +01:00
|
|
|
runtime_m()->profilehz = hz;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
/* Used by the os package to raise SIGPIPE. */
|
|
|
|
|
|
|
|
void os_sigpipe (void) __asm__ ("libgo_os.os.sigpipe");
|
|
|
|
|
|
|
|
void
|
|
|
|
os_sigpipe (void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset (&sa, 0, sizeof sa);
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
|
|
|
|
i = sigemptyset (&sa.sa_mask);
|
|
|
|
__go_assert (i == 0);
|
|
|
|
|
|
|
|
if (sigaction (SIGPIPE, &sa, NULL) != 0)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
raise (SIGPIPE);
|
|
|
|
}
|