Updated Branches: refs/heads/lua_config f3830380f -> 557d1a24f
Run it through indent with our indentation rules Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/557d1a24 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/557d1a24 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/557d1a24 Branch: refs/heads/lua_config Commit: 557d1a24f307e48639d53a0a35c07f5ad1ec37dc Parents: f383038 Author: Leif Hedstrom <zw...@apache.org> Authored: Tue Jan 14 20:49:06 2014 -0700 Committer: Leif Hedstrom <zw...@apache.org> Committed: Tue Jan 14 20:49:06 2014 -0700 ---------------------------------------------------------------------- iocore/net/SSLUtils.cc | 16 ++- proxy/luaConfig.cc | 299 +++++++++++++++++++++++++------------------- proxy/luaConfig.h | 43 ++++--- 3 files changed, 201 insertions(+), 157 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/557d1a24/iocore/net/SSLUtils.cc ---------------------------------------------------------------------- diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc index 9cd6700..f77287e 100644 --- a/iocore/net/SSLUtils.cc +++ b/iocore/net/SSLUtils.cc @@ -784,10 +784,12 @@ ssl_extract_certificate( } static int -SSLUtils_lua_ssl_store_ssl_context(lua_State *L) { +SSLUtils_lua_ssl_store_ssl_context(lua_State* L) +{ const SSLConfigParams * params; SSLCertLookup * lookup; xptr<char> dest_ip, ssl_key_name, ssl_ca_name, ssl_cert_name, ticket_key_filename; + params = (const SSLConfigParams *) lua_touserdata(L, lua_upvalueindex(1)); lookup = (SSLCertLookup *) lua_touserdata(L, lua_upvalueindex(2)); #define LUAGETF(name) do { \ @@ -800,7 +802,7 @@ SSLUtils_lua_ssl_store_ssl_context(lua_State *L) { LUAGETF(ssl_ca_name); LUAGETF(ssl_cert_name); // TODO: ticket_key_filename stuff should be popped here I think - if(!ssl_store_ssl_context(params, lookup, dest_ip, ssl_cert_name, ssl_ca_name, ssl_key_name, -1, ticket_key_filename)) + if (!ssl_store_ssl_context(params, lookup, dest_ip, ssl_cert_name, ssl_ca_name, ssl_key_name, -1, ticket_key_filename)) lua_pushboolean(L,0); else lua_pushboolean(L,1); @@ -808,19 +810,15 @@ SSLUtils_lua_ssl_store_ssl_context(lua_State *L) { } void -SSLUtils_lua_ssl_context( - lua_State * L, - const SSLConfigParams * params, - SSLCertLookup * lookup) { +SSLUtils_lua_ssl_context(lua_State* L, const SSLConfigParams* params, SSLCertLookup*lookup) +{ lua_pushlightuserdata(L, (void *)params); lua_pushlightuserdata(L, (void *)lookup); lua_pushcclosure(L, SSLUtils_lua_ssl_store_ssl_context, 2); } bool -SSLParseCertificateConfiguration( - const SSLConfigParams * params, - SSLCertLookup * lookup) +SSLParseCertificateConfiguration(const SSLConfigParams* params, SSLCertLookup* lookup) { char * tok_state = NULL; char * line = NULL; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/557d1a24/proxy/luaConfig.cc ---------------------------------------------------------------------- diff --git a/proxy/luaConfig.cc b/proxy/luaConfig.cc index 7dd845d..7354ca4 100644 --- a/proxy/luaConfig.cc +++ b/proxy/luaConfig.cc @@ -32,27 +32,34 @@ luaConfig globalLuaConfig("tsconfig"); static int -tsrec_newindex_func(lua_State *L) { +tsrec_newindex_func(lua_State * L) +{ luaL_error(L, "cannot assign to ats.config_t, invoked instead"); return 0; } static int -tsrec_index_func(lua_State *L) { +tsrec_index_func(lua_State * L) +{ const char *opath = NULL; char path[1024]; assert(lua_gettop(L) == 2); - if(!strcmp(lua_tostring(L,2),"_path")) return 0; - lua_pushvalue(L,2); - lua_rawget(L,1); - if(lua_istable(L,-1)) return 1; - lua_pop(L,1); - - lua_pushstring(L,"_path"); - lua_rawget(L,1); - if(lua_isstring(L,-1)) opath = lua_tostring(L,-1); - if(opath == NULL) snprintf(path, sizeof(path), "%s", lua_tostring(L,2)); - else snprintf(path, sizeof(path), "%s.%s", opath, lua_tostring(L,2)); + if (!strcmp(lua_tostring(L, 2), "_path")) + return 0; + lua_pushvalue(L, 2); + lua_rawget(L, 1); + if (lua_istable(L, -1)) + return 1; + lua_pop(L, 1); + + lua_pushstring(L, "_path"); + lua_rawget(L, 1); + if (lua_isstring(L, -1)) + opath = lua_tostring(L, -1); + if (opath == NULL) + snprintf(path, sizeof(path), "%s", lua_tostring(L, 2)); + else + snprintf(path, sizeof(path), "%s.%s", opath, lua_tostring(L, 2)); lua_newtable(L); lua_pushstring(L, path); @@ -61,18 +68,19 @@ tsrec_index_func(lua_State *L) { luaL_getmetatable(L, "ats.config_t"); lua_setmetatable(L, -2); - lua_pushvalue(L,2); /* requested name */ - lua_pushvalue(L,-2); - lua_rawset(L,1); + lua_pushvalue(L, 2); /* requested name */ + lua_pushvalue(L, -2); + lua_rawset(L, 1); return 1; } static int -tsrec_next_record(lua_State *L) { +tsrec_next_record(lua_State * L) +{ RecRecord *r = NULL; int *idx, i, num_records, upidx; upidx = lua_upvalueindex(1); - idx = (int *)lua_touserdata(L,upidx); + idx = (int *) lua_touserdata(L, upidx); i = *idx; ink_mutex_acquire(&g_rec_config_lock); @@ -84,15 +92,16 @@ tsrec_next_record(lua_State *L) { break; r = NULL; } - if(r != NULL) rec_mutex_acquire(&(r->lock)); + if (r != NULL) + rec_mutex_acquire(&(r->lock)); ink_mutex_release(&g_rec_config_lock); - *idx = i+1; + *idx = i + 1; - if(r != NULL) { + if (r != NULL) { lua_newtable(L); lua_pushstring(L, r->name); lua_setfield(L, -2, "_path"); - + luaL_getmetatable(L, "ats.config_t"); lua_setmetatable(L, -2); rec_mutex_release(&(r->lock)); @@ -102,13 +111,13 @@ tsrec_next_record(lua_State *L) { } static int -tsrec_dispatch_method(lua_State *L, const char *bpath, const char *method) { - if(!strcmp(method, "name")) { +tsrec_dispatch_method(lua_State * L, const char *bpath, const char *method) +{ + if (!strcmp(method, "name")) { lua_pushstring(L, bpath); return 1; - } - else if(!strcmp(method, "list")) { - int *idx = (int *)lua_newuserdata(L, sizeof(int)); + } else if (!strcmp(method, "list")) { + int *idx = (int *) lua_newuserdata(L, sizeof(int)); *idx = 0; lua_pushcclosure(L, tsrec_next_record, 1); return 1; @@ -118,109 +127,127 @@ tsrec_dispatch_method(lua_State *L, const char *bpath, const char *method) { } static int -tsrec_call_func(lua_State *L) { +tsrec_call_func(lua_State * L) +{ RecT rec_type; RecDataT data_type; const char *opath = NULL; int nargs = lua_gettop(L); - lua_pushstring(L,"_path"); - lua_rawget(L,1); - opath = lua_tostring(L,-1); - lua_pop(L,1); + lua_pushstring(L, "_path"); + lua_rawget(L, 1); + opath = lua_tostring(L, -1); + lua_pop(L, 1); - if(lua_istable(L,2)) { + if (lua_istable(L, 2)) { const char *bpath; - lua_pushstring(L,"_path"); - lua_rawget(L,2); - bpath = lua_tostring(L,-1); - if(bpath == NULL) return tsrec_dispatch_method(L,"",opath); + lua_pushstring(L, "_path"); + lua_rawget(L, 2); + bpath = lua_tostring(L, -1); + if (bpath == NULL) + return tsrec_dispatch_method(L, "", opath); size_t blen = strlen(bpath); - lua_pop(L,1); - if(blen > strlen(opath) || - strncmp(bpath, opath, blen) || - opath[blen] != '.') { + lua_pop(L, 1); + if (blen > strlen(opath) || strncmp(bpath, opath, blen) || opath[blen] != '.') { luaL_error(L, "impossible method call: %s", opath + blen + 1); } - return tsrec_dispatch_method(L,bpath,opath + blen + 1); + return tsrec_dispatch_method(L, bpath, opath + blen + 1); } - if(RecGetRecordType(opath, &rec_type)) + if (RecGetRecordType(opath, &rec_type)) luaL_error(L, "Could not find record type '%s'", opath); - if(RecGetRecordDataType(opath, &data_type)) + if (RecGetRecordDataType(opath, &data_type)) luaL_error(L, "Could not find record data type '%s'", opath); - if(nargs == 1) { + if (nargs == 1) { /* return the value */ - switch(data_type) { - case RECD_INT: - RecInt v_int; - RecGetRecordInt(opath, &v_int); - lua_pushinteger(L,v_int); - return 1; - break; - case RECD_FLOAT: - RecFloat v_float; - RecGetRecordFloat(opath, &v_float); - lua_pushnumber(L,v_float); - return 1; - break; - case RECD_STRING: - RecString v_string; - RecGetRecordString_Xmalloc(opath, &v_string); - lua_pushstring(L, v_string); - ats_free(v_string); - return 1; - break; - case RECD_COUNTER: - RecCounter v_counter; - RecGetRecordCounter(opath, &v_counter); - lua_pushinteger(L,v_counter); - return 1; - break; - default: - luaL_error(L, "Unknown type of record: %s", opath); - break; - } - return 0; - } - - switch(data_type) { + switch (data_type) { case RECD_INT: - RecSetRecordInt(opath, (RecInt)luaL_checkint(L,2)); + RecInt v_int; + RecGetRecordInt(opath, &v_int); + lua_pushinteger(L, v_int); + return 1; break; case RECD_FLOAT: - RecSetRecordFloat(opath, (RecFloat)luaL_checknumber(L,2)); + RecFloat v_float; + RecGetRecordFloat(opath, &v_float); + lua_pushnumber(L, v_float); + return 1; break; case RECD_STRING: - RecSetRecordString(opath, (const RecString)lua_tostring(L,2)); + RecString v_string; + RecGetRecordString_Xmalloc(opath, &v_string); + lua_pushstring(L, v_string); + ats_free(v_string); + return 1; break; case RECD_COUNTER: - RecSetRecordCounter(opath, (RecCounter)luaL_checklong(L,2)); + RecCounter v_counter; + RecGetRecordCounter(opath, &v_counter); + lua_pushinteger(L, v_counter); + return 1; break; default: luaL_error(L, "Unknown type of record: %s", opath); break; + } + return 0; + } + + switch (data_type) { + case RECD_INT: + RecSetRecordInt(opath, (RecInt) luaL_checkint(L, 2)); + break; + case RECD_FLOAT: + RecSetRecordFloat(opath, (RecFloat) luaL_checknumber(L, 2)); + break; + case RECD_STRING: + RecSetRecordString(opath, (const RecString) lua_tostring(L, 2)); + break; + case RECD_COUNTER: + RecSetRecordCounter(opath, (RecCounter) luaL_checklong(L, 2)); + break; + default: + luaL_error(L, "Unknown type of record: %s", opath); + break; } return 0; } static int -lua_ats_log(lua_State *L) { +lua_ats_log(lua_State * L) +{ int type = lua_tointeger(L, lua_upvalueindex(1)); - switch(type) { - case 0: Error("%s", lua_tostring(L,1)); break; - case 1: Warning("%s", lua_tostring(L,1)); break; - case 2: Note("%s", lua_tostring(L,1)); break; - case 3: Status("%s", lua_tostring(L,1)); break; - case 4: Emergency("%s", lua_tostring(L,1)); break; - case 5: Fatal("%s", lua_tostring(L,1)); break; - case 6: Debug(lua_tostring(L,1), "%s", lua_tostring(L,2)); break; - default: - luaL_error(L, "unknown internal log type"); - break; + switch (type) { + case 0: + Error("%s", lua_tostring(L, 1)); + break; + case 1: + Warning("%s", lua_tostring(L, 1)); + break; + case 2: + Note("%s", lua_tostring(L, 1)); + break; + case 3: + Status("%s", lua_tostring(L, 1)); + break; + case 4: + Emergency("%s", lua_tostring(L, 1)); + break; + case 5: + Fatal("%s", lua_tostring(L, 1)); + break; + case 6: + Debug(lua_tostring(L, 1), "%s", lua_tostring(L, 2)); + break; + default: + luaL_error(L, "unknown internal log type"); + break; } return 0; } -void luaopen_ats(lua_State *L) { + +void +luaopen_ats(lua_State * L) +{ luaL_newmetatable(L, "ats.config_t"); lua_pushcclosure(L, tsrec_index_func, 0); lua_setfield(L, -2, "__index"); @@ -238,49 +265,55 @@ void luaopen_ats(lua_State *L) { luaL_getmetatable(L, "ats.config_t"); lua_setmetatable(L, -2); lua_setfield(L, -2, "config"); - - lua_newtable(L); + + lua_newtable(L); #define mkLog(id,name) do { \ lua_pushinteger(L,id); \ lua_pushcclosure(L, lua_ats_log, 1); \ lua_setfield(L, -2, #name); \ } while(0) - mkLog(0,error); - mkLog(1,warning); - mkLog(2,note); - mkLog(3,status); - mkLog(4,emergency); - mkLog(5,fatal); - mkLog(6,debug); + mkLog(0, error); + mkLog(1, warning); + mkLog(2, note); + mkLog(3, status); + mkLog(4, emergency); + mkLog(5, fatal); + mkLog(6, debug); lua_setfield(L, -2, "log"); } -void luaConfig::records() { +void +luaConfig::records() +{ int rv; lua_State *L = getL(); lua_getglobal(L, "tsconfig"); lua_getfield(L, -1, "config"); - if(lua_isnil(L,-1)) { - lua_pop(L,1); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); return; } rv = lua_pcall(L, 0, 1, 0); - if(rv != 0) { - ink_error("tsconfig.config() failed: %s\n", lua_tostring(L,-1)); + if (rv != 0) { + ink_error("tsconfig.config() failed: %s\n", lua_tostring(L, -1)); } - lua_pop(L,1); + lua_pop(L, 1); } -void drop_lua_state_holder(void *vls) { - struct luaConfigStateHolder *state_holder = (struct luaConfigStateHolder *)vls; +void +drop_lua_state_holder(void *vls) +{ + struct luaConfigStateHolder *state_holder = (struct luaConfigStateHolder *) vls; lua_close(state_holder->states[0]); lua_close(state_holder->states[1]); } -void luaConfig::boot() { +void +luaConfig::boot() +{ assert(state_holder.states[0] == NULL); assert(state_holder.states[1] == NULL); - char system_config_directory[PATH_NAME_MAX + 1]; // Layout->sysconfdir + char system_config_directory[PATH_NAME_MAX + 1]; // Layout->sysconfdir ink_strlcpy(system_config_directory, Layout::get()->sysconfdir, PATH_NAME_MAX); char config_file_path[PATH_NAME_MAX]; @@ -291,9 +324,11 @@ void luaConfig::boot() { setL(1, open(config_file_path, config_module)); } -lua_State *luaConfig::open(const char *config_file_path, const char *module) { +lua_State * +luaConfig::open(const char *config_file_path, const char *module) +{ lua_State *L = luaL_newstate(); - if(L == NULL) { + if (L == NULL) { printf("[TrafficServer] failed to initialized lua.\n"); exit(1); } @@ -302,15 +337,14 @@ lua_State *luaConfig::open(const char *config_file_path, const char *module) { lua_getglobal(L, "package"); lua_pushstring(L, config_file_path); lua_setfield(L, -2, "path"); - lua_pop(L,1); + lua_pop(L, 1); luaopen_ats(L); // UrlRewrite::luaopen(L); // TODO: Not available now - lua_getglobal(L,"require"); - lua_pushstring(L,module); - if(lua_pcall(L,1,1,0) != 0) { - printf("[TrafficServer] failed to run lua config '%s'\n%s", config_file_path, - lua_tostring(L,-1)); + lua_getglobal(L, "require"); + lua_pushstring(L, module); + if (lua_pcall(L, 1, 1, 0) != 0) { + printf("[TrafficServer] failed to run lua config '%s'\n%s", config_file_path, lua_tostring(L, -1)); exit(1); } lua_pop(L, lua_gettop(L)); @@ -318,23 +352,26 @@ lua_State *luaConfig::open(const char *config_file_path, const char *module) { } int -luaConfig::call(lua_State *L, const char *method, int nargs) { +luaConfig::call(lua_State * L, const char *method, int nargs) +{ lua_getglobal(L, "tsconfig"); lua_getfield(L, -1, method); - if(lua_isnil(L,-1)) { - lua_pop(L,2); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); return -1; } - lua_remove(L,-2); - lua_insert(L, 0 - (nargs+1)); - if(lua_pcall(L, nargs, 0, 0) != 0) { - Error("lua call(%s) failed: %s\n", method, lua_tostring(L,-1)); + lua_remove(L, -2); + lua_insert(L, 0 - (nargs + 1)); + if (lua_pcall(L, nargs, 0, 0) != 0) { + Error("lua call(%s) failed: %s\n", method, lua_tostring(L, -1)); return -1; } return 0; } -void luaConfigInit() { +void +luaConfigInit() +{ globalLuaConfig.boot(); globalLuaConfig.records(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/557d1a24/proxy/luaConfig.h ---------------------------------------------------------------------- diff --git a/proxy/luaConfig.h b/proxy/luaConfig.h index 28e0576..0492bbe 100644 --- a/proxy/luaConfig.h +++ b/proxy/luaConfig.h @@ -36,49 +36,58 @@ void luaConfigInit(); void drop_lua_state_holder(void *); -struct luaConfigStateHolder { +struct luaConfigStateHolder +{ public: - luaConfigStateHolder() : active(0), uses_remaining(0) { + luaConfigStateHolder():active(0), uses_remaining(0) + { states[0] = states[1] = NULL; } lua_State *states[2]; - int active; + int active; int64_t uses_remaining; }; class luaConfig { public: - luaConfig(const char *mod) : config_module(mod) { + luaConfig(const char *mod):config_module(mod) + { ink_thread_key_create(&state_key, drop_lua_state_holder); } void boot(); void records(); - inline lua_State *getL() { return getL(-1); } + inline lua_State *getL() + { + return getL(-1); + } int call(lua_State *, const char *method, int nargs); - inline int call(const char *method, int nargs) { + inline int call(const char *method, int nargs) + { return call(getL(), method, nargs); } private: - lua_State *open(const char *path, const char *module); - inline void setL(int which, lua_State *L) { + lua_State * open(const char *path, const char *module); + inline void setL(int which, lua_State * L) + { assert(which >= 0 && which < 2); struct luaConfigStateHolder *state_holder; - state_holder = (struct luaConfigStateHolder *)ink_thread_getspecific(state_key); - if(state_holder == NULL) { - state_holder = new struct luaConfigStateHolder(); - ink_thread_setspecific(state_key, (void *)state_holder); + state_holder = (struct luaConfigStateHolder *) ink_thread_getspecific(state_key); + if (state_holder == NULL) { + state_holder = new struct luaConfigStateHolder (); + ink_thread_setspecific(state_key, (void *) state_holder); } state_holder->states[which] = L; } - inline lua_State *getL(int which) { + inline lua_State *getL(int which) + { assert(which >= -1 && which < 2); struct luaConfigStateHolder *state_holder; - state_holder = (struct luaConfigStateHolder *)ink_thread_getspecific(state_key); - if(state_holder == NULL) { - state_holder = new struct luaConfigStateHolder(); - ink_thread_setspecific(state_key, (void *)state_holder); + state_holder = (struct luaConfigStateHolder *) ink_thread_getspecific(state_key); + if (state_holder == NULL) { + state_holder = new struct luaConfigStateHolder (); + ink_thread_setspecific(state_key, (void *) state_holder); } return state_holder->states[(which < 0) ? state_holder->active : which]; }