Terminating Lua Subprocess
The usual way to do this is to use lua_sethook to schedule a callback for every count
VM instruction; when the lua_Hook callback function occurs after an excessive amount of time, when your hook function may raise an error , forcing your guarded call to be managed .
a source to share
Doug's answer already provides the default needed to restrict the execution of normal lua code. If you need to restrict this for security reasons, you should be aware that there are known ways to use calls to the lua library, such as string matching functions), to create practical infinite loops. The command counting hook won't catch them for you, since the lua command does not increase when a function call is made to c. To solve this caliber, you need OS-level constraints (separate process, interrupt from SIGALRM?)
a source to share
For OS level constraints such as the mention of kaizer.se, one good approach for running standalone Lua systems on * nix systems is to use ulimit -t 1
to limit the Lua process to one second of cpu time. This is the CGI script approach that the live demo on Lua.org uses.
For an application like the one you described, the best option is to use environment resources.
a source to share