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