1
0

hmain.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. SAFE_ALLOC(argv, 2 * sizeof(char*));
  16. SAFE_ALLOC(argv[0], MAX_PATH);
  17. get_executable_path(argv[0], MAX_PATH);
  18. }
  19. get_run_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));
  20. //printf("run_dir=%s\n", g_main_ctx.run_dir);
  21. strncpy(g_main_ctx.program_name, hv_basename(argv[0]), sizeof(g_main_ctx.program_name));
  22. #ifdef OS_WIN
  23. if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
  24. *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
  25. }
  26. #endif
  27. //printf("program_name=%s\n", g_main_ctx.program_name);
  28. char logdir[MAX_PATH] = {0};
  29. snprintf(logdir, sizeof(logdir), "%s/logs", g_main_ctx.run_dir);
  30. hv_mkdir(logdir);
  31. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
  32. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
  33. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
  34. hlog_set_file(g_main_ctx.logfile);
  35. g_main_ctx.pid = getpid();
  36. g_main_ctx.oldpid = getpid_from_pidfile();
  37. #ifdef OS_UNIX
  38. if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
  39. g_main_ctx.oldpid = -1;
  40. }
  41. #else
  42. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, g_main_ctx.oldpid);
  43. if (hproc == NULL) {
  44. g_main_ctx.oldpid = -1;
  45. }
  46. else {
  47. CloseHandle(hproc);
  48. }
  49. #endif
  50. // save arg
  51. int i = 0;
  52. g_main_ctx.os_argv = argv;
  53. g_main_ctx.argc = 0;
  54. g_main_ctx.arg_len = 0;
  55. for (i = 0; argv[i]; ++i) {
  56. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  57. }
  58. g_main_ctx.argc = i;
  59. char* argp = NULL;
  60. SAFE_ALLOC(argp, g_main_ctx.arg_len);
  61. SAFE_ALLOC(g_main_ctx.save_argv, (g_main_ctx.argc + 1) * sizeof(char*));
  62. char* cmdline = NULL;
  63. SAFE_ALLOC(cmdline, 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 = NULL;
  86. SAFE_ALLOC(envp, g_main_ctx.env_len);
  87. SAFE_ALLOC(g_main_ctx.save_envp, (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) {
  197. if (delim == arg || delim == arg+arg_len-1 || delim-arg > MAX_OPTION) {
  198. printf("Invalid option '%s'\n", argv[i]);
  199. return -10;
  200. }
  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. g_main_ctx.pid = hv_getpid();
  286. fprintf(fp, "%d\n", (int)g_main_ctx.pid);
  287. fclose(fp);
  288. hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  289. atexit(delete_pidfile);
  290. return 0;
  291. }
  292. void delete_pidfile() {
  293. hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  294. remove(g_main_ctx.pidfile);
  295. }
  296. pid_t getpid_from_pidfile() {
  297. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  298. if (fp == NULL) {
  299. // hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  300. return -1;
  301. }
  302. int pid = -1;
  303. fscanf(fp, "%d", &pid);
  304. fclose(fp);
  305. return pid;
  306. }
  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_main_ctx.worker_processes; ++i) {
  319. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  320. kill(g_main_ctx.proc_ctxs[i].pid, SIGKILL);
  321. g_main_ctx.proc_ctxs[i].pid = -1;
  322. }
  323. exit(0);
  324. break;
  325. case SIGCHLD:
  326. {
  327. pid_t pid = 0;
  328. int status = 0;
  329. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  330. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  331. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  332. proc_ctx_t* ctx = g_main_ctx.proc_ctxs + i;
  333. if (ctx->pid == pid) {
  334. ctx->pid = -1;
  335. // NOTE: avoid frequent crash and restart
  336. time_t run_time = time(NULL) - ctx->start_time;
  337. if (ctx->spawn_cnt < 3 || run_time > 3600) {
  338. hproc_spawn(ctx);
  339. }
  340. else {
  341. hloge("proc crash, pid=%d spawn_cnt=%d run_time=%us",
  342. pid, ctx->spawn_cnt, (unsigned int)run_time);
  343. bool have_worker = false;
  344. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  345. if (g_main_ctx.proc_ctxs[i].pid > 0) {
  346. have_worker = true;
  347. break;
  348. }
  349. }
  350. if (!have_worker) {
  351. hlogw("No alive worker process, exit master process!");
  352. exit(0);
  353. }
  354. }
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. break;
  361. case SIGNAL_RELOAD:
  362. if (g_main_ctx.reload_fn) {
  363. g_main_ctx.reload_fn(g_main_ctx.reload_userdata);
  364. if (getpid_from_pidfile() == getpid()) {
  365. // master send SIGNAL_RELOAD => workers
  366. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  367. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  368. kill(g_main_ctx.proc_ctxs[i].pid, SIGNAL_RELOAD);
  369. }
  370. }
  371. }
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  378. g_main_ctx.reload_fn = reload_fn;
  379. g_main_ctx.reload_userdata = reload_userdata;
  380. signal(SIGINT, signal_handler);
  381. signal(SIGCHLD, signal_handler);
  382. signal(SIGNAL_TERMINATE, signal_handler);
  383. signal(SIGNAL_RELOAD, signal_handler);
  384. return 0;
  385. }
  386. #elif defined(OS_WIN)
  387. #include <mmsystem.h> // for timeSetEvent
  388. // win32 use Event
  389. //static HANDLE s_hEventTerm = NULL;
  390. static HANDLE s_hEventReload = NULL;
  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 (g_main_ctx.reload_fn) {
  407. g_main_ctx.reload_fn(g_main_ctx.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. g_main_ctx.reload_fn = reload_fn;
  419. g_main_ctx.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. //hv_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 signal_handle(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. hv_sleep(1);
  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. hv_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_main_ctx.worker_fn) {
  492. g_main_ctx.worker_fn(g_main_ctx.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_main_ctx.worker_threads; ++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_main_ctx.worker_threads = worker_threads;
  522. g_main_ctx.worker_fn = worker_fn;
  523. g_main_ctx.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_main_ctx.worker_processes != 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_main_ctx.worker_processes = worker_processes;
  550. int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
  551. SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);
  552. proc_ctx_t* ctx = g_main_ctx.proc_ctxs;
  553. for (int i = 0; i < g_main_ctx.worker_processes; ++i, ++ctx) {
  554. ctx->init = worker_init;
  555. ctx->proc = worker_proc;
  556. hproc_spawn(ctx);
  557. hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
  558. }
  559. g_main_ctx.pid = getpid();
  560. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  561. if (wait) {
  562. while (1) hv_sleep (1);
  563. }
  564. }
  565. return 0;
  566. }