uloop.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <lua.h>
  21. #include <lualib.h>
  22. #include <lauxlib.h>
  23. #include "../uloop.h"
  24. #include "../list.h"
  25. struct lua_uloop_fd {
  26. struct uloop_fd fd;
  27. int r;
  28. int fd_r;
  29. };
  30. struct lua_uloop_timeout {
  31. struct uloop_timeout t;
  32. int r;
  33. };
  34. struct lua_uloop_process {
  35. struct uloop_process p;
  36. int r;
  37. };
  38. static lua_State *state;
  39. static void *
  40. ul_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
  41. {
  42. void *ret = lua_newuserdata(L, size);
  43. memset(ret, 0, size);
  44. lua_createtable(L, 0, 2);
  45. lua_pushvalue(L, -1);
  46. lua_setfield(L, -2, "__index");
  47. lua_pushcfunction(L, gc);
  48. lua_setfield(L, -2, "__gc");
  49. lua_pushvalue(L, -1);
  50. lua_setmetatable(L, -3);
  51. lua_pushvalue(L, -2);
  52. luaI_openlib(L, NULL, reg, 1);
  53. lua_pushvalue(L, -2);
  54. return ret;
  55. }
  56. static void ul_timer_cb(struct uloop_timeout *t)
  57. {
  58. struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
  59. lua_getglobal(state, "__uloop_cb");
  60. lua_rawgeti(state, -1, tout->r);
  61. lua_remove(state, -2);
  62. lua_call(state, 0, 0);
  63. }
  64. static int ul_timer_set(lua_State *L)
  65. {
  66. struct lua_uloop_timeout *tout;
  67. double set;
  68. if (!lua_isnumber(L, -1)) {
  69. lua_pushstring(L, "invalid arg list");
  70. lua_error(L);
  71. return 0;
  72. }
  73. set = lua_tointeger(L, -1);
  74. tout = lua_touserdata(L, 1);
  75. uloop_timeout_set(&tout->t, set);
  76. return 1;
  77. }
  78. static int ul_timer_free(lua_State *L)
  79. {
  80. struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
  81. uloop_timeout_cancel(&tout->t);
  82. /* obj.__index.__gc = nil , make sure executing only once*/
  83. lua_getfield(L, -1, "__index");
  84. lua_pushstring(L, "__gc");
  85. lua_pushnil(L);
  86. lua_settable(L, -3);
  87. lua_getglobal(state, "__uloop_cb");
  88. luaL_unref(state, -1, tout->r);
  89. return 1;
  90. }
  91. static const luaL_Reg timer_m[] = {
  92. { "set", ul_timer_set },
  93. { "cancel", ul_timer_free },
  94. { NULL, NULL }
  95. };
  96. static int ul_timer(lua_State *L)
  97. {
  98. struct lua_uloop_timeout *tout;
  99. int set = 0;
  100. int ref;
  101. if (lua_isnumber(L, -1)) {
  102. set = lua_tointeger(L, -1);
  103. lua_pop(L, 1);
  104. }
  105. if (!lua_isfunction(L, -1)) {
  106. lua_pushstring(L, "invalid arg list");
  107. lua_error(L);
  108. return 0;
  109. }
  110. lua_getglobal(L, "__uloop_cb");
  111. lua_pushvalue(L, -2);
  112. ref = luaL_ref(L, -2);
  113. tout = ul_create_userdata(L, sizeof(*tout), timer_m, ul_timer_free);
  114. tout->r = ref;
  115. tout->t.cb = ul_timer_cb;
  116. if (set)
  117. uloop_timeout_set(&tout->t, set);
  118. return 1;
  119. }
  120. static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
  121. {
  122. struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
  123. lua_getglobal(state, "__uloop_cb");
  124. lua_rawgeti(state, -1, ufd->r);
  125. lua_remove(state, -2);
  126. /* push fd object */
  127. lua_getglobal(state, "__uloop_fds");
  128. lua_rawgeti(state, -1, ufd->fd_r);
  129. lua_remove(state, -2);
  130. /* push events */
  131. lua_pushinteger(state, events);
  132. lua_call(state, 2, 0);
  133. }
  134. static int get_sock_fd(lua_State* L, int idx) {
  135. int fd;
  136. if(lua_isnumber(L, idx)) {
  137. fd = lua_tonumber(L, idx);
  138. } else {
  139. luaL_checktype(L, idx, LUA_TUSERDATA);
  140. lua_getfield(L, idx, "getfd");
  141. if(lua_isnil(L, -1))
  142. return luaL_error(L, "socket type missing 'getfd' method");
  143. lua_pushvalue(L, idx - 1);
  144. lua_call(L, 1, 1);
  145. fd = lua_tointeger(L, -1);
  146. lua_pop(L, 1);
  147. }
  148. return fd;
  149. }
  150. static int ul_ufd_delete(lua_State *L)
  151. {
  152. struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
  153. uloop_fd_delete(&ufd->fd);
  154. /* obj.__index.__gc = nil , make sure executing only once*/
  155. lua_getfield(L, -1, "__index");
  156. lua_pushstring(L, "__gc");
  157. lua_pushnil(L);
  158. lua_settable(L, -3);
  159. lua_getglobal(state, "__uloop_cb");
  160. luaL_unref(state, -1, ufd->r);
  161. lua_remove(state, -1);
  162. lua_getglobal(state, "__uloop_fds");
  163. luaL_unref(state, -1, ufd->fd_r);
  164. lua_remove(state, -1);
  165. return 1;
  166. }
  167. static const luaL_Reg ufd_m[] = {
  168. { "delete", ul_ufd_delete },
  169. { NULL, NULL }
  170. };
  171. static int ul_ufd_add(lua_State *L)
  172. {
  173. struct lua_uloop_fd *ufd;
  174. int fd = 0;
  175. unsigned int flags = 0;
  176. int ref;
  177. int fd_ref;
  178. if (lua_isnumber(L, -1)) {
  179. flags = lua_tointeger(L, -1);
  180. lua_pop(L, 1);
  181. }
  182. if (!lua_isfunction(L, -1)) {
  183. lua_pushstring(L, "invalid arg list");
  184. lua_error(L);
  185. return 0;
  186. }
  187. fd = get_sock_fd(L, -2);
  188. lua_getglobal(L, "__uloop_cb");
  189. lua_pushvalue(L, -2);
  190. ref = luaL_ref(L, -2);
  191. lua_pop(L, 1);
  192. lua_getglobal(L, "__uloop_fds");
  193. lua_pushvalue(L, -3);
  194. fd_ref = luaL_ref(L, -2);
  195. lua_pop(L, 1);
  196. ufd = ul_create_userdata(L, sizeof(*ufd), ufd_m, ul_ufd_delete);
  197. ufd->r = ref;
  198. ufd->fd.fd = fd;
  199. ufd->fd_r = fd_ref;
  200. ufd->fd.cb = ul_ufd_cb;
  201. if (flags)
  202. uloop_fd_add(&ufd->fd, flags);
  203. return 1;
  204. }
  205. static int ul_process_free(lua_State *L)
  206. {
  207. struct lua_uloop_process *proc = lua_touserdata(L, 1);
  208. /* obj.__index.__gc = nil , make sure executing only once*/
  209. lua_getfield(L, -1, "__index");
  210. lua_pushstring(L, "__gc");
  211. lua_pushnil(L);
  212. lua_settable(L, -3);
  213. if (proc->r != LUA_NOREF) {
  214. uloop_process_delete(&proc->p);
  215. lua_getglobal(state, "__uloop_cb");
  216. luaL_unref(state, -1, proc->r);
  217. lua_remove(state, -1);
  218. }
  219. return 1;
  220. }
  221. static const luaL_Reg process_m[] = {
  222. { "delete", ul_process_free },
  223. { NULL, NULL }
  224. };
  225. static void ul_process_cb(struct uloop_process *p, int ret)
  226. {
  227. struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
  228. lua_getglobal(state, "__uloop_cb");
  229. lua_rawgeti(state, -1, proc->r);
  230. luaL_unref(state, -2, proc->r);
  231. proc->r = LUA_NOREF;
  232. lua_remove(state, -2);
  233. lua_pushinteger(state, ret >> 8);
  234. lua_call(state, 1, 0);
  235. }
  236. static int ul_process(lua_State *L)
  237. {
  238. struct lua_uloop_process *proc;
  239. pid_t pid;
  240. int ref;
  241. if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
  242. !lua_istable(L, -3) || !lua_isstring(L, -4)) {
  243. lua_pushstring(L, "invalid arg list");
  244. lua_error(L);
  245. return 0;
  246. }
  247. pid = fork();
  248. if (pid == -1) {
  249. lua_pushstring(L, "failed to fork");
  250. lua_error(L);
  251. return 0;
  252. }
  253. if (pid == 0) {
  254. /* child */
  255. int argn = lua_objlen(L, -3);
  256. int envn = lua_objlen(L, -2);
  257. char** argp = malloc(sizeof(char*) * (argn + 2));
  258. char** envp = malloc(sizeof(char*) * (envn + 1));
  259. int i = 1;
  260. if (!argp || !envp)
  261. _exit(-1);
  262. argp[0] = (char*) lua_tostring(L, -4);
  263. for (i = 1; i <= argn; i++) {
  264. lua_rawgeti(L, -3, i);
  265. argp[i] = (char*) lua_tostring(L, -1);
  266. lua_pop(L, 1);
  267. }
  268. argp[i] = NULL;
  269. for (i = 1; i <= envn; i++) {
  270. lua_rawgeti(L, -2, i);
  271. envp[i - 1] = (char*) lua_tostring(L, -1);
  272. lua_pop(L, 1);
  273. }
  274. envp[i - 1] = NULL;
  275. execve(*argp, argp, envp);
  276. _exit(-1);
  277. }
  278. lua_getglobal(L, "__uloop_cb");
  279. lua_pushvalue(L, -2);
  280. ref = luaL_ref(L, -2);
  281. proc = ul_create_userdata(L, sizeof(*proc), process_m, ul_process_free);
  282. proc->r = ref;
  283. proc->p.pid = pid;
  284. proc->p.cb = ul_process_cb;
  285. uloop_process_add(&proc->p);
  286. return 1;
  287. }
  288. static int ul_init(lua_State *L)
  289. {
  290. uloop_init();
  291. lua_pushboolean(L, 1);
  292. return 1;
  293. }
  294. static int ul_run(lua_State *L)
  295. {
  296. uloop_run();
  297. lua_pushboolean(L, 1);
  298. return 1;
  299. }
  300. static int ul_end(lua_State *L)
  301. {
  302. uloop_end();
  303. return 1;
  304. }
  305. static luaL_reg uloop_func[] = {
  306. {"init", ul_init},
  307. {"run", ul_run},
  308. {"timer", ul_timer},
  309. {"process", ul_process},
  310. {"fd_add", ul_ufd_add},
  311. {"cancel", ul_end},
  312. {NULL, NULL},
  313. };
  314. /* avoid warnings about missing declarations */
  315. int luaopen_uloop(lua_State *L);
  316. int luaclose_uloop(lua_State *L);
  317. int luaopen_uloop(lua_State *L)
  318. {
  319. state = L;
  320. lua_createtable(L, 1, 0);
  321. lua_setglobal(L, "__uloop_cb");
  322. lua_createtable(L, 1, 0);
  323. lua_setglobal(L, "__uloop_fds");
  324. luaL_openlib(L, "uloop", uloop_func, 0);
  325. lua_pushstring(L, "_VERSION");
  326. lua_pushstring(L, "1.0");
  327. lua_rawset(L, -3);
  328. lua_pushstring(L, "ULOOP_READ");
  329. lua_pushinteger(L, ULOOP_READ);
  330. lua_rawset(L, -3);
  331. lua_pushstring(L, "ULOOP_WRITE");
  332. lua_pushinteger(L, ULOOP_WRITE);
  333. lua_rawset(L, -3);
  334. lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
  335. lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
  336. lua_rawset(L, -3);
  337. lua_pushstring(L, "ULOOP_BLOCKING");
  338. lua_pushinteger(L, ULOOP_BLOCKING);
  339. lua_rawset(L, -3);
  340. return 1;
  341. }
  342. int luaclose_uloop(lua_State *L)
  343. {
  344. lua_pushstring(L, "Called");
  345. return 1;
  346. }