[svn-commits] mnicholson: branch mnicholson/asttest r247905 - in /team/mnicholson/asttest/a...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Fri Feb 19 10:54:40 CST 2010
Author: mnicholson
Date: Fri Feb 19 10:54:37 2010
New Revision: 247905
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=247905
Log:
Added the ability to pass a timeout to proc:wait()
Added the proc:term_or_kill() function (and the asterisk:term_or_kill() function). This function will send the term signal, wait 10 seconds, then send the kill signal.
Modified:
team/mnicholson/asttest/asttest/lua/astlib.lua
team/mnicholson/asttest/asttest/lua/proclib.c
team/mnicholson/asttest/asttest/lua/proclib.lua
team/mnicholson/asttest/asttest/self-tests/astlib_load_config/test.lua
team/mnicholson/asttest/asttest/self-tests/astlib_load_config_in_dir/test.lua
team/mnicholson/asttest/asttest/self-tests/astlib_manager/test.lua
team/mnicholson/asttest/asttest/self-tests/spawn_asterisk/test.lua
Modified: team/mnicholson/asttest/asttest/lua/astlib.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/lua/astlib.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.lua (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.lua Fri Feb 19 10:54:37 2010
@@ -61,19 +61,24 @@
return self:wait()
end
-function asterisk:wait()
+function asterisk:wait(timeout)
if not proc then return nil, "error" end
- return self.proc:wait()
-end
-
-function asterisk:term()
+ return self.proc:wait(timeout)
+end
+
+function asterisk:term(timeout)
if not proc then return nil, "error" end
- return self.proc:term()
+ return self.proc:term(timeout)
end
function asterisk:kill()
if not proc then return nil, "error" end
return self.proc:kill()
+end
+
+function asterisk:term_or_kill()
+ if not proc then return nil, "error" end
+ return self.proc:term_or_kill()
end
function asterisk:__newindex(conffile_name, conffile)
Modified: team/mnicholson/asttest/asttest/lua/proclib.c
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/lua/proclib.c?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/lua/proclib.c (original)
+++ team/mnicholson/asttest/asttest/lua/proclib.c Fri Feb 19 10:54:37 2010
@@ -31,6 +31,8 @@
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
+#include <sys/time.h>
+#include <time.h>
/*!
* \brief [lua_CFunction proc.exec] Start a process.
@@ -134,8 +136,18 @@
}
/*!
+ * \brief Convert a timeval struct to milliseconds.
+ * \param tv the timeval struct to operate on
+ * \return the value of the given timeval expressed in milliseconds
+ */
+static long tv2ms(struct timeval *tv) {
+ return (tv->tv_sec * 1000 + tv->tv_usec / 1000);
+}
+
+/*!
* \brief [lua_CFunction proc:wait] Wait for the process to end.
* \param L the lua state to use
+ * \param timeout an optional timeout in milliseconds
*
* This function calls waitpid on the running process.
*
@@ -146,8 +158,20 @@
static int wait_proc(lua_State *L) {
pid_t pid;
int status;
+ int res;
+ long timeout = luaL_optlong(L, 2, -1);
+ long start;
+ struct timeval tv;
luaL_checktype(L, 1, LUA_TTABLE);
+
+ if (gettimeofday(&tv, NULL)) {
+ lua_pushliteral(L, "error running gettimeofday() for timeout calculations: ");
+ lua_pushstring(L, strerror(errno));
+ lua_concat(L, 2);
+ return lua_error(L);
+ }
+ start = tv2ms(&tv);
/* get the pid of this process */
lua_getfield(L, 1, "pid");
@@ -160,11 +184,37 @@
pid = *(pid_t *) lua_touserdata(L, -1);
lua_pop(L, 1);
- if (waitpid(pid, &status, 0) == -1) {
- /* waitpid failed */
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- return 2;
+ if (timeout >= 0) {
+ while ((res = waitpid(pid, &status, WNOHANG)) == 0) {
+ /* check the timeout */
+ if (gettimeofday(&tv, NULL)) {
+ lua_pushliteral(L, "error running gettimeofday() for timeout calculations: ");
+ lua_pushstring(L, strerror(errno));
+ lua_concat(L, 2);
+ return lua_error(L);
+ }
+
+ if (timeout < tv2ms(&tv) - start) {
+ lua_pushnil(L);
+ lua_pushliteral(L, "timeout");
+ return 2;
+ }
+ usleep(1);
+ }
+
+ if (res == -1) {
+ /* waitpid failed */
+ lua_pushnil(L);
+ lua_pushliteral(L, "error");
+ return 2;
+ }
+ } else {
+ if (waitpid(pid, &status, 0) == -1) {
+ /* waitpid failed */
+ lua_pushnil(L);
+ lua_pushliteral(L, "error");
+ return 2;
+ }
}
if (WIFEXITED(status)) {
@@ -232,6 +282,7 @@
/*!
* \brief [lua_CFunction proc:term] Terminate process with SIGTERM.
* \param L the lua state to use
+ * \param timeout an optional timeout in milliseconds that will be passed to proc:wait()
*
* This function sends SIGTERM then calls proc:wait() and returns the result.
*
@@ -240,6 +291,7 @@
*/
static int terminate_proc(lua_State *L) {
pid_t pid;
+ long timeout = luaL_optlong(L, 2, -1);
luaL_checktype(L, 1, LUA_TTABLE);
@@ -264,7 +316,13 @@
lua_pushcfunction(L, wait_proc);
lua_pushvalue(L, 1);
- lua_call(L, 1, 2);
+ if (timeout == -1) {
+ lua_pushnil(L);
+ } else {
+ lua_pushvalue(L, 2);
+ }
+
+ lua_call(L, 2, 2);
return 2;
}
@@ -288,7 +346,6 @@
int luaopen_proclib(lua_State *L) {
/* register our functions */
luaL_register(L, "proc", proclib);
- lua_pop(L, 1);
/* set up the pid metatable */
luaL_newmetatable(L, "proclib_pid");
@@ -304,6 +361,9 @@
lua_settable(L, -3);
luaL_register(L, NULL, proc_table);
+ /* store this table as 'proc.proc' as well */
+ lua_setfield(L, -2, "proc");
+
lua_pop(L, 1);
/* load the lua portion of the lib */
Modified: team/mnicholson/asttest/asttest/lua/proclib.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/lua/proclib.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/lua/proclib.lua (original)
+++ team/mnicholson/asttest/asttest/lua/proclib.lua Fri Feb 19 10:54:37 2010
@@ -23,3 +23,13 @@
return p:wait()
end
+-- sent the term signal and give the process 10 seconds to exit then kill it
+function proc:term_or_kill()
+ local res, err = self:term(10000)
+ if not res and err == 'timeout' then
+ return self:kill()
+ end
+
+ return res, err
+end
+
Modified: team/mnicholson/asttest/asttest/self-tests/astlib_load_config/test.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/self-tests/astlib_load_config/test.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/self-tests/astlib_load_config/test.lua (original)
+++ team/mnicholson/asttest/asttest/self-tests/astlib_load_config/test.lua Fri Feb 19 10:54:37 2010
@@ -19,7 +19,7 @@
a = ast.new()
a:load_config("expected_test.conf")
a:spawn()
-a:term()
+a:term_or_kill()
test_conf_path = a.work_area .. "/etc/asterisk/expected_test.conf"
Modified: team/mnicholson/asttest/asttest/self-tests/astlib_load_config_in_dir/test.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/self-tests/astlib_load_config_in_dir/test.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/self-tests/astlib_load_config_in_dir/test.lua (original)
+++ team/mnicholson/asttest/asttest/self-tests/astlib_load_config_in_dir/test.lua Fri Feb 19 10:54:37 2010
@@ -19,7 +19,7 @@
a = ast.new()
a:load_config("dir/expected_test.conf")
a:spawn()
-a:term()
+a:term_or_kill()
test_conf_path = a.work_area .. "/etc/asterisk/expected_test.conf"
Modified: team/mnicholson/asttest/asttest/self-tests/astlib_manager/test.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/self-tests/astlib_manager/test.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/self-tests/astlib_manager/test.lua (original)
+++ team/mnicholson/asttest/asttest/self-tests/astlib_manager/test.lua Fri Feb 19 10:54:37 2010
@@ -29,7 +29,7 @@
r = m(logoff)
-status, err = a:term()
+status, err = a:term_or_kill()
if not status then
fail("error terminating asterisk: " .. err)
end
Modified: team/mnicholson/asttest/asttest/self-tests/spawn_asterisk/test.lua
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/self-tests/spawn_asterisk/test.lua?view=diff&rev=247905&r1=247904&r2=247905
==============================================================================
--- team/mnicholson/asttest/asttest/self-tests/spawn_asterisk/test.lua (original)
+++ team/mnicholson/asttest/asttest/self-tests/spawn_asterisk/test.lua Fri Feb 19 10:54:37 2010
@@ -3,7 +3,7 @@
a = ast.new()
a:spawn()
posix.sleep(1)
-res, err = a:term()
+res, err = a:term_or_kill()
print(res)
print(err)
More information about the svn-commits
mailing list