Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process::daemon() supports redirection of input and output #2871

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/process/daemon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$fp = fopen(__DIR__.'/output.txt', 'a');
Swoole\Process::daemon(1, 1, [null, $fp, $fp]);

sleep(1);

fwrite(STDOUT, "ERROR 1\n");
fwrite(STDOUT, "ERROR 2\n");
fwrite(STDOUT, "ERROR 3\n");

fwrite(STDERR, "ERROR 4\n");
fwrite(STDERR, "ERROR 5\n");
fwrite(STDERR, "ERROR 6\n");
35 changes: 33 additions & 2 deletions swoole_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_process_daemon, 0, 0, 0)
ZEND_ARG_INFO(0, nochdir)
ZEND_ARG_INFO(0, noclose)
ZEND_ARG_INFO(0, pipes)
ZEND_END_ARG_INFO()

#ifdef HAVE_CPU_AFFINITY
Expand Down Expand Up @@ -1016,11 +1017,41 @@ static PHP_METHOD(swoole_process, daemon)
{
zend_bool nochdir = 1;
zend_bool noclose = 1;
zval *zpipes = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|bb", &nochdir, &noclose) == FAILURE)
ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(nochdir)
Z_PARAM_BOOL(noclose)
Z_PARAM_ARRAY(zpipes)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

zval *elem;
int fd = 0;

if (zpipes)
{
RETURN_FALSE;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zpipes), elem)
{
if (!ZVAL_IS_NULL(elem))
{
int new_fd = swoole_convert_to_fd(elem);
if (new_fd >= 0)
{
if (dup2(new_fd, fd) < 0)
{
swSysWarn("dup2(%d, %d) failed", new_fd, fd);
}
}
}
if (fd++ == 2)
{
break;
}
}
ZEND_HASH_FOREACH_END();
}

RETURN_BOOL(daemon(nochdir, noclose) == 0);
}

Expand Down
55 changes: 55 additions & 0 deletions tests/swoole_process/daemon.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
swoole_process: daemon
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

const FILE = __DIR__ . '/output.txt';
use Swoole\Process;

$process = new Process('python_process', true);
$pid = $process->start();

function python_process(swoole_process $worker)
{
$fp = fopen(FILE, 'a');
Process::daemon(1, 1, [null, $fp, $fp]);

fwrite(STDOUT, "ERROR 1\n");
fwrite(STDOUT, "ERROR 2\n");
fwrite(STDOUT, "ERROR 3\n");

fwrite(STDERR, "ERROR 4\n");
fwrite(STDERR, "ERROR 5\n");
fwrite(STDERR, "END\n");

}

Process::wait();

$fp = fopen(FILE, 'r');
for ($i = 0; $i < 100; $i++) {
$line = fgets($fp);
if (empty($line)) {
usleep(100000);
} else {
echo $line;
if ($line == "END\n") {
break;
}
}
}
unlink(FILE);

?>
--EXPECT--
ERROR 1
ERROR 2
ERROR 3
ERROR 4
ERROR 5
END