skynet_main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "skynet.h"
  2. #include "skynet_imp.h"
  3. #include "skynet_env.h"
  4. #include "skynet_server.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <lua.h>
  9. #include <lualib.h>
  10. #include <lauxlib.h>
  11. #include <signal.h>
  12. #include <assert.h>
  13. static int
  14. optint(const char *key, int opt) {
  15. const char * str = skynet_getenv(key);
  16. if (str == NULL) {
  17. char tmp[20];
  18. sprintf(tmp,"%d",opt);
  19. skynet_setenv(key, tmp);
  20. return opt;
  21. }
  22. return strtol(str, NULL, 10);
  23. }
  24. static int
  25. optboolean(const char *key, int opt) {
  26. const char * str = skynet_getenv(key);
  27. if (str == NULL) {
  28. skynet_setenv(key, opt ? "true" : "false");
  29. return opt;
  30. }
  31. return strcmp(str,"true")==0;
  32. }
  33. static const char *
  34. optstring(const char *key,const char * opt) {
  35. const char * str = skynet_getenv(key);
  36. if (str == NULL) {
  37. if (opt) {
  38. skynet_setenv(key, opt);
  39. opt = skynet_getenv(key);
  40. }
  41. return opt;
  42. }
  43. return str;
  44. }
  45. static void
  46. _init_env(lua_State *L) {
  47. lua_pushnil(L); /* first key */
  48. while (lua_next(L, -2) != 0) {
  49. int keyt = lua_type(L, -2);
  50. if (keyt != LUA_TSTRING) {
  51. fprintf(stderr, "Invalid config table\n");
  52. exit(1);
  53. }
  54. const char * key = lua_tostring(L,-2);
  55. if (lua_type(L,-1) == LUA_TBOOLEAN) {
  56. int b = lua_toboolean(L,-1);
  57. skynet_setenv(key,b ? "true" : "false" );
  58. } else {
  59. const char * value = lua_tostring(L,-1);
  60. if (value == NULL) {
  61. fprintf(stderr, "Invalid config table key = %s\n", key);
  62. exit(1);
  63. }
  64. skynet_setenv(key,value);
  65. }
  66. lua_pop(L,1);
  67. }
  68. lua_pop(L,1);
  69. }
  70. int sigign() {
  71. struct sigaction sa;
  72. sa.sa_handler = SIG_IGN;
  73. sa.sa_flags = 0;
  74. sigemptyset(&sa.sa_mask);
  75. sigaction(SIGPIPE, &sa, 0);
  76. return 0;
  77. }
  78. static const char * load_config = "\
  79. local result = {}\n\
  80. local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\
  81. local sep = package.config:sub(1,1)\n\
  82. local current_path = [[.]]..sep\n\
  83. local function include(filename)\n\
  84. local last_path = current_path\n\
  85. local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\
  86. if path then\n\
  87. if path:sub(1,1) == sep then -- root\n\
  88. current_path = path\n\
  89. else\n\
  90. current_path = current_path .. path\n\
  91. end\n\
  92. else\n\
  93. name = filename\n\
  94. end\n\
  95. local f = assert(io.open(current_path .. name))\n\
  96. local code = assert(f:read [[*a]])\n\
  97. code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\
  98. f:close()\n\
  99. assert(load(code,[[@]]..filename,[[t]],result))()\n\
  100. current_path = last_path\n\
  101. end\n\
  102. setmetatable(result, { __index = { include = include } })\n\
  103. local config_name = ...\n\
  104. include(config_name)\n\
  105. setmetatable(result, nil)\n\
  106. return result\n\
  107. ";
  108. int
  109. main(int argc, char *argv[]) {
  110. const char * config_file = NULL ;
  111. if (argc > 1) {
  112. config_file = argv[1];
  113. } else {
  114. fprintf(stderr, "Need a config file. Please read skynet wiki : https://github.com/cloudwu/skynet/wiki/Config\n"
  115. "usage: skynet configfilename\n");
  116. return 1;
  117. }
  118. skynet_globalinit();
  119. skynet_env_init();
  120. sigign();
  121. struct skynet_config config;
  122. #ifdef LUA_CACHELIB
  123. // init the lock of code cache
  124. luaL_initcodecache();
  125. #endif
  126. struct lua_State *L = luaL_newstate();
  127. luaL_openlibs(L); // link lua lib
  128. int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t");
  129. assert(err == LUA_OK);
  130. lua_pushstring(L, config_file);
  131. err = lua_pcall(L, 1, 1, 0);
  132. if (err) {
  133. fprintf(stderr,"%s\n",lua_tostring(L,-1));
  134. lua_close(L);
  135. return 1;
  136. }
  137. _init_env(L);
  138. lua_close(L);
  139. config.thread = optint("thread",8);
  140. config.module_path = optstring("cpath","./cservice/?.so");
  141. config.harbor = optint("harbor", 1);
  142. config.bootstrap = optstring("bootstrap","snlua bootstrap");
  143. config.daemon = optstring("daemon", NULL);
  144. config.logger = optstring("logger", NULL);
  145. config.logservice = optstring("logservice", "logger");
  146. config.profile = optboolean("profile", 1);
  147. skynet_start(&config);
  148. skynet_globalexit();
  149. return 0;
  150. }