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 # MacOS system() doesn't have good return value
20 $numtests = ($^O eq 'VMS') ? 14 : ($^O eq 'MacOS') ? 0 : 17;
24 plan(tests => $numtests);
26 my $native_success = 0;
27 $native_success = 1 if $^O eq 'VMS';
33 is( $exit >> 8, 0, 'Normal exit' );
34 is( $exit, $?, 'Normal exit $?' );
35 is( ${^CHILD_ERROR_NATIVE}, $native_success, 'Normal exit ${^CHILD_ERROR_NATIVE}' );
38 my $posix_ok = eval { require POSIX; };
39 my $wait_macros_ok = defined &POSIX::WIFEXITED;
41 $exit = run('exit 42');
42 is( $exit >> 8, 42, 'Non-zero exit' );
43 is( $exit, $?, 'Non-zero exit $?' );
44 isnt( !${^CHILD_ERROR_NATIVE}, 0, 'Non-zero exit ${^CHILD_ERROR_NATIVE}' );
46 skip("No POSIX", 3) unless $posix_ok;
47 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
48 ok(POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
49 ok(!POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
50 is(POSIX::WEXITSTATUS(${^CHILD_ERROR_NATIVE}), 42, "WEXITSTATUS");
54 skip("Skip signals and core dump tests on Win32", 7) if $^O eq 'MSWin32';
56 $exit = run('kill 15, $$; sleep(1);');
58 is( $exit & 127, 15, 'Term by signal' );
59 ok( !($exit & 128), 'No core dump' );
60 is( $? & 127, 15, 'Term by signal $?' );
61 isnt( ${^CHILD_ERROR_NATIVE}, 0, 'Term by signal ${^CHILD_ERROR_NATIVE}' );
63 skip("No POSIX", 3) unless $posix_ok;
64 skip("No POSIX wait macros", 3) unless $wait_macros_ok;
65 ok(!POSIX::WIFEXITED(${^CHILD_ERROR_NATIVE}), "WIFEXITED");
66 ok(POSIX::WIFSIGNALED(${^CHILD_ERROR_NATIVE}), "WIFSIGNALED");
67 is(POSIX::WTERMSIG(${^CHILD_ERROR_NATIVE}), 15, "WTERMSIG");
73 # On VMS, successful returns from system() are reported 0, VMS errors that
74 # can not be translated to UNIX are reported as EVMSERR, which has a value
75 # of 65535. Codes from 2 through 7 are assumed to be from non-compliant
76 # VMS systems and passed through. Programs written to use _POSIX_EXIT()
77 # codes like GNV will pass the numbers 2 through 255 encoded in the
78 # C facility by multiplying the number by 8 and adding %x35A000 to it.
79 # Perl will decode that number from children back to it's internal status.
81 # For native VMS status codes, success codes are odd numbered, error codes
82 # are even numbered. The 3 LSBs of the code indicate if the success is
83 # an informational message or the severity of the failure.
85 # Because the failure codes for the tests of the CLI facility status codes can
86 # not be translated to UNIX error codes, they will be reported as EVMSERR,
87 # even though Perl will exit with them having the VMS status codes.
89 # Note that this is testing the perl exit() routine, and not the VMS
92 # The value %x1000000 has been added to the exit code to prevent the
93 # status message from being sent to the STDOUT and STDERR stream.
95 # Double quotes are needed to pass these commands through DCL to PERL
97 $exit = run("exit 268632065"); # %CLI-S-NORMAL
98 is( $exit, 0, 'PERL success exit' );
99 is( ${^CHILD_ERROR_NATIVE} & 7, 1, 'VMS success exit' );
101 $exit = run("exit 268632067"); # %CLI-I-NORMAL
102 is( $exit, 0, 'PERL informational exit' );
103 is( ${^CHILD_ERROR_NATIVE} & 7, 3, 'VMS informational exit' );
105 $exit = run("exit 268632064"); # %CLI-W-NORMAL
106 is( $exit != 0, 1, 'Perl warning exit' );
107 is( ${^CHILD_ERROR_NATIVE} & 7, 0, 'VMS warning exit' );
109 $exit = run("exit 268632066"); # %CLI-E-NORMAL
110 is( $exit != 0, 1, 'Perl error exit' );
111 is( ${^CHILD_ERROR_NATIVE} & 7, 2, 'VMS error exit' );
113 $exit = run("exit 268632068"); # %CLI-F-NORMAL
114 is( $exit != 0, 1, 'Perl fatal error exit' );
115 is( ${^CHILD_ERROR_NATIVE} & 7, 4, 'VMS fatal exit' );
119 $exit = run("END { \$? = $exit_arg }");
121 # On VMS, in the child process the actual exit status will be SS$_ABORT,
122 # or 44, which is what you get from any non-zero value of $? except for
123 # 65535 that has been dePOSIXified by STATUS_UNIX_SET. If $? is set to
124 # 65535 internally when there is a VMS status code that is valid, and
125 # when Perl exits, it will set that status code.
127 # In this test on VMS, the child process exit with a SS$_ABORT, which
128 # the parent stores in ${^CHILD_ERROR_NATIVE}. The SS$_ABORT code is
129 # then translated to the UNIX code EINTR which has the value of 4 on VMS.
131 # This is complex because Perl translates internally generated UNIX
132 # status codes to SS$_ABORT on exit, but passes through unmodified UNIX
133 # status codes that exit() is called with by scripts.
135 $exit_arg = 4 if $^O eq 'VMS';
137 is( $exit >> 8, $exit_arg, 'Changing $? in END block' );