This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/loc_tools.pl: Consider thread 0 always locale-safe
[perl5.git] / t / run / exit.t
CommitLineData
14cf881c
MS
1#!./perl
2#
3# Tests for perl exit codes, playing with $?, etc...
4
5
6BEGIN {
7 chdir 't' if -d 't';
69026470 8 @INC = qw(. ../lib);
14cf881c
MS
9}
10
14cf881c
MS
11# Run some code, return its wait status.
12sub run {
13 my($code) = shift;
6cd63c21 14 $code = "\"" . $code . "\"" if $^O eq 'VMS'; #VMS needs quotes for this.
755bf813 15 return system($^X, "-e", $code);
14cf881c
MS
16}
17
0ab78052 18BEGIN {
7b903762 19 $numtests = ($^O eq 'VMS') ? 16 : 17;
0ab78052 20}
14cf881c 21
1a3aec58
JM
22
23my $vms_exit_mode = 0;
24
25if ($^O eq 'VMS') {
26 if (eval 'require VMS::Feature') {
27 $vms_exit_mode = !(VMS::Feature::current("posix_exit"));
28 } else {
e08e1e1d
JM
29 my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
30 my $env_posix_ex = $ENV{'PERL_VMS_POSIX_EXIT'} || '';
31 my $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
32 my $posix_ex = $env_posix_ex =~ /^[ET1]/i;
1a3aec58
JM
33 if (($unix_rpt || $posix_ex) ) {
34 $vms_exit_mode = 0;
35 } else {
36 $vms_exit_mode = 1;
37 }
38 }
39 $numtests = 29 unless $vms_exit_mode;
40}
41
1ae3d757 42require "./test.pl";
69026470 43plan(tests => $numtests);
55a9fe1d 44
6cd63c21
JM
45my $native_success = 0;
46 $native_success = 1 if $^O eq 'VMS';
47
55a9fe1d 48my $exit, $exit_arg;
14cf881c
MS
49
50$exit = run('exit');
51is( $exit >> 8, 0, 'Normal exit' );
e5218da5 52is( $exit, $?, 'Normal exit $?' );
6cd63c21 53is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
14cf881c 54
1a3aec58 55if (!$vms_exit_mode) {
e5218da5 56 my $posix_ok = eval { require POSIX; };
5c5be41c 57 my $wait_macros_ok = defined &POSIX::WIFEXITED;
d781deb6 58 eval { POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}) };
ac0638ad 59 $wait_macros_ok = 0 if $@;
55a9fe1d
CB
60 $exit = run('exit 42');
61 is( $exit >> 8, 42, 'Non-zero exit' );
e5218da5
GA
62 is( $exit, $?, 'Non-zero exit $?' );
63 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
5c5be41c
SH
64 SKIP: {
65 skip("No POSIX", 3) unless $posix_ok;
66 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
67 ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
68 ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
69 is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
e5218da5
GA
70 }
71
5c5be41c 72 SKIP: {
1a3aec58
JM
73 skip("Skip signals and core dump tests on Win32 and VMS", 7)
74 if ($^O eq 'MSWin32' || $^O eq 'VMS');
75
76 #TODO VMS will backtrace on this test and exits with code of 0
77 #instead of 15.
5c5be41c
SH
78
79 $exit = run('kill 15, $$; sleep(1);');
e5218da5 80
5c5be41c
SH
81 is( $exit & 127, 15, 'Term by signal' );
82 ok( !($exit & 128), 'No core dump' );
83 is( $? & 127, 15, 'Term by signal $?' );
84 isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
85 SKIP: {
e5218da5 86 skip("No POSIX", 3) unless $posix_ok;
5c5be41c 87 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
e5218da5
GA
88 ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
89 ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
90 is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
5c5be41c 91 }
e5218da5 92 }
55a9fe1d 93
1a3aec58
JM
94}
95
96if ($^O eq 'VMS') {
55a9fe1d 97
6cd63c21
JM
98# On VMS, successful returns from system() are reported 0, VMS errors that
99# can not be translated to UNIX are reported as EVMSERR, which has a value
100# of 65535. Codes from 2 through 7 are assumed to be from non-compliant
101# VMS systems and passed through. Programs written to use _POSIX_EXIT()
102# codes like GNV will pass the numbers 2 through 255 encoded in the
103# C facility by multiplying the number by 8 and adding %x35A000 to it.
104# Perl will decode that number from children back to it's internal status.
105#
106# For native VMS status codes, success codes are odd numbered, error codes
107# are even numbered. The 3 LSBs of the code indicate if the success is
108# an informational message or the severity of the failure.
109#
110# Because the failure codes for the tests of the CLI facility status codes can
111# not be translated to UNIX error codes, they will be reported as EVMSERR,
112# even though Perl will exit with them having the VMS status codes.
113#
114# Note that this is testing the perl exit() routine, and not the VMS
115# DCL EXIT statement.
116#
117# The value %x1000000 has been added to the exit code to prevent the
118# status message from being sent to the STDOUT and STDERR stream.
119#
120# Double quotes are needed to pass these commands through DCL to PERL
55a9fe1d 121
6cd63c21 122 $exit = run("exit 268632065"); # %CLI-S-NORMAL
7a7fd8e0 123 is( $exit >> 8, 0, 'PERL success exit' );
6cd63c21 124 is( ${^CHILD_ERROR_NATIVE} & 7, 1, 'VMS success exit' );
55a9fe1d 125
6cd63c21 126 $exit = run("exit 268632067"); # %CLI-I-NORMAL
7a7fd8e0 127 is( $exit >> 8, 0, 'PERL informational exit' );
6cd63c21 128 is( ${^CHILD_ERROR_NATIVE} & 7, 3, 'VMS informational exit' );
55a9fe1d 129
6cd63c21 130 $exit = run("exit 268632064"); # %CLI-W-NORMAL
7a7fd8e0 131 is( $exit >> 8, 1, 'Perl warning exit' );
6cd63c21 132 is( ${^CHILD_ERROR_NATIVE} & 7, 0, 'VMS warning exit' );
55a9fe1d 133
6cd63c21 134 $exit = run("exit 268632066"); # %CLI-E-NORMAL
7a7fd8e0 135 is( $exit >> 8, 2, 'Perl error exit' );
6cd63c21 136 is( ${^CHILD_ERROR_NATIVE} & 7, 2, 'VMS error exit' );
55a9fe1d 137
6cd63c21 138 $exit = run("exit 268632068"); # %CLI-F-NORMAL
7a7fd8e0 139 is( $exit >> 8, 4, 'Perl fatal error exit' );
6cd63c21 140 is( ${^CHILD_ERROR_NATIVE} & 7, 4, 'VMS fatal exit' );
7a7fd8e0
JM
141
142 $exit = run("exit 02015320012"); # POSIX exit code 1
143 is( $exit >> 8, 1, 'Posix exit code 1' );
144
145 $exit = run("exit 02015323771"); # POSIX exit code 255
146 is( $exit >> 8 , 255, 'Posix exit code 255' );
55a9fe1d 147}
14cf881c 148
412a0271
CB
149$exit_arg = 42;
150$exit = run("END { \$? = $exit_arg }");
151
e08e1e1d 152# On VMS, in the child process the actual exit status will be SS$_ABORT,
6cd63c21
JM
153# or 44, which is what you get from any non-zero value of $? except for
154# 65535 that has been dePOSIXified by STATUS_UNIX_SET. If $? is set to
155# 65535 internally when there is a VMS status code that is valid, and
156# when Perl exits, it will set that status code.
157#
158# In this test on VMS, the child process exit with a SS$_ABORT, which
159# the parent stores in ${^CHILD_ERROR_NATIVE}. The SS$_ABORT code is
160# then translated to the UNIX code EINTR which has the value of 4 on VMS.
161#
162# This is complex because Perl translates internally generated UNIX
163# status codes to SS$_ABORT on exit, but passes through unmodified UNIX
164# status codes that exit() is called with by scripts.
165
1a3aec58 166$exit_arg = (44 & 7) if $vms_exit_mode;
412a0271
CB
167
168is( $exit >> 8, $exit_arg, 'Changing $? in END block' );