xref: /php-src/ext/pcntl/tests/002.phpt (revision 1bdb0fdd)
1--TEST--
2pcntl: pcntl_sigprocmask(), pcntl_sigwaitinfo(), pcntl_sigtimedwait()
3--EXTENSIONS--
4pcntl
5posix
6--SKIPIF--
7<?php
8if (
9    !function_exists('pcntl_sigprocmask')
10    or !function_exists('pcntl_sigwaitinfo')
11    or !function_exists('pcntl_sigtimedwait')
12) { die('skip required functionality is not available'); }
13elseif (!defined('CLD_EXITED')) die('skip CLD_EXITED not defined');
14elseif (getenv("SKIP_REPEAT")) die("skip cannot be repeated");
15elseif (str_contains(PHP_OS, 'FreeBSD')) die('skip Results in parallel test runner hang on FreeBSD');
16?>
17--FILE--
18<?php
19
20$pid = pcntl_fork();
21if ($pid == -1) {
22    die('failed');
23} else if ($pid) {
24    pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD,(string)SIGTERM));
25    $oldset = array();
26    pcntl_sigprocmask(SIG_UNBLOCK, array(SIGINT), $oldset);
27    var_dump(in_array(SIGCHLD, $oldset));
28    var_dump(in_array(SIGTERM, $oldset));
29
30    posix_kill(posix_getpid(), SIGTERM);
31    $signo = pcntl_sigwaitinfo(array(SIGTERM), $siginfo);
32    echo "signo == SIGTERM\n";
33    var_dump($signo === SIGTERM && $signo === $siginfo['signo']);
34    echo "code === SI_USER || SI_NOINFO\n";
35    if (defined('SI_NOINFO')) {
36        var_dump(($siginfo['code'] === SI_USER) || ($siginfo['code'] === SI_NOINFO));
37    } else {
38        var_dump($siginfo['code'] === SI_USER);
39    }
40
41    pcntl_signal(SIGCHLD, function($signo){});
42    posix_kill($pid, SIGTERM);
43    $signo = pcntl_sigwaitinfo(array((string)SIGCHLD), $siginfo);
44    echo "signo == SIGCHLD\n";
45    var_dump($signo === SIGCHLD && $signo === $siginfo['signo']);
46    echo "code === CLD_KILLED\n";
47    var_dump($siginfo['code'] === CLD_KILLED);
48    echo "signo === SIGCHLD\n";
49    var_dump($siginfo['signo'] === SIGCHLD);
50    echo "signo === uid\n";
51    var_dump($siginfo['uid'] === posix_getuid());
52    echo "signo === pid\n";
53    var_dump($siginfo['pid'] === $pid);
54    pcntl_waitpid($pid, $status);
55} else {
56    $siginfo = NULL;
57    pcntl_sigtimedwait(array(SIGINT), $siginfo, 3600, 0);
58    exit;
59}
60
61?>
62--EXPECT--
63bool(true)
64bool(true)
65signo == SIGTERM
66bool(true)
67code === SI_USER || SI_NOINFO
68bool(true)
69signo == SIGCHLD
70bool(true)
71code === CLD_KILLED
72bool(true)
73signo === SIGCHLD
74bool(true)
75signo === uid
76bool(true)
77signo === pid
78bool(true)
79