| | @@ -2068,7 +2068,7 @@ |
| 2068 | 2068 | (* Catches the signals sigs: blocks them, so that they don't do what |
| 2069 | 2069 | they would, and gives them on an fd, which a later catch adds to and |
| 2070 | 2070 | 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. *) |
| 2072 | 2072 | proc catch*(sigs: []INT): INT; |
| 2073 | 2073 | var m: U64; i, fd: INT; |
| 2074 | 2074 | begin |
| | @@ -2099,11 +2099,21 @@ |
| 2099 | 2099 | end; |
| 2100 | 2100 | |
| 2101 | 2101 | (* 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. *) |
| 2103 | 2104 | 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 *) |
| 2105 | 2106 | begin |
| 2106 | 2107 | 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; |
| 2107 | 2117 | return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) |
| 2108 | 2118 | end; |
| 2109 | 2119 | |
| | @@ -2499,6 +2509,44 @@ |
| 2499 | 2509 | close(l); |
| 2500 | 2510 | end; |
| 2501 | 2511 | |
| 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 | + |
| 2502 | 2550 | (* The test of what a program asks of itself: a signal ignored or not, |
| 2503 | 2551 | an environment variable unset or empty, and the processors. *) |
| 2504 | 2552 | proc testProcess; |
| | @@ -2867,6 +2915,7 @@ |
| 2867 | 2915 | testShell; |
| 2868 | 2916 | testNet; |
| 2869 | 2917 | testProcess; |
| 2918 | + testGiveUp; |
| 2870 | 2919 | testServer; |
| 2871 | 2920 | testMaps; |
| 2872 | 2921 | end; |