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. // worker_processes
  89. int worker_processes = 0;
  90. str = g_conf_ctx.parser->GetValue("worker_processes");
  91. if (str.size() != 0) {
  92. if (strcmp(str.c_str(), "auto") == 0) {
  93. worker_processes = get_ncpu();
  94. hlogd("worker_processes=ncpu=%d", worker_processes);
  95. }
  96. else {
  97. worker_processes = atoi(str.c_str());
  98. }
  99. }
  100. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  101. // port
  102. int port = 0;
  103. const char* szPort = get_arg("p");
  104. if (szPort) {
  105. port = atoi(szPort);
  106. }
  107. if (port == 0) {
  108. port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
  109. }
  110. if (port == 0) {
  111. printf("Please config listen port!\n");
  112. exit(-10);
  113. }
  114. g_conf_ctx.port = port;
  115. return 0;
  116. }
  117. void master_init(void* userdata) {
  118. #ifdef OS_UNIX
  119. char proctitle[256] = {0};
  120. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  121. setproctitle(proctitle);
  122. signal(SIGNAL_RELOAD, signal_handler);
  123. #endif
  124. }
  125. void worker_init(void* userdata) {
  126. #ifdef OS_UNIX
  127. char proctitle[256] = {0};
  128. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  129. setproctitle(proctitle);
  130. signal(SIGNAL_RELOAD, signal_handler);
  131. #endif
  132. }
  133. static void on_reload(void* userdata) {
  134. hlogi("reload confile [%s]", g_main_ctx.confile);
  135. parse_confile(g_main_ctx.confile);
  136. }
  137. int main(int argc, char** argv) {
  138. // g_main_ctx
  139. main_ctx_init(argc, argv);
  140. if (argc == 1) {
  141. print_help();
  142. exit(10);
  143. }
  144. //int ret = parse_opt(argc, argv, options);
  145. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  146. if (ret != 0) {
  147. print_help();
  148. exit(ret);
  149. }
  150. /*
  151. printf("---------------arg------------------------------\n");
  152. printf("%s\n", g_main_ctx.cmdline);
  153. for (auto& pair : g_main_ctx.arg_kv) {
  154. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  155. }
  156. for (auto& item : g_main_ctx.arg_list) {
  157. printf("%s\n", item.c_str());
  158. }
  159. printf("================================================\n");
  160. */
  161. /*
  162. printf("---------------env------------------------------\n");
  163. for (auto& pair : g_main_ctx.env_kv) {
  164. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  165. }
  166. printf("================================================\n");
  167. */
  168. // help
  169. if (get_arg("h")) {
  170. print_help();
  171. exit(0);
  172. }
  173. // version
  174. if (get_arg("v")) {
  175. print_version();
  176. exit(0);
  177. }
  178. // logfile
  179. hlog_set_file(g_main_ctx.logfile);
  180. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  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. }