From 6e9cdb367d4bc28a79d1d8109bee3c7c33682a51 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 23 May 2024 02:23:20 +0300 Subject: [PATCH 82/82] Add luatimeout server setting See osdn #44898 Signed-off-by: Marko Lindqvist --- common/game.c | 4 +++- common/game.h | 22 ++++++++++++++-------- common/scriptcore/luascript.c | 7 +++---- server/settings.c | 7 +++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/common/game.c b/common/game.c index 7fa5732409..47de54edca 100644 --- a/common/game.c +++ b/common/game.c @@ -331,9 +331,11 @@ static void game_defaults(bool keep_ruleset_value) /* Veteran system. */ game.veteran = NULL; - /* player colors */ + /* Player colors */ game.plr_bg_color = NULL; + game.lua_timeout = GAME_DEFAULT_LUA_TIMEOUT; + if (is_server()) { /* All settings only used by the server (./server/ and ./ai/ */ sz_strlcpy(game.server.allow_take, GAME_DEFAULT_ALLOW_TAKE); diff --git a/common/game.h b/common/game.h index ab61fbb35e..25e54edb92 100644 --- a/common/game.h +++ b/common/game.h @@ -90,18 +90,20 @@ struct civ_game { struct packet_calendar_info calendar; struct packet_timeout_info tinfo; - struct government *default_government; /* may be overridden by nation */ + struct government *default_government; /* May be overridden by nation */ struct government *government_during_revolution; - struct conn_list *all_connections; /* including not yet established */ - struct conn_list *est_connections; /* all established client conns */ - struct conn_list *glob_observers; /* global observers */ + struct conn_list *all_connections; /* Including not yet established */ + struct conn_list *est_connections; /* All established client conns */ + struct conn_list *glob_observers; /* Global observers */ struct conn_list *web_client_connections; /* Connections from web client */ - struct veteran_system *veteran; /* veteran system */ + struct veteran_system *veteran; /* Veteran system */ struct rgbcolor *plr_bg_color; + int lua_timeout; + struct { /* Items given to all players at game start. * Client gets this info for help purposes only. */ @@ -522,7 +524,7 @@ static inline bool is_ruleset_compat_mode(void) #define GAME_DEFAULT_MGR_FOODNEEDED TRUE -/* definition of the migration distance in relation to the current city +/* Definition of the migration distance in relation to the current city * radius; 0 means that migration is possible if the second city is within * the city radius */ #define GAME_DEFAULT_MGR_DISTANCE 0 @@ -742,13 +744,17 @@ static inline bool is_ruleset_compat_mode(void) #define GAME_DEFAULT_AIRLIFTINGSTYLE AIRLIFTING_CLASSICAL #define GAME_DEFAULT_PERSISTENTREADY PERSISTENTR_DISABLED -#define GAME_MAX_READ_RECURSION 10 /* max recursion for the read command */ +#define GAME_MAX_READ_RECURSION 10 /* Max recursion for the read command */ #define GAME_DEFAULT_KICK_TIME 1800 /* 1800 seconds = 30 minutes. */ #define GAME_MIN_KICK_TIME 0 /* 0 = disabling. */ #define GAME_MAX_KICK_TIME 86400 /* 86400 seconds = 24 hours. */ -/* Max distance from the capital used to calculat the bribe cost. */ +#define GAME_DEFAULT_LUA_TIMEOUT 5 +#define GAME_MIN_LUA_TIMEOUT 5 +#define GAME_MAX_LUA_TIMEOUT 30 + +/* Max distance from the capital used to calculate the bribe cost. */ #define GAME_UNIT_BRIBE_DIST_MAX 32 /* Max number of recursive transports. */ diff --git a/common/scriptcore/luascript.c b/common/scriptcore/luascript.c index 2a95f76ea8..076effa22c 100644 --- a/common/scriptcore/luascript.c +++ b/common/scriptcore/luascript.c @@ -41,7 +41,6 @@ Configuration for script execution time limits. Checkinterval is the number of executed lua instructions between checking. Disabled if 0. *****************************************************************************/ -#define LUASCRIPT_MAX_EXECUTION_TIME_SEC 5.0 #define LUASCRIPT_CHECKINTERVAL 10000 /* The name used for the freeciv lua struct saved in the lua state. */ @@ -232,9 +231,9 @@ static void luascript_exec_check(lua_State *L, lua_Debug *ar) lua_getfield(L, LUA_REGISTRYINDEX, "freeciv_exec_clock"); exec_clock = lua_tonumber(L, -1); lua_pop(L, 1); - if ((float)(clock() - exec_clock)/CLOCKS_PER_SEC - > LUASCRIPT_MAX_EXECUTION_TIME_SEC) { - luaL_error(L, "Execution time limit exceeded in script"); + if ((float)(clock() - exec_clock) / CLOCKS_PER_SEC + > game.lua_timeout) { + luaL_error(L, _("Execution time limit exceeded in script")); } } diff --git a/server/settings.c b/server/settings.c index 77999c1208..2188d9149f 100644 --- a/server/settings.c +++ b/server/settings.c @@ -3252,6 +3252,13 @@ static struct setting settings[] = { "affect users kicked in the past."), NULL, NULL, NULL, GAME_MIN_KICK_TIME, GAME_MAX_KICK_TIME, GAME_DEFAULT_KICK_TIME) + GEN_INT("luatimeout", game.lua_timeout, + SSET_RULES_FLEXIBLE, SSET_INTERNAL, SSET_RARE, ALLOW_HACK, ALLOW_HACK, + N_("Lua timeout"), + N_("Time in seconds that a single lua script can run before it gets " + "forcibly terminated."), NULL, NULL, NULL, + GAME_MIN_LUA_TIMEOUT, GAME_MAX_LUA_TIMEOUT, GAME_DEFAULT_LUA_TIMEOUT) + GEN_STRING_NRS("metamessage", game.server.meta_info.user_message, SSET_META, SSET_INTERNAL, SSET_RARE, ALLOW_CTRL, ALLOW_CTRL, N_("Metaserver info line"), -- 2.43.0