This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
svleak.t: Enable syntax error tests under -Dmad
[perl5.git] / t / op / sigdispatch.t
CommitLineData
8f3964af
NC
1#!perl -w
2
3# We assume that TestInit has been used.
4
5BEGIN {
6 require './test.pl';
7}
8
9use strict;
0c1bf4c7 10use Config;
8f3964af 11
100c03aa 12plan tests => 29;
6281d426 13$| = 1;
8f3964af 14
011c3814 15watchdog(15);
8f3964af
NC
16
17$SIG{ALRM} = sub {
18 die "Alarm!\n";
19};
20
21pass('before the first loop');
22
23alarm 2;
24
25eval {
26 1 while 1;
27};
28
29is($@, "Alarm!\n", 'after the first loop');
30
31pass('before the second loop');
32
33alarm 2;
34
35eval {
36 while (1) {
37 }
38};
39
40is($@, "Alarm!\n", 'after the second loop');
0c1bf4c7
LT
41
42SKIP: {
61fb63a6 43 skip('We can\'t test blocking without sigprocmask', 17)
b93d0e62 44 if is_miniperl() || !$Config{d_sigprocmask};
0ac0889d
RU
45 skip('This doesn\'t work on $^O threaded builds RT#88814', 17)
46 if $^O =~ /openbsd|cygwin/ && $Config{useithreads};
0c1bf4c7
LT
47
48 require POSIX;
61fb63a6
NC
49 my $pending = POSIX::SigSet->new();
50 is POSIX::sigpending($pending), '0 but true', 'sigpending';
51 is $pending->ismember(&POSIX::SIGUSR1), 0, 'SIGUSR1 is not pending';
0c1bf4c7
LT
52 my $new = POSIX::SigSet->new(&POSIX::SIGUSR1);
53 POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new);
54
55 my $gotit = 0;
56 $SIG{USR1} = sub { $gotit++ };
eb796c7f 57 kill 'SIGUSR1', $$;
7fe50b8b 58 is $gotit, 0, 'Haven\'t received third signal yet';
0ac0889d
RU
59
60 diag "2nd sigpending crashes on cygwin" if $^O eq 'cygwin';
61fb63a6
NC
61 is POSIX::sigpending($pending), '0 but true', 'sigpending';
62 is $pending->ismember(&POSIX::SIGUSR1), 1, 'SIGUSR1 is pending';
0c1bf4c7
LT
63
64 my $old = POSIX::SigSet->new();
65 POSIX::sigsuspend($old);
66 is $gotit, 1, 'Received third signal';
61fb63a6
NC
67 is POSIX::sigpending($pending), '0 but true', 'sigpending';
68 is $pending->ismember(&POSIX::SIGUSR1), 0, 'SIGUSR1 is no longer pending';
0c1bf4c7 69
7fe50b8b 70 {
eb796c7f 71 kill 'SIGUSR1', $$;
7fe50b8b
LT
72 local $SIG{USR1} = sub { die "FAIL\n" };
73 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $old);
74 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 is blocked';
75 eval { POSIX::sigsuspend(POSIX::SigSet->new) };
76 is $@, "FAIL\n", 'Exception is thrown, so received fourth signal';
77 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $old);
23292af3
CB
78TODO:
79 {
80 local $::TODO = "Needs investigation" if $^O eq 'VMS';
7fe50b8b 81 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 is still blocked';
23292af3 82 }
7fe50b8b
LT
83 }
84
554bc0f4 85 POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new);
eb796c7f 86 kill 'SIGUSR1', $$;
554bc0f4
LT
87 is $gotit, 1, 'Haven\'t received fifth signal yet';
88 POSIX::sigprocmask(&POSIX::SIG_UNBLOCK, $new, $old);
89 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 was still blocked';
7fe50b8b 90 is $gotit, 2, 'Received fifth signal';
c22d665b
LT
91
92 # test unsafe signal handlers in combination with exceptions
8aed2c65
DM
93
94 SKIP: {
95 # #89718: on old linux kernels, this test hangs. No-ones thought
96 # of a reliable way to probe for this, so for now, just skip the
97 # tests on production releases
98 skip("some OSes hang here", 3) if (int($]*1000) & 1) == 0;
99
100 my $action = POSIX::SigAction->new(sub { $gotit--, die }, POSIX::SigSet->new, 0);
101 POSIX::sigaction(&POSIX::SIGALRM, $action);
102 eval {
103 alarm 1;
104 my $set = POSIX::SigSet->new;
105 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $set);
106 is $set->ismember(&POSIX::SIGALRM), 0, "SIGALRM is not blocked on attempt $_";
107 POSIX::sigsuspend($set);
108 } for 1..2;
109 is $gotit, 0, 'Received both signals';
110 }
0c1bf4c7 111}
011c3814 112
476c37e2
NC
113SKIP: {
114 skip("alarm cannot interrupt blocking system calls on $^O", 2)
0ac0889d 115 if $^O =~ /MSWin32|cygwin|VMS/;
011c3814
DM
116 # RT #88774
117 # make sure the signal handler's called in an eval block *before*
118 # the eval is popped
119
120 $SIG{'ALRM'} = sub { die "HANDLER CALLED\n" };
121
122 eval {
123 alarm(2);
124 select(undef,undef,undef,10);
125 };
126 alarm(0);
127 is($@, "HANDLER CALLED\n", 'block eval');
128
129 eval q{
130 alarm(2);
131 select(undef,undef,undef,10);
132 };
133 alarm(0);
134 is($@, "HANDLER CALLED\n", 'string eval');
135}
84c7b88c
BF
136
137eval { $SIG{"__WARN__\0"} = sub { 1 } };
138like $@, qr/No such hook: __WARN__\\0 at/, q!Fetching %SIG hooks with an extra trailing nul is nul-clean!;
139
140eval { $SIG{"__DIE__\0whoops"} = sub { 1 } };
141like $@, qr/No such hook: __DIE__\\0whoops at/;
142
143{
144 use warnings;
145 my $w;
146 local $SIG{__WARN__} = sub { $w = shift };
147
148 $SIG{"KILL\0"} = sub { 1 };
149 like $w, qr/No such signal: SIGKILL\\0 at/, 'Arbitrary signal lookup through %SIG is clean';
150}
100c03aa
JL
151
152# [perl #45173]
153{
6281d426
TC
154 my $int_called;
155 local $SIG{INT} = sub { $int_called = 1; };
100c03aa
JL
156 $@ = "died";
157 is($@, "died");
6281d426
TC
158 kill 'INT', $$;
159 # this is needed to ensure signal delivery on MSWin32
160 sleep(1);
161 is($int_called, 1);
100c03aa
JL
162 is($@, "died");
163}