1
0

hmain.cpp 18 KB

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