Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions ext/standard/proc_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,31 @@
#define USE_POSIX_SPAWN

/* The non-_np variant is in macOS 26 (and _np deprecated) */
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR
#if defined(__APPLE__) && defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR) && defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP)
static inline int php_posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t *actions, const char *path)
{
/* The standardized symbol is weak-linked when building with a newer SDK. */
# if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunguarded-availability-new"
# endif
if (posix_spawn_file_actions_addchdir != NULL) {
return posix_spawn_file_actions_addchdir(actions, path);
}
# if defined(__clang__)
# pragma clang diagnostic pop
# endif

return posix_spawn_file_actions_addchdir_np(actions, path);
}
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR php_posix_spawn_file_actions_addchdir
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir"
#elif defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR)
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR posix_spawn_file_actions_addchdir
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir"
#else
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR posix_spawn_file_actions_addchdir_np
#define POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "posix_spawn_file_actions_addchdir_np"
#endif
#endif

Expand Down Expand Up @@ -1401,7 +1422,7 @@ PHP_FUNCTION(proc_open)
if (cwd) {
r = POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR(&factions, cwd);
if (r != 0) {
php_error_docref(NULL, E_WARNING, ZEND_TOSTR(POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR) "() failed: %s", strerror(r));
php_error_docref(NULL, E_WARNING, POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NAME "() failed: %s", strerror(r));
}
}

Expand Down
38 changes: 38 additions & 0 deletions ext/standard/tests/general_functions/proc_open_cwd_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
proc_open() with cwd changes child working directory
--SKIPIF--
<?php
if (!function_exists("proc_open")) echo "skip proc_open() is not available";
?>
--FILE--
<?php
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
$cwd = __DIR__ . DIRECTORY_SEPARATOR . 'proc_open_cwd_basic';
@mkdir($cwd);

$descriptors = [
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$proc = proc_open([$php, '-n', '-r', 'echo basename(getcwd()), "\n";'], $descriptors, $pipes, $cwd);

var_dump(is_resource($proc));
echo stream_get_contents($pipes[1]);
echo stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
var_dump(proc_close($proc));

rmdir($cwd);
?>
--CLEAN--
<?php
$cwd = __DIR__ . DIRECTORY_SEPARATOR . 'proc_open_cwd_basic';
if (is_dir($cwd)) {
rmdir($cwd);
}
?>
--EXPECT--
bool(true)
proc_open_cwd_basic
int(0)
Loading