hmain.cpp 9.5 KB

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