hmain.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include "hmain.h"
  2. #include <signal.h>
  3. #include "hbase.h"
  4. #include "hlog.h"
  5. #include "herr.h"
  6. #include "htime.h"
  7. #include "hthread.h"
  8. #ifdef OS_DARWIN
  9. #include <crt_externs.h>
  10. #define environ (*_NSGetEnviron())
  11. #endif
  12. main_ctx_t g_main_ctx;
  13. int g_worker_processes_num = 0;
  14. int g_worker_threads_num = 0;
  15. proc_ctx_t* g_worker_processes = NULL;
  16. procedure_t g_worker_fn = NULL;
  17. void* g_worker_userdata = NULL;
  18. int main_ctx_init(int argc, char** argv) {
  19. if (argc == 0 || argv == NULL) {
  20. argc = 1;
  21. argv = (char**)malloc(2*sizeof(char*));
  22. argv[0] = (char*)malloc(MAX_PATH);
  23. argv[1] = NULL;
  24. get_executable_path(argv[0], MAX_PATH);
  25. }
  26. get_run_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));
  27. //printf("run_dir=%s\n", g_main_ctx.run_dir);
  28. strncpy(g_main_ctx.program_name, hv_basename(argv[0]), sizeof(g_main_ctx.program_name));
  29. #ifdef OS_WIN
  30. if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
  31. *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
  32. }
  33. #endif
  34. //printf("program_name=%s\n", g_main_ctx.program_name);
  35. char logpath[MAX_PATH] = {0};
  36. snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_dir);
  37. hv_mkdir(logpath);;
  38. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
  39. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
  40. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
  41. hlog_set_file(g_main_ctx.logfile);
  42. g_main_ctx.pid = getpid();
  43. g_main_ctx.oldpid = getpid_from_pidfile();
  44. #ifdef OS_UNIX
  45. if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
  46. g_main_ctx.oldpid = -1;
  47. }
  48. #else
  49. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, g_main_ctx.oldpid);
  50. if (hproc == NULL) {
  51. g_main_ctx.oldpid = -1;
  52. }
  53. else {
  54. CloseHandle(hproc);
  55. }
  56. #endif
  57. // save arg
  58. int i = 0;
  59. g_main_ctx.os_argv = argv;
  60. g_main_ctx.argc = 0;
  61. g_main_ctx.arg_len = 0;
  62. for (i = 0; argv[i]; ++i) {
  63. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  64. }
  65. g_main_ctx.argc = i;
  66. char* argp = (char*)malloc(g_main_ctx.arg_len);
  67. memset(argp, 0, g_main_ctx.arg_len);
  68. g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
  69. char* cmdline = (char*)malloc(g_main_ctx.arg_len);
  70. g_main_ctx.cmdline = cmdline;
  71. for (i = 0; argv[i]; ++i) {
  72. g_main_ctx.save_argv[i] = argp;
  73. strcpy(g_main_ctx.save_argv[i], argv[i]);
  74. argp += strlen(argv[i]) + 1;
  75. strcpy(cmdline, argv[i]);
  76. cmdline += strlen(argv[i]);
  77. *cmdline = ' ';
  78. ++cmdline;
  79. }
  80. g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
  81. g_main_ctx.cmdline[g_main_ctx.arg_len-1] = '\0';
  82. #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_DARWIN)
  83. // save env
  84. g_main_ctx.os_envp = environ;
  85. g_main_ctx.envc = 0;
  86. g_main_ctx.env_len = 0;
  87. for (i = 0; environ[i]; ++i) {
  88. g_main_ctx.env_len += strlen(environ[i]) + 1;
  89. }
  90. g_main_ctx.envc = i;
  91. char* envp = (char*)malloc(g_main_ctx.env_len);
  92. memset(envp, 0, g_main_ctx.env_len);
  93. g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
  94. for (i = 0; environ[i]; ++i) {
  95. g_main_ctx.save_envp[i] = envp;
  96. strcpy(g_main_ctx.save_envp[i], environ[i]);
  97. envp += strlen(environ[i]) + 1;
  98. }
  99. g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
  100. // parse env
  101. for (i = 0; environ[i]; ++i) {
  102. char* b = environ[i];
  103. char* delim = strchr(b, '=');
  104. if (delim == NULL) {
  105. continue;
  106. }
  107. g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
  108. }
  109. #endif
  110. return 0;
  111. }
  112. #define UNDEFINED_OPTION -1
  113. static int get_arg_type(int short_opt, const char* options) {
  114. if (options == NULL) return UNDEFINED_OPTION;
  115. const char* p = options;
  116. while (*p && *p != short_opt) ++p;
  117. if (*p == '\0') return UNDEFINED_OPTION;
  118. if (*(p+1) == ':') return REQUIRED_ARGUMENT;
  119. return NO_ARGUMENT;
  120. }
  121. int parse_opt(int argc, char** argv, const char* options) {
  122. for (int i = 1; argv[i]; ++i) {
  123. char* p = argv[i];
  124. if (*p != '-') {
  125. g_main_ctx.arg_list.push_back(argv[i]);
  126. continue;
  127. }
  128. while (*++p) {
  129. int arg_type = get_arg_type(*p, options);
  130. if (arg_type == UNDEFINED_OPTION) {
  131. printf("Invalid option '%c'\n", *p);
  132. return -20;
  133. } else if (arg_type == NO_ARGUMENT) {
  134. g_main_ctx.arg_kv[std::string(p, 1)] = OPTION_ENABLE;
  135. continue;
  136. } else if (arg_type == REQUIRED_ARGUMENT) {
  137. if (*(p+1) != '\0') {
  138. g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
  139. break;
  140. } else if (argv[i+1] != NULL) {
  141. g_main_ctx.arg_kv[std::string(p, 1)] = argv[++i];
  142. break;
  143. } else {
  144. printf("Option '%c' requires param\n", *p);
  145. return -30;
  146. }
  147. }
  148. }
  149. }
  150. return 0;
  151. }
  152. static const option_t* get_option(const char* opt, const option_t* long_options, int size) {
  153. if (opt == NULL || long_options == NULL) return NULL;
  154. int len = strlen(opt);
  155. if (len == 0) return NULL;
  156. if (len == 1) {
  157. for (int i = 0; i < size; ++i) {
  158. if (long_options[i].short_opt == *opt) {
  159. return &long_options[i];
  160. }
  161. }
  162. } else {
  163. for (int i = 0; i < size; ++i) {
  164. if (strcmp(long_options[i].long_opt, opt) == 0) {
  165. return &long_options[i];
  166. }
  167. }
  168. }
  169. return NULL;
  170. }
  171. #define MAX_OPTION 32
  172. // opt type
  173. #define NOPREFIX_OPTION 0
  174. #define SHORT_OPTION -1
  175. #define LONG_OPTION -2
  176. int parse_opt_long(int argc, char** argv, const option_t* long_options, int size) {
  177. char opt[MAX_OPTION+1] = {0};
  178. for (int i = 1; argv[i]; ++i) {
  179. char* arg = argv[i];
  180. int opt_type = NOPREFIX_OPTION;
  181. // prefix
  182. if (*arg == OPTION_PREFIX) {
  183. ++arg;
  184. opt_type = SHORT_OPTION;
  185. if (*arg == OPTION_PREFIX) {
  186. ++arg;
  187. opt_type = LONG_OPTION;
  188. }
  189. }
  190. int arg_len = strlen(arg);
  191. // delim
  192. char* delim = strchr(arg, OPTION_DELIM);
  193. if (delim == arg || delim == arg+arg_len-1 || delim-arg > MAX_OPTION) {
  194. printf("Invalid option '%s'\n", argv[i]);
  195. return -10;
  196. }
  197. if (delim) {
  198. memcpy(opt, arg, delim-arg);
  199. opt[delim-arg] = '\0';
  200. } else {
  201. if (opt_type == SHORT_OPTION) {
  202. *opt = *arg;
  203. opt[1] = '\0';
  204. } else {
  205. strncpy(opt, arg, MAX_OPTION);
  206. }
  207. }
  208. // get_option
  209. const option_t* pOption = get_option(opt, long_options, size);
  210. if (pOption == NULL) {
  211. if (delim == NULL && opt_type == NOPREFIX_OPTION) {
  212. g_main_ctx.arg_list.push_back(arg);
  213. continue;
  214. } else {
  215. printf("Invalid option: '%s'\n", argv[i]);
  216. return -10;
  217. }
  218. }
  219. const char* value = NULL;
  220. if (pOption->arg_type == NO_ARGUMENT) {
  221. // -h
  222. value = OPTION_ENABLE;
  223. } else if (pOption->arg_type == REQUIRED_ARGUMENT) {
  224. if (delim) {
  225. // --port=80
  226. value = delim+1;
  227. } else {
  228. if (opt_type == SHORT_OPTION && *(arg+1) != '\0') {
  229. // p80
  230. value = arg+1;
  231. } else if (argv[i+1] != NULL) {
  232. // --port 80
  233. value = argv[++i];
  234. } else {
  235. printf("Option '%s' requires parament\n", opt);
  236. return -20;
  237. }
  238. }
  239. }
  240. // preferred to use short_opt as key
  241. if (pOption->short_opt > 0) {
  242. g_main_ctx.arg_kv[std::string(1, pOption->short_opt)] = value;
  243. } else if (pOption->long_opt) {
  244. g_main_ctx.arg_kv[pOption->long_opt] = value;
  245. }
  246. }
  247. return 0;
  248. }
  249. const char* get_arg(const char* key) {
  250. auto iter = g_main_ctx.arg_kv.find(key);
  251. if (iter == g_main_ctx.arg_kv.end()) {
  252. return NULL;
  253. }
  254. return iter->second.c_str();
  255. }
  256. const char* get_env(const char* key) {
  257. auto iter = g_main_ctx.env_kv.find(key);
  258. if (iter == g_main_ctx.env_kv.end()) {
  259. return NULL;
  260. }
  261. return iter->second.c_str();
  262. }
  263. #ifdef OS_UNIX
  264. /*
  265. * memory layout
  266. * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
  267. */
  268. void setproctitle(const char* title) {
  269. //printf("proctitle=%s\n", title);
  270. int len = g_main_ctx.arg_len + g_main_ctx.env_len;
  271. if (g_main_ctx.os_argv && len) {
  272. strncpy(g_main_ctx.os_argv[0], title, len-1);
  273. }
  274. }
  275. #endif
  276. int create_pidfile() {
  277. FILE* fp = fopen(g_main_ctx.pidfile, "w");
  278. if (fp == NULL) {
  279. hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  280. return -1;
  281. }
  282. char pid[16] = {0};
  283. snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
  284. fwrite(pid, 1, strlen(pid), fp);
  285. fclose(fp);
  286. hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  287. atexit(delete_pidfile);
  288. return 0;
  289. }
  290. void delete_pidfile() {
  291. hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  292. remove(g_main_ctx.pidfile);
  293. }
  294. pid_t getpid_from_pidfile() {
  295. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  296. if (fp == NULL) {
  297. // hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  298. return -1;
  299. }
  300. char pid[64];
  301. int readbytes = fread(pid, 1, sizeof(pid), fp);
  302. fclose(fp);
  303. return readbytes <= 0 ? -1 : atoi(pid);
  304. }
  305. static procedure_t s_reload_fn = NULL;
  306. static void* s_reload_userdata = NULL;
  307. #ifdef OS_UNIX
  308. // unix use signal
  309. #include <sys/wait.h>
  310. void signal_handler(int signo) {
  311. hlogi("pid=%d recv signo=%d", getpid(), signo);
  312. switch (signo) {
  313. case SIGINT:
  314. case SIGNAL_TERMINATE:
  315. hlogi("killall processes");
  316. signal(SIGCHLD, SIG_IGN);
  317. // master send SIGKILL => workers
  318. for (int i = 0; i < g_worker_processes_num; ++i) {
  319. if (g_worker_processes[i].pid <= 0) break;
  320. kill(g_worker_processes[i].pid, SIGKILL);
  321. g_worker_processes[i].pid = -1;
  322. }
  323. exit(0);
  324. break;
  325. case SIGNAL_RELOAD:
  326. if (s_reload_fn) {
  327. s_reload_fn(s_reload_userdata);
  328. if (getpid_from_pidfile() == getpid()) {
  329. // master send SIGNAL_RELOAD => workers
  330. for (int i = 0; i < g_worker_processes_num; ++i) {
  331. if (g_worker_processes[i].pid <= 0) break;
  332. kill(g_worker_processes[i].pid, SIGNAL_RELOAD);
  333. }
  334. }
  335. }
  336. break;
  337. case SIGCHLD:
  338. {
  339. pid_t pid = 0;
  340. int status = 0;
  341. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  342. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  343. for (int i = 0; i < g_worker_processes_num; ++i) {
  344. if (g_worker_processes[i].pid == pid) {
  345. g_worker_processes[i].pid = -1;
  346. hproc_spawn(&g_worker_processes[i]);
  347. break;
  348. }
  349. }
  350. }
  351. }
  352. break;
  353. default:
  354. break;
  355. }
  356. }
  357. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  358. s_reload_fn = reload_fn;
  359. s_reload_userdata = reload_userdata;
  360. signal(SIGINT, signal_handler);
  361. signal(SIGCHLD, signal_handler);
  362. signal(SIGNAL_TERMINATE, signal_handler);
  363. signal(SIGNAL_RELOAD, signal_handler);
  364. return 0;
  365. }
  366. #elif defined(OS_WIN)
  367. // win32 use Event
  368. //static HANDLE s_hEventTerm = NULL;
  369. static HANDLE s_hEventReload = NULL;
  370. #include <mmsystem.h>
  371. #ifdef _MSC_VER
  372. #pragma comment(lib, "winmm.lib")
  373. #endif
  374. void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  375. DWORD ret;
  376. /*
  377. ret = WaitForSingleObject(s_hEventTerm, 0);
  378. if (ret == WAIT_OBJECT_0) {
  379. hlogi("pid=%d recv event [TERM]", getpid());
  380. if (getpid_from_pidfile() == getpid()) {
  381. timeKillEvent(uTimerID);
  382. exit(0);
  383. }
  384. }
  385. */
  386. ret = WaitForSingleObject(s_hEventReload, 0);
  387. if (ret == WAIT_OBJECT_0) {
  388. hlogi("pid=%d recv event [RELOAD]", getpid());
  389. if (s_reload_fn) {
  390. s_reload_fn(s_reload_userdata);
  391. }
  392. }
  393. }
  394. void signal_cleanup() {
  395. //CloseHandle(s_hEventTerm);
  396. //s_hEventTerm = NULL;
  397. CloseHandle(s_hEventReload);
  398. s_hEventReload = NULL;
  399. }
  400. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  401. s_reload_fn = reload_fn;
  402. s_reload_userdata = reload_userdata;
  403. char eventname[MAX_PATH] = {0};
  404. //snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  405. //s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  406. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  407. snprintf(eventname, sizeof(eventname), "%s_reload_event", g_main_ctx.program_name);
  408. s_hEventReload = CreateEvent(NULL, FALSE, FALSE, eventname);
  409. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  410. atexit(signal_cleanup);
  411. return 0;
  412. }
  413. #endif
  414. static void kill_proc(int pid) {
  415. #ifdef OS_UNIX
  416. kill(pid, SIGNAL_TERMINATE);
  417. #else
  418. //SetEvent(s_hEventTerm);
  419. //sleep(1);
  420. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  421. if (hproc) {
  422. TerminateProcess(hproc, 0);
  423. CloseHandle(hproc);
  424. }
  425. #endif
  426. }
  427. void signal_handle(const char* signal) {
  428. if (strcmp(signal, "start") == 0) {
  429. if (g_main_ctx.oldpid > 0) {
  430. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  431. exit(0);
  432. }
  433. } else if (strcmp(signal, "stop") == 0) {
  434. if (g_main_ctx.oldpid > 0) {
  435. kill_proc(g_main_ctx.oldpid);
  436. printf("%s stop/waiting\n", g_main_ctx.program_name);
  437. } else {
  438. printf("%s is already stopped\n", g_main_ctx.program_name);
  439. }
  440. exit(0);
  441. } else if (strcmp(signal, "restart") == 0) {
  442. if (g_main_ctx.oldpid > 0) {
  443. kill_proc(g_main_ctx.oldpid);
  444. printf("%s stop/waiting\n", g_main_ctx.program_name);
  445. msleep(1000);
  446. }
  447. } else if (strcmp(signal, "status") == 0) {
  448. if (g_main_ctx.oldpid > 0) {
  449. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  450. } else {
  451. printf("%s stop/waiting\n", g_main_ctx.program_name);
  452. }
  453. exit(0);
  454. } else if (strcmp(signal, "reload") == 0) {
  455. if (g_main_ctx.oldpid > 0) {
  456. printf("reload confile [%s]\n", g_main_ctx.confile);
  457. #ifdef OS_UNIX
  458. kill(g_main_ctx.oldpid, SIGNAL_RELOAD);
  459. #else
  460. SetEvent(s_hEventReload);
  461. #endif
  462. }
  463. sleep(1);
  464. exit(0);
  465. } else {
  466. printf("Invalid signal: '%s'\n", signal);
  467. exit(0);
  468. }
  469. printf("%s start/running\n", g_main_ctx.program_name);
  470. }
  471. // master-workers processes
  472. static HTHREAD_ROUTINE(worker_thread) {
  473. hlogi("worker_thread pid=%ld tid=%ld", hv_getpid(), hv_gettid());
  474. if (g_worker_fn) {
  475. g_worker_fn(g_worker_userdata);
  476. }
  477. return 0;
  478. }
  479. static void worker_init(void* userdata) {
  480. #ifdef OS_UNIX
  481. char proctitle[256] = {0};
  482. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  483. setproctitle(proctitle);
  484. signal(SIGNAL_RELOAD, signal_handler);
  485. #endif
  486. }
  487. static void worker_proc(void* userdata) {
  488. for (int i = 1; i < g_worker_threads_num; ++i) {
  489. hthread_create(worker_thread, NULL);
  490. }
  491. worker_thread(NULL);
  492. }
  493. int master_workers_run(procedure_t worker_fn, void* worker_userdata,
  494. int worker_processes, int worker_threads, bool wait) {
  495. #ifdef OS_WIN
  496. // NOTE: Windows not provide MultiProcesses
  497. if (worker_threads == 0) {
  498. // MultiProcesses => MultiThreads
  499. worker_threads = worker_processes;
  500. }
  501. worker_processes = 0;
  502. #endif
  503. if (worker_threads == 0) worker_threads = 1;
  504. g_worker_threads_num = worker_threads;
  505. g_worker_fn = worker_fn;
  506. g_worker_userdata = worker_userdata;
  507. if (worker_processes == 0) {
  508. // single process
  509. if (wait) {
  510. for (int i = 1; i < worker_threads; ++i) {
  511. hthread_create(worker_thread, NULL);
  512. }
  513. worker_thread(NULL);
  514. }
  515. else {
  516. for (int i = 0; i < worker_threads; ++i) {
  517. hthread_create(worker_thread, NULL);
  518. }
  519. }
  520. }
  521. else {
  522. if (g_worker_processes_num != 0) {
  523. return ERR_OVER_LIMIT;
  524. }
  525. // master-workers processes
  526. #ifdef OS_UNIX
  527. char proctitle[256] = {0};
  528. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  529. setproctitle(proctitle);
  530. signal(SIGNAL_RELOAD, signal_handler);
  531. #endif
  532. g_worker_processes_num = worker_processes;
  533. int bytes = g_worker_processes_num * sizeof(proc_ctx_t);
  534. g_worker_processes = (proc_ctx_t*)malloc(bytes);
  535. memset(g_worker_processes, 0, bytes);
  536. proc_ctx_t* ctx = g_worker_processes;
  537. for (int i = 0; i < g_worker_processes_num; ++i, ++ctx) {
  538. ctx->init = worker_init;
  539. ctx->proc = worker_proc;
  540. hproc_spawn(ctx);
  541. hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
  542. }
  543. g_main_ctx.pid = getpid();
  544. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  545. if (wait) {
  546. while (1) sleep (1);
  547. }
  548. }
  549. return 0;;
  550. }