hmain.cpp.tmpl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "h.h"
  2. #include "hmain.h"
  3. main_ctx_t g_main_ctx;
  4. static int create_pidfile();
  5. static void delete_pidfile();
  6. static pid_t getpid_from_pidfile();
  7. int create_pidfile() {
  8. FILE* fp = fopen(g_main_ctx.pidfile, "w");
  9. if (fp == NULL) {
  10. printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
  11. return -10;
  12. }
  13. char pid[16] = {0};
  14. snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
  15. fwrite(pid, 1, strlen(pid), fp);
  16. fclose(fp);
  17. atexit(delete_pidfile);
  18. return 0;
  19. }
  20. void delete_pidfile() {
  21. remove(g_main_ctx.pidfile);
  22. }
  23. pid_t getpid_from_pidfile() {
  24. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  25. if (fp == NULL) {
  26. //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
  27. return -1;
  28. }
  29. char pid[64];
  30. int readbytes = fread(pid, 1, sizeof(pid), fp);
  31. if (readbytes <= 0) {
  32. printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
  33. return -1;
  34. }
  35. return atoi(pid);
  36. }
  37. int main_ctx_init(int argc, char** argv) {
  38. g_main_ctx.pid = getpid();
  39. char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
  40. if (cwd == NULL) {
  41. printf("getcwd error\n");
  42. }
  43. //printf("run_path=%s\n", g_main_ctx.run_path);
  44. const char* b = argv[0];
  45. const char* e = b;
  46. while (*e) ++e;
  47. --e;
  48. while (e >= b) {
  49. if (*e == '/' || *e == '\\') {
  50. break;
  51. }
  52. --e;
  53. }
  54. strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
  55. //printf("program_name=%s\n", g_main_ctx.program_name);
  56. // save arg
  57. int i = 0;
  58. g_main_ctx.os_argv = argv;
  59. g_main_ctx.argc = 0;
  60. g_main_ctx.arg_len = 0;
  61. for (i = 0; argv[i]; ++i) {
  62. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  63. }
  64. g_main_ctx.argc = i;
  65. char* argp = (char*)malloc(g_main_ctx.arg_len);
  66. memset(argp, 0, g_main_ctx.arg_len);
  67. g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
  68. for (i = 0; argv[i]; ++i) {
  69. g_main_ctx.save_argv[i] = argp;
  70. strcpy(g_main_ctx.save_argv[i], argv[i]);
  71. argp += strlen(argv[i]) + 1;
  72. }
  73. g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
  74. // save env
  75. g_main_ctx.os_envp = environ;
  76. g_main_ctx.envc = 0;
  77. g_main_ctx.env_len = 0;
  78. for (i = 0; environ[i]; ++i) {
  79. g_main_ctx.env_len += strlen(environ[i]) + 1;
  80. }
  81. g_main_ctx.envc = i;
  82. char* envp = (char*)malloc(g_main_ctx.env_len);
  83. memset(envp, 0, g_main_ctx.env_len);
  84. g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
  85. for (i = 0; environ[i]; ++i) {
  86. g_main_ctx.save_envp[i] = envp;
  87. strcpy(g_main_ctx.save_envp[i], environ[i]);
  88. envp += strlen(environ[i]) + 1;
  89. }
  90. g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
  91. // parse env
  92. for (i = 0; environ[i]; ++i) {
  93. char* b = environ[i];
  94. char* delim = strchr(b, '=');
  95. if (delim == NULL) {
  96. continue;
  97. }
  98. g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
  99. }
  100. /*
  101. // print argv and envp
  102. printf("---------------arg------------------------------\n");
  103. for (auto& pair : g_main_ctx.arg_kv) {
  104. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  105. }
  106. printf("---------------env------------------------------\n");
  107. for (auto& pair : g_main_ctx.env_kv) {
  108. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  109. }
  110. printf("PWD=%s\n", get_env("PWD"));
  111. printf("USER=%s\n", get_env("USER"));
  112. printf("HOME=%s\n", get_env("HOME"));
  113. printf("LANG=%s\n", get_env("LANG"));
  114. printf("TERM=%s\n", get_env("TERM"));
  115. printf("SHELL=%s\n", get_env("SHELL"));
  116. printf("================================================\n");
  117. */
  118. char logpath[MAX_PATH] = {0};
  119. snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
  120. MKDIR(logpath);
  121. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
  122. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
  123. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
  124. g_main_ctx.confile_parser = NULL;
  125. g_main_ctx.oldpid = getpid_from_pidfile();
  126. create_pidfile();
  127. return 0;
  128. }
  129. const char* get_arg(const char* key) {
  130. auto iter = g_main_ctx.arg_kv.find(key);
  131. if (iter == g_main_ctx.arg_kv.end()) {
  132. return NULL;
  133. }
  134. return iter->second.c_str();
  135. }
  136. const char* get_env(const char* key) {
  137. auto iter = g_main_ctx.env_kv.find(key);
  138. if (iter == g_main_ctx.env_kv.end()) {
  139. return NULL;
  140. }
  141. return iter->second.c_str();
  142. }
  143. #ifdef __unix__
  144. /*
  145. * memory layout
  146. * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
  147. */
  148. void setproctitle(const char* title) {
  149. //printf("proctitle=%s\n", title);
  150. memset(g_main_ctx.os_argv[0], 0, g_main_ctx.arg_len + g_main_ctx.env_len);
  151. strncpy(g_main_ctx.os_argv[0], title, g_main_ctx.arg_len + g_main_ctx.env_len);
  152. }
  153. #endif
  154. // unix short style
  155. static char options[] = "hvc:td";
  156. static char detail_options[] = "\
  157. -h : print help\n\
  158. -v : print version\n\
  159. -c confile : set configure file, default etc/${program}.conf\n\
  160. -t : test configure file and exit\n\
  161. -d : daemon\n\
  162. ";
  163. void print_version() {
  164. printf("%s version %s\n", g_main_ctx.program_name, get_compile_version());
  165. }
  166. void print_help() {
  167. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  168. printf("Options:\n%s\n", detail_options);
  169. }
  170. #define INVALID_OPTION -1
  171. #define FLAG_OPTION 1
  172. #define PARMA_OPTION 2
  173. int get_option(char opt) {
  174. char* p = options;
  175. while (*p && *p != opt) ++p;
  176. if (*p == '\0') return INVALID_OPTION;
  177. if (*(p+1) == ':') return PARMA_OPTION;
  178. return FLAG_OPTION;
  179. }
  180. int parse_cmdline(int argc, char** argv) {
  181. int i = 1;
  182. while (argv[i]) {
  183. char* p = argv[i];
  184. if (*p != '-') {
  185. printf("Invalid argv[%d]: %s\n", i, argv[i]);
  186. exit(-10);
  187. }
  188. while (*++p) {
  189. switch (get_option(*p)) {
  190. case INVALID_OPTION:
  191. printf("Invalid option: '%c'\n", *p);
  192. exit(-20);
  193. case FLAG_OPTION:
  194. g_main_ctx.arg_kv[std::string(p, 1)] = "true";
  195. break;
  196. case PARMA_OPTION:
  197. if (*(p+1) != '\0') {
  198. g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
  199. ++i;
  200. goto next_option;
  201. } else if (argv[i+1] != NULL) {
  202. g_main_ctx.arg_kv[std::string(p, 1)] = argv[i+1];
  203. i += 2;
  204. goto next_option;
  205. } else {
  206. printf("Option '%c' requires param\n", *p);
  207. exit(-30);
  208. }
  209. }
  210. }
  211. ++i;
  212. next_option:
  213. continue;
  214. }
  215. return 0;
  216. }
  217. int main(int argc, char** argv) {
  218. main_ctx_init(argc, argv);
  219. parse_cmdline(argc, argv);
  220. /*
  221. #ifdef __unix__
  222. char proctitle[256] = {0};
  223. snprintf(proctitle, sizeof(proctitle), "%s: master process", g_main_ctx.program_name);
  224. setproctitle(proctitle);
  225. #endif
  226. */
  227. if (get_arg("h")) {
  228. print_help();
  229. exit(0);
  230. }
  231. if (get_arg("v")) {
  232. print_version();
  233. exit(0);
  234. }
  235. const char* confile = get_arg("c");
  236. if (confile) {
  237. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  238. }
  239. //printf("load confile [%s]\n", g_conf_ctx.confile);
  240. if (get_arg("t")) {
  241. exit(0);
  242. }
  243. #ifdef __unix__
  244. if (get_arg("d")) {
  245. // nochdir, noclose
  246. int ret = daemon(1, 1);
  247. if (ret != 0) {
  248. printf("daemon error: %d\n", ret);
  249. exit(-10);
  250. }
  251. }
  252. #endif
  253. hlog_set_file(g_main_ctx.logfile);
  254. hlog_set_level(LOG_LEVEL_DEBUG);
  255. hlogi("version: %s", get_compile_version());
  256. // add code below
  257. return 0;
  258. }