hmain.c 20 KB

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