[asterisk-commits] mnicholson: branch mnicholson/asttest r247166 - /team/mnicholson/asttest/astt...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Feb 17 09:52:01 CST 2010
Author: mnicholson
Date: Wed Feb 17 09:51:58 2010
New Revision: 247166
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=247166
Log:
Converted astlib to use proclib when spawning asterisk.
Modified:
team/mnicholson/asttest/asttest/lua/astlib.c
team/mnicholson/asttest/asttest/lua/astlib.lua
Modified: team/mnicholson/asttest/asttest/lua/astlib.c
URL: http://svnview.digium.com/svn/asterisk/team/mnicholson/asttest/asttest/lua/astlib.c?view=diff&rev=247166&r1=247165&r2=247166
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.c (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.c Wed Feb 17 09:51:58 2010
@@ -312,211 +312,6 @@
}
/*!
- * \brief [lua_CFunction asterisk:_spawn] Start asterisk.
- * \param L the lua state to use
- *
- * This function forks and execs asterisk.
- */
-static int spawn_asterisk(lua_State *L) {
- const char *asterisk;
- const char *asterisk_conf;
- pid_t pid;
- pid_t *p;
-
- luaL_checktype(L, 1, LUA_TTABLE);
-
- /* XXX maybe see if pid is set. Right now, if pid is set, the previous
- * asterisk process will be killed (during gc) and a new one will
- * replace it. */
-
- /* get the index for this instance */
- lua_getfield(L, 1, "asterisk_binary");
- asterisk = lua_tostring(L, -1);
-
- /* get the location of the asterisk.conf file */
- lua_getfield(L, 1, "asterisk_conf");
- asterisk_conf = lua_tostring(L, -1);
-
- /* start asterisk */
- pid = fork();
- if (pid == 0) {
- execl(asterisk, asterisk, "-f", "-g", "-q", "-m", "-C", asterisk_conf, (char *) NULL);
- exit(1);
- } else if (pid == -1) {
- lua_pushliteral(L, "error spawning asterisk (fork error): ");
- lua_pushstring(L, strerror(errno));
- lua_concat(L, 2);
- return lua_error(L);
- }
-
- /* store the pid */
- lua_pushliteral(L, "pid");
- p = lua_newuserdata(L, sizeof(pid_t));
- *p = pid;
- luaL_getmetatable(L, "astlib_pid");
- lua_setmetatable(L, -2);
- lua_rawset(L, 1);
-
- return 0;
-}
-
-/*!
- * \brief [lua_CFunction astlib_pid:__gc] Kill asterisk.
- * \param L the lua state to use
- *
- * This is the gc function for the pid userdata. This function will send a
- * TERM signal to asterisk and then run waitpid.
- */
-static int pid_gc(lua_State *L) {
- pid_t *p = luaL_checkudata(L, 1, "astlib_pid");
- if (kill(*p, SIGTERM)) {
- return 0;
- }
-#if 0 /* XXX maybe we should also do SIGKILL here? */
- sleep(1);
- kill(*p, SIGKILL);
-#endif
- waitpid(*p, NULL, 0);
- return 0;
-}
-
-/*!
- * \brief [lua_CFunction asterisk:wait] Wait for the asterisk process to end.
- * \param L the lua state to use
- *
- * This function calls waitpid on the running asterisk process.
- *
- * \return a tuple containg the exit code, nil and the signal that caused
- * asterisk to exit, or nil and a string describing the error ("error" or
- * "core" currently).
- */
-static int wait_asterisk(lua_State *L) {
- pid_t pid;
- int status;
-
- luaL_checktype(L, 1, LUA_TTABLE);
-
- /* get the pid of this process */
- lua_getfield(L, 1, "pid");
- if (lua_isnil(L, -1)) {
- /* no process found */
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- return 2;
- }
- 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 (WIFEXITED(status)) {
- lua_pushinteger(L, WEXITSTATUS(status));
- lua_pushnil(L);
- } else if (WIFSIGNALED(status)) {
- lua_pushnil(L);
- if (WCOREDUMP(status))
- lua_pushliteral(L, "core");
- else
- lua_pushinteger(L, WTERMSIG(status));
- } else {
- lua_pushliteral(L, "unknown error running waitpid for asterisk");
- return lua_error(L);
- }
-
- /* unset the pid */
- lua_pushliteral(L, "pid");
- lua_pushnil(L);
- lua_rawset(L, 1);
-
- return 2;
-}
-
-/*!
- * \brief [lua_CFunction asterisk:kill] Kill asterisk with SIGKILL.
- * \param L the lua state to use
- *
- * This function sends asterisk SIGKILL then calls asterisk:wait() and returns
- * the result.
- *
- * \return same as wait_asterisk() and in the case of "error" an additional
- * error description and errno may be returned
- */
-static int kill_asterisk(lua_State *L) {
- pid_t pid;
-
- luaL_checktype(L, 1, LUA_TTABLE);
-
- /* get the pid of this process */
- lua_getfield(L, 1, "pid");
- if (lua_isnil(L, -1)) {
- /* no process found */
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- return 2;
- }
- pid = *(pid_t *) lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- if (kill(pid, SIGKILL)) {
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- lua_pushstring(L, strerror(errno));
- lua_pushinteger(L, errno);
- return 4;
- }
-
- lua_pushcfunction(L, wait_asterisk);
- lua_pushvalue(L, 1);
- lua_call(L, 1, 2);
- return 2;
-}
-
-/*!
- * \brief [lua_CFunction asterisk:term] Terminate asterisk with SIGTERM.
- * \param L the lua state to use
- *
- * This function sends asterisk SIGTERM then calls asterisk:wait() and returns
- * the result.
- *
- * \return same as wait_asterisk() and in the case of "error" an additional
- * error description and errno may be returned
- */
-static int terminate_asterisk(lua_State *L) {
- pid_t pid;
-
- luaL_checktype(L, 1, LUA_TTABLE);
-
- /* get the pid of this process */
- lua_getfield(L, 1, "pid");
- if (lua_isnil(L, -1)) {
- /* no process found */
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- return 2;
- }
- pid = *(pid_t *) lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- if (kill(pid, SIGTERM)) {
- lua_pushnil(L);
- lua_pushliteral(L, "error");
- lua_pushstring(L, strerror(errno));
- lua_pushinteger(L, errno);
- return 4;
- }
-
- lua_pushcfunction(L, wait_asterisk);
- lua_pushvalue(L, 1);
- lua_call(L, 1, 2);
- return 2;
-}
-
-/*!
* \brief [lua_CFunction ast:unlink] Unlink the given file.
* \param L the lua state to use
*
@@ -534,19 +329,10 @@
{NULL, NULL},
};
-static luaL_Reg asterisk_pid[] = {
- {"__gc", pid_gc},
- {NULL, NULL},
-};
-
static luaL_Reg asterisk_table[] = {
{"_new", new_asterisk},
- {"_spawn", spawn_asterisk},
{"clean_work_area", clean_work_area},
{"create_work_area", create_work_area},
- {"wait", wait_asterisk},
- {"term", terminate_asterisk},
- {"kill", kill_asterisk},
{NULL, NULL},
};
@@ -566,11 +352,6 @@
/* set up the 'path' variable */
lua_pushstring(L, asterisk_path);
lua_setfield(L, -2, "path");
-
- /* set up the pid metatable */
- luaL_newmetatable(L, "astlib_pid");
- luaL_register(L, NULL, asterisk_pid);
- lua_pop(L, 1);
/* set up the 'asterisk' table and add some functions to it */
lua_newtable(L);
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=247166&r1=247165&r2=247166
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.lua (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.lua Wed Feb 17 09:51:58 2010
@@ -36,6 +36,14 @@
return a
end
+function asterisk:_spawn()
+ p = proc.exec(self.asterisk_binary,
+ "-f", "-g", "-q", "-m",
+ "-C", self.asterisk_conf
+ )
+ rawset(self, "proc", p)
+end
+
function asterisk:spawn()
self:clean_work_area()
self:create_work_area()
@@ -46,7 +54,22 @@
function asterisk:spawn_and_wait()
self:spawn()
- self:wait()
+ return self:wait()
+end
+
+function asterisk:wait()
+ if not proc then return nil, "error" end
+ return self.proc:wait()
+end
+
+function asterisk:term()
+ if not proc then return nil, "error" end
+ return self.proc:term()
+end
+
+function asterisk:kill()
+ if not proc then return nil, "error" end
+ return self.proc:kill()
end
function asterisk:__newindex(conffile_name, conffile)
More information about the asterisk-commits
mailing list