raz / commit 8b69ed1a

commit
8b69ed1af570159934a7d0bfcea65b549c183ac7
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
8be02157
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

patch · log

3 files changed, +96 -5

doc/raz-lib.md (history) modified +3 -1

@@ -616,7 +616,9 @@
616616 signals `Rt` reports, SIGSEGV and SIGFPE, is -22 (EINVAL). `ignore`
617617 makes a signal do nothing, as a shell ignores `sigTtou` so that it can
618618 take its terminal back, and `ignore(sig, false)` gives it back what it
619-does at first. A program that `spawn` starts begins with neither.
619+does at first. On a signal that `catch` took, either gives it up: the
620+signal is no longer caught, with its default action or ignored, and
621+`catch`'s fd gives the signals still caught. A program that `spawn` starts begins with neither.
620622 `ignored(sig)` says whether a signal does nothing now, because the
621623 program ignored it or because the one that started this one left it
622624 so, as a program in the background or under `nohup` finds `sigInt` and

lib/Sys.linux-x86_64.raz (history) modified +52 -3

@@ -2068,7 +2068,7 @@
20682068 (* Catches the signals sigs: blocks them, so that they don't do what
20692069 they would, and gives them on an fd, which a later catch adds to and
20702070 gives again; or minus an errno. Only the thread that catches them
2071- blocks them, and threads it starts later. *)
2071+ blocks them, and threads it starts later. ignore gives a signal up. *)
20722072 proc catch*(sigs: []INT): INT;
20732073 var m: U64; i, fd: INT;
20742074 begin
@@ -2099,11 +2099,21 @@
20992099 end;
21002100
21012101 (* Makes the signal sig do nothing, or, if not yes, what it does at
2102- first: 0, or minus an errno. *)
2102+ first: 0, or minus an errno. A signal catch took is given up: it is
2103+ unblocked in this thread, and catch's fd no longer gives it. *)
21032104 proc ignore*(sig: INT; yes: BOOL): INT;
2104-var act: [4]U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *)
2105+var act: [4]U64; bit, m: U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *)
21052106 begin
21062107 if yes then act[0] := 1; end;
2108+ if (sig >= 1) and (sig <= 64) then
2109+ bit := U64(1) shl (sig - 1);
2110+ if caughtSigs & bit # 0 then
2111+ caughtSigs := caughtSigs & ~bit;
2112+ m := caughtSigs;
2113+ _ := SYSCALL(289, sigFd, ADR(m), 8, 80800H); (* signalfd4: the signals still caught *)
2114+ _ := SYSCALL(14, 1, ADR(bit), 0, 8); (* rt_sigprocmask: SIG_UNBLOCK *)
2115+ end;
2116+ end;
21072117 return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *)
21082118 end;
21092119
@@ -2499,6 +2509,44 @@
24992509 close(l);
25002510 end;
25012511
2512+(* Runs a copy of this program that catches sigUsr1 and sigUsr2, gives
2513+ up sigUsr1 by ignore(sigUsr1, yes) and sends itself send. The copy
2514+ exits 0 if sigUsr1 then does nothing, or sigUsr2 still comes on
2515+ catch's fd, and 1 if not; this says how it ended. *)
2516+proc giveUpChild(yes: BOOL; send: INT): (Status, INT);
2517+var child, c, fd: INT; st: Status;
2518+begin
2519+ child := fork();
2520+ if child = 0 then
2521+ fd := catch([sigUsr1, sigUsr2]);
2522+ if fd < 0 then exit(3); end;
2523+ while caught(fd) # 0 do end; (* what the parent had left there *)
2524+ if (ignore(sigUsr1, yes) # 0) or (kill(pid(), send) # 0) then exit(3); end;
2525+ if send = sigUsr1 then
2526+ if ready(fd, 100) then exit(1); end; (* it ought to have ended, or be ignored *)
2527+ exit(0);
2528+ end;
2529+ if not ready(fd, 1000) or (caught(fd) # sigUsr2) then exit(1); end;
2530+ exit(0);
2531+ end;
2532+ _, st, c := wait(child, true);
2533+ return st, c;
2534+end;
2535+
2536+(* The test of giving a caught signal up: with the default back it
2537+ ends the program; ignored it does nothing; and the signals still
2538+ caught still come on catch's fd. *)
2539+proc testGiveUp;
2540+var st: Status; c: INT;
2541+begin
2542+ st, c := giveUpChild(false, sigUsr1);
2543+ if (st # killed) or (c # sigUsr1) then exit(2); end;
2544+ st, c := giveUpChild(true, sigUsr1);
2545+ if (st # exited) or (c # 0) then exit(2); end;
2546+ st, c := giveUpChild(false, sigUsr2);
2547+ if (st # exited) or (c # 0) then exit(2); end;
2548+end;
2549+
25022550 (* The test of what a program asks of itself: a signal ignored or not,
25032551 an environment variable unset or empty, and the processors. *)
25042552 proc testProcess;
@@ -2867,6 +2915,7 @@
28672915 testShell;
28682916 testNet;
28692917 testProcess;
2918+ testGiveUp;
28702919 testServer;
28712920 testMaps;
28722921 end;

lib/Sys.raz (history) modified +41 -1

@@ -2044,7 +2044,8 @@
20442044 end;
20452045
20462046 (* Makes the signal sig do nothing, or, if not yes, what it does at
2047- first: 0, or minus an errno. *)
2047+ first: 0, or minus an errno. A signal catch took is given up: it is
2048+ caught no more (a number already on catch's fd is still given). *)
20482049 proc ignore*(sig: INT; yes: BOOL): INT;
20492050 var act: Sigaction;
20502051 begin
@@ -2453,6 +2454,44 @@
24532454 close(l);
24542455 end;
24552456
2457+(* Runs a copy of this program that catches sigUsr1 and sigUsr2, gives
2458+ up sigUsr1 by ignore(sigUsr1, yes) and sends itself send. The copy
2459+ exits 0 if sigUsr1 then does nothing, or sigUsr2 still comes on
2460+ catch's fd, and 1 if not; this says how it ended. *)
2461+proc giveUpChild(yes: BOOL; send: INT): (Status, INT);
2462+var child, c, fd: INT; st: Status;
2463+begin
2464+ child := fork();
2465+ if child = 0 then
2466+ fd := catch([sigUsr1, sigUsr2]);
2467+ if fd < 0 then exit(3); end;
2468+ while caught(fd) # 0 do end; (* what the parent had left there *)
2469+ if (ignore(sigUsr1, yes) # 0) or (kill(pid(), send) # 0) then exit(3); end;
2470+ if send = sigUsr1 then
2471+ if ready(fd, 100) then exit(1); end; (* it ought to have ended, or be ignored *)
2472+ exit(0);
2473+ end;
2474+ if not ready(fd, 1000) or (caught(fd) # sigUsr2) then exit(1); end;
2475+ exit(0);
2476+ end;
2477+ _, st, c := wait(child, true);
2478+ return st, c;
2479+end;
2480+
2481+(* The test of giving a caught signal up: with the default back it
2482+ ends the program; ignored it does nothing; and the signals still
2483+ caught still come on catch's fd. *)
2484+proc testGiveUp;
2485+var st: Status; c: INT;
2486+begin
2487+ st, c := giveUpChild(false, sigUsr1);
2488+ if (st # killed) or (c # sigUsr1) then exit(2); end;
2489+ st, c := giveUpChild(true, sigUsr1);
2490+ if (st # exited) or (c # 0) then exit(2); end;
2491+ st, c := giveUpChild(false, sigUsr2);
2492+ if (st # exited) or (c # 0) then exit(2); end;
2493+end;
2494+
24562495 (* The test of what a program asks of itself: a signal ignored or not,
24572496 an environment variable unset or empty, and the processors. *)
24582497 proc testProcess;
@@ -2820,6 +2859,7 @@
28202859 testShell;
28212860 testNet;
28222861 testProcess;
2862+ testGiveUp;
28232863 testServer;
28242864 testMaps;
28252865 end;