main.cpp.tmpl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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]
  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
  106. #define SIGNAL_TERMINATE SIGTERM
  107. #include <sys/wait.h>
  108. void signal_handler(int signo) {
  109. hlogi("pid=%d recv signo=%d", getpid(), signo);
  110. switch (signo) {
  111. case SIGINT:
  112. case SIGNAL_TERMINATE:
  113. hlogi("killall processes");
  114. signal(SIGCHLD, SIG_IGN);
  115. for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
  116. if (s_worker_processes[i].pid <= 0) break;
  117. kill(s_worker_processes[i].pid, SIGKILL);
  118. s_worker_processes[i].pid = -1;
  119. }
  120. exit(0);
  121. break;
  122. case SIGCHLD:
  123. {
  124. pid_t pid = 0;
  125. int status = 0;
  126. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  127. hlogw("proc stop/waiting, pid=%d status=%d", pid, status);
  128. for (int i = 0; i < MAXNUM_WORKER_PROCESSES; ++i) {
  129. if (s_worker_processes[i].pid == pid) {
  130. s_worker_processes[i].pid = -1;
  131. create_proc(&s_worker_processes[i]);
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. int signal_init() {
  143. signal(SIGINT, signal_handler);
  144. signal(SIGCHLD, signal_handler);
  145. signal(SIGNAL_TERMINATE, signal_handler);
  146. atexit(signal_cleanup);
  147. return 0;
  148. }
  149. void signal_cleanup() {
  150. }
  151. #elif defined(OS_WIN)
  152. // win32 use Event
  153. static HANDLE s_hEventTerm = NULL;
  154. #include <mmsystem.h>
  155. #ifdef _MSC_VER
  156. #pragma comment(lib, "winmm.lib")
  157. #endif
  158. void WINAPI on_timer(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) {
  159. DWORD ret = WaitForSingleObject(s_hEventTerm, 0);
  160. if (ret == WAIT_OBJECT_0) {
  161. timeKillEvent(uTimerID);
  162. hlogi("pid=%d recv event [TERM]", getpid());
  163. exit(0);
  164. }
  165. }
  166. int signal_init() {
  167. char eventname[MAX_PATH] = {0};
  168. snprintf(eventname, sizeof(eventname), "%s_term_event", g_main_ctx.program_name);
  169. s_hEventTerm = CreateEvent(NULL, FALSE, FALSE, eventname);
  170. //s_hEventTerm = OpenEvent(EVENT_ALL_ACCESS, FALSE, eventname);
  171. timeSetEvent(1000, 1000, on_timer, 0, TIME_PERIODIC);
  172. atexit(signal_cleanup);
  173. return 0;
  174. }
  175. void signal_cleanup() {
  176. CloseHandle(s_hEventTerm);
  177. s_hEventTerm = NULL;
  178. }
  179. #endif
  180. void handle_signal() {
  181. const char* signal = get_arg("s");
  182. if (signal) {
  183. if (strcmp(signal, "start") == 0) {
  184. if (g_main_ctx.oldpid > 0) {
  185. printf("%s is already running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  186. exit(0);
  187. }
  188. } else if (strcmp(signal, "stop") == 0) {
  189. if (g_main_ctx.oldpid > 0) {
  190. #ifdef OS_UNIX
  191. kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
  192. #else
  193. SetEvent(s_hEventTerm);
  194. #endif
  195. printf("%s stop/waiting\n", g_main_ctx.program_name);
  196. } else {
  197. printf("%s is already stopped\n", g_main_ctx.program_name);
  198. }
  199. exit(0);
  200. } else if (strcmp(signal, "restart") == 0) {
  201. if (g_main_ctx.oldpid > 0) {
  202. #ifdef OS_UNIX
  203. kill(g_main_ctx.oldpid, SIGNAL_TERMINATE);
  204. #else
  205. SetEvent(s_hEventTerm);
  206. #endif
  207. printf("%s stop/waiting\n", g_main_ctx.program_name);
  208. msleep(1000);
  209. }
  210. } else if (strcmp(signal, "status") == 0) {
  211. if (g_main_ctx.oldpid > 0) {
  212. printf("%s start/running, pid=%d\n", g_main_ctx.program_name, g_main_ctx.oldpid);
  213. } else {
  214. printf("%s stop/waiting\n", g_main_ctx.program_name);
  215. }
  216. exit(0);
  217. } else {
  218. printf("Invalid signal: '%s'\n", signal);
  219. exit(0);
  220. }
  221. printf("%s start/running\n", g_main_ctx.program_name);
  222. }
  223. }
  224. int main(int argc, char** argv) {
  225. // g_main_ctx
  226. main_ctx_init(argc, argv);
  227. if (argc == 1) {
  228. print_help();
  229. exit(10);
  230. }
  231. //int ret = parse_opt(argc, argv, options);
  232. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  233. if (ret != 0) {
  234. print_help();
  235. exit(ret);
  236. }
  237. /*
  238. printf("---------------arg------------------------------\n");
  239. printf("%s\n", g_main_ctx.cmdline);
  240. for (auto& pair : g_main_ctx.arg_kv) {
  241. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  242. }
  243. for (auto& item : g_main_ctx.arg_list) {
  244. printf("%s\n", item.c_str());
  245. }
  246. printf("================================================\n");
  247. */
  248. /*
  249. printf("---------------env------------------------------\n");
  250. for (auto& pair : g_main_ctx.env_kv) {
  251. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  252. }
  253. printf("================================================\n");
  254. */
  255. // help
  256. if (get_arg("h")) {
  257. print_help();
  258. exit(0);
  259. }
  260. // version
  261. if (get_arg("v")) {
  262. print_version();
  263. exit(0);
  264. }
  265. // logfile
  266. hlog_set_file(g_main_ctx.logfile);
  267. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  268. // confile
  269. const char* confile = get_arg("c");
  270. if (confile) {
  271. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  272. }
  273. // g_conf_ctx
  274. parse_confile(g_main_ctx.confile);
  275. // test
  276. if (get_arg("t")) {
  277. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  278. exit(0);
  279. }
  280. // signal
  281. signal_init();
  282. handle_signal();
  283. #ifdef OS_UNIX
  284. // daemon
  285. if (get_arg("d")) {
  286. // nochdir, noclose
  287. int ret = daemon(1, 1);
  288. if (ret != 0) {
  289. printf("daemon error: %d\n", ret);
  290. exit(-10);
  291. }
  292. // parent process exit after daemon, so pid changed.
  293. g_main_ctx.pid = getpid();
  294. }
  295. // proctitle
  296. char proctitle[256] = {0};
  297. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  298. setproctitle(proctitle);
  299. #endif
  300. // pidfile
  301. create_pidfile();
  302. hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
  303. // master-worker proc
  304. memset(s_worker_processes, 0, sizeof(s_worker_processes));
  305. for (int i = 0; i < g_conf_ctx.worker_processes; ++i) {
  306. proc_ctx_t* ctx = &s_worker_processes[i];
  307. snprintf(ctx->proctitle, sizeof(ctx->proctitle), "%s: worker process", g_main_ctx.program_name);
  308. ctx->proc = worker_proc;
  309. ctx->userdata = NULL;
  310. create_proc(ctx);
  311. }
  312. master_proc(NULL);
  313. return 0;
  314. }
  315. void master_proc(void* userdata) {
  316. while(1) msleep(1000);
  317. }
  318. void worker_proc(void* userdata) {
  319. while(1) msleep(1000);
  320. }