002.phpt 3.1 KB

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