Example #1
0
/* init */
rstatus_t
script_init(struct server_pool *pool, const char *lua_path)
{
    lua_State *L;

    L = luaL_newstate();                        /* Create Lua state variable */
    pool->L = L;
    luaL_openlibs(L);                           /* Load Lua libraries */
    char path[MAX_PATH_LEN] = {'\0'};
    char *lua_name = "redis.lua";

    strcat(path, lua_path);
    strcat(path, "/");
    strcat(path, lua_name);

    set_lua_path(L, lua_path);

    if (luaL_loadfile(L, path)) {
        log_debug(LOG_VERB, "init lua script failed - %s", lua_tostring(L, -1));
        return NC_ERROR;
    }

    lua_getglobal(L, "string");
    luaL_register(L, 0, stringext);
    lua_setglobal(L, "string");

    lua_pushlightuserdata(L, pool);
    lua_setglobal(L, "__pool");

    if (lua_pcall(L, 0, 0, 0) != 0) {
        log_error("call lua script failed - %s", lua_tostring(L, -1));
    }

    return NC_OK;
}
Example #2
0
int
config_gatekeeper(const char *lua_base_dir, const char *gatekeeper_config_file)
{
	int ret;
	char lua_entry_path[128];
	lua_State *lua_state;

	ret = snprintf(lua_entry_path, sizeof(lua_entry_path), \
			"%s/%s", lua_base_dir, gatekeeper_config_file);
	RTE_VERIFY(ret > 0 && ret < (int)sizeof(lua_entry_path));

	lua_state = luaL_newstate();
	if (!lua_state) {
		G_LOG(ERR, "config: failed to create new Lua state\n");
		return -1;
	}

	luaL_openlibs(lua_state);
	luaL_register(lua_state, "staticlib", staticlib);
	set_lua_path(lua_state, lua_base_dir);
	ret = luaL_loadfile(lua_state, lua_entry_path);
	if (ret != 0) {
		G_LOG(ERR, "config: %s\n", lua_tostring(lua_state, -1));
		ret = -1;
		goto out;
	}

	/*
	 * Calls a function in protected mode.
	 * int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
	 * @nargs: the number of arguments that you pushed onto the stack.
	 * @nresults: the number of results that the funtion will push onto
	 * the stack.
	 * @errfunc: if "0", it represents the error message returned on
	 * the stack is exactly the original error message.
	 * Otherwise, it presents the index of the error handling function.
	 */
	ret = lua_pcall(lua_state, 0, 0, 0);
	if (ret != 0) {
		G_LOG(ERR, "config: %s\n", lua_tostring(lua_state, -1));
		ret = -1;
		goto out;
	}

	/* Function to be called. */
	lua_getglobal(lua_state, "gatekeeper_init");
	ret = lua_pcall(lua_state, 0, 1, 0);
	if (ret != 0) {
		G_LOG(ERR, "config: %s\n", lua_tostring(lua_state, -1));
		ret = -1;
		goto out;
	}

	ret = luaL_checkinteger(lua_state, -1);
	if (ret < 0)
		G_LOG(ERR, "config: gatekeeper_init() return value is %d\n",
			ret);

out:
	lua_close(lua_state);
	return ret;
}