3 # Tests for perl exit codes, playing with $?, etc...
11 # Run some code, return its wait status.
14 $code = "\"" . $code . "\"" if $^O eq 'VMS'; #VMS needs quotes for this.
15 return system($^X, "-e", $code);
19 $numtests = ($^O eq 'VMS') ? 16 : 17;
23 my $vms_exit_mode = 0;
26 if (eval 'require VMS::Feature') {
27 $vms_exit_mode = !(VMS::Feature::current("posix_exit"));
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) ) {
39 $numtests = 29 unless $vms_exit_mode;
43 plan(tests => $numtests);
45 my $native_success = 0;
46 $native_success = 1 if $^O eq 'VMS';
51 is( $exit >> 8, 0, 'Normal exit' );
52 is( $exit, $?, 'Normal exit $?' );
53 is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
55 if (!$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}' );
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");
73 skip("Skip signals and core dump tests on Win32 and VMS", 7)
74 if ($^O eq 'MSWin32' || $^O eq 'VMS');
76 #TODO VMS will backtrace on this test and exits with code of 0
79 $exit = run('kill 15, $$; sleep(1);');
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}' );
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");
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.
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.
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.
114 # Note that this is testing the perl exit() routine, and not the VMS
115 # DCL EXIT statement.
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.
120 # Double quotes are needed to pass these commands through DCL to PERL
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' );
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' );
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' );
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' );
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' );
142 $exit = run("exit 02015320012"); # POSIX exit code 1
143 is( $exit >> 8, 1, 'Posix exit code 1' );
145 $exit = run("exit 02015323771"); # POSIX exit code 255
146 is( $exit >> 8 , 255, 'Posix exit code 255' );
150 $exit = run("END { \$? = $exit_arg }");
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.
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.
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.
166 $exit_arg = (44 & 7) if $vms_exit_mode;
168 is( $exit >> 8, $exit_arg, 'Changing $? in END block' );