xref: /PHP-7.1/ext/pcntl/tests/002.phpt (revision 638af1ec)
1--TEST--
2pcntl: pcntl_sigprocmask(), pcntl_sigwaitinfo(), pcntl_sigtimedwait()
3--SKIPIF--
4<?php
5	if (!extension_loaded('pcntl')) die('skip pcntl extension not available');
6	elseif (!extension_loaded('posix')) die('skip posix extension not available');
7	elseif (!function_exists('pcntl_sigwaitinfo') or !function_exists('pcntl_sigtimedwait')) die('skip required functionality is not available');
8	elseif (!defined('CLD_EXITED')) die('skip CLD_EXITED not defined');
9?>
10--FILE--
11<?php
12
13$pid = pcntl_fork();
14if ($pid == -1) {
15	die('failed');
16} else if ($pid) {
17	pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD,(string)SIGTERM));
18	$oldset = array();
19	pcntl_sigprocmask(SIG_BLOCK, array(), $oldset);
20	var_dump(in_array(SIGCHLD, $oldset));
21	var_dump(in_array(SIGTERM, $oldset));
22
23	posix_kill(posix_getpid(), SIGTERM);
24	$signo = pcntl_sigwaitinfo(array(SIGTERM), $siginfo);
25	echo "signo == SIGTERM\n";
26	var_dump($signo === SIGTERM && $signo === $siginfo['signo']);
27	echo "code === SI_USER || SI_NOINFO\n";
28	if (defined('SI_NOINFO')) {
29		var_dump(($siginfo['code'] === SI_USER) || ($siginfo['code'] === SI_NOINFO));
30	} else {
31		var_dump($siginfo['code'] === SI_USER);
32	}
33
34	pcntl_signal(SIGCHLD, function($signo){});
35	posix_kill($pid, SIGTERM);
36	$signo = pcntl_sigwaitinfo(array((string)SIGCHLD), $siginfo);
37	echo "signo == SIGCHLD\n";
38	var_dump($signo === SIGCHLD && $signo === $siginfo['signo']);
39	echo "code === CLD_KILLED\n";
40	var_dump($siginfo['code'] === CLD_KILLED);
41	echo "signo === SIGCHLD\n";
42	var_dump($siginfo['signo'] === SIGCHLD);
43	echo "signo === uid\n";
44	var_dump($siginfo['uid'] === posix_getuid());
45	echo "signo === pid\n";
46	var_dump($siginfo['pid'] === $pid);
47	pcntl_waitpid($pid, $status);
48
49	set_error_handler(function($errno, $errstr) { echo "Error triggered\n"; }, E_WARNING);
50
51	echo "sigprocmask with invalid arguments\n";
52
53	/* Valgrind expectedly complains about this:
54         * "sigprocmask: unknown 'how' field 2147483647"
55	 * Skip */
56	if (getenv("USE_ZEND_ALLOC") !== '0') {
57		var_dump(pcntl_sigprocmask(PHP_INT_MAX, array(SIGTERM)));
58	} else {
59		echo "Error triggered\n";
60		echo "bool(false)\n";
61	}
62	var_dump(pcntl_sigprocmask(SIG_SETMASK, array(0)));
63
64	echo "sigwaitinfo with invalid arguments\n";
65	var_dump(pcntl_sigwaitinfo(array(0)));
66
67	echo "sigtimedwait with invalid arguments\n";
68	var_dump(pcntl_sigtimedwait(array(SIGTERM), $signo, PHP_INT_MAX, PHP_INT_MAX));
69} else {
70	$siginfo = NULL;
71	pcntl_sigtimedwait(array(SIGINT), $siginfo, 3600, 0);
72	exit;
73}
74
75?>
76--EXPECTF--
77bool(true)
78bool(true)
79signo == SIGTERM
80bool(true)
81code === SI_USER || SI_NOINFO
82bool(true)
83signo == SIGCHLD
84bool(true)
85code === CLD_KILLED
86bool(true)
87signo === SIGCHLD
88bool(true)
89signo === uid
90bool(true)
91signo === pid
92bool(true)
93sigprocmask with invalid arguments
94Error triggered
95bool(false)
96Error triggered
97bool(false)
98sigwaitinfo with invalid arguments
99Error triggered
100bool(false)
101sigtimedwait with invalid arguments
102Error triggered
103int(-1)
104