wrap.pl.in 4.7 KB

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