1
0

hmain.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. char pid[16] = {0};
  287. int len = snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
  288. int nwrite = fwrite(pid, 1, len, fp);
  289. if (nwrite != len) {
  290. fprintf(stderr, "fwrite failed!\n");
  291. exit(-1);
  292. }
  293. fclose(fp);
  294. hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  295. atexit(delete_pidfile);
  296. return 0;
  297. }
  298. void delete_pidfile() {
  299. hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  300. remove(g_main_ctx.pidfile);
  301. }
  302. pid_t getpid_from_pidfile() {
  303. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  304. if (fp == NULL) {
  305. // hloge("fopen('%s') error: %d", g_main_ctx.pidfile, errno);
  306. return -1;
  307. }
  308. char pid[64];
  309. int readbytes = fread(pid, 1, sizeof(pid), fp);
  310. fclose(fp);
  311. return readbytes <= 0 ? -1 : atoi(pid);
  312. }
  313. #ifdef OS_UNIX
  314. // unix use signal
  315. #include <sys/wait.h>
  316. void signal_handler(int signo) {
  317. hlogi("pid=%d recv signo=%d", getpid(), signo);
  318. switch (signo) {
  319. case SIGINT:
  320. case SIGNAL_TERMINATE:
  321. hlogi("killall processes");
  322. signal(SIGCHLD, SIG_IGN);
  323. // master send SIGKILL => workers
  324. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  325. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  326. kill(g_main_ctx.proc_ctxs[i].pid, SIGKILL);
  327. g_main_ctx.proc_ctxs[i].pid = -1;
  328. }
  329. exit(0);
  330. break;
  331. case SIGCHLD:
  332. {
  333. pid_t pid = 0;
  334. int status = 0;
  335. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  336. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  337. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  338. proc_ctx_t* ctx = g_main_ctx.proc_ctxs + i;
  339. if (ctx->pid == pid) {
  340. ctx->pid = -1;
  341. // NOTE: avoid frequent crash and restart
  342. time_t run_time = time(NULL) - ctx->start_time;
  343. if (ctx->spawn_cnt < 3 || run_time > 3600) {
  344. hproc_spawn(ctx);
  345. }
  346. else {
  347. hloge("proc crash, pid=%d spawn_cnt=%d run_time=%us",
  348. pid, ctx->spawn_cnt, (unsigned int)run_time);
  349. bool have_worker = false;
  350. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  351. if (g_main_ctx.proc_ctxs[i].pid > 0) {
  352. have_worker = true;
  353. break;
  354. }
  355. }
  356. if (!have_worker) {
  357. hlogw("No alive worker process, exit master process!");
  358. exit(0);
  359. }
  360. }
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. break;
  367. case SIGNAL_RELOAD:
  368. if (g_main_ctx.reload_fn) {
  369. g_main_ctx.reload_fn(g_main_ctx.reload_userdata);
  370. if (getpid_from_pidfile() == getpid()) {
  371. // master send SIGNAL_RELOAD => workers
  372. for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
  373. if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
  374. kill(g_main_ctx.proc_ctxs[i].pid, SIGNAL_RELOAD);
  375. }
  376. }
  377. }
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  384. g_main_ctx.reload_fn = reload_fn;
  385. g_main_ctx.reload_userdata = reload_userdata;
  386. signal(SIGINT, signal_handler);
  387. signal(SIGCHLD, signal_handler);
  388. signal(SIGNAL_TERMINATE, signal_handler);
  389. signal(SIGNAL_RELOAD, signal_handler);
  390. return 0;
  391. }
  392. #elif defined(OS_WIN)
  393. #include <mmsystem.h> // for timeSetEvent
  394. // win32 use Event
  395. //static HANDLE s_hEventTerm = NULL;
  396. static HANDLE s_hEventReload = NULL;
  397. void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  398. DWORD ret;
  399. /*
  400. ret = WaitForSingleObject(s_hEventTerm, 0);
  401. if (ret == WAIT_OBJECT_0) {
  402. hlogi("pid=%d recv event [TERM]", getpid());
  403. if (getpid_from_pidfile() == getpid()) {
  404. timeKillEvent(uTimerID);
  405. exit(0);
  406. }
  407. }
  408. */
  409. ret = WaitForSingleObject(s_hEventReload, 0);
  410. if (ret == WAIT_OBJECT_0) {
  411. hlogi("pid=%d recv event [RELOAD]", getpid());
  412. if (g_main_ctx.reload_fn) {
  413. g_main_ctx.reload_fn(g_main_ctx.reload_userdata);
  414. }
  415. }
  416. }
  417. void signal_cleanup() {
  418. //CloseHandle(s_hEventTerm);
  419. //s_hEventTerm = NULL;
  420. CloseHandle(s_hEventReload);
  421. s_hEventReload = NULL;
  422. }
  423. int signal_init(procedure_t reload_fn, void* reload_userdata) {
  424. g_main_ctx.reload_fn = reload_fn;
  425. g_main_ctx.reload_userdata = reload_userdata;
  426. char eventname[MAX_PATH] = {0};
  427. //snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  428. //s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  429. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  430. snprintf(eventname, sizeof(eventname), "%s_reload_event", g_main_ctx.program_name);
  431. s_hEventReload = CreateEvent(NULL, FALSE, FALSE, eventname);
  432. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  433. atexit(signal_cleanup);
  434. return 0;
  435. }
  436. #endif
  437. static void kill_proc(int pid) {
  438. #ifdef OS_UNIX
  439. kill(pid, SIGNAL_TERMINATE);
  440. #else
  441. //SetEvent(s_hEventTerm);
  442. //sleep(1);
  443. HANDLE hproc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  444. if (hproc) {
  445. TerminateProcess(hproc, 0);
  446. CloseHandle(hproc);
  447. }
  448. #endif
  449. }
  450. void signal_handle(const char* signal) {
  451. if (strcmp(signal, "start") == 0) {
  452. if (g_main_ctx.oldpid > 0) {
  453. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  454. exit(0);
  455. }
  456. } else if (strcmp(signal, "stop") == 0) {
  457. if (g_main_ctx.oldpid > 0) {
  458. kill_proc(g_main_ctx.oldpid);
  459. printf("%s stop/waiting\n", g_main_ctx.program_name);
  460. } else {
  461. printf("%s is already stopped\n", g_main_ctx.program_name);
  462. }
  463. exit(0);
  464. } else if (strcmp(signal, "restart") == 0) {
  465. if (g_main_ctx.oldpid > 0) {
  466. kill_proc(g_main_ctx.oldpid);
  467. printf("%s stop/waiting\n", g_main_ctx.program_name);
  468. msleep(1000);
  469. }
  470. } else if (strcmp(signal, "status") == 0) {
  471. if (g_main_ctx.oldpid > 0) {
  472. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  473. } else {
  474. printf("%s stop/waiting\n", g_main_ctx.program_name);
  475. }
  476. exit(0);
  477. } else if (strcmp(signal, "reload") == 0) {
  478. if (g_main_ctx.oldpid > 0) {
  479. printf("reload confile [%s]\n", g_main_ctx.confile);
  480. #ifdef OS_UNIX
  481. kill(g_main_ctx.oldpid, SIGNAL_RELOAD);
  482. #else
  483. SetEvent(s_hEventReload);
  484. #endif
  485. }
  486. sleep(1);
  487. exit(0);
  488. } else {
  489. printf("Invalid signal: '%s'\n", signal);
  490. exit(0);
  491. }
  492. printf("%s start/running\n", g_main_ctx.program_name);
  493. }
  494. // master-workers processes
  495. static HTHREAD_ROUTINE(worker_thread) {
  496. hlogi("worker_thread pid=%ld tid=%ld", hv_getpid(), hv_gettid());
  497. if (g_main_ctx.worker_fn) {
  498. g_main_ctx.worker_fn(g_main_ctx.worker_userdata);
  499. }
  500. return 0;
  501. }
  502. static void worker_init(void* userdata) {
  503. #ifdef OS_UNIX
  504. char proctitle[256] = {0};
  505. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  506. setproctitle(proctitle);
  507. signal(SIGNAL_RELOAD, signal_handler);
  508. #endif
  509. }
  510. static void worker_proc(void* userdata) {
  511. for (int i = 1; i < g_main_ctx.worker_threads; ++i) {
  512. hthread_create(worker_thread, NULL);
  513. }
  514. worker_thread(NULL);
  515. }
  516. int master_workers_run(procedure_t worker_fn, void* worker_userdata,
  517. int worker_processes, int worker_threads, bool wait) {
  518. #ifdef OS_WIN
  519. // NOTE: Windows not provide MultiProcesses
  520. if (worker_threads == 0) {
  521. // MultiProcesses => MultiThreads
  522. worker_threads = worker_processes;
  523. }
  524. worker_processes = 0;
  525. #endif
  526. if (worker_threads == 0) worker_threads = 1;
  527. g_main_ctx.worker_threads = worker_threads;
  528. g_main_ctx.worker_fn = worker_fn;
  529. g_main_ctx.worker_userdata = worker_userdata;
  530. if (worker_processes == 0) {
  531. // single process
  532. if (wait) {
  533. for (int i = 1; i < worker_threads; ++i) {
  534. hthread_create(worker_thread, NULL);
  535. }
  536. worker_thread(NULL);
  537. }
  538. else {
  539. for (int i = 0; i < worker_threads; ++i) {
  540. hthread_create(worker_thread, NULL);
  541. }
  542. }
  543. }
  544. else {
  545. if (g_main_ctx.worker_processes != 0) {
  546. return ERR_OVER_LIMIT;
  547. }
  548. // master-workers processes
  549. #ifdef OS_UNIX
  550. char proctitle[256] = {0};
  551. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  552. setproctitle(proctitle);
  553. signal(SIGNAL_RELOAD, signal_handler);
  554. #endif
  555. g_main_ctx.worker_processes = worker_processes;
  556. int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
  557. SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);
  558. proc_ctx_t* ctx = g_main_ctx.proc_ctxs;
  559. for (int i = 0; i < g_main_ctx.worker_processes; ++i, ++ctx) {
  560. ctx->init = worker_init;
  561. ctx->proc = worker_proc;
  562. hproc_spawn(ctx);
  563. hlogi("workers[%d] start/running, pid=%d", i, ctx->pid);
  564. }
  565. g_main_ctx.pid = getpid();
  566. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  567. if (wait) {
  568. while (1) sleep (1);
  569. }
  570. }
  571. return 0;;
  572. }