| 1 |
#!/bin/sh |
| 2 |
exec guile -q -s "$0" "$@" |
| 3 |
!# |
| 4 |
;;; test-sigaction-fork --- Signal thread vs. fork, again. |
| 5 |
;;; |
| 6 |
;;; Copyright (C) 2024 Free Software Foundation, Inc. |
| 7 |
;;; |
| 8 |
;;; This library is free software; you can redistribute it and/or |
| 9 |
;;; modify it under the terms of the GNU Lesser General Public |
| 10 |
;;; License as published by the Free Software Foundation; either |
| 11 |
;;; version 3 of the License, or (at your option) any later version. |
| 12 |
;;; |
| 13 |
;;; This library is distributed in the hope that it will be useful, |
| 14 |
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
;;; Lesser General Public License for more details. |
| 17 |
;;; |
| 18 |
;;; You should have received a copy of the GNU Lesser General Public |
| 19 |
;;; License along with this library; if not, write to the Free Software |
| 20 |
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
|
| 22 |
;;; Test the bug described at <https://bugs.gnu.org/68087>: the signal |
| 23 |
;;; thread would not be restarted after a call to 'primitive-fork', |
| 24 |
;;; leading signals to be silently ignored. |
| 25 |
|
| 26 |
(use-modules (ice-9 match)) |
| 27 |
|
| 28 |
(define signals-handled |
| 29 |
;; List of signals handled. |
| 30 |
'()) |
| 31 |
|
| 32 |
(define parent |
| 33 |
;; PID of the parent process. |
| 34 |
(getpid)) |
| 35 |
|
| 36 |
(unless (provided? 'fork) |
| 37 |
(exit 77)) |
| 38 |
|
| 39 |
;; This call spawns the signal delivery thread as a side effect. |
| 40 |
(sigaction SIGALRM |
| 41 |
(lambda (signal) |
| 42 |
(call-with-blocked-asyncs |
| 43 |
(lambda () |
| 44 |
(set! signals-handled |
| 45 |
(cons `(first-handler . ,(getpid)) |
| 46 |
signals-handled)))))) |
| 47 |
|
| 48 |
(kill (getpid) SIGALRM) |
| 49 |
(while (null? signals-handled) ;let the async run |
| 50 |
(sleep 1)) |
| 51 |
|
| 52 |
(match (primitive-fork) |
| 53 |
(0 |
| 54 |
(pk 'child (getpid) signals-handled) |
| 55 |
(kill (getpid) SIGALRM) ;first handler |
| 56 |
(sleep 1) ;let the async run |
| 57 |
(sigaction SIGALRM |
| 58 |
(lambda (signal) |
| 59 |
(call-with-blocked-asyncs |
| 60 |
(lambda () |
| 61 |
(set! signals-handled |
| 62 |
(cons `(second-handler . ,(getpid)) |
| 63 |
signals-handled)))))) |
| 64 |
(kill (getpid) SIGALRM) ;second handler |
| 65 |
(sleep 1) ;give asyncs one more chance to run |
| 66 |
(format (current-error-port) "signals handled by the child + parent: ~s~%" |
| 67 |
signals-handled) |
| 68 |
(exit (equal? signals-handled |
| 69 |
`((second-handler . ,(getpid)) |
| 70 |
(first-handler . ,(getpid)) |
| 71 |
(first-handler . ,parent))))) |
| 72 |
|
| 73 |
(child |
| 74 |
(kill (getpid) SIGALRM) ;first handler |
| 75 |
(sleep 1) ;give asyncs one more chance to run |
| 76 |
(format (current-error-port) "signals handled by the parent: ~s~%" |
| 77 |
signals-handled) |
| 78 |
(exit (and (equal? signals-handled |
| 79 |
`((first-handler . ,parent) |
| 80 |
(first-handler . ,parent))) |
| 81 |
(zero? (cdr (waitpid child))))))) |
| 82 |
|
| 83 |
;;; Local Variables: |
| 84 |
;;; mode: scheme |
| 85 |
;;; End: |