Checker.php 8.3 KB

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