1<?php 2 3function get_fpm_path() /* {{{ */ 4{ 5 $php_path = getenv("TEST_PHP_EXECUTABLE"); 6 for ($i = 0; $i < 2; $i++) { 7 $slash_pos = strrpos($php_path, "/"); 8 if ($slash_pos) { 9 $php_path = substr($php_path, 0, $slash_pos); 10 } else { 11 return false; 12 } 13 } 14 15 16 if ($php_path && is_dir($php_path)) { 17 if (file_exists($php_path."/fpm/php-fpm") && is_executable($php_path."/fpm/php-fpm")) { 18 /* gotcha */ 19 return $php_path."/fpm/php-fpm"; 20 } 21 $php_sbin_fpm = $php_path."/sbin/php-fpm"; 22 if (file_exists($php_sbin_fpm) && is_executable($php_sbin_fpm)) { 23 return $php_sbin_fpm; 24 } 25 } 26 return false; 27} 28/* }}} */ 29 30function run_fpm($config, &$out = false, $extra_args = '') /* {{{ */ 31{ 32 $cfg = dirname(__FILE__).'/test-fpm-config.tmp'; 33 file_put_contents($cfg, $config); 34 $desc = []; 35 if ($out !== false) { 36 $desc = [1 => array('pipe', 'w')]; 37 } 38 /* Since it's not possible to spawn a process under linux without using a 39 * shell in php (why?!?) we need a little shell trickery, so that we can 40 * actually kill php-fpm */ 41 $fpm = proc_open('killit () { kill $child; }; trap killit TERM; '.get_fpm_path().' -F -O -y '.$cfg.' '.$extra_args.' 2>&1 & child=$!; wait', $desc, $pipes); 42 register_shutdown_function( 43 function($fpm) use($cfg) { 44 @unlink($cfg); 45 if (is_resource($fpm)) { 46 @proc_terminate($fpm); 47 while (proc_get_status($fpm)['running']) { 48 usleep(10000); 49 } 50 } 51 }, 52 $fpm 53 ); 54 if ($out !== false) { 55 $out = $pipes[1]; 56 } 57 return $fpm; 58} 59/* }}} */ 60 61function test_fpm_conf($config, &$msg = NULL) { /* {{{ */ 62 $cfg = dirname(__FILE__).'/test-fpm-config.tmp'; 63 file_put_contents($cfg, $config); 64 exec(get_fpm_path() . ' -t -y ' . $cfg . ' 2>&1', $output, $code); 65 if ($code) { 66 $msg = preg_replace("/\[.+?\]/", "", $output[0]); 67 return false; 68 } 69 return true; 70} 71/* }}} */ 72 73function run_fpm_till($needle, $config, $max = 10) /* {{{ */ 74{ 75 $i = 0; 76 $fpm = run_fpm($config, $tail); 77 if (is_resource($fpm)) { 78 while($i < $max) { 79 $i++; 80 $line = fgets($tail); 81 if(preg_match($needle, $line) === 1) { 82 break; 83 } 84 } 85 if ($i >= $max) { 86 $line = false; 87 } 88 proc_terminate($fpm); 89 stream_get_contents($tail); 90 fclose($tail); 91 proc_close($fpm); 92 } 93 return $line; 94} 95/* }}} */ 96 97function fpm_display_log($tail, $n=1, $ignore='systemd') { /* {{{ */ 98 /* Read $n lines or until EOF */ 99 while ($n>0 || ($n<0 && !feof($tail))) { 100 $a = fgets($tail); 101 if (empty($ignore) || !strpos($a, $ignore)) { 102 echo $a; 103 $n--; 104 } 105 } 106} /* }}} */ 107 108function run_request($host, $port, $uri='/ping', $query='', $headers=array()) { /* {{{ */ 109 require_once 'fcgi.inc'; 110 $client = new Adoy\FastCGI\Client($host, $port); 111 $params = array_merge(array( 112 'GATEWAY_INTERFACE' => 'FastCGI/1.0', 113 'REQUEST_METHOD' => 'GET', 114 'SCRIPT_FILENAME' => $uri, 115 'SCRIPT_NAME' => $uri, 116 'QUERY_STRING' => $query, 117 'REQUEST_URI' => $uri . ($query ? '?'.$query : ""), 118 'DOCUMENT_URI' => $uri, 119 'SERVER_SOFTWARE' => 'php/fcgiclient', 120 'REMOTE_ADDR' => '127.0.0.1', 121 'REMOTE_PORT' => '9985', 122 'SERVER_ADDR' => '127.0.0.1', 123 'SERVER_PORT' => '80', 124 'SERVER_NAME' => php_uname('n'), 125 'SERVER_PROTOCOL' => 'HTTP/1.1', 126 'CONTENT_TYPE' => '', 127 'CONTENT_LENGTH' => 0 128 ), $headers); 129 return $client->request($params, false)."\n"; 130} 131/* }}} */ 132