xref: /openssl/util/wrap.pl.in (revision b28b3128)
1#! {- $config{HASHBANGPERL} -}
2
3use strict;
4use warnings;
5
6use File::Basename;
7use File::Spec::Functions;
8
9BEGIN {
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
21my $there = canonpath(catdir(dirname($0), updir()));
22my $std_engines = catdir($there, 'engines');
23my $std_providers = catdir($there, 'providers');
24my $std_openssl_conf = catdir($there, 'apps/openssl.cnf');
25my $unix_shlib_wrap = catfile($there, 'util/shlib_wrap.sh');
26my $std_openssl_conf_include;
27
28if ($ARGV[0] eq '-fips') {
29    $std_openssl_conf = {-
30         use Cwd qw(abs_path);
31
32         "'" . abs_path(catfile($config{sourcedir}, 'test/fips-and-base.cnf')) . "'";
33         -};
34    shift;
35
36    $std_openssl_conf_include = catdir($there, 'providers');
37}
38
39if ($ARGV[0] eq '-jitter') {
40    $std_openssl_conf = {-
41         use Cwd qw(abs_path);
42
43         "'" . abs_path(catfile($config{sourcedir}, 'test/default-and-jitter.cnf')) . "'";
44         -};
45    shift;
46
47    $std_openssl_conf_include = catdir($there, 'providers');
48}
49
50
51local $ENV{OPENSSL_CONF_INCLUDE} = $std_openssl_conf_include
52    if defined $std_openssl_conf_include
53       &&($ENV{OPENSSL_CONF_INCLUDE} // '') eq ''
54       && -d $std_openssl_conf_include;
55local $ENV{OPENSSL_ENGINES} = $std_engines
56    if ($ENV{OPENSSL_ENGINES} // '') eq '' && -d $std_engines;
57local $ENV{OPENSSL_MODULES} = $std_providers
58    if ($ENV{OPENSSL_MODULES} // '') eq '' && -d $std_providers;
59local $ENV{OPENSSL_CONF} = $std_openssl_conf
60    if ($ENV{OPENSSL_CONF} // '') eq '' && -f $std_openssl_conf;
61{-
62     # For VMS, we define logical names to get the libraries properly
63     # defined.
64     use File::Spec::Functions qw(rel2abs);
65
66     if ($^O eq "VMS") {
67         my $bldtop = rel2abs($config{builddir});
68         my %names =
69             map { platform->sharedname($_) => $bldtop.platform->sharedlib($_) }
70             grep { !$unified_info{attributes}->{libraries}->{$_}->{noinst} }
71             @{$unified_info{libraries}};
72
73         foreach (sort keys %names) {
74             $OUT .= "local \$ENV\{'$_'\} = '$names{$_}';\n";
75         }
76     }
77-}
78my $use_system = 0;
79my @cmd;
80
81if ($^O eq 'VMS') {
82    # VMS needs the command to be appropriately quotified
83    @cmd = fixup_cmd(@ARGV);
84} elsif (-x $unix_shlib_wrap) {
85    @cmd = ( $unix_shlib_wrap, @ARGV );
86} else {
87    # Hope for the best
88    @cmd = ( @ARGV );
89}
90
91# The exec() statement on MSWin32 doesn't seem to give back the exit code
92# from the call, so we resort to using system() instead.
93my $waitcode = system @cmd;
94
95# According to documentation, -1 means that system() couldn't run the command,
96# otherwise, the value is similar to the Unix wait() status value
97# (exitcode << 8 | signalcode)
98die "wrap.pl: Failed to execute '", join(' ', @cmd), "': $!\n"
99    if $waitcode == -1;
100
101# When the subprocess aborted on a signal, we simply raise the same signal.
102kill(($? & 255) => $$) if ($? & 255) != 0;
103
104# If that didn't stop this script, mimic what Unix shells do, by
105# converting the signal code to an exit code by setting the high bit.
106# This only happens on Unix flavored operating systems, the others don't
107# have this sort of signaling to date, and simply leave the low byte zero.
108exit(($? & 255) | 128) if ($? & 255) != 0;
109
110# When not a signal, just shift down the subprocess exit code and use that.
111my $exitcode = $? >> 8;
112
113# For VMS, perl recommendations is to emulate what the C library exit() does
114# for all non-zero exit codes, except we set the error severity rather than
115# success.
116# Ref: https://perldoc.perl.org/perlport#exit
117#      https://perldoc.perl.org/perlvms#$?
118if ($^O eq 'VMS' && $exitcode != 0) {
119    $exitcode =
120        0x35a000                # C facility code
121        + ($exitcode * 8)       # shift up to make space for the 3 severity bits
122        + 2                     # Severity: E(rror)
123        + 0x10000000;           # bit 28 set => the shell stays silent
124}
125exit($exitcode);
126