1
0

hmain.cpp 18 KB

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