Skip to content

Commit a3ea8fd

Browse files
committed
add stubs for signal_ignore, signal_disable
1 parent d49e3db commit a3ea8fd

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/runtime/runtime_unix.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package runtime
44

55
import (
6+
"syscall"
67
"unsafe"
78
)
89

@@ -262,23 +263,45 @@ func init() {
262263
// A channel size of 1 should be sufficient in most cases, but using 4 just
263264
// to be sure.
264265
signalChan = make(chan uint32, 4)
266+
signalIgnored = make([]bool, 65) // 65 is the highest signal number we support
265267
}
266268

267269
var signalChan chan uint32
270+
var signalIgnored []bool // TODO: replace with more efficient bitmap?
268271

269272
//go:linkname signal_enable os/signal.signal_enable
270-
func signal_enable(s uint32) {
273+
func signal_enable(sig uint32) {
271274
// It's easier to implement this function in C.
272-
tinygo_signal_enable(s)
275+
tinygo_signal_enable(sig)
273276
}
274277

275278
//export tinygo_signal_enable
276279
func tinygo_signal_enable(s uint32)
277280

281+
func signal_disable(sig uint32) {
282+
tinygo_signal_disable(sig)
283+
}
284+
285+
//export tinygo_signal_disable
286+
func tinygo_signal_disable(sig uint32)
287+
288+
// Ignore the given signal by adding it into the signalIgnored array.
289+
// If the signal is received, it will be ignored in the tinygo_signal_handler.
290+
// The signals SIGKILL and SIGSTOP cannot be caught or ignored. man (2) signal
291+
func tinygo_signal_ignore(sig uint32) {
292+
if syscall.Signal(sig) != syscall.SIGKILL && syscall.Signal(sig) != syscall.SIGSTOP {
293+
signalIgnored[sig] = true
294+
}
295+
}
296+
278297
// void tinygo_signal_handler(int sig);
279298
//
280299
//export tinygo_signal_handler
281300
func tinygo_signal_handler(s int32) {
301+
if signalIgnored[s] {
302+
return
303+
}
304+
282305
select {
283306
case signalChan <- uint32(s):
284307
default:

src/runtime/signal.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
//go:build none
1+
// go:build none
22

33
// Ignore the //go:build above. This file is manually included on Linux and
44
// MacOS to provide os/signal support.
55

6+
// see man (7) feature_test_macros
7+
// _XOPEN_SOURCE is needed to control the definitions that are exposed by
8+
// system header files when a program is compiled
9+
#define _XOPEN_SOURCE 700
610
#include <stdint.h>
711
#include <signal.h>
812
#include <unistd.h>
@@ -11,9 +15,19 @@
1115
void tinygo_signal_handler(int sig);
1216

1317
// Enable a signal from the runtime.
14-
void tinygo_signal_enable(uint32_t sig) {
15-
struct sigaction act = { 0 };
18+
void tinygo_signal_enable(uint32_t sig)
19+
{
20+
struct sigaction act = {0};
1621
act.sa_flags = SA_SIGINFO;
1722
act.sa_handler = &tinygo_signal_handler;
1823
sigaction(sig, &act, NULL);
1924
}
25+
26+
// Disable a signal from the runtime.
27+
// Defer the signal handler to the default handler for now.
28+
void tinygo_signal_disable(uint32_t sig)
29+
{
30+
struct sigaction act = {0};
31+
act.sa_handler = SIG_DFL;
32+
sigaction(sig, &act, NULL);
33+
}

0 commit comments

Comments
 (0)