1
0

hmain.cpp 18 KB

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