| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strictscip-perl cpan POSIX-2008 0.28 strict/; |
| 4 | use warningsscip-perl cpan POSIX-2008 0.28 warnings/; |
| 5 | use sigtrapscip-perl cpan POSIX-2008 0.28 sigtrap/ qw(die normal-signals error-signals); |
| 6 | |
| 7 | use Configscip-perl cpan POSIX-2008 0.28 Config/; |
| 8 | use Errnoscip-perl cpan POSIX-2008 0.28 Errno/; |
| 9 | use Fcntlscip-perl cpan POSIX-2008 0.28 Fcntl/ qw(:DEFAULT); |
| 10 | use File::Tempscip-perl cpan POSIX-2008 0.28 File/Temp/ 'mktemp'; |
| 11 | use Test::Morescip-perl cpan POSIX-2008 0.28 Test/More/ tests => 8; |
| 12 | |
| 13 | use POSIX::2008scip-perl cpan POSIX-2008 0.28 POSIX/` 2008`/; |
| 14 | |
| 15 | my $perlpath = $Configscip-perl cpan POSIX-2008 0.28 main/`%Config`.{perlpath}; |
| 16 | if ($^Oscip-perl cpan POSIX-2008 0.28 main/`$^O`. ne 'VMS' && $perlpath !~ m/$Config{_exe}$/i) { |
| 17 | $perlpath .= $Configscip-perl cpan POSIX-2008 0.28 main/`%Config`.{_exe}; |
| 18 | } |
| 19 | |
| 20 | my $rv; |
| 21 | my $tmpf = mktempscip-perl cpan POSIX-2008 0.28 main/mktemp().('X'x20); |
| 22 | my $envp = {_ec_ => 73}; |
| 23 | my $argv = ['perl', '-e', 'exit($ENV{_ec_})']; |
| 24 | |
| 25 | SKIP: { |
| 26 | if (! defined &POSIX::2008::execveat) { |
| 27 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'execveat() UNAVAILABLE', 4; |
| 28 | } |
| 29 | |
| 30 | $rv = eval { |
| 31 | POSIX::2008::execveat(POSIX::2008::AT_FDCWD, $tmpf, 'foo', {}); |
| 32 | }; |
| 33 | # Note: The /i is needed because some Perls say "ARRAY", others say "array". |
| 34 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not an ARRAY/i, 'execveat fails due to non-arrayref'); |
| 35 | |
| 36 | $rv = eval { |
| 37 | POSIX::2008::execveat(POSIX::2008::AT_FDCWD, $tmpf, [$tmpf], 'foo'); |
| 38 | }; |
| 39 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not a HASH/, 'execveat fails due to non-hashref'); |
| 40 | |
| 41 | $rv = POSIX::2008::execveat(POSIX::2008::AT_FDCWD, $tmpf, [$tmpf], {}); |
| 42 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $!scip-perl cpan POSIX-2008 0.28 main/`%!`.{ENOENT}, "execveat fails with ENOENT for $tmpf"); |
| 43 | |
| 44 | my $pid = forkscip-perl cpan POSIX-2008 0.28 main/fork().(); |
| 45 | if ($pid) { |
| 46 | waitscip-perl cpan POSIX-2008 0.28 main/wait().(); |
| 47 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($?scip-perl cpan POSIX-2008 0.28 main/`$?`.>>8, '==', 73, 'execveat() returned 73'); |
| 48 | } |
| 49 | elsif (definedscip-perl cpan POSIX-2008 0.28 main/defined(). $pid) { |
| 50 | POSIX::2008::execveat(POSIX::2008::AT_FDCWD, $perlpath, $argv, $envp); |
| 51 | diagscip-perl cpan POSIX-2008 0.28 main/diag().("execveat() failed for $perlpath: $!"); |
| 52 | exitscip-perl cpan POSIX-2008 0.28 main/exit().(1); |
| 53 | } |
| 54 | else { |
| 55 | diescip-perl cpan POSIX-2008 0.28 main/die(). "Could not fork: $!"; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | SKIP: { |
| 60 | my $omode = |
| 61 | definedscip-perl cpan POSIX-2008 0.28 main/defined(). &POSIX::2008::O_EXEC ? &POSIX::2008::O_EXEC : |
| 62 | defined &POSIX::2008::O_PATH ? &POSIX::2008::O_PATH : |
| 63 | O_RDONLY; |
| 64 | |
| 65 | if (! defined &POSIX::2008::fexecve) { |
| 66 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'fexecve() UNAVAILABLE', 4; |
| 67 | } |
| 68 | if (! sysopenscip-perl cpan POSIX-2008 0.28 main/sysopen(). my $fh, $perlpath, $omode) { |
| 69 | diagscip-perl cpan POSIX-2008 0.28 main/diag().("sysopen($perlpath, $omode) failed: $! (skipping fexecve() test)"); |
| 70 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'fexecve() not testable', 4; |
| 71 | } |
| 72 | |
| 73 | $rv = eval { |
| 74 | POSIX::2008::fexecvescip-perl cpan POSIX-2008 0.28 main/fexecve().(1337, 'foo', {}); |
| 75 | }; |
| 76 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not an ARRAY/i, 'fexecve fails due to non-arrayref'); |
| 77 | |
| 78 | $rv = eval { |
| 79 | POSIX::2008::fexecvescip-perl cpan POSIX-2008 0.28 main/fexecve().(1337, [], 'foo'); |
| 80 | }; |
| 81 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not a HASH/, 'fexecve fails due to non-hashref'); |
| 82 | |
| 83 | $rv = POSIX::2008::fexecvescip-perl cpan POSIX-2008 0.28 main/fexecve().(1337, [], {}); |
| 84 | # ENOENT occurs when fexecve() uses execveat() with a /proc filesystem path. |
| 85 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && ($!scip-perl cpan POSIX-2008 0.28 main/`%!`.{ENOENT} || $!scip-perl cpan POSIX-2008 0.28 main/`%!`.{EBADF} || $!scip-perl cpan POSIX-2008 0.28 main/`%!`.{EINVAL}), |
| 86 | 'fexecve fails with invalid fd'); |
| 87 | diagscip-perl cpan POSIX-2008 0.28 main/diag().('(/proc not mounted?)') if $!scip-perl cpan POSIX-2008 0.28 main/`%!`.{ENOSYS}; |
| 88 | |
| 89 | my $pid = forkscip-perl cpan POSIX-2008 0.28 main/fork().(); |
| 90 | if ($pid) { |
| 91 | waitscip-perl cpan POSIX-2008 0.28 main/wait().(); |
| 92 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($?scip-perl cpan POSIX-2008 0.28 main/`$?`.>>8, '==', 73, 'fexecve() returned 73'); |
| 93 | } |
| 94 | elsif (definedscip-perl cpan POSIX-2008 0.28 main/defined(). $pid) { |
| 95 | my $omode = |
| 96 | definedscip-perl cpan POSIX-2008 0.28 main/defined(). &POSIX::2008::O_EXEC ? &POSIX::2008::O_EXEC : |
| 97 | defined &POSIX::2008::O_PATH ? &POSIX::2008::O_PATH : |
| 98 | O_RDONLY; |
| 99 | if (! sysopenscip-perl cpan POSIX-2008 0.28 main/sysopen(). my $fh, $perlpath, $omode) { |
| 100 | diagscip-perl cpan POSIX-2008 0.28 main/diag().("sysopen($perlpath, $omode) failed: $!"); |
| 101 | } |
| 102 | else { |
| 103 | POSIX::2008::fexecvescip-perl cpan POSIX-2008 0.28 main/fexecve().($fh, $argv, $envp); |
| 104 | diagscip-perl cpan POSIX-2008 0.28 main/diag().("fexecve() failed: $!".($!scip-perl cpan POSIX-2008 0.28 main/`%!`.{ENOSYS}?' (/proc not mounted?)':'')); |
| 105 | } |
| 106 | exitscip-perl cpan POSIX-2008 0.28 main/exit().(1); |
| 107 | } |
| 108 | else { |
| 109 | diescip-perl cpan POSIX-2008 0.28 main/die(). "Could not fork: $!"; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | END { |
| 114 | unlinkscip-perl cpan POSIX-2008 0.28 main/unlink(). $tmpf if definedscip-perl cpan POSIX-2008 0.28 main/defined(). $tmpf; |
| 115 | } |