From 8b69ed1af570159934a7d0bfcea65b549c183ac7 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:59:33 +0100 Subject: [PATCH] Sys.ignore gives a signal catch took up: unblocked and no longer on catch's fd (signalfd mask updated), with its default or ignored; a test in both Sys backends --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 5482733..b960624 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -616,7 +616,9 @@ signals `Rt` reports, SIGSEGV and SIGFPE, is -22 (EINVAL). `ignore` makes a signal do nothing, as a shell ignores `sigTtou` so that it can take its terminal back, and `ignore(sig, false)` gives it back what it -does at first. A program that `spawn` starts begins with neither. +does at first. On a signal that `catch` took, either gives it up: the +signal is no longer caught, with its default action or ignored, and +`catch`'s fd gives the signals still caught. A program that `spawn` starts begins with neither. `ignored(sig)` says whether a signal does nothing now, because the program ignored it or because the one that started this one left it so, as a program in the background or under `nohup` finds `sigInt` and diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 923a29f..a147e27 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -2068,7 +2068,7 @@ (* Catches the signals sigs: blocks them, so that they don't do what they would, and gives them on an fd, which a later catch adds to and gives again; or minus an errno. Only the thread that catches them - blocks them, and threads it starts later. *) + blocks them, and threads it starts later. ignore gives a signal up. *) proc catch*(sigs: []INT): INT; var m: U64; i, fd: INT; begin @@ -2099,11 +2099,21 @@ end; (* Makes the signal sig do nothing, or, if not yes, what it does at - first: 0, or minus an errno. *) + first: 0, or minus an errno. A signal catch took is given up: it is + unblocked in this thread, and catch's fd no longer gives it. *) proc ignore*(sig: INT; yes: BOOL): INT; -var act: [4]U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *) +var act: [4]U64; bit, m: U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *) begin if yes then act[0] := 1; end; + if (sig >= 1) and (sig <= 64) then + bit := U64(1) shl (sig - 1); + if caughtSigs & bit # 0 then + caughtSigs := caughtSigs & ~bit; + m := caughtSigs; + _ := SYSCALL(289, sigFd, ADR(m), 8, 80800H); (* signalfd4: the signals still caught *) + _ := SYSCALL(14, 1, ADR(bit), 0, 8); (* rt_sigprocmask: SIG_UNBLOCK *) + end; + end; return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) end; @@ -2499,6 +2509,44 @@ close(l); end; +(* Runs a copy of this program that catches sigUsr1 and sigUsr2, gives + up sigUsr1 by ignore(sigUsr1, yes) and sends itself send. The copy + exits 0 if sigUsr1 then does nothing, or sigUsr2 still comes on + catch's fd, and 1 if not; this says how it ended. *) +proc giveUpChild(yes: BOOL; send: INT): (Status, INT); +var child, c, fd: INT; st: Status; +begin + child := fork(); + if child = 0 then + fd := catch([sigUsr1, sigUsr2]); + if fd < 0 then exit(3); end; + while caught(fd) # 0 do end; (* what the parent had left there *) + if (ignore(sigUsr1, yes) # 0) or (kill(pid(), send) # 0) then exit(3); end; + if send = sigUsr1 then + if ready(fd, 100) then exit(1); end; (* it ought to have ended, or be ignored *) + exit(0); + end; + if not ready(fd, 1000) or (caught(fd) # sigUsr2) then exit(1); end; + exit(0); + end; + _, st, c := wait(child, true); + return st, c; +end; + +(* The test of giving a caught signal up: with the default back it + ends the program; ignored it does nothing; and the signals still + caught still come on catch's fd. *) +proc testGiveUp; +var st: Status; c: INT; +begin + st, c := giveUpChild(false, sigUsr1); + if (st # killed) or (c # sigUsr1) then exit(2); end; + st, c := giveUpChild(true, sigUsr1); + if (st # exited) or (c # 0) then exit(2); end; + st, c := giveUpChild(false, sigUsr2); + if (st # exited) or (c # 0) then exit(2); end; +end; + (* The test of what a program asks of itself: a signal ignored or not, an environment variable unset or empty, and the processors. *) proc testProcess; @@ -2867,6 +2915,7 @@ testShell; testNet; testProcess; + testGiveUp; testServer; testMaps; end; diff --git a/lib/Sys.raz b/lib/Sys.raz index 56d0737..3c07e85 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -2044,7 +2044,8 @@ end; (* Makes the signal sig do nothing, or, if not yes, what it does at - first: 0, or minus an errno. *) + first: 0, or minus an errno. A signal catch took is given up: it is + caught no more (a number already on catch's fd is still given). *) proc ignore*(sig: INT; yes: BOOL): INT; var act: Sigaction; begin @@ -2453,6 +2454,44 @@ close(l); end; +(* Runs a copy of this program that catches sigUsr1 and sigUsr2, gives + up sigUsr1 by ignore(sigUsr1, yes) and sends itself send. The copy + exits 0 if sigUsr1 then does nothing, or sigUsr2 still comes on + catch's fd, and 1 if not; this says how it ended. *) +proc giveUpChild(yes: BOOL; send: INT): (Status, INT); +var child, c, fd: INT; st: Status; +begin + child := fork(); + if child = 0 then + fd := catch([sigUsr1, sigUsr2]); + if fd < 0 then exit(3); end; + while caught(fd) # 0 do end; (* what the parent had left there *) + if (ignore(sigUsr1, yes) # 0) or (kill(pid(), send) # 0) then exit(3); end; + if send = sigUsr1 then + if ready(fd, 100) then exit(1); end; (* it ought to have ended, or be ignored *) + exit(0); + end; + if not ready(fd, 1000) or (caught(fd) # sigUsr2) then exit(1); end; + exit(0); + end; + _, st, c := wait(child, true); + return st, c; +end; + +(* The test of giving a caught signal up: with the default back it + ends the program; ignored it does nothing; and the signals still + caught still come on catch's fd. *) +proc testGiveUp; +var st: Status; c: INT; +begin + st, c := giveUpChild(false, sigUsr1); + if (st # killed) or (c # sigUsr1) then exit(2); end; + st, c := giveUpChild(true, sigUsr1); + if (st # exited) or (c # 0) then exit(2); end; + st, c := giveUpChild(false, sigUsr2); + if (st # exited) or (c # 0) then exit(2); end; +end; + (* The test of what a program asks of itself: a signal ignored or not, an environment variable unset or empty, and the processors. *) proc testProcess; @@ -2820,6 +2859,7 @@ testShell; testNet; testProcess; + testGiveUp; testServer; testMaps; end;