1
0

hmain_test.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. /*
  5. * @build: make examples
  6. * @usage: bin/hmain_test -h
  7. * bin/hmain_test -v
  8. *
  9. * bin/hmain_test -c etc/hmain_test.conf -d
  10. * ps aux | grep hmain_test
  11. *
  12. * bin/hmain_test -s stop
  13. * ps aux | grep hmain_test
  14. *
  15. */
  16. typedef struct conf_ctx_s {
  17. IniParser* parser;
  18. int loglevel;
  19. int worker_processes;
  20. int worker_threads;
  21. int port;
  22. } conf_ctx_t;
  23. conf_ctx_t g_conf_ctx;
  24. inline void conf_ctx_init(conf_ctx_t* ctx) {
  25. ctx->parser = new IniParser;
  26. ctx->loglevel = LOG_LEVEL_DEBUG;
  27. ctx->worker_processes = 0;
  28. ctx->worker_threads = 0;
  29. ctx->port = 0;
  30. }
  31. static void print_version();
  32. static void print_help();
  33. static int parse_confile(const char* confile);
  34. static void worker_fn(void* userdata);
  35. // short options
  36. static const char options[] = "hvc:ts:dp:";
  37. // long options
  38. static const option_t long_options[] = {
  39. {'h', "help", NO_ARGUMENT},
  40. {'v', "version", NO_ARGUMENT},
  41. {'c', "confile", REQUIRED_ARGUMENT},
  42. {'t', "test", NO_ARGUMENT},
  43. {'s', "signal", REQUIRED_ARGUMENT},
  44. {'d', "daemon", NO_ARGUMENT},
  45. {'p', "port", REQUIRED_ARGUMENT}
  46. };
  47. static const char detail_options[] = R"(
  48. -h|--help Print this information
  49. -v|--version Print version
  50. -c|--confile <confile> Set configure file, default etc/{program}.conf
  51. -t|--test Test configure file and exit
  52. -s|--signal <signal> Send <signal> to process,
  53. <signal>=[start,stop,restart,status,reload]
  54. -d|--daemon Daemonize
  55. -p|--port <port> Set listen port
  56. )";
  57. void print_version() {
  58. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  59. }
  60. void print_help() {
  61. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  62. printf("Options:\n%s\n", detail_options);
  63. }
  64. int parse_confile(const char* confile) {
  65. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  66. if (ret != 0) {
  67. printf("Load confile [%s] failed: %d\n", confile, ret);
  68. exit(-40);
  69. }
  70. // logfile
  71. string str = g_conf_ctx.parser->GetValue("logfile");
  72. if (!str.empty()) {
  73. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  74. }
  75. hlog_set_file(g_main_ctx.logfile);
  76. // loglevel
  77. str = g_conf_ctx.parser->GetValue("loglevel");
  78. if (!str.empty()) {
  79. hlog_set_level_by_str(str.c_str());
  80. }
  81. // log_filesize
  82. str = g_conf_ctx.parser->GetValue("log_filesize");
  83. if (!str.empty()) {
  84. hlog_set_max_filesize_by_str(str.c_str());
  85. }
  86. // log_remain_days
  87. str = g_conf_ctx.parser->GetValue("log_remain_days");
  88. if (!str.empty()) {
  89. hlog_set_remain_days(atoi(str.c_str()));
  90. }
  91. // log_fsync
  92. str = g_conf_ctx.parser->GetValue("log_fsync");
  93. if (!str.empty()) {
  94. logger_enable_fsync(hlog, getboolean(str.c_str()));
  95. }
  96. // first log here
  97. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  98. hlog_fsync();
  99. // worker_processes
  100. int worker_processes = 0;
  101. str = g_conf_ctx.parser->GetValue("worker_processes");
  102. if (str.size() != 0) {
  103. if (strcmp(str.c_str(), "auto") == 0) {
  104. worker_processes = get_ncpu();
  105. hlogd("worker_processes=ncpu=%d", worker_processes);
  106. }
  107. else {
  108. worker_processes = atoi(str.c_str());
  109. }
  110. }
  111. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  112. // worker_threads
  113. int worker_threads = g_conf_ctx.parser->Get<int>("worker_threads");
  114. g_conf_ctx.worker_threads = LIMIT(0, worker_threads, 16);
  115. // port
  116. int port = 0;
  117. const char* szPort = get_arg("p");
  118. if (szPort) {
  119. port = atoi(szPort);
  120. }
  121. if (port == 0) {
  122. port = g_conf_ctx.parser->Get<int>("port");
  123. }
  124. if (port == 0) {
  125. printf("Please config listen port!\n");
  126. exit(-10);
  127. }
  128. g_conf_ctx.port = port;
  129. hlogi("parse_confile('%s') OK", confile);
  130. return 0;
  131. }
  132. static void on_reload(void* userdata) {
  133. hlogi("reload confile [%s]", g_main_ctx.confile);
  134. parse_confile(g_main_ctx.confile);
  135. }
  136. int main(int argc, char** argv) {
  137. // g_main_ctx
  138. main_ctx_init(argc, argv);
  139. if (argc == 1) {
  140. print_help();
  141. exit(10);
  142. }
  143. // int ret = parse_opt(argc, argv, options);
  144. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  145. if (ret != 0) {
  146. print_help();
  147. exit(ret);
  148. }
  149. /*
  150. printf("---------------arg------------------------------\n");
  151. printf("%s\n", g_main_ctx.cmdline);
  152. for (auto& pair : g_main_ctx.arg_kv) {
  153. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  154. }
  155. for (auto& item : g_main_ctx.arg_list) {
  156. printf("%s\n", item.c_str());
  157. }
  158. printf("================================================\n");
  159. printf("---------------env------------------------------\n");
  160. for (auto& pair : g_main_ctx.env_kv) {
  161. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  162. }
  163. printf("================================================\n");
  164. */
  165. // help
  166. if (get_arg("h")) {
  167. print_help();
  168. exit(0);
  169. }
  170. // version
  171. if (get_arg("v")) {
  172. print_version();
  173. exit(0);
  174. }
  175. // g_conf_ctx
  176. conf_ctx_init(&g_conf_ctx);
  177. const char* confile = get_arg("c");
  178. if (confile) {
  179. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  180. }
  181. parse_confile(g_main_ctx.confile);
  182. // test
  183. if (get_arg("t")) {
  184. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  185. exit(0);
  186. }
  187. // signal
  188. signal_init(on_reload);
  189. const char* signal = get_arg("s");
  190. if (signal) {
  191. signal_handle(signal);
  192. }
  193. #ifdef OS_UNIX
  194. // daemon
  195. if (get_arg("d")) {
  196. // nochdir, noclose
  197. int ret = daemon(1, 1);
  198. if (ret != 0) {
  199. printf("daemon error: %d\n", ret);
  200. exit(-10);
  201. }
  202. }
  203. #endif
  204. // pidfile
  205. create_pidfile();
  206. master_workers_run(worker_fn, (void*)(intptr_t)g_conf_ctx.port, g_conf_ctx.worker_processes, g_conf_ctx.worker_threads);
  207. return 0;
  208. }
  209. void worker_fn(void* userdata) {
  210. long port = (long)(intptr_t)(userdata);
  211. while (1) {
  212. printf("port=%ld pid=%ld tid=%ld\n", port, hv_getpid(), hv_gettid());
  213. hv_delay(60000);
  214. }
  215. }