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