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 $numtests = ($^O eq 'VMS') ? 16 : 17; 20} 21 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 { 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; 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 42require "test.pl"; 43plan(tests => $numtests); 44 45my $native_success = 0; 46 $native_success = 1 if $^O eq 'VMS'; 47 48my $exit, $exit_arg; 49 50$exit = run('exit'); 51is( $exit >> 8, 0, 'Normal exit' ); 52is( $exit, $?, 'Normal exit $?' ); 53is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' ); 54 55if (!$vms_exit_mode) { 56 my $posix_ok = eval { require POSIX; }; 57 my $wait_macros_ok = defined &POSIX::WIFEXITED; 58 eval { POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}) }; 59 $wait_macros_ok = 0 if $@; 60 $exit = run('exit 42'); 61 is( $exit >> 8, 42, 'Non-zero exit' ); 62 is( $exit, $?, 'Non-zero exit $?' ); 63 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' ); 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"); 70 } 71 72 SKIP: { 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. 78 79 $exit = run('kill 15, $$; sleep(1);'); 80 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: { 86 skip("No POSIX", 3) unless $posix_ok; 87 skip("No POSIX wait macros", 3) unless $wait_macros_ok; 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"); 91 } 92 } 93 94} 95 96if ($^O eq 'VMS') { 97 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 121 122 $exit = run("exit 268632065"); # %CLI-S-NORMAL 123 is( $exit >> 8, 0, 'PERL success exit' ); 124 is( ${^CHILD_ERROR_NATIVE} & 7, 1, 'VMS success exit' ); 125 126 $exit = run("exit 268632067"); # %CLI-I-NORMAL 127 is( $exit >> 8, 0, 'PERL informational exit' ); 128 is( ${^CHILD_ERROR_NATIVE} & 7, 3, 'VMS informational exit' ); 129 130 $exit = run("exit 268632064"); # %CLI-W-NORMAL 131 is( $exit >> 8, 1, 'Perl warning exit' ); 132 is( ${^CHILD_ERROR_NATIVE} & 7, 0, 'VMS warning exit' ); 133 134 $exit = run("exit 268632066"); # %CLI-E-NORMAL 135 is( $exit >> 8, 2, 'Perl error exit' ); 136 is( ${^CHILD_ERROR_NATIVE} & 7, 2, 'VMS error exit' ); 137 138 $exit = run("exit 268632068"); # %CLI-F-NORMAL 139 is( $exit >> 8, 4, 'Perl fatal error exit' ); 140 is( ${^CHILD_ERROR_NATIVE} & 7, 4, 'VMS fatal exit' ); 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' ); 147} 148 149$exit_arg = 42; 150$exit = run("END { \$? = $exit_arg }"); 151 152# On VMS, in the child process the actual exit status will be SS$_ABORT, 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 166$exit_arg = (44 & 7) if $vms_exit_mode; 167 168is( $exit >> 8, $exit_arg, 'Changing $? in END block' ); 169