jshn: read and write 64-bit integers
[project/libubox.git] / lua / uloop.c
index 2a0a516..1b0389f 100644 (file)
@@ -43,6 +43,26 @@ struct lua_uloop_process {
 
 static lua_State *state;
 
+static void *
+ul_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
+{
+       void *ret = lua_newuserdata(L, size);
+
+       memset(ret, 0, size);
+       lua_createtable(L, 0, 2);
+       lua_pushvalue(L, -1);
+       lua_setfield(L, -2, "__index");
+       lua_pushcfunction(L, gc);
+       lua_setfield(L, -2, "__gc");
+       lua_pushvalue(L, -1);
+       lua_setmetatable(L, -3);
+       lua_pushvalue(L, -2);
+       luaI_openlib(L, NULL, reg, 1);
+       lua_pushvalue(L, -2);
+
+       return ret;
+}
+
 static void ul_timer_cb(struct uloop_timeout *t)
 {
        struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
@@ -74,12 +94,21 @@ static int ul_timer_set(lua_State *L)
        return 1;
 }
 
+static int ul_timer_remaining(lua_State *L)
+{
+       struct lua_uloop_timeout *tout;
+
+       tout = lua_touserdata(L, 1);
+       lua_pushnumber(L, uloop_timeout_remaining(&tout->t));
+       return 1;
+}
+
 static int ul_timer_free(lua_State *L)
 {
        struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
-       
+
        uloop_timeout_cancel(&tout->t);
-       
+
        /* obj.__index.__gc = nil , make sure executing only once*/
        lua_getfield(L, -1, "__index");
        lua_pushstring(L, "__gc");
@@ -94,6 +123,7 @@ static int ul_timer_free(lua_State *L)
 
 static const luaL_Reg timer_m[] = {
        { "set", ul_timer_set },
+       { "remaining", ul_timer_remaining },
        { "cancel", ul_timer_free },
        { NULL, NULL }
 };
@@ -120,22 +150,10 @@ static int ul_timer(lua_State *L)
        lua_pushvalue(L, -2);
        ref = luaL_ref(L, -2);
 
-       tout = lua_newuserdata(L, sizeof(*tout));
-       lua_createtable(L, 0, 2);
-       lua_pushvalue(L, -1);
-       lua_setfield(L, -2, "__index");
-       lua_pushcfunction(L, ul_timer_free);
-       lua_setfield(L, -2, "__gc");
-       lua_pushvalue(L, -1);
-       lua_setmetatable(L, -3);
-       lua_pushvalue(L, -2);
-       luaI_openlib(L, NULL, timer_m, 1);
-       lua_pushvalue(L, -2);
-
-       memset(tout, 0, sizeof(*tout));
-
+       tout = ul_create_userdata(L, sizeof(*tout), timer_m, ul_timer_free);
        tout->r = ref;
        tout->t.cb = ul_timer_cb;
+
        if (set)
                uloop_timeout_set(&tout->t, set);
 
@@ -181,7 +199,7 @@ static int get_sock_fd(lua_State* L, int idx) {
 static int ul_ufd_delete(lua_State *L)
 {
        struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
-       
+
        uloop_fd_delete(&ufd->fd);
 
        /* obj.__index.__gc = nil , make sure executing only once*/
@@ -238,21 +256,7 @@ static int ul_ufd_add(lua_State *L)
        fd_ref = luaL_ref(L, -2);
        lua_pop(L, 1);
 
-       ufd = lua_newuserdata(L, sizeof(*ufd));
-
-       lua_createtable(L, 0, 2);
-       lua_pushvalue(L, -1);
-       lua_setfield(L, -2, "__index");
-       lua_pushcfunction(L, ul_ufd_delete);
-       lua_setfield(L, -2, "__gc");
-       lua_pushvalue(L, -1);
-       lua_setmetatable(L, -3);
-       lua_pushvalue(L, -2);
-       luaI_openlib(L, NULL, ufd_m, 1);
-       lua_pushvalue(L, -2);
-
-       memset(ufd, 0, sizeof(*ufd));
-
+       ufd = ul_create_userdata(L, sizeof(*ufd), ufd_m, ul_ufd_delete);
        ufd->r = ref;
        ufd->fd.fd = fd;
        ufd->fd_r = fd_ref;
@@ -263,6 +267,45 @@ static int ul_ufd_add(lua_State *L)
        return 1;
 }
 
+static int ul_process_free(lua_State *L)
+{
+       struct lua_uloop_process *proc = lua_touserdata(L, 1);
+
+       /* obj.__index.__gc = nil , make sure executing only once*/
+       lua_getfield(L, -1, "__index");
+       lua_pushstring(L, "__gc");
+       lua_pushnil(L);
+       lua_settable(L, -3);
+
+       if (proc->r != LUA_NOREF) {
+               uloop_process_delete(&proc->p);
+
+               lua_getglobal(state, "__uloop_cb");
+               luaL_unref(state, -1, proc->r);
+               lua_remove(state, -1);
+       }
+
+       return 1;
+}
+
+static int ul_process_pid(lua_State *L)
+{
+       struct lua_uloop_process *proc = lua_touserdata(L, 1);
+
+       if (proc->p.pid) {
+               lua_pushnumber(L, proc->p.pid);
+               return 1;
+       }
+
+       return 0;
+}
+
+static const luaL_Reg process_m[] = {
+       { "delete", ul_process_free },
+       { "pid", ul_process_pid },
+       { NULL, NULL }
+};
+
 static void ul_process_cb(struct uloop_process *p, int ret)
 {
        struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
@@ -271,6 +314,7 @@ static void ul_process_cb(struct uloop_process *p, int ret)
        lua_rawgeti(state, -1, proc->r);
 
        luaL_unref(state, -2, proc->r);
+       proc->r = LUA_NOREF;
        lua_remove(state, -2);
        lua_pushinteger(state, ret >> 8);
        lua_call(state, 1, 0);
@@ -304,9 +348,12 @@ static int ul_process(lua_State *L)
                int argn = lua_objlen(L, -3);
                int envn = lua_objlen(L, -2);
                char** argp = malloc(sizeof(char*) * (argn + 2));
-               char** envp = malloc(sizeof(char*) * envn + 1);
+               char** envp = malloc(sizeof(char*) * (envn + 1));
                int i = 1;
 
+               if (!argp || !envp)
+                       _exit(-1);
+
                argp[0] = (char*) lua_tostring(L, -4);
                for (i = 1; i <= argn; i++) {
                        lua_rawgeti(L, -3, i);
@@ -323,16 +370,14 @@ static int ul_process(lua_State *L)
                envp[i - 1] = NULL;
 
                execve(*argp, argp, envp);
-               exit(-1);
+               _exit(-1);
        }
 
        lua_getglobal(L, "__uloop_cb");
        lua_pushvalue(L, -2);
        ref = luaL_ref(L, -2);
 
-       proc = lua_newuserdata(L, sizeof(*proc));
-       memset(proc, 0, sizeof(*proc));
-
+       proc = ul_create_userdata(L, sizeof(*proc), process_m, ul_process_free);
        proc->r = ref;
        proc->p.pid = pid;
        proc->p.cb = ul_process_cb;