Skip to content

Commit 573d361

Browse files
committed
add signal.Ignore, add signal.Ignored, update signal tests, add Exited()
Signed-off-by: leongross <leon.gross@9elements.com>
1 parent a3ea8fd commit 573d361

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/os/exec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (p *ProcessState) Sys() interface{} {
4747
return nil // TODO
4848
}
4949

50+
func (p *ProcessState) Exited() bool {
51+
return false // TODO
52+
}
53+
5054
// ExitCode returns the exit code of the exited process, or -1
5155
// if the process hasn't exited or was terminated by a signal.
5256
func (p *ProcessState) ExitCode() int {

src/runtime/runtime_unix.go

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

55
import (
6-
"syscall"
76
"unsafe"
87
)
98

@@ -267,7 +266,17 @@ func init() {
267266
}
268267

269268
var signalChan chan uint32
270-
var signalIgnored []bool // TODO: replace with more efficient bitmap?
269+
var signalIgnored []bool
270+
271+
// SignalIgnored returns the signalIgnored array.
272+
// Do not expose signalIgnored directly, as it is mutable.
273+
//
274+
//export SignalIgnored
275+
func SignalIgnored() []bool {
276+
sigs := make([]bool, len(signalIgnored))
277+
copy(sigs, signalIgnored)
278+
return sigs
279+
}
271280

272281
//go:linkname signal_enable os/signal.signal_enable
273282
func signal_enable(sig uint32) {
@@ -278,6 +287,7 @@ func signal_enable(sig uint32) {
278287
//export tinygo_signal_enable
279288
func tinygo_signal_enable(s uint32)
280289

290+
// go: link signal_disable os/signal.signal_disable
281291
func signal_disable(sig uint32) {
282292
tinygo_signal_disable(sig)
283293
}
@@ -288,10 +298,22 @@ func tinygo_signal_disable(sig uint32)
288298
// Ignore the given signal by adding it into the signalIgnored array.
289299
// If the signal is received, it will be ignored in the tinygo_signal_handler.
290300
// 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-
}
301+
//
302+
// func tinygo_signal_ignore(sig uint32) {
303+
// if syscall.Signal(sig) != syscall.SIGKILL && syscall.Signal(sig) != syscall.SIGSTOP {
304+
// signalIgnored[sig] = true
305+
// }
306+
// }
307+
308+
//export tinygo_signal_ignore
309+
func tinygo_signal_ignore(sig uint32)
310+
311+
// go: link signal_ignore os/signal.signal_ignore
312+
func signal_ignore(sig uint32) {
313+
// keep track of ignored signal for Ignore(sig os.Signal)
314+
// the ignore logic itself is tracked by the kernel https://elixir.bootlin.com/linux/v6.10/source/kernel/signal.c#L4142
315+
signalIgnored[sig] = true
316+
tinygo_signal_ignore(sig)
295317
}
296318

297319
// void tinygo_signal_handler(int sig);

src/runtime/signal.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.
@@ -31,3 +31,11 @@ void tinygo_signal_disable(uint32_t sig)
3131
act.sa_handler = SIG_DFL;
3232
sigaction(sig, &act, NULL);
3333
}
34+
35+
// Ignore a signal from the runtime.
36+
void tinygo_signal_ignore(uint32_t sig)
37+
{
38+
struct sigaction act = {0};
39+
act.sa_handler = SIG_IGN;
40+
sigaction(sig, &act, NULL);
41+
}

testdata/signal/signal.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,23 @@ func main() {
2525
}
2626
}
2727
}()
28+
// test signal_enable
2829

29-
// Send the signal.
30-
syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
30+
// test signal_disable
31+
32+
// test signal_ignore
33+
signal.Ignore(syscall.SIGUSR1)
34+
if signal.Ignored(syscall.SIGUSR1) {
35+
println("SIGUSR1 is ignored")
36+
} else {
37+
println("SIGUSR1 is not ignored")
38+
}
3139

40+
// test signal_ignore SIGKILL and SIGSTOP that cannot be caught or ignored
41+
signal.Ignore()
42+
43+
// send the signal.
44+
syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
3245
time.Sleep(time.Millisecond * 100)
3346
println("exiting signal program")
3447
}

0 commit comments

Comments
 (0)