summaryrefslogtreecommitdiff
path: root/sapi/cli
diff options
context:
space:
mode:
authorAnatol Belski <ab@php.net>2019-02-08 18:10:31 -0800
committerAnatol Belski <ab@php.net>2019-02-08 18:10:31 -0800
commit12bfd9a5f58c12b8f63011c130ec3bf6605ea33b (patch)
treedc907993e27cad70d20e016fbe78a390ad53735a /sapi/cli
parente1dd8cd678e72475fe28f450657fb81665beaccc (diff)
downloadphp-git-12bfd9a5f58c12b8f63011c130ec3bf6605ea33b.tar.gz
Implement FR #77377 handle CTRL+C in Windows
Diffstat (limited to 'sapi/cli')
-rw-r--r--sapi/cli/tests/sapi_windows_set_ctrl_handler.phpt75
1 files changed, 75 insertions, 0 deletions
diff --git a/sapi/cli/tests/sapi_windows_set_ctrl_handler.phpt b/sapi/cli/tests/sapi_windows_set_ctrl_handler.phpt
new file mode 100644
index 0000000000..a5cb3ef03e
--- /dev/null
+++ b/sapi/cli/tests/sapi_windows_set_ctrl_handler.phpt
@@ -0,0 +1,75 @@
+--TEST--
+sapi_windows_set_ctrl_handler()
+--SKIPIF--
+<?php
+
+include "skipinf.inc";
+
+if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
+ die("skip this test is for Windows platforms only");
+?>
+--FILE--
+<?php
+
+$is_child = isset($argv[1]);
+
+
+if ($is_child) {
+ function handler($evt)
+ {
+ echo "\nCaught " . get_evt_name($evt), ", wait, wait ...!\n";
+ exit;
+ }
+
+ sapi_windows_set_ctrl_handler('handler');
+
+ while(1) usleep(100);
+} else {
+ $cmd = PHP_BINARY . " -n " . $argv[0] . " 1";
+ $spec = [0 => ["pipe", "r"], 1 => ["pipe", "w"]];
+
+ $proc = proc_open($cmd, $spec, $pipes, NULL, NULL, ["bypass_shell" => true, "create_process_group" => true]);
+
+ if (!is_resource($proc)) {
+ die("Failed to start child. ");
+ }
+
+ $child_pid = proc_get_status($proc)["pid"];
+ echo "Started child $child_pid\n";
+ usleep(300);
+
+ $cmd = "tasklist /FI \"PID eq $child_pid\" /NH";
+ echo "Running `$cmd` to check the process indeed exists:\n";
+ echo trim(shell_exec($cmd)) . "\n";
+
+ $evt = PHP_WINDOWS_EVENT_CTRL_C;
+ echo "Sending ", get_evt_name($evt), " to child $child_pid\n";
+ $ret = sapi_windows_generate_ctrl_event($evt, $child_pid);
+
+ echo "Child said: \"", trim(fread($pipes[1], 48)), "\"\n";
+
+ echo ($ret ? "Successfully" : "Unsuccessfuly"), " sent ", get_evt_name($evt), " to child $child_pid\n";
+
+ proc_close($proc);
+}
+
+function get_evt_name(int $evt) : ?string
+{
+ if (PHP_WINDOWS_EVENT_CTRL_C == $evt) {
+ return "CTRL+C";
+ } if (PHP_WINDOWS_EVENT_CTRL_BREAK == $evt) {
+ return "CTRL+BREAK";
+ }
+
+ return NULL;
+}
+
+?>
+--EXPECTF--
+Started child %d
+Running `tasklist /FI "PID eq %d" /NH` to check the process indeed exists:
+php.exe%w%d%s1%s
+Sending CTRL+C to child %d
+Child said: "Caught CTRL+C, wait, wait ...!"
+Successfully sent CTRL+C to child %d
+