main.cpp.tmpl 7.7 KB

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