main.cpp.tmpl 7.9 KB

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