main.cpp.tmpl 8.7 KB

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