1
0

main.cpp.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "h.h"
  2. #define DEFAULT_WORKER_PROCESSES 4
  3. #define MAXNUM_WORKER_PROCESSES 1024
  4. static proc_ctx_t s_worker_processes[MAXNUM_WORKER_PROCESSES];
  5. typedef struct conf_ctx_s {
  6. IniParser* parser;
  7. int loglevel;
  8. int worker_processes;
  9. int port;
  10. } conf_ctx_t;
  11. conf_ctx_t g_conf_ctx;
  12. inline void conf_ctx_init(conf_ctx_t* ctx) {
  13. ctx->parser = new IniParser;
  14. ctx->loglevel = LOG_LEVEL_DEBUG;
  15. ctx->worker_processes = 0;
  16. ctx->port = 0;
  17. }
  18. static void print_version();
  19. static void print_help();
  20. static int parse_confile(const char* confile);
  21. static int signal_init();
  22. static void signal_cleanup();
  23. static void handle_signal();
  24. static void master_proc(void* userdata);
  25. static void worker_proc(void* userdata);
  26. // short options
  27. static const char options[] = "hvc:ts:dp:";
  28. // long options
  29. static const option_t long_options[] = {
  30. {'h', "help", NO_ARGUMENT},
  31. {'v', "version", NO_ARGUMENT},
  32. {'c', "confile", REQUIRED_ARGUMENT},
  33. {'t', "test", NO_ARGUMENT},
  34. {'s', "signal", REQUIRED_ARGUMENT},
  35. {'d', "daemon", NO_ARGUMENT},
  36. {'p', "port", REQUIRED_ARGUMENT}
  37. };
  38. static const char detail_options[] = R"(
  39. -h|--help Print this information
  40. -v|--version Print version
  41. -c|--confile <confile> Set configure file, default etc/{program}.conf
  42. -t|--test Test Configure file and exit
  43. -s|--signal <signal> Send <signal> to process,
  44. <signal>=[start,stop,restart,status,reload]
  45. -d|--daemon Daemonize
  46. -p|--port <port> Set listen port
  47. )";
  48. void print_version() {
  49. printf("%s version %s\n", g_main_ctx.program_name, get_compile_version());
  50. }
  51. void print_help() {
  52. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  53. printf("Options:\n%s\n", detail_options);
  54. }
  55. int parse_confile(const char* confile) {
  56. conf_ctx_init(&g_conf_ctx);
  57. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  58. if (ret != 0) {
  59. printf("Load confile [%s] failed: %d\n", confile, ret);
  60. exit(-40);
  61. }
  62. // loglevel
  63. const char* szLoglevel = g_conf_ctx.parser->GetValue("loglevel").c_str();
  64. if (stricmp(szLoglevel, "DEBUG") == 0) {
  65. g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
  66. } else if (stricmp(szLoglevel, "INFO") == 0) {
  67. g_conf_ctx.loglevel = LOG_LEVEL_INFO;
  68. } else if (stricmp(szLoglevel, "WARN") == 0) {
  69. g_conf_ctx.loglevel = LOG_LEVEL_WARN;
  70. } else if (stricmp(szLoglevel, "ERROR") == 0) {
  71. g_conf_ctx.loglevel = LOG_LEVEL_ERROR;
  72. } else {
  73. g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
  74. }
  75. hlog_set_level(g_conf_ctx.loglevel);
  76. // worker_processes
  77. int worker_processes = 0;
  78. worker_processes = atoi(g_conf_ctx.parser->GetValue("worker_processes").c_str());
  79. if (worker_processes <= 0 || worker_processes > MAXNUM_WORKER_PROCESSES) {
  80. worker_processes = get_ncpu();
  81. hlogd("worker_processes=ncpu=%d", worker_processes);
  82. }
  83. if (worker_processes <= 0 || worker_processes > MAXNUM_WORKER_PROCESSES) {
  84. worker_processes = DEFAULT_WORKER_PROCESSES;
  85. }
  86. g_conf_ctx.worker_processes = worker_processes;
  87. // port
  88. int port = 0;
  89. const char* szPort = get_arg("p");
  90. if (szPort) {
  91. port = atoi(szPort);
  92. }
  93. if (port == 0) {
  94. port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
  95. }
  96. if (port == 0) {
  97. printf("Please config listen port!\n");
  98. exit(-10);
  99. }
  100. g_conf_ctx.port = port;
  101. return 0;
  102. }
  103. #ifdef OS_UNIX
  104. // unix use signal
  105. // we use SIGTERM to quit process, SIGUSR1 to reload confile
  106. #define SIGNAL_TERMINATE SIGTERM
  107. #define SIGNAL_RELOAD SIGUSR1
  108. #include <sys/wait.h>
  109. void signal_handler(int signo) {
  110. hlogi("pid=%d recv signo=%d", getpid(), signo);
  111. switch (signo) {
  112. case SIGINT:
  113. case SIGNAL_TERMINATE:
  114. hlogi("killall processes");
  115. signal(SIGCHLD, SIG_IGN);
  116. for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
  117. if (s_worker_processes[i].pid <= 0) break;
  118. kill(s_worker_processes[i].pid, SIGKILL);
  119. s_worker_processes[i].pid = -1;
  120. }
  121. exit(0);
  122. break;
  123. case SIGNAL_RELOAD:
  124. hlogi("reload confile [%s]", g_main_ctx.confile);
  125. parse_confile(g_main_ctx.confile);
  126. break;
  127. case SIGCHLD:
  128. {
  129. pid_t pid = 0;
  130. int status = 0;
  131. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  132. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  133. for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
  134. if (s_worker_processes[i].pid == pid) {
  135. s_worker_processes[i].pid = -1;
  136. create_proc(&s_worker_processes[i]);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. int signal_init() {
  148. signal(SIGINT, signal_handler);
  149. signal(SIGCHLD, signal_handler);
  150. signal(SIGNAL_TERMINATE, signal_handler);
  151. signal(SIGNAL_RELOAD, signal_handler);
  152. atexit(signal_cleanup);
  153. return 0;
  154. }
  155. void signal_cleanup() {
  156. }
  157. #elif defined(OS_WIN)
  158. // win32 use Event
  159. static HANDLE s_hEventTerm = NULL;
  160. static HANDLE s_hEventReload = NULL;
  161. #include <mmsystem.h>
  162. #ifdef _MSC_VER
  163. #pragma comment(lib, "winmm.lib")
  164. #endif
  165. void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  166. DWORD ret = WaitForSingleObject(s_hEventTerm, 0);
  167. if (ret == WAIT_OBJECT_0) {
  168. timeKillEvent(uTimerID);
  169. hlogi("pid=%d recv event [TERM]", getpid());
  170. exit(0);
  171. }
  172. ret = WaitForSingleObject(s_hEventReload, 0);
  173. if (ret == WAIT_OBJECT_0) {
  174. hlogi("pid=%d recv event [RELOAD]", getpid());
  175. parse_confile(g_main_ctx.confile);
  176. }
  177. }
  178. int signal_init() {
  179. char eventname[MAX_PATH] = {0};
  180. snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  181. s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  182. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  183. snprintf(eventname, sizeof(eventname), "%s_reload_event", g_main_ctx.program_name);
  184. s_hEventReload = CreateEvent(NULL, FALSE, FALSE, eventname);
  185. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  186. atexit(signal_cleanup);
  187. return 0;
  188. }
  189. void signal_cleanup() {
  190. CloseHandle(s_hEventTerm);
  191. s_hEventTerm = NULL;
  192. CloseHandle(s_hEventReload);
  193. s_hEventReload = NULL;
  194. }
  195. #endif
  196. void handle_signal() {
  197. const char* signal = get_arg("s");
  198. if (signal) {
  199. if (strcmp(signal, "start") == 0) {
  200. if (g_main_ctx.oldpid > 0) {
  201. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  202. exit(0);
  203. }
  204. } else if (strcmp(signal, "stop") == 0) {
  205. if (g_main_ctx.oldpid > 0) {
  206. #ifdef OS_UNIX
  207. kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
  208. #else
  209. SetEvent(s_hEventTerm);
  210. #endif
  211. printf("%s stop/waiting\n", g_main_ctx.program_name);
  212. } else {
  213. printf("%s is already stopped\n", g_main_ctx.program_name);
  214. }
  215. exit(0);
  216. } else if (strcmp(signal, "restart") == 0) {
  217. if (g_main_ctx.oldpid > 0) {
  218. #ifdef OS_UNIX
  219. kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
  220. #else
  221. SetEvent(s_hEventTerm);
  222. #endif
  223. printf("%s stop/waiting\n", g_main_ctx.program_name);
  224. msleep(1000);
  225. }
  226. } else if (strcmp(signal, "status") == 0) {
  227. if (g_main_ctx.oldpid > 0) {
  228. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  229. } else {
  230. printf("%s stop/waiting\n", g_main_ctx.program_name);
  231. }
  232. exit(0);
  233. } else if (strcmp(signal, "reload") == 0) {
  234. if (g_main_ctx.oldpid > 0) {
  235. printf("reload confile [%s]\n", g_main_ctx.confile);
  236. #ifdef __unix__
  237. kill(g_main_ctx.oldpid, SIGNAL_RELOAD);
  238. #else
  239. SetEvent(s_hEventReload);
  240. #endif
  241. }
  242. sleep(1);
  243. exit(0);
  244. } else {
  245. printf("Invalid signal: '%s'\n", signal);
  246. exit(0);
  247. }
  248. printf("%s start/running\n", g_main_ctx.program_name);
  249. }
  250. }
  251. int main(int argc, char** argv) {
  252. // g_main_ctx
  253. main_ctx_init(argc, argv);
  254. if (argc == 1) {
  255. print_help();
  256. exit(10);
  257. }
  258. //int ret = parse_opt(argc, argv, options);
  259. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  260. if (ret != 0) {
  261. print_help();
  262. exit(ret);
  263. }
  264. /*
  265. printf("---------------arg------------------------------\n");
  266. printf("%s\n", g_main_ctx.cmdline);
  267. for (auto& pair : g_main_ctx.arg_kv) {
  268. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  269. }
  270. for (auto& item : g_main_ctx.arg_list) {
  271. printf("%s\n", item.c_str());
  272. }
  273. printf("================================================\n");
  274. */
  275. /*
  276. printf("---------------env------------------------------\n");
  277. for (auto& pair : g_main_ctx.env_kv) {
  278. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  279. }
  280. printf("================================================\n");
  281. */
  282. // help
  283. if (get_arg("h")) {
  284. print_help();
  285. exit(0);
  286. }
  287. // version
  288. if (get_arg("v")) {
  289. print_version();
  290. exit(0);
  291. }
  292. // logfile
  293. hlog_set_file(g_main_ctx.logfile);
  294. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  295. // confile
  296. const char* confile = get_arg("c");
  297. if (confile) {
  298. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  299. }
  300. // g_conf_ctx
  301. parse_confile(g_main_ctx.confile);
  302. // test
  303. if (get_arg("t")) {
  304. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  305. exit(0);
  306. }
  307. // signal
  308. signal_init();
  309. handle_signal();
  310. #ifdef OS_UNIX
  311. // daemon
  312. if (get_arg("d")) {
  313. // nochdir, noclose
  314. int ret = daemon(1, 1);
  315. if (ret != 0) {
  316. printf("daemon error: %d\n", ret);
  317. exit(-10);
  318. }
  319. // parent process exit after daemon, so pid changed.
  320. g_main_ctx.pid = getpid();
  321. }
  322. // proctitle
  323. char proctitle[256] = {0};
  324. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  325. setproctitle(proctitle);
  326. #endif
  327. // pidfile
  328. create_pidfile();
  329. hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
  330. // master-worker proc
  331. memset(s_worker_processes, 0, sizeof(s_worker_processes));
  332. for (int i = 0; i < g_conf_ctx.worker_processes; ++i) {
  333. proc_ctx_t* ctx = &s_worker_processes[i];
  334. snprintf(ctx->proctitle, sizeof(ctx->proctitle), "%s: worker process", g_main_ctx.program_name);
  335. ctx->proc = worker_proc;
  336. ctx->userdata = NULL;
  337. create_proc(ctx);
  338. }
  339. master_proc(NULL);
  340. return 0;
  341. }
  342. void master_proc(void* userdata) {
  343. while(1) msleep(1000);
  344. }
  345. void worker_proc(void* userdata) {
  346. while(1) msleep(1000);
  347. }