hmain.cpp 18 KB

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