hmain.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "hmain.h"
  2. #include <signal.h> // for kill
  3. #include <errno.h>
  4. #include "hplatform.h"
  5. #include "hlog.h"
  6. main_ctx_t g_main_ctx;
  7. int main_ctx_init(int argc, char** argv) {
  8. g_main_ctx.pid = getpid();
  9. char* cwd = getcwd(g_main_ctx.run_path, sizeof(g_main_ctx.run_path));
  10. if (cwd == NULL) {
  11. printf("getcwd error\n");
  12. }
  13. //printf("run_path=%s\n", g_main_ctx.run_path);
  14. const char* b = argv[0];
  15. const char* e = b;
  16. while (*e) ++e;
  17. --e;
  18. while (e >= b) {
  19. if (*e == '/' || *e == '\\') {
  20. break;
  21. }
  22. --e;
  23. }
  24. strncpy(g_main_ctx.program_name, e+1, sizeof(g_main_ctx.program_name));
  25. #ifdef OS_WIN
  26. if (strcmp(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4, ".exe") == 0) {
  27. *(g_main_ctx.program_name+strlen(g_main_ctx.program_name)-4) = '\0';
  28. }
  29. #endif
  30. //printf("program_name=%s\n", g_main_ctx.program_name);
  31. // save arg
  32. int i = 0;
  33. g_main_ctx.os_argv = argv;
  34. g_main_ctx.argc = 0;
  35. g_main_ctx.arg_len = 0;
  36. for (i = 0; argv[i]; ++i) {
  37. g_main_ctx.arg_len += strlen(argv[i]) + 1;
  38. }
  39. g_main_ctx.argc = i;
  40. char* argp = (char*)malloc(g_main_ctx.arg_len);
  41. memset(argp, 0, g_main_ctx.arg_len);
  42. g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
  43. char* cmdline = (char*)malloc(g_main_ctx.arg_len);
  44. g_main_ctx.cmdline = cmdline;
  45. for (i = 0; argv[i]; ++i) {
  46. g_main_ctx.save_argv[i] = argp;
  47. strcpy(g_main_ctx.save_argv[i], argv[i]);
  48. argp += strlen(argv[i]) + 1;
  49. strcpy(cmdline, argv[i]);
  50. cmdline += strlen(argv[i]);
  51. *cmdline = ' ';
  52. ++cmdline;
  53. }
  54. g_main_ctx.save_argv[g_main_ctx.argc] = NULL;
  55. g_main_ctx.cmdline[g_main_ctx.arg_len-1] = '\0';
  56. #if defined(OS_WIN) || defined(OS_LINUX)
  57. // save env
  58. g_main_ctx.os_envp = environ;
  59. g_main_ctx.envc = 0;
  60. g_main_ctx.env_len = 0;
  61. for (i = 0; environ[i]; ++i) {
  62. g_main_ctx.env_len += strlen(environ[i]) + 1;
  63. }
  64. g_main_ctx.envc = i;
  65. char* envp = (char*)malloc(g_main_ctx.env_len);
  66. memset(envp, 0, g_main_ctx.env_len);
  67. g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
  68. for (i = 0; environ[i]; ++i) {
  69. g_main_ctx.save_envp[i] = envp;
  70. strcpy(g_main_ctx.save_envp[i], environ[i]);
  71. envp += strlen(environ[i]) + 1;
  72. }
  73. g_main_ctx.save_envp[g_main_ctx.envc] = NULL;
  74. // parse env
  75. for (i = 0; environ[i]; ++i) {
  76. char* b = environ[i];
  77. char* delim = strchr(b, '=');
  78. if (delim == NULL) {
  79. continue;
  80. }
  81. g_main_ctx.env_kv[std::string(b, delim-b)] = std::string(delim+1);
  82. }
  83. #endif
  84. char logpath[MAX_PATH] = {0};
  85. snprintf(logpath, sizeof(logpath), "%s/logs", g_main_ctx.run_path);
  86. MKDIR(logpath);
  87. snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_path, g_main_ctx.program_name);
  88. snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_path, g_main_ctx.program_name);
  89. snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.confile), "%s/logs/%s.log", g_main_ctx.run_path, g_main_ctx.program_name);
  90. g_main_ctx.oldpid = getpid_from_pidfile();
  91. #ifdef OS_UNIX
  92. if (kill(g_main_ctx.oldpid, 0) == -1 && errno == ESRCH) {
  93. g_main_ctx.oldpid = -1;
  94. }
  95. #endif
  96. return 0;
  97. }
  98. #define UNDEFINED_OPTION -1
  99. static int get_arg_type(int short_opt, const char* options) {
  100. if (options == NULL) return UNDEFINED_OPTION;
  101. const char* p = options;
  102. while (*p && *p != short_opt) ++p;
  103. if (*p == '\0') return UNDEFINED_OPTION;
  104. if (*(p+1) == ':') return REQUIRED_ARGUMENT;
  105. return NO_ARGUMENT;
  106. }
  107. int parse_opt(int argc, char** argv, const char* options) {
  108. for (int i = 1; argv[i]; ++i) {
  109. char* p = argv[i];
  110. if (*p != '-') {
  111. g_main_ctx.arg_list.push_back(argv[i]);
  112. continue;
  113. }
  114. while (*++p) {
  115. int arg_type = get_arg_type(*p, options);
  116. if (arg_type == UNDEFINED_OPTION) {
  117. printf("Invalid option '%c'\n", *p);
  118. return -20;
  119. } else if (arg_type == NO_ARGUMENT) {
  120. g_main_ctx.arg_kv[std::string(p, 1)] = OPTION_ENABLE;
  121. continue;
  122. } else if (arg_type == REQUIRED_ARGUMENT) {
  123. if (*(p+1) != '\0') {
  124. g_main_ctx.arg_kv[std::string(p, 1)] = p+1;
  125. break;
  126. } else if (argv[i+1] != NULL) {
  127. g_main_ctx.arg_kv[std::string(p, 1)] = argv[++i];
  128. break;
  129. } else {
  130. printf("Option '%c' requires param\n", *p);
  131. return -30;
  132. }
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. static const option_t* get_option(const char* opt, const option_t* long_options, int size) {
  139. if (opt == NULL || long_options == NULL) return NULL;
  140. int len = strlen(opt);
  141. if (len == 0) return NULL;
  142. if (len == 1) {
  143. for (int i = 0; i < size; ++i) {
  144. if (long_options[i].short_opt == *opt) {
  145. return &long_options[i];
  146. }
  147. }
  148. } else {
  149. for (int i = 0; i < size; ++i) {
  150. if (strcmp(long_options[i].long_opt, opt) == 0) {
  151. return &long_options[i];
  152. }
  153. }
  154. }
  155. return NULL;
  156. }
  157. #define MAX_OPTION 32
  158. // opt type
  159. #define NOPREFIX_OPTION 0
  160. #define SHORT_OPTION -1
  161. #define LONG_OPTION -2
  162. int parse_opt_long(int argc, char** argv, const option_t* long_options, int size) {
  163. char opt[MAX_OPTION+1] = {0};
  164. for (int i = 1; argv[i]; ++i) {
  165. char* arg = argv[i];
  166. int opt_type = NOPREFIX_OPTION;
  167. // prefix
  168. if (*arg == OPTION_PREFIX) {
  169. ++arg;
  170. opt_type = SHORT_OPTION;
  171. if (*arg == OPTION_PREFIX) {
  172. ++arg;
  173. opt_type = LONG_OPTION;
  174. }
  175. }
  176. int arg_len = strlen(arg);
  177. // delim
  178. char* delim = strchr(arg, OPTION_DELIM);
  179. if (delim == arg || delim == arg+arg_len-1 || delim-arg > MAX_OPTION) {
  180. printf("Invalid option '%s'\n", argv[i]);
  181. return -10;
  182. }
  183. if (delim) {
  184. memcpy(opt, arg, delim-arg);
  185. opt[delim-arg] = '\0';
  186. } else {
  187. if (opt_type == SHORT_OPTION) {
  188. *opt = *arg;
  189. opt[1] = '\0';
  190. } else {
  191. strncpy(opt, arg, MAX_OPTION);
  192. }
  193. }
  194. // get_option
  195. const option_t* pOption = get_option(opt, long_options, size);
  196. if (pOption == NULL) {
  197. if (delim == NULL && opt_type == NOPREFIX_OPTION) {
  198. g_main_ctx.arg_list.push_back(arg);
  199. continue;
  200. } else {
  201. printf("Invalid option: '%s'\n", argv[i]);
  202. return -10;
  203. }
  204. }
  205. const char* value = NULL;
  206. if (pOption->arg_type == NO_ARGUMENT) {
  207. // -h
  208. value = OPTION_ENABLE;
  209. } else if (pOption->arg_type == REQUIRED_ARGUMENT) {
  210. if (delim) {
  211. // --port=80
  212. value = delim+1;
  213. } else {
  214. if (opt_type == SHORT_OPTION && *(arg+1) != '\0') {
  215. // p80
  216. value = arg+1;
  217. } else if (argv[i+1] != NULL) {
  218. // --port 80
  219. value = argv[++i];
  220. } else {
  221. printf("Option '%s' requires parament\n", opt);
  222. return -20;
  223. }
  224. }
  225. }
  226. // preferred to use short_opt as key
  227. if (pOption->short_opt > 0) {
  228. g_main_ctx.arg_kv[std::string(1, pOption->short_opt)] = value;
  229. } else if (pOption->long_opt) {
  230. g_main_ctx.arg_kv[pOption->long_opt] = value;
  231. }
  232. }
  233. return 0;
  234. }
  235. const char* get_arg(const char* key) {
  236. auto iter = g_main_ctx.arg_kv.find(key);
  237. if (iter == g_main_ctx.arg_kv.end()) {
  238. return NULL;
  239. }
  240. return iter->second.c_str();
  241. }
  242. const char* get_env(const char* key) {
  243. auto iter = g_main_ctx.env_kv.find(key);
  244. if (iter == g_main_ctx.env_kv.end()) {
  245. return NULL;
  246. }
  247. return iter->second.c_str();
  248. }
  249. #ifdef OS_UNIX
  250. /*
  251. * memory layout
  252. * argv[0]\0argv[1]\0argv[n]\0env[0]\0env[1]\0env[n]\0
  253. */
  254. void setproctitle(const char* title) {
  255. //printf("proctitle=%s\n", title);
  256. memset(g_main_ctx.os_argv[0], 0, g_main_ctx.arg_len + g_main_ctx.env_len);
  257. strncpy(g_main_ctx.os_argv[0], title, g_main_ctx.arg_len + g_main_ctx.env_len);
  258. }
  259. #endif
  260. int create_pidfile() {
  261. FILE* fp = fopen(g_main_ctx.pidfile, "w");
  262. if (fp == NULL) {
  263. printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
  264. return -10;
  265. }
  266. char pid[16] = {0};
  267. snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
  268. fwrite(pid, 1, strlen(pid), fp);
  269. fclose(fp); atexit(delete_pidfile);
  270. hlogi("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
  271. return 0;
  272. }
  273. void delete_pidfile() {
  274. remove(g_main_ctx.pidfile);
  275. hlogi("delete_pidfile [%s]", g_main_ctx.pidfile);
  276. }
  277. pid_t getpid_from_pidfile() {
  278. FILE* fp = fopen(g_main_ctx.pidfile, "r");
  279. if (fp == NULL) {
  280. //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
  281. return -1;
  282. }
  283. char pid[64];
  284. int readbytes = fread(pid, 1, sizeof(pid), fp);
  285. fclose(fp);
  286. if (readbytes <= 0) {
  287. //printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
  288. return -1;
  289. }
  290. return atoi(pid);
  291. }