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