hmain.cpp 18 KB

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