Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / util / wrap.pl.in
1 #! {- $config{HASHBANGPERL} -}
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7 use File::Spec::Functions;
8
9 BEGIN {
10     # This method corresponds exactly to 'use OpenSSL::Util',
11     # but allows us to use a platform specific file spec.
12     require {-
13          use Cwd qw(abs_path);
14
15          "'" . abs_path(catfile($config{sourcedir},
16                                 'util', 'perl', 'OpenSSL', 'Util.pm')) . "'";
17          -};
18     OpenSSL::Util->import();
19 }
20
21 my $there = canonpath(catdir(dirname($0), updir()));
22 my $std_engines = catdir($there, 'engines');
23 my $std_providers = catdir($there, 'providers');
24 my $std_openssl_conf = catdir($there, 'apps/openssl.cnf');
25 my $unix_shlib_wrap = catfile($there, 'util/shlib_wrap.sh');
26
27 $ENV{OPENSSL_ENGINES} = $std_engines
28     if ($ENV{OPENSSL_ENGINES} // '') eq '' && -d $std_engines;
29 $ENV{OPENSSL_MODULES} = $std_providers
30     if ($ENV{OPENSSL_MODULES} // '') eq '' && -d $std_providers;
31 $ENV{OPENSSL_CONF} = $std_openssl_conf
32     if ($ENV{OPENSSL_CONF} // '') eq '' && -f $std_openssl_conf;
33
34 my $use_system = 0;
35 my @cmd;
36
37 if ($^O eq 'VMS') {
38     # VMS needs the command to be appropriately quotified
39     @cmd = fixup_cmd(@ARGV);
40 } elsif (-x $unix_shlib_wrap) {
41     @cmd = ( $unix_shlib_wrap, @ARGV );
42 } else {
43     # Hope for the best
44     @cmd = ( @ARGV );
45 }
46
47 # The exec() statement on MSWin32 doesn't seem to give back the exit code
48 # from the call, so we resort to using system() instead.
49 my $waitcode = system @cmd;
50
51 # According to documentation, -1 means that system() couldn't run the command,
52 # otherwise, the value is similar to the Unix wait() status value
53 # (exitcode << 8 | signalcode)
54 die "wrap.pl: Failed to execute '", join(' ', @cmd), "': $!\n"
55     if $waitcode == -1;
56
57 # When the subprocess aborted on a signal, mimic what Unix shells do, by
58 # converting the signal code to an exit code by setting the high bit.
59 # This only happens on Unix flavored operating systems, the others don't
60 # have this sort of signaling to date, and simply leave the low byte zero.
61 exit(($? & 255) | 128) if ($? & 255) != 0;
62
63 # When not a signal, just shift down the subprocess exit code and use that.
64 my $exitcode = $? >> 8;
65
66 # For VMS, perl recommendations is to emulate what the C library exit() does
67 # for all non-zero exit codes, except we set the error severity rather than
68 # success.
69 # Ref: https://perldoc.perl.org/perlport#exit
70 #      https://perldoc.perl.org/perlvms#$?
71 if ($^O eq 'VMS' && $exitcode != 0) {
72     $exitcode =
73         0x35a000                # C facility code
74         + ($exitcode * 8)       # shift up to make space for the 3 severity bits
75         + 2                     # Severity: E(rror)
76         + 0x10000000;           # bit 28 set => the shell stays silent
77 }
78 exit($exitcode);