1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the sj-i/php-profiler package. |
5
|
|
|
* |
6
|
|
|
* (c) sji <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace PhpProfiler\Lib\Libc\Unistd; |
15
|
|
|
|
16
|
|
|
use FFI\CInteger; |
17
|
|
|
|
18
|
|
|
class Execvp |
19
|
|
|
{ |
20
|
|
|
/** @var \FFI\Libc\execvp_ffi */ |
21
|
|
|
private \FFI $ffi; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
/** @var \FFI\Libc\execvp_ffi */ |
26
|
|
|
$this->ffi = \FFI::cdef(' |
|
|
|
|
27
|
|
|
int execvp(const char *file, char *const argv[]); |
28
|
|
|
', 'libc.so.6'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @param list<string> $argv */ |
|
|
|
|
32
|
|
|
public function execvp(string $file, array $argv): int |
33
|
|
|
{ |
34
|
|
|
/** @var CInteger $zero */ |
35
|
|
|
$zero = \FFI::new('long', false, true); |
36
|
|
|
$zero->cdata = 0; |
37
|
|
|
$null = \FFI::cast('void *', $zero); |
38
|
|
|
|
39
|
|
|
$args = [$file, ...$argv]; |
40
|
|
|
$size = \count($args) + 1; |
41
|
|
|
/** @var \FFI\CArray $argv_real */ |
42
|
|
|
$argv_real = \FFI::new('char *[' . $size . ']', false, true); |
43
|
|
|
foreach ($args as $key => $item) { |
44
|
|
|
$item_len = strlen($item); |
|
|
|
|
45
|
|
|
$item_len_nul = $item_len + 1; |
46
|
|
|
/** @var \FFI\CArray $argv_item */ |
47
|
|
|
$argv_item = \FFI::new("char[{$item_len_nul}]", false, true); |
48
|
|
|
\FFI::memcpy($argv_item, $item, $item_len); |
49
|
|
|
$argv_item[$item_len] = "\0"; |
50
|
|
|
$argv_real[$key] = $argv_item; |
51
|
|
|
} |
52
|
|
|
$argv_real[$key + 1] = $null; |
|
|
|
|
53
|
|
|
return $this->ffi->execvp($file, $argv_real); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|