main.cpp.tmpl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_filesize
  86. str = g_conf_ctx.parser->GetValue("log_filesize");
  87. if (!str.empty()) {
  88. int num = atoi(str.c_str());
  89. if (num > 0) {
  90. // 16 16M 16MB
  91. const char* p = str.c_str() + str.size() - 1;
  92. char unit;
  93. if (*p >= '0' && *p <= '9') unit = 'M';
  94. else if (*p == 'B') unit = *(p-1);
  95. else unit = *p;
  96. unsigned long long filesize = num;
  97. switch (unit) {
  98. case 'K': filesize <<= 10; break;
  99. case 'M': filesize <<= 20; break;
  100. case 'G': filesize <<= 30; break;
  101. default: filesize <<= 20; break;
  102. }
  103. hlog_set_max_filesize(filesize);
  104. }
  105. }
  106. // log_remain_days
  107. str = g_conf_ctx.parser->GetValue("log_remain_days");
  108. if (!str.empty()) {
  109. hlog_set_remain_days(atoi(str.c_str()));
  110. }
  111. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  112. // worker_processes
  113. int worker_processes = 0;
  114. str = g_conf_ctx.parser->GetValue("worker_processes");
  115. if (str.size() != 0) {
  116. if (strcmp(str.c_str(), "auto") == 0) {
  117. worker_processes = get_ncpu();
  118. hlogd("worker_processes=ncpu=%d", worker_processes);
  119. }
  120. else {
  121. worker_processes = atoi(str.c_str());
  122. }
  123. }
  124. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  125. // port
  126. int port = 0;
  127. const char* szPort = get_arg("p");
  128. if (szPort) {
  129. port = atoi(szPort);
  130. }
  131. if (port == 0) {
  132. port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
  133. }
  134. if (port == 0) {
  135. printf("Please config listen port!\n");
  136. exit(-10);
  137. }
  138. g_conf_ctx.port = port;
  139. return 0;
  140. }
  141. void master_init(void* userdata) {
  142. #ifdef OS_UNIX
  143. char proctitle[256] = {0};
  144. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  145. setproctitle(proctitle);
  146. signal(SIGNAL_RELOAD, signal_handler);
  147. #endif
  148. }
  149. void worker_init(void* userdata) {
  150. #ifdef OS_UNIX
  151. char proctitle[256] = {0};
  152. snprintf(proctitle, sizeof(proctitle), "%s: worker process", g_main_ctx.program_name);
  153. setproctitle(proctitle);
  154. signal(SIGNAL_RELOAD, signal_handler);
  155. #endif
  156. }
  157. static void on_reload(void* userdata) {
  158. hlogi("reload confile [%s]", g_main_ctx.confile);
  159. parse_confile(g_main_ctx.confile);
  160. }
  161. int main(int argc, char** argv) {
  162. // g_main_ctx
  163. main_ctx_init(argc, argv);
  164. if (argc == 1) {
  165. print_help();
  166. exit(10);
  167. }
  168. //int ret = parse_opt(argc, argv, options);
  169. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  170. if (ret != 0) {
  171. print_help();
  172. exit(ret);
  173. }
  174. /*
  175. printf("---------------arg------------------------------\n");
  176. printf("%s\n", g_main_ctx.cmdline);
  177. for (auto& pair : g_main_ctx.arg_kv) {
  178. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  179. }
  180. for (auto& item : g_main_ctx.arg_list) {
  181. printf("%s\n", item.c_str());
  182. }
  183. printf("================================================\n");
  184. */
  185. /*
  186. printf("---------------env------------------------------\n");
  187. for (auto& pair : g_main_ctx.env_kv) {
  188. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  189. }
  190. printf("================================================\n");
  191. */
  192. // help
  193. if (get_arg("h")) {
  194. print_help();
  195. exit(0);
  196. }
  197. // version
  198. if (get_arg("v")) {
  199. print_version();
  200. exit(0);
  201. }
  202. // g_conf_ctx
  203. conf_ctx_init(&g_conf_ctx);
  204. const char* confile = get_arg("c");
  205. if (confile) {
  206. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  207. }
  208. parse_confile(g_main_ctx.confile);
  209. // test
  210. if (get_arg("t")) {
  211. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  212. exit(0);
  213. }
  214. // signal
  215. signal_init(on_reload);
  216. const char* signal = get_arg("s");
  217. if (signal) {
  218. handle_signal(signal);
  219. }
  220. #ifdef OS_UNIX
  221. // daemon
  222. if (get_arg("d")) {
  223. // nochdir, noclose
  224. int ret = daemon(1, 1);
  225. if (ret != 0) {
  226. printf("daemon error: %d\n", ret);
  227. exit(-10);
  228. }
  229. // parent process exit after daemon, so pid changed.
  230. g_main_ctx.pid = getpid();
  231. }
  232. #endif
  233. // pidfile
  234. create_pidfile();
  235. if (g_conf_ctx.worker_processes == 0) {
  236. // single process
  237. worker_proc(NULL);
  238. }
  239. else {
  240. // master-workers processes
  241. g_worker_processes_num = g_conf_ctx.worker_processes;
  242. int bytes = sizeof(proc_ctx_t) * g_worker_processes_num;
  243. g_worker_processes = (proc_ctx_t*)malloc(bytes);
  244. if (g_worker_processes == NULL) {
  245. return ERR_MALLOC;
  246. }
  247. memset(g_worker_processes, 0, bytes);
  248. for (int i = 0; i < g_worker_processes_num; ++i) {
  249. proc_ctx_t* ctx = &g_worker_processes[i];
  250. ctx->init = worker_init;
  251. ctx->init_userdata = NULL;
  252. ctx->proc = worker_proc;
  253. ctx->proc_userdata = NULL;
  254. spawn_proc(ctx);
  255. hlogi("worker[%d] start/running, pid=%d", i, ctx->pid);
  256. }
  257. hlogi("master start/running, pid=%d", g_main_ctx.pid);
  258. master_init(NULL);
  259. master_proc(NULL);
  260. }
  261. return 0;
  262. }
  263. void master_proc(void* userdata) {
  264. while(1) sleep(1);
  265. }
  266. void worker_proc(void* userdata) {
  267. while(1) sleep(1);
  268. }