예제 #1
0
파일: luaif.c 프로젝트: OlegGirko/ldbox
/* Read string variables from lua.
 * Note that this function is exported from liblb.so (for lb-show etc): */
char *lb__read_string_variable_from_lua__(const char *name)
{
	struct lbcontext *lbif;
	char *cp;

	lbif = get_lbcontext_lua();
	cp = read_string_variable_from_lua(lbif, name);
	release_lbcontext(lbif);
	return(cp);
}
예제 #2
0
/* Read string variables from lua.
 * Note that this function is exported from libsb2.so (for sb2-show etc): */
char *sb2__read_string_variable_from_lua__(const char *name)
{
	struct lua_instance *luaif;
	char *cp;

	luaif = get_lua();
	cp = read_string_variable_from_lua(luaif, name);
	release_lua(luaif);
	return(cp);
}
예제 #3
0
/* Read string variables from lua.
 * Note that this function is exported from libsb2.so (for sb2-show etc): */
char *sb2__read_string_variable_from_lua__(const char *name)
{
	struct sb2context *sb2if;
	char *cp;

	sb2if = get_sb2context_lua();
	cp = read_string_variable_from_lua(sb2if, name);
	release_sb2context(sb2if);
	return(cp);
}
예제 #4
0
파일: luaif.c 프로젝트: OlegGirko/ldbox
void lbcontext_initialize_lua(struct lbcontext *lbctx)
{
	char *main_lua_script = NULL;
	char *lua_if_version = NULL;

	/* return immediately if already been here */
	if (!lbctx || lbctx->lua) return;

	if (asprintf(&main_lua_script, "%s/lua_scripts/main.lua",
	     ldbox_session_dir) < 0) {
		LB_LOG(LB_LOGLEVEL_ERROR,
			"lbcontext_initialize_lua: asprintf failed to allocate memory");
		return;
	}
		
	LB_LOG(LB_LOGLEVEL_INFO, "Loading '%s'", main_lua_script);

	lbctx->lua = luaL_newstate();
	lua_atpanic(lbctx->lua, lb_lua_panic);

	disable_mapping(lbctx);
	luaL_openlibs(lbctx->lua);
	lua_bind_lb_functions(lbctx->lua); /* register our lb_ functions */
	lua_bind_ruletree_functions(lbctx->lua); /* register our ruletree_ functions */
	lua_bind_lblib_functions(lbctx->lua); /* register lblib.* functions */

	load_and_execute_lua_file(lbctx, main_lua_script);

	enable_mapping(lbctx);

	/* check Lua/C interface version. */
	lua_if_version = read_string_variable_from_lua(lbctx,
		"lb_lua_c_interface_version");
	if (!lua_if_version) {
		LB_LOG(LB_LOGLEVEL_ERROR, "FATAL ERROR: "
			"lb's Lua scripts didn't provide"
			" 'lb_lua_c_interface_version' identifier!");
		exit(1);
	}
	if (strcmp(lua_if_version, LB_LUA_C_INTERFACE_VERSION)) {
		LB_LOG(LB_LOGLEVEL_ERROR, "FATAL ERROR: "
			"lb's Lua script interface version mismatch:"
			" scripts provide '%s', but '%s' was expected",
			lua_if_version, LB_LUA_C_INTERFACE_VERSION);
		exit(1);
	}
	free(lua_if_version);

	LB_LOG(LB_LOGLEVEL_INFO, "lua initialized.");
	LB_LOG(LB_LOGLEVEL_NOISE, "gettop=%d", lua_gettop(lbctx->lua));

	free(main_lua_script);
}
예제 #5
0
static struct lua_instance *alloc_lua(void)
{
	struct lua_instance *tmp;
	char *main_lua_script = NULL;
	char *lua_if_version = NULL;

	if (pthread_getspecific_fnptr) {
		tmp = (*pthread_getspecific_fnptr)(lua_key);
		if (tmp != NULL) {
			SB_LOG(SB_LOGLEVEL_DEBUG,
				"alloc_lua: already done (pt-getspec.)");
			return(tmp);
		}
	} else if (my_lua_instance) {
		SB_LOG(SB_LOGLEVEL_DEBUG,
			"alloc_lua: already done (has my_lua_instance)");
		return(my_lua_instance);
	}

	tmp = malloc(sizeof(struct lua_instance));
	if (!tmp) {
		SB_LOG(SB_LOGLEVEL_ERROR,
			"alloc_lua: Failed to allocate memory");
		return(NULL);
	}
	memset(tmp, 0, sizeof(struct lua_instance));

	if (pthread_setspecific_fnptr) {
		(*pthread_setspecific_fnptr)(lua_key, tmp);
	} else {
		my_lua_instance = tmp;
	}
	
	if (!sbox_session_dir || !*sbox_session_dir) {
		SB_LOG(SB_LOGLEVEL_ERROR,
			"alloc_lua: no SBOX_SESSION_DIR");
		return(NULL); /* can't live without a session */
	}
	sbox_session_dir = strdup(sbox_session_dir);

	if (asprintf(&main_lua_script, "%s/lua_scripts/main.lua",
	     sbox_session_dir) < 0) {
		SB_LOG(SB_LOGLEVEL_ERROR,
			"alloc_lua: asprintf failed to allocate memory");
		return(NULL);
	}
		
	SB_LOG(SB_LOGLEVEL_DEBUG, "Loading '%s'", main_lua_script);

	tmp->lua = luaL_newstate();

	disable_mapping(tmp);
	luaL_openlibs(tmp->lua);
	lua_bind_sb_functions(tmp->lua); /* register our sb_ functions */

	load_and_execute_lua_file(tmp, main_lua_script);

	enable_mapping(tmp);

	/* check Lua/C interface version. */
	lua_if_version = read_string_variable_from_lua(tmp,
		"sb2_lua_c_interface_version");
	if (!lua_if_version) {
		SB_LOG(SB_LOGLEVEL_ERROR, "FATAL ERROR: "
			"sb2's Lua scripts didn't provide"
			" 'sb2_lua_c_interface_version' identifier!");
		exit(1);
	}
	if (strcmp(lua_if_version, SB2_LUA_C_INTERFACE_VERSION)) {
		SB_LOG(SB_LOGLEVEL_ERROR, "FATAL ERROR: "
			"sb2's Lua script interface version mismatch:"
			" scripts provide '%s', but '%s' was expected",
			lua_if_version, SB2_LUA_C_INTERFACE_VERSION);
		exit(1);
	}
	free(lua_if_version);

	SB_LOG(SB_LOGLEVEL_DEBUG, "lua initialized.");
	SB_LOG(SB_LOGLEVEL_NOISE, "gettop=%d", lua_gettop(tmp->lua));

	free(main_lua_script);
	return(tmp);
}