main.cpp.tmpl 7.7 KB

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