From 644ccf204a9f5ab3437dfd4021b87dd633ccdcaf Mon Sep 17 00:00:00 2001 From: findstr Date: Wed, 15 May 2024 12:04:19 +0800 Subject: [PATCH] refine default libpath luailbcpath implement always include dir(os.argv[0])/lualib/?.lua and dir(os.argv[0])/luaclib/?.so --- silly-src/main.c | 1 + silly-src/silly.h | 1 + silly-src/silly_conf.h | 9 ------ silly-src/silly_run.c | 6 ++-- silly-src/silly_worker.c | 64 +++++++++++++++++++--------------------- 5 files changed, 35 insertions(+), 46 deletions(-) diff --git a/silly-src/main.c b/silly-src/main.c index 23272a5..ec86d60 100644 --- a/silly-src/main.c +++ b/silly-src/main.c @@ -147,6 +147,7 @@ int main(int argc, char *argv[]) struct silly_config config; config.argc = argc; config.argv = argv; + config.selfpath = argv[0]; config.selfname = selfname(argv[0]); if (argc < 2) { print_help(config.selfname); diff --git a/silly-src/silly.h b/silly-src/silly.h index 5b24758..cd6ad06 100644 --- a/silly-src/silly.h +++ b/silly-src/silly.h @@ -36,6 +36,7 @@ struct silly_config { int timeraffinity; int argc; char **argv; + const char *selfpath; const char *selfname; char bootstrap[PATH_MAX]; char lualib_path[PATH_MAX]; diff --git a/silly-src/silly_conf.h b/silly-src/silly_conf.h index 3d7b8c2..d5d4b93 100644 --- a/silly-src/silly_conf.h +++ b/silly-src/silly_conf.h @@ -21,15 +21,6 @@ #define LUA_GC_STEP (100) //KiB #endif -#ifndef DEFAULT_LUA_PATH -#define DEFAULT_LUA_PATH "lualib/?.lua" -#endif - -#ifndef DEFAULT_LUA_CPATH -#define DEFAULT_LUA_CPATH "luaclib/?.so" -#endif - - //(1 << 16) = 65536 #define SOCKET_MAX_EXP (16) #define TIMER_RESOLUTION (10) //ms diff --git a/silly-src/silly_run.c b/silly-src/silly_run.c index 3e74514..6ab3ba1 100644 --- a/silly-src/silly_run.c +++ b/silly-src/silly_run.c @@ -180,12 +180,12 @@ silly_run(const struct silly_config *config) silly_worker_init(); silly_monitor_init(); srand(time(NULL)); - thread_create(&pid[0], thread_socket, NULL, config->socketaffinity); - thread_create(&pid[1], thread_timer, NULL, config->timeraffinity); - thread_create(&pid[2], thread_worker, (void *)config, config->workeraffinity); silly_log_info("%s %s is running ...\n", config->selfname, SILLY_RELEASE); silly_log_info("cpu affinity setting, timer:%d, socket:%d, worker:%d\n", config->timeraffinity, config->socketaffinity, config->workeraffinity); + thread_create(&pid[0], thread_socket, NULL, config->socketaffinity); + thread_create(&pid[1], thread_timer, NULL, config->timeraffinity); + thread_create(&pid[2], thread_worker, (void *)config, config->workeraffinity); monitor_check(); for (i = 0; i < 3; i++) pthread_join(pid[i], NULL); diff --git a/silly-src/silly_worker.c b/silly-src/silly_worker.c index eb9aaa0..4da2121 100644 --- a/silly-src/silly_worker.c +++ b/silly-src/silly_worker.c @@ -13,8 +13,8 @@ #include "silly_monitor.h" #include "silly_worker.h" -#define max(a, b) ((a) > (b) ? (a) : (b)) -#define WARNING_THRESHOLD (64) +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define WARNING_THRESHOLD (64) struct silly_worker { int argc; @@ -91,38 +91,26 @@ silly_worker_callback(void (*callback)(struct lua_State *L, struct silly_message return ; } -static int -setlibpath(lua_State *L, const char *libpath, const char *clibpath) +static void +setlibpath(lua_State *L, const char *pathname, const char *libpath) { - const char *path; - const char *cpath; - size_t sz1 = strlen(libpath); - size_t sz2 = strlen(clibpath); - size_t sz3; - size_t sz4; + size_t sz1; + size_t sz2 = strlen(libpath); size_t need_sz; - + const char *path; + if (sz2 == 0) + return ; lua_getglobal(L, "package"); - lua_getfield(L, -1, "path"); - path = luaL_checklstring(L, -1, &sz3); - - lua_getfield(L, -2, "cpath"); - cpath = luaL_checklstring(L, -1, &sz4); - - need_sz = max(sz1, sz2) + max(sz3, sz4) + 1; + lua_getfield(L, -1, pathname); + path = luaL_checklstring(L, -1, &sz1); + need_sz = sz2 + sz1 + 1; char new_path[need_sz]; - snprintf(new_path, need_sz, "%s;%s", libpath, path); lua_pushstring(L, new_path); - lua_setfield(L, -4, "path"); - - snprintf(new_path, need_sz, "%s;%s", clibpath, cpath); - lua_pushstring(L, new_path); - lua_setfield(L, -4, "cpath"); - + lua_setfield(L, -3, pathname); //clear the stack lua_settop(L, 0); - return 0; + return ; } static void * @@ -164,6 +152,8 @@ void silly_worker_start(const struct silly_config *config) { int err; + int dir_len; + int lib_len; lua_State *L = lua_newstate(lua_alloc, NULL); luaL_openlibs(L); W->argc = config->argc; @@ -173,14 +163,20 @@ silly_worker_start(const struct silly_config *config) #else lua_gc(L, LUA_GCGEN, 0, 0); #endif - setlibpath(L, DEFAULT_LUA_PATH, DEFAULT_LUA_CPATH); - err = setlibpath(L, config->lualib_path, config->lualib_cpath); - if (unlikely(err != 0)) { - silly_log_error("[worker] set lua libpath fail,%s\n", - lua_tostring(L, -1)); - lua_close(L); - exit(-1); - } + //set load path + lib_len = max(sizeof("lualib/?.lua"), sizeof("luaclib/?.so")); + dir_len = config->selfname - config->selfpath; + char buf[dir_len + lib_len]; + setlibpath(L, "path", config->lualib_path); + setlibpath(L, "cpath", config->lualib_cpath); + setlibpath(L, "path", "./lualib/?.lua"); + setlibpath(L, "cpath", "./luaclib/?.so"); + memcpy(buf, config->selfpath, dir_len); + memcpy(buf + dir_len, "lualib/?.lua", sizeof("lualib/?.lua")); + setlibpath(L, "path", buf); + memcpy(buf + dir_len, "luaclib/?.so", sizeof("luaclib/?.so")); + setlibpath(L, "cpath", buf); + //exec core.start() lua_pushcfunction(L, ltraceback); fetch_core_start(L); err = luaL_loadfile(L, config->bootstrap);