main.cpp.tmpl 8.6 KB

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