Checker.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Man\Core\Lib;
  3. /**
  4. * 环境检查相关
  5. *
  6. * @author walkor <worker-man@qq.com>
  7. */
  8. class Checker
  9. {
  10. /**
  11. * 检查启动worker进程的的用户是否合法
  12. * @return void
  13. */
  14. public static function checkWorkerUserName($worker_user)
  15. {
  16. if($worker_user)
  17. {
  18. $user_info = posix_getpwnam($worker_user);
  19. return !empty($user_info);
  20. }
  21. }
  22. /**
  23. * 检查扩展支持情况
  24. * @return void
  25. */
  26. public static function checkExtension()
  27. {
  28. // 扩展名=>是否是必须
  29. $need_map = array(
  30. 'posix' => true,
  31. 'pcntl' => true,
  32. 'sysvshm' => false,
  33. 'sysvmsg' => false,
  34. 'libevent' => false,
  35. 'proctitle' => false,
  36. );
  37. // 检查每个扩展支持情况
  38. echo "----------------------EXTENSION--------------------\n";
  39. $pad_length = 26;
  40. foreach($need_map as $ext_name=>$must_required)
  41. {
  42. $suport = extension_loaded($ext_name);
  43. if($must_required && !$suport)
  44. {
  45. \Man\Core\Master::notice($ext_name. " [NOT SUPORT BUT REQUIRED] \tYou have to compile CLI version of PHP with --enable-{$ext_name} \tServer start fail");
  46. exit($ext_name. " \033[31;40m [NOT SUPORT BUT REQUIRED] \033[0m\n\n\033[31;40mYou have to compile CLI version of PHP with --enable-{$ext_name} \033[0m\n\n\033[31;40mServer start fail\033[0m\n\n");
  47. }
  48. // 支持扩展
  49. if($suport)
  50. {
  51. echo str_pad($ext_name, $pad_length), "\033[32;40m [OK] \033[0m\n";
  52. }
  53. // 不支持
  54. else
  55. {
  56. // ev uv inotify不是必须
  57. if('proctitle' == $ext_name)
  58. {
  59. continue;
  60. }
  61. echo str_pad($ext_name, $pad_length), "\033[33;40m [NOT SUPORT] \033[0m\n";
  62. }
  63. }
  64. }
  65. /**
  66. * 检查禁用的函数
  67. * @return void
  68. */
  69. public static function checkDisableFunction()
  70. {
  71. // 可能禁用的函数
  72. $check_func_map = array(
  73. 'stream_socket_server',
  74. 'stream_socket_client',
  75. 'pcntl_signal_dispatch',
  76. );
  77. if($disable_func_string = ini_get("disable_functions"))
  78. {
  79. $disable_func_map = array_flip(explode(',', $disable_func_string));
  80. }
  81. // 遍历查看是否有禁用的函数
  82. foreach($check_func_map as $func)
  83. {
  84. if(isset($disable_func_map[$func]))
  85. {
  86. \Man\Core\Master::notice("Function $func may be disabled\tPlease check disable_functions in php.ini \t Server start fail");
  87. exit("\n\033[31;40mFunction $func may be disabled\nPlease check disable_functions in php.ini\033[0m\n\n\033[31;40mServer start fail\033[0m\n\n");
  88. }
  89. }
  90. }
  91. /**
  92. * 检查worker配置、worker语法错误等
  93. * @return void
  94. */
  95. public static function checkWorkersConfig()
  96. {
  97. $pad_length = 26;
  98. $total_worker_count = 0;
  99. // 检查worker 是否有语法错误
  100. echo "----------------------WORKERS--------------------\n";
  101. foreach (Config::getAllWorkers() as $worker_name=>$config)
  102. {
  103. if(empty($config['start_workers']))
  104. {
  105. \Man\Core\Master::notice(str_pad($worker_name, $pad_length)." [start_workers not set]\tServer start fail");
  106. exit(str_pad($worker_name, $pad_length)."\033[31;40m [start_workers not set]\033[0m\n\n\033[31;40mServer start fail\033[0m\n");
  107. }
  108. $total_worker_count += $config['start_workers'];
  109. // 语法检查
  110. if(0 != self::checkSyntaxError(WORKERMAN_ROOT_DIR . "workers/$worker_name.php", $worker_name))
  111. {
  112. unset(Config::instance()->config[$worker_name]);
  113. \Man\Core\Master::notice("$worker_name has Fatal Err");
  114. echo str_pad($worker_name, $pad_length),"\033[31;40m [Fatal Err] \033[0m\n";
  115. continue;
  116. }
  117. if(isset($config['user']))
  118. {
  119. $worker_user = $config['user'];
  120. if(!self::checkWorkerUserName($worker_user))
  121. {
  122. echo str_pad($worker_name, $pad_length),"\033[31;40m [FAIL] \033[0m\n";
  123. \Man\Core\Master::notice("Can not run $worker_name processes as user $worker_user , User $worker_user not exists\tServer start fail");
  124. exit("\n\033[31;40mCan not run $worker_name processes as user $worker_user , User $worker_user not exists\033[0m\n\n\033[31;40mServer start fail\033[0m\n\n");
  125. }
  126. }
  127. echo str_pad($worker_name, $pad_length),"\033[32;40m [OK] \033[0m\n";
  128. }
  129. if($total_worker_count > \Man\Core\Master::SERVER_MAX_WORKER_COUNT)
  130. {
  131. \Man\Core\Master::notice("Number of worker processes can not be more than " . \Man\Core\Master::SERVER_MAX_WORKER_COUNT . ".\tPlease check start_workers in " . WORKERMAN_ROOT_DIR . "config/main.php\tServer start fail");
  132. exit("\n\033[31;40mNumber of worker processes can not be more than " . \Man\Core\Master::SERVER_MAX_WORKER_COUNT . ".\nPlease check start_workers in " . WORKERMAN_ROOT_DIR . "config/main.php\033[0m\n\n\033[31;40mServer start fail\033[0m\n");
  133. }
  134. echo "-------------------------------------------------\n";
  135. }
  136. /**
  137. * 检查worker文件是否有语法错误
  138. * @param string $worker_name
  139. * @return int 0:无语法错误 其它:可能有语法错误
  140. */
  141. public static function checkSyntaxError($file, $class_name = null)
  142. {
  143. $pid = pcntl_fork();
  144. // 父进程
  145. if($pid > 0)
  146. {
  147. // 退出状态不为0说明可能有语法错误
  148. $pid = pcntl_wait($status);
  149. return $status;
  150. }
  151. // 子进程
  152. elseif($pid == 0)
  153. {
  154. // 载入对应worker
  155. require_once $file;
  156. if($class_name && !class_exists($class_name))
  157. {
  158. throw new \Exception("Class $class_name not exists");
  159. }
  160. exit(0);
  161. }
  162. }
  163. /**
  164. * 检查打开文件限制
  165. * @return void
  166. */
  167. public static function checkLimit()
  168. {
  169. if($limit_info = posix_getrlimit())
  170. {
  171. if('unlimited' != $limit_info['soft openfiles'] && $limit_info['soft openfiles'] < \Man\Core\Master::MIN_SOFT_OPEN_FILES)
  172. {
  173. echo "Notice : Soft open files now is {$limit_info['soft openfiles']}, We recommend greater than " . \Man\Core\Master::MIN_SOFT_OPEN_FILES . "\n";
  174. }
  175. if('unlimited' != $limit_info['hard filesize'] && $limit_info['hard filesize'] < \Man\Core\Master::MIN_SOFT_OPEN_FILES)
  176. {
  177. echo "Notice : Hard open files now is {$limit_info['hard filesize']}, We recommend greater than " . \Man\Core\Master::MIN_HARD_OPEN_FILES . "\n";
  178. }
  179. }
  180. }
  181. /**
  182. * 检查配置的pid文件是否可写
  183. * @return void
  184. */
  185. public static function checkPidFile()
  186. {
  187. // 已经有进程pid可能server已经启动
  188. if(@file_get_contents(WORKERMAN_PID_FILE))
  189. {
  190. \Man\Core\Master::notice("Server already started", true);
  191. exit;
  192. }
  193. if(is_dir(WORKERMAN_PID_FILE))
  194. {
  195. exit("\n\033[31;40mpid-file ".WORKERMAN_PID_FILE." is Directory\033[0m\n\n\033[31;40mServer start failed\033[0m\n\n");
  196. }
  197. $pid_dir = dirname(WORKERMAN_PID_FILE);
  198. if(!is_dir($pid_dir))
  199. {
  200. if(!mkdir($pid_dir, true))
  201. {
  202. exit("Create dir $pid_dir fail\n");
  203. }
  204. }
  205. if(!is_writeable($pid_dir))
  206. {
  207. exit("\n\033[31;40mYou should start the server as root\033[0m\n\n\033[31;40mServer start failed\033[0m\n\n");
  208. }
  209. }
  210. }