main.cpp.tmpl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "h.h"
  2. #include "hmain.h"
  3. typedef struct conf_ctx_s {
  4. IniParser* parser;
  5. int loglevel;
  6. int worker_processes;
  7. int port;
  8. } conf_ctx_t;
  9. conf_ctx_t g_conf_ctx;
  10. inline void conf_ctx_init(conf_ctx_t* ctx) {
  11. ctx->parser = new IniParser;
  12. ctx->loglevel = LOG_LEVEL_DEBUG;
  13. ctx->worker_processes = 0;
  14. ctx->port = 0;
  15. }
  16. static void print_version();
  17. static void print_help();
  18. static int parse_confile(const char* confile);
  19. static void master_proc(void* userdata);
  20. static void worker_init(void* userdata);
  21. static void worker_proc(void* userdata);
  22. // short options
  23. static const char options[] = "hvc:ts:dp:";
  24. // long options
  25. static const option_t long_options[] = {
  26. {'h', "help", NO_ARGUMENT},
  27. {'v', "version", NO_ARGUMENT},
  28. {'c', "confile", REQUIRED_ARGUMENT},
  29. {'t', "test", NO_ARGUMENT},
  30. {'s', "signal", REQUIRED_ARGUMENT},
  31. {'d', "daemon", NO_ARGUMENT},
  32. {'p', "port", REQUIRED_ARGUMENT}
  33. };
  34. static const char detail_options[] = R"(
  35. -h|--help Print this information
  36. -v|--version Print version
  37. -c|--confile <confile> Set configure file, default etc/{program}.conf
  38. -t|--test Test Configure file and exit
  39. -s|--signal <signal> Send <signal> to process,
  40. <signal>=[start,stop,restart,status,reload]
  41. -d|--daemon Daemonize
  42. -p|--port <port> Set listen port
  43. )";
  44. void print_version() {
  45. printf("%s version %s\n", g_main_ctx.program_name, get_compile_version());
  46. }
  47. void print_help() {
  48. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  49. printf("Options:\n%s\n", detail_options);
  50. }
  51. int parse_confile(const char* confile) {
  52. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  53. if (ret != 0) {
  54. printf("Load confile [%s] failed: %d\n", confile, ret);
  55. exit(-40);
  56. }
  57. // logfile
  58. string str = g_conf_ctx.parser->GetValue("logfile");
  59. if (!str.empty()) {
  60. hlog_set_file(str.c_str());
  61. }
  62. // loglevel
  63. const char* szLoglevel = g_conf_ctx.parser->GetValue("loglevel").c_str();
  64. if (stricmp(szLoglevel, "VERBOSE") == 0) {
  65. g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
  66. } else if (stricmp(szLoglevel, "DEBUG") == 0) {
  67. g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
  68. } else if (stricmp(szLoglevel, "INFO") == 0) {
  69. g_conf_ctx.loglevel = LOG_LEVEL_INFO;
  70. } else if (stricmp(szLoglevel, "WARN") == 0) {
  71. g_conf_ctx.loglevel = LOG_LEVEL_WARN;
  72. } else if (stricmp(szLoglevel, "ERROR") == 0) {
  73. g_conf_ctx.loglevel = LOG_LEVEL_ERROR;
  74. } else if (stricmp(szLoglevel, "FATAL") == 0) {
  75. g_conf_ctx.loglevel = LOG_LEVEL_FATAL;
  76. } else if (stricmp(szLoglevel, "SILENT") == 0) {
  77. g_conf_ctx.loglevel = LOG_LEVEL_SILENT;
  78. } else {
  79. g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
  80. }
  81. hlog_set_level(g_conf_ctx.loglevel);
  82. // log_remain_days
  83. str = g_conf_ctx.parser->GetValue("log_remain_days");
  84. if (!str.empty()) {
  85. hlog_set_remain_days(atoi(str.c_str()));
  86. }
  87. // worker_processes
  88. int worker_processes = 0;
  89. str = g_conf_ctx.parser->GetValue("worker_processes");
  90. if (str.size() != 0) {
  91. if (strcmp(str.c_str(), "auto") == 0) {
  92. worker_processes = get_ncpu();
  93. hlogd("worker_processes=ncpu=%d", worker_processes);
  94. }
  95. else {
  96. worker_processes = atoi(str.c_str());
  97. }
  98. }
  99. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  100. // port
  101. int port = 0;
  102. const char* szPort = get_arg("p");
  103. if (szPort) {
  104. port = atoi(szPort);
  105. }
  106. if (port == 0) {
  107. port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
  108. }
  109. if (port == 0) {
  110. printf("Please config listen port!\n");
  111. exit(-10);
  112. }
  113. g_conf_ctx.port = port;
  114. return 0;
  115. }
  116. void worker_init(void* userdata) {
  117. #ifdef OS_UNIX
  118. char proctitle[256] = {0};
  119. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  120. setproctitle(proctitle);
  121. signal(SIGNAL_RELOAD, signal_handler);
  122. #endif
  123. }
  124. static void on_reload(void* userdata) {
  125. hlogi("reload confile [%s]", g_main_ctx.confile);
  126. parse_confile(g_main_ctx.confile);
  127. }
  128. int main(int argc, char** argv) {
  129. // g_main_ctx
  130. main_ctx_init(argc, argv);
  131. if (argc == 1) {
  132. print_help();
  133. exit(10);
  134. }
  135. //int ret = parse_opt(argc, argv, options);
  136. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  137. if (ret != 0) {
  138. print_help();
  139. exit(ret);
  140. }
  141. /*
  142. printf("---------------arg------------------------------\n");
  143. printf("%s\n", g_main_ctx.cmdline);
  144. for (auto& pair : g_main_ctx.arg_kv) {
  145. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  146. }
  147. for (auto& item : g_main_ctx.arg_list) {
  148. printf("%s\n", item.c_str());
  149. }
  150. printf("================================================\n");
  151. */
  152. /*
  153. printf("---------------env------------------------------\n");
  154. for (auto& pair : g_main_ctx.env_kv) {
  155. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  156. }
  157. printf("================================================\n");
  158. */
  159. // help
  160. if (get_arg("h")) {
  161. print_help();
  162. exit(0);
  163. }
  164. // version
  165. if (get_arg("v")) {
  166. print_version();
  167. exit(0);
  168. }
  169. // logfile
  170. hlog_set_file(g_main_ctx.logfile);
  171. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  172. // confile
  173. const char* confile = get_arg("c");
  174. if (confile) {
  175. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  176. }
  177. // g_conf_ctx
  178. conf_ctx_init(&g_conf_ctx);
  179. parse_confile(g_main_ctx.confile);
  180. // test
  181. if (get_arg("t")) {
  182. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  183. exit(0);
  184. }
  185. // signal
  186. signal_init(on_reload);
  187. const char* signal = get_arg("s");
  188. if (signal) {
  189. handle_signal(signal);
  190. }
  191. #ifdef OS_UNIX
  192. // daemon
  193. if (get_arg("d")) {
  194. // nochdir, noclose
  195. int ret = daemon(1, 1);
  196. if (ret != 0) {
  197. printf("daemon error: %d\n", ret);
  198. exit(-10);
  199. }
  200. // parent process exit after daemon, so pid changed.
  201. g_main_ctx.pid = getpid();
  202. }
  203. #endif
  204. // pidfile
  205. create_pidfile();
  206. hlogi("%s start/running, pid=%d", g_main_ctx.program_name, g_main_ctx.pid);
  207. if (g_conf_ctx.worker_processes == 0) {
  208. // single process
  209. worker_proc(NULL);
  210. }
  211. else {
  212. // master-workers processes
  213. g_worker_processes_num = g_conf_ctx.worker_processes;
  214. int bytes = sizeof(proc_ctx_t) * g_worker_processes_num;
  215. g_worker_processes = (proc_ctx_t*)malloc(bytes);
  216. if (g_worker_processes == NULL) {
  217. return ERR_MALLOC;
  218. }
  219. memset(g_worker_processes, 0, bytes);
  220. for (int i = 0; i < g_worker_processes_num; ++i) {
  221. proc_ctx_t* ctx = &g_worker_processes[i];
  222. ctx->init = worker_init;
  223. ctx->init_userdata = NULL;
  224. ctx->proc = worker_proc;
  225. ctx->proc_userdata = NULL;
  226. create_proc(ctx);
  227. }
  228. #ifdef OS_UNIX
  229. char proctitle[256] = {0};
  230. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  231. setproctitle(proctitle);
  232. #endif
  233. master_proc(NULL);
  234. }
  235. return 0;
  236. }
  237. void master_proc(void* userdata) {
  238. while(1) msleep(1000);
  239. }
  240. void worker_proc(void* userdata) {
  241. while(1) msleep(1000);
  242. }