002.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --TEST--
  2. pcntl: 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. $pid = pcntl_fork();
  13. if ($pid == -1) {
  14. die('failed');
  15. } else if ($pid) {
  16. pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD,(string)SIGTERM));
  17. $oldset = array();
  18. pcntl_sigprocmask(SIG_BLOCK, array(), $oldset);
  19. var_dump(in_array(SIGCHLD, $oldset));
  20. var_dump(in_array(SIGTERM, $oldset));
  21. posix_kill(posix_getpid(), SIGTERM);
  22. $signo = pcntl_sigwaitinfo(array(SIGTERM), $siginfo);
  23. echo "signo == SIGTERM\n";
  24. var_dump($signo === SIGTERM && $signo === $siginfo['signo']);
  25. echo "code === SI_USER || SI_NOINFO\n";
  26. if (defined('SI_NOINFO')) {
  27. var_dump(($siginfo['code'] === SI_USER) || ($siginfo['code'] === SI_NOINFO));
  28. } else {
  29. var_dump($siginfo['code'] === SI_USER);
  30. }
  31. pcntl_signal(SIGCHLD, function($signo){});
  32. posix_kill($pid, SIGTERM);
  33. $signo = pcntl_sigwaitinfo(array((string)SIGCHLD), $siginfo);
  34. echo "signo == SIGCHLD\n";
  35. var_dump($signo === SIGCHLD && $signo === $siginfo['signo']);
  36. echo "code === CLD_KILLED\n";
  37. var_dump($siginfo['code'] === CLD_KILLED);
  38. echo "signo === SIGCHLD\n";
  39. var_dump($siginfo['signo'] === SIGCHLD);
  40. echo "signo === uid\n";
  41. var_dump($siginfo['uid'] === posix_getuid());
  42. echo "signo === pid\n";
  43. var_dump($siginfo['pid'] === $pid);
  44. pcntl_waitpid($pid, $status);
  45. set_error_handler(function($errno, $errstr) { echo "Error triggered\n"; }, E_WARNING);
  46. echo "sigprocmask with invalid arguments\n";
  47. /* Valgrind expectedly complains about this:
  48. * "sigprocmask: unknown 'how' field 2147483647"
  49. * Skip */
  50. if (getenv("USE_ZEND_ALLOC") !== '0') {
  51. var_dump(pcntl_sigprocmask(PHP_INT_MAX, array(SIGTERM)));
  52. } else {
  53. echo "Error triggered\n";
  54. echo "bool(false)\n";
  55. }
  56. var_dump(pcntl_sigprocmask(SIG_SETMASK, array(0)));
  57. echo "sigwaitinfo with invalid arguments\n";
  58. var_dump(pcntl_sigwaitinfo(array(0)));
  59. echo "sigtimedwait with invalid arguments\n";
  60. var_dump(pcntl_sigtimedwait(array(SIGTERM), $signo, PHP_INT_MAX, PHP_INT_MAX));
  61. } else {
  62. $siginfo = NULL;
  63. pcntl_sigtimedwait(array(SIGINT), $siginfo, 3600, 0);
  64. exit;
  65. }
  66. ?>
  67. --EXPECTF--
  68. bool(true)
  69. bool(true)
  70. signo == SIGTERM
  71. bool(true)
  72. code === SI_USER || SI_NOINFO
  73. bool(true)
  74. signo == SIGCHLD
  75. bool(true)
  76. code === CLD_KILLED
  77. bool(true)
  78. signo === SIGCHLD
  79. bool(true)
  80. signo === uid
  81. bool(true)
  82. signo === pid
  83. bool(true)
  84. sigprocmask with invalid arguments
  85. Error triggered
  86. bool(false)
  87. Error triggered
  88. bool(false)
  89. sigwaitinfo with invalid arguments
  90. Error triggered
  91. bool(false)
  92. sigtimedwait with invalid arguments
  93. Error triggered
  94. int(-1)