Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
	const char *config = "config";
	struct pixel_config cfg;
	lua_State *L;
	if (argc > 1) {
		config = argv[1];
	}
	signin();
	pixel_init();
	L = luaL_newstate();
	if (LUA_OK != luaL_dofile(L, config)) {
		fprintf(stderr, "load config:%s\n", lua_tostring(L, -1));
		return 1;
	}
	_init_env(L);
	cfg.thread = optint("thread", 8);
	cfg.harbor = optint("harbor", 1);
	cfg.module_path = optstring("cservice", "./?.so");
	cfg.bootstrap = optstring("bootstrap", "lua bootstrap");
	cfg.logfile = optstring("log", 0);
	lua_close(L);
	pixel_start(&cfg);
	pixel_unit();
	return 0;
}
Ejemplo n.º 2
0
void pool_debug(const char *fmt,...)
{
	va_list		ap;
#ifdef HAVE_ASPRINTF
	char		*fmt2;
    int         len;
#endif

#ifdef HAVE_SIGPROCMASK
	sigset_t oldmask;
#else
	int	oldmask;
#endif

	if (run_as_pcp_child)
	{
		if (!debug)
			return;
	}
	else
	{
		if (pool_config->debug_level <= 0)
			return;
	}

	POOL_SETMASK2(&BlockSig, &oldmask);

	/* Write message to syslog */
	if (pool_config->logsyslog == 1)
	{
		va_start(ap, fmt);
		vsyslog(pool_config->syslog_facility | LOG_DEBUG, fmt, ap);
		va_end(ap);
		POOL_SETMASK(&oldmask);
		return;
	}

#ifdef HAVE_ASPRINTF
	len = asprintf(&fmt2, "%s %s\n", optstring(1), fmt);

	if (len >= 0 && fmt2)
	{
		va_start(ap, fmt);
		vfprintf(stderr, fmt2, ap);
		va_end(ap);
		fflush(stderr);
		free(fmt2);
	}
#else
	fprintf(stderr, "%s %s", optstring(1));

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, "\n");
#endif

	POOL_SETMASK(&oldmask);
}
Ejemplo n.º 3
0
static int
init_cb(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) {
	lua_State *L = l->L;
	l->ctx = ctx;
	lua_gc(L, LUA_GCSTOP, 0);
	lua_pushboolean(L, 1);  /* signal for libraries to ignore env. vars. */
	lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
	luaL_openlibs(L);
	lua_pushlightuserdata(L, ctx);
	lua_setfield(L, LUA_REGISTRYINDEX, "skynet_context");
	luaL_requiref(L, "skynet.codecache", codecache , 0);
	lua_pop(L,1);

	const char *path = optstring(ctx, "lua_path","./lualib/?.lua;./lualib/?/init.lua");
	lua_pushstring(L, path);
	lua_setglobal(L, "LUA_PATH");
	const char *cpath = optstring(ctx, "lua_cpath","./luaclib/?.so");
	lua_pushstring(L, cpath);
	lua_setglobal(L, "LUA_CPATH");
	const char *service = optstring(ctx, "luaservice", "./service/?.lua");
	lua_pushstring(L, service);
	lua_setglobal(L, "LUA_SERVICE");
	const char *preload = skynet_command(ctx, "GETENV", "preload");
	lua_pushstring(L, preload);
	lua_setglobal(L, "LUA_PRELOAD");

	lua_pushcfunction(L, traceback);
	assert(lua_gettop(L) == 1);

	const char * loader = optstring(ctx, "lualoader", "./lualib/loader.lua");

	int r = luaL_loadfile(L,loader);
	if (r != LUA_OK) {
		skynet_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1));
		report_launcher_error(ctx);
		return 1;
	}
	lua_pushlstring(L, args, sz);
	r = lua_pcall(L,1,0,1);
	if (r != LUA_OK) {
		skynet_error(ctx, "lua loader error : %s", lua_tostring(L, -1));
		report_launcher_error(ctx);
		return 1;
	}
	lua_settop(L,0);
	if (lua_getfield(L, LUA_REGISTRYINDEX, "memlimit") == LUA_TNUMBER) {
		size_t limit = lua_tointeger(L, -1);
		l->mem_limit = limit;
		skynet_error(ctx, "Set memory limit to %.2f M", (float)limit / (1024 * 1024));
		lua_pushnil(L);
		lua_setfield(L, LUA_REGISTRYINDEX, "memlimit");
	}
	lua_pop(L, 1);

	lua_gc(L, LUA_GCRESTART, 0);

	return 0;
}
Ejemplo n.º 4
0
int
main(int argc, char *argv[]) {
	const char * config_file = "config";
	if (argc > 1) {
		config_file = argv[1];
	}
	skynet_env_init();

	sigign();

	struct skynet_config config;

	// lua 相关的一些初始化
	struct lua_State *L = luaL_newstate();
	luaL_openlibs(L);	// link lua lib
	lua_close(L);

	L = luaL_newstate();

	int err = luaL_dofile(L, config_file);
	if (err) {
		fprintf(stderr,"%s\n",lua_tostring(L,-1));
		lua_close(L);
		return 1;
	} 

	// 初始化 lua 环境
	_init_env(L);

	const char *path = optstring("lua_path","./lualib/?.lua;./lualib/?/init.lua");
	setenv("LUA_PATH",path,1);

	const char *cpath = optstring("lua_cpath","./luaclib/?.so");
	setenv("LUA_CPATH",cpath,1);
	optstring("luaservice","./service/?.lua");

	// 加载配置项
	config.thread =  optint("thread",8);
	config.module_path = optstring("cpath","./service/?.so");
	config.logger = optstring("logger",NULL);
	config.harbor = optint("harbor", 1);
	config.master = optstring("master","127.0.0.1:2012");
	config.start = optstring("start","main.lua");
	config.local = optstring("address","127.0.0.1:2525");
	config.standalone = optstring("standalone",NULL);

	lua_close(L);

	skynet_start(&config);

	printf("skynet exit\n");

	return 0;
}
Ejemplo n.º 5
0
/***
Get value of environment variable, or _all_ variables.
@function getenv
@see getenv(3)
@string[opt] name if nil, get all
@return value if name given, otherwise a name-indexed table of values.
@usage for a,b in pairs(posix.getenv()) do print(a, b) end
*/
static int
Pgetenv(lua_State *L)
{
	checknargs(L, 1);
	if (lua_isnoneornil(L, 1))
	{
		extern char **environ;
		char **e;
		lua_newtable(L);
		for (e=environ; *e!=NULL; e++)
		{
			char *s=*e;
			char *eq=strchr(s, '=');
			if (eq==NULL)		/* will this ever happen? */
			{
				lua_pushstring(L, s);
				lua_pushboolean(L, 1);
			}
			else
			{
				lua_pushlstring(L, s, eq-s);
				lua_pushstring(L, eq+1);
			}
			lua_settable(L, -3);
		}
	}
	else
		lua_pushstring(L, getenv(optstring(L, 1,
			"lua_isnoneornil prevents this happening")));
	return 1;
}
Ejemplo n.º 6
0
int
main(int argc, char *argv[]) {
	const char * config_file = NULL ;
	if (argc > 1) {
		config_file = argv[1];
	} else {
		fprintf(stderr, "Need a config file.\n");
		return 1;
	}
	server_globalinit();
	server_env_init();

	sigign();
	server_pid = (int)getpid();

	struct server_config config;

	struct lua_State *L = lua_newstate(server_lalloc, NULL);
	luaL_openlibs(L);

	int r = luaL_loadfile(L, config_file);
	if (r) {
		fprintf(stderr,"luaL_loadfile err:%s\n",lua_tostring(L,-1));
		lua_close(L);
		return 1;
	} 
	int err = lua_pcall(L,0,1,0);
	if (err) {
		fprintf(stderr,"lua_pcall config file err:%s\n",lua_tostring(L,-1));
		lua_close(L);
		return 1;
	} 
	_init_env(L);

	config.harbor = optint("harbor", 1);
	config.thread =  optint("thread",8);
	config.logger = optstring("logger", NULL);
	config.bootstrap = optstring("bootstrap","snlua bootstrap");
	config.module_path = optstring("cpath","./cservice/?.so");

	lua_close(L);
	
	//启动服务
	server_start(&config);
	
	return 0;
}
Ejemplo n.º 7
0
int
main(int argc, char *argv[]) {
	const char * config_file = "config";
	if (argc > 1) {
		config_file = argv[1];
	}
	skynet_globalinit();
	skynet_env_init();

#if !defined(__WIN32__)
	sigign();
#endif

	struct skynet_config config;

	struct lua_State *L = lua_newstate(skynet_lalloc, NULL);
	luaL_openlibs(L);	// link lua lib
	lua_close(L);

	L = luaL_newstate();

	int err = luaL_dofile(L, config_file);
	if (err) {
		fprintf(stderr,"%s\n",lua_tostring(L,-1));
		lua_close(L);
		return 1;
	} 
	_init_env(L);

#ifdef LUA_CACHELIB
  printf("Skynet lua code cache enable\n");
#endif

	config.thread =  optint("thread",8);
	config.module_path = optstring("cpath","./cservice/?.dll");
	config.harbor = optint("harbor", 1);
	config.bootstrap = optstring("bootstrap","snlua bootstrap");

	lua_close(L);

	skynet_start(&config);
	skynet_globalexit();

	printf("skynet exit\n");

	return 0;
}
Ejemplo n.º 8
0
float float_from_optarg(const char *optarg) {
    // Check at most the first 8 characters are numerical
    std::string optstring(optarg);
    std::string string_float = optstring.substr(0, 8);
    for (std::string::iterator it = string_float.begin(); it != string_float.end(); ++it) {
        if (!isdigit(*it) && (*it) != '.') {
            fprintf(stderr, "This doesn't look like a usable float: %s\n", optarg);
            exit(1);
        }
    }
    return std::atof(string_float.c_str());
}
Ejemplo n.º 9
0
Archivo: lua.c Proyecto: zhoukk/routine
static int _launch(struct pixel *ctx, struct lua *lua, const char *param) {
	const char *loader, *path, *cpath, *service_path;
	lua_State *L = lua->L;
	lua->ctx = ctx;
	lua_gc(L, LUA_GCSTOP, 0);
	luaL_openlibs(L);
	lua_pushlightuserdata(L, ctx);
	lua_setfield(L, LUA_REGISTRYINDEX, "pixel");
	luaL_requiref(L, "pixel.c", pixel_lua, 0);
	luaL_requiref(L, "pixel.serial", pixel_serial, 0);
	lua_settop(L, 0);
	path = optstring(ctx, "lua_path", "./?.lua;./lualib/?.lua");
	lua_pushstring(L, path);
	lua_setglobal(L, "LUA_PATH");
	cpath = optstring(ctx, "lua_cpath", "./?.so");
	lua_pushstring(L, cpath);
	lua_setglobal(L, "LUA_CPATH");
	service_path = optstring(ctx, "lua_service", "./service/?.lua");
	lua_pushstring(L, service_path);
	lua_setglobal(L, "LUA_SERVICE");
	lua_pushcfunction(L, traceback);
	assert(lua_gettop(L) == 1);
	loader = optstring(ctx, "lua_loader", "./lualib/loader.lua");
	if (LUA_OK != luaL_loadfile(L, loader)) {
		pixel_log(ctx, "luaL_loadfile %s\n", lua_tostring(L, -1));
		return -1;
	}
	lua_pushstring(L, param);
	if (LUA_OK != lua_pcall(L, 1, 0, 1)) {
		pixel_log(ctx, "lua loader %s\n", lua_tostring(L, -1));
		return -1;
	}
	lua_settop(L, 0);
	lua_gc(L, LUA_GCRESTART, 0);
	return 0;
}
Ejemplo n.º 10
0
/***
Set an environment variable for this process.
(Child processes will inherit this)
@function setenv
@string name
@string[opt] value (maybe nil, meaning 'unset')
@param[opt] overwrite non-nil prevents overwriting a variable
@treturn[1] int `0`, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see setenv(3)
*/
static int
Psetenv(lua_State *L)
{
	const char *name=luaL_checkstring(L, 1);
	const char *value=optstring(L, 2, NULL);
	checknargs(L, 3);
	if (value==NULL)
	{
		unsetenv(name);
		return pushresult(L, 0, NULL);
	}
	else
	{
		int overwrite=lua_isnoneornil(L, 3) || lua_toboolean(L, 3);
		return pushresult(L, setenv(name,value,overwrite), NULL);
	}
}
Ejemplo n.º 11
0
/***
Check real user's permissions for a file.
@function access
@string path file to act on
@string[opt="f"] mode can contain 'r','w','x' and 'f'
@treturn[1] int `0`, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see access(2)
@usage status, errstr, errno = P.access("/etc/passwd", "rw")
*/
static int
Paccess(lua_State *L)
{
	int mode=F_OK;
	const char *path=luaL_checkstring(L, 1);
	const char *s;
	checknargs(L, 2);
	for (s=optstring(L, 2, "f"); *s!=0 ; s++)
		switch (*s)
		{
			case ' ': break;
			case 'r': mode |= R_OK; break;
			case 'w': mode |= W_OK; break;
			case 'x': mode |= X_OK; break;
			case 'f': mode |= F_OK; break;
			default: badoption(L, 2, "mode", *s); break;
		}
	return pushresult(L, access(path, mode), path);
}
Ejemplo n.º 12
0
/***
Iterator over all files in named directory.
@function files
@string[opt="."] path directory to act on
@return an iterator
*/
static int
Pfiles(lua_State *L)
{
	const char *path = optstring(L, 1, ".");
	DIR **d;
	checknargs(L, 1);
	d = (DIR **)lua_newuserdata(L, sizeof(DIR *));
	if (luaL_newmetatable(L, PACKAGE_NAME " dir handle"))
	{
		lua_pushcfunction(L, dir_gc);
		lua_setfield(L, -2, "__gc");
	}
	lua_setmetatable(L, -2);
	*d = opendir(path);
	if (*d == NULL)
		return pusherror(L, path);
	lua_pushcclosure(L, aux_files, 1);
	return 1;
}
Ejemplo n.º 13
0
int Dbck_options::
set_opts(int argc, char **argv)
{
	int c;
	char *opts = optstring();

	while (-1 != (c = getopt(argc,(char **)argv,opts))) {
		if (!set_option(c, optarg)) {
			//print help
			return 0;
		}
	}
	_dbdirectories = new _Tt_string_list;
	for (; optind<argc; ++optind) {
		_Tt_string s(argv[optind]);
		_dbdirectories->append(s);
	}
	return 1;
}
Ejemplo n.º 14
0
/***
Find all files in this directory matching a shell pattern.
@function glob
@string[opt="*"] pat shell glob pattern
@treturn table matching filenames
@see glob(3)
@see glob.lua
*/
static int
Pglob(lua_State *L)
{
	const char *pattern = optstring(L, 1, "*");
	glob_t globres;

	checknargs(L, 1);
	if (glob(pattern, 0, NULL, &globres))
		return pusherror(L, pattern);
	else
	{
		unsigned int i;
		lua_newtable(L);
		for (i=1; i<=globres.gl_pathc; i++)
		{
			lua_pushstring(L, globres.gl_pathv[i-1]);
			lua_rawseti(L, -2, i);
		}
		globfree(&globres);
		return 1;
	}
}
Ejemplo n.º 15
0
/***
Contents of directory.
@function dir
@string[opt="."] path directory to act on
@treturn table contents of *path*
@see dir.lua
*/
static int
Pdir(lua_State *L)
{
	const char *path = optstring(L, 1, ".");
	DIR *d;
	checknargs(L, 1);
	d = opendir(path);
	if (d == NULL)
		return pusherror(L, path);
	else
	{
		int i;
		struct dirent *entry;
		lua_newtable(L);
		for (i=1; (entry = readdir(d)) != NULL; i++)
		{
			lua_pushstring(L, entry->d_name);
			lua_rawseti(L, -2, i);
		}
		closedir(d);
		lua_pushinteger(L, i-1);
		return 2;
	}
}
Ejemplo n.º 16
0
int
main(int argc, char *argv[]) {
#if defined(__WIN32__)
	WSADATA WSAData;

	if(WSAStartup(MAKEWORD(2, 2), &WSAData))//初始化
	{
		printf("initializationing error!\n");
		WSACleanup();
		exit(0);
	}
#endif

	const char * config_file = "config";
	if (argc > 1) {
		config_file = argv[1];
	}
	skynet_globalinit();
	skynet_env_init();

#if !defined(__WIN32__)
	sigign();
#endif

	struct skynet_config config;

	struct lua_State *L = lua_newstate(skynet_lalloc, NULL);
	luaL_openlibs(L);	// link lua lib
	lua_close(L);

	L = luaL_newstate();

	int err = luaL_dofile(L, config_file);
	if (err) {
		fprintf(stderr,"%s\n",lua_tostring(L,-1));
		lua_close(L);
		return 1;
	} 
	_init_env(L);

#ifdef LUA_CACHELIB
  printf("Skynet lua code cache enable\n");
#endif

	config.thread =  optint("thread",8);
	config.module_path = optstring("cpath","./cservice/?.so");
	config.logger = optstring("logger",NULL);
	config.harbor = optint("harbor", 1);
	config.master = optstring("master","127.0.0.1:2012");
	config.bootstrap = optstring("bootstrap","snlua bootstrap");
	config.local = optstring("address","127.0.0.1:2525");
	config.standalone = optstring("standalone",NULL);

	lua_close(L);

	skynet_start(&config);
	skynet_globalexit();

	printf("skynet exit\n");

	return 0;
}
Ejemplo n.º 17
0
/***
Network address and service translation.
@function getaddrinfo
@string host name of a host
@string service name of service
@tparam[opt] PosixAddrInfo hints table
@treturn[1] list of @{sockaddr} tables
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see getaddrinfo(2)
@usage
local res, errmsg, errcode = posix.getaddrinfo ("www.lua.org", "http",
  { family = P.IF_INET, socktype = P.SOCK_STREAM }
)
*/
static int
Pgetaddrinfo(lua_State *L)
{
	int n = 1;
	const char *host = optstring(L, 1, NULL);
	const char *service = NULL;
	struct addrinfo *res, hints;
	hints.ai_family = PF_UNSPEC;

	checknargs(L, 3);

	switch (lua_type(L, 2))
	{
		case LUA_TNONE:
		case LUA_TNIL:
			if (host == NULL)
				argtypeerror(L, 2, "string or int");
			break;
		case LUA_TNUMBER:
		case LUA_TSTRING:
			service = lua_tostring(L, 2);
			break;
		default:
			argtypeerror(L, 2, "string, int or nil");
			break;
	}

	switch (lua_type(L, 3))
	{
		case LUA_TNONE:
		case LUA_TNIL:
			break;
		case LUA_TTABLE:
			checkfieldnames (L, 3, Sai_fields);
			hints.ai_family   = optintfield(L, 3, "family", PF_UNSPEC);
			hints.ai_socktype = optintfield(L, 3, "socktype", 0);
			hints.ai_protocol = optintfield(L, 3, "protocol", 0);
			hints.ai_flags    = optintfield(L, 3, "flags", 0);
			break;
		default:
			argtypeerror(L, 3, "table or nil");
			break;
	}

	{
		int r;
		if ((r = getaddrinfo(host, service, &hints, &res)) != 0)
		{
			lua_pushnil(L);
			lua_pushstring(L, gai_strerror(r));
			lua_pushinteger(L, r);
			return 3;
		}
	}

	/* Copy getaddrinfo() result into Lua table */
	{
		struct addrinfo *p;
		lua_newtable(L);
		for (p = res; p != NULL; p = p->ai_next)
		{
			lua_pushnumber(L, n++);
			pushsockaddrinfo(L, p->ai_family, p->ai_addr);
			pushnumberfield("socktype",  p->ai_socktype);
			pushstringfield("canonname", p->ai_canonname);
			pushnumberfield("protocol",  p->ai_protocol);
			lua_settable(L, -3);
		}
	}
	freeaddrinfo(res);


	return 1;
}
Ejemplo n.º 18
0
//lua 状态机的初始化
static int
_init(struct snlua *l, struct server_context *ctx, const char * args, size_t sz) {
	lua_State *L = l->L;
	l->ctx = ctx;
	lua_gc(L, LUA_GCSTOP, 0);//停止gc
	luaL_openlibs(L);//打开相关库
	
	//注册ctx到lua注册表
	lua_pushlightuserdata(L, ctx);
	lua_setfield(L, LUA_REGISTRYINDEX, "server_context");//lua_setfield:做一个等价于 t[k] = v 的操作, 这里 t 是给出的有效索引 index 处的值, 而 v 是栈顶的那个值

	//设置全局变量
	const char *path = optstring(ctx, "lua_path","./lualib/?.lua;./lualib/?/init.lua");
	lua_pushstring(L, path);
	lua_setglobal(L, "LUA_PATH");

	const char *cpath = optstring(ctx, "lua_cpath","./luaclib/?.so");
	lua_pushstring(L, cpath);
	lua_setglobal(L, "LUA_CPATH");
	
	const char *service = optstring(ctx, "luaservice", "./service/?.lua");
	lua_pushstring(L, service);
	lua_setglobal(L, "LUA_SERVICE");
	
	const char *preload = server_cmd_command(ctx, "GETENV", "preload");
	lua_pushstring(L, preload);
	lua_setglobal(L, "LUA_PRELOAD");

	lua_pushcfunction(L, traceback);
	assert(lua_gettop(L) == 1);

	//载入首个lua文件,生成chunk到栈顶
	const char * loader = optstring(ctx, "lualoader", "./lualib/loader.lua");
	int r = luaL_loadfile(L, loader);
	if (r != LUA_OK) {
		server_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1));
		return 1;
	}
	lua_pushlstring(L, args, sz);
	/*
		lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
		nargs:传入参数个数
		nresults:需要返回参数个数
		errfunc:
			0 返回原始错误信息
				lua_errrun:运行时错误。
				lua_errmem:内存分配错误。对于此类错误,lua并不调用错误处理函数。
				lua_errerr:运行时错误处理函数误差。
			非0 即处理错误信息函数所在当前栈的位置,如上面执行了lua_pushcfunction(L, traceback);所以errfunc应该为1
	*/
	r = lua_pcall(L,1,0,1);//执行loader.lua
	if (r != LUA_OK) {
		server_error(ctx, "lua loader error : %s", lua_tostring(L, -1));
		return 1;
	}

	//把栈上所有元素移除
	lua_settop(L,0);

	//重启gc
	lua_gc(L, LUA_GCRESTART, 0);

	return 0;
}