main.cpp.tmpl 7.7 KB

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