main.cpp.tmpl 10 KB

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