uloop.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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_remaining(lua_State *L)
  79. {
  80. struct lua_uloop_timeout *tout;
  81. tout = lua_touserdata(L, 1);
  82. lua_pushnumber(L, uloop_timeout_remaining(&tout->t));
  83. return 1;
  84. }
  85. static int ul_timer_free(lua_State *L)
  86. {
  87. struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
  88. uloop_timeout_cancel(&tout->t);
  89. /* obj.__index.__gc = nil , make sure executing only once*/
  90. lua_getfield(L, -1, "__index");
  91. lua_pushstring(L, "__gc");
  92. lua_pushnil(L);
  93. lua_settable(L, -3);
  94. lua_getglobal(state, "__uloop_cb");
  95. luaL_unref(state, -1, tout->r);
  96. return 1;
  97. }
  98. static const luaL_Reg timer_m[] = {
  99. { "set", ul_timer_set },
  100. { "remaining", ul_timer_remaining },
  101. { "cancel", ul_timer_free },
  102. { NULL, NULL }
  103. };
  104. static int ul_timer(lua_State *L)
  105. {
  106. struct lua_uloop_timeout *tout;
  107. int set = 0;
  108. int ref;
  109. if (lua_isnumber(L, -1)) {
  110. set = lua_tointeger(L, -1);
  111. lua_pop(L, 1);
  112. }
  113. if (!lua_isfunction(L, -1)) {
  114. lua_pushstring(L, "invalid arg list");
  115. lua_error(L);
  116. return 0;
  117. }
  118. lua_getglobal(L, "__uloop_cb");
  119. lua_pushvalue(L, -2);
  120. ref = luaL_ref(L, -2);
  121. tout = ul_create_userdata(L, sizeof(*tout), timer_m, ul_timer_free);
  122. tout->r = ref;
  123. tout->t.cb = ul_timer_cb;
  124. if (set)
  125. uloop_timeout_set(&tout->t, set);
  126. return 1;
  127. }
  128. static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
  129. {
  130. struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
  131. lua_getglobal(state, "__uloop_cb");
  132. lua_rawgeti(state, -1, ufd->r);
  133. lua_remove(state, -2);
  134. /* push fd object */
  135. lua_getglobal(state, "__uloop_fds");
  136. lua_rawgeti(state, -1, ufd->fd_r);
  137. lua_remove(state, -2);
  138. /* push events */
  139. lua_pushinteger(state, events);
  140. lua_call(state, 2, 0);
  141. }
  142. static int get_sock_fd(lua_State* L, int idx) {
  143. int fd;
  144. if(lua_isnumber(L, idx)) {
  145. fd = lua_tonumber(L, idx);
  146. } else {
  147. luaL_checktype(L, idx, LUA_TUSERDATA);
  148. lua_getfield(L, idx, "getfd");
  149. if(lua_isnil(L, -1))
  150. return luaL_error(L, "socket type missing 'getfd' method");
  151. lua_pushvalue(L, idx - 1);
  152. lua_call(L, 1, 1);
  153. fd = lua_tointeger(L, -1);
  154. lua_pop(L, 1);
  155. }
  156. return fd;
  157. }
  158. static int ul_ufd_delete(lua_State *L)
  159. {
  160. struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
  161. uloop_fd_delete(&ufd->fd);
  162. /* obj.__index.__gc = nil , make sure executing only once*/
  163. lua_getfield(L, -1, "__index");
  164. lua_pushstring(L, "__gc");
  165. lua_pushnil(L);
  166. lua_settable(L, -3);
  167. lua_getglobal(state, "__uloop_cb");
  168. luaL_unref(state, -1, ufd->r);
  169. lua_remove(state, -1);
  170. lua_getglobal(state, "__uloop_fds");
  171. luaL_unref(state, -1, ufd->fd_r);
  172. lua_remove(state, -1);
  173. return 1;
  174. }
  175. static const luaL_Reg ufd_m[] = {
  176. { "delete", ul_ufd_delete },
  177. { NULL, NULL }
  178. };
  179. static int ul_ufd_add(lua_State *L)
  180. {
  181. struct lua_uloop_fd *ufd;
  182. int fd = 0;
  183. unsigned int flags = 0;
  184. int ref;
  185. int fd_ref;
  186. if (lua_isnumber(L, -1)) {
  187. flags = lua_tointeger(L, -1);
  188. lua_pop(L, 1);
  189. }
  190. if (!lua_isfunction(L, -1)) {
  191. lua_pushstring(L, "invalid arg list");
  192. lua_error(L);
  193. return 0;
  194. }
  195. fd = get_sock_fd(L, -2);
  196. lua_getglobal(L, "__uloop_cb");
  197. lua_pushvalue(L, -2);
  198. ref = luaL_ref(L, -2);
  199. lua_pop(L, 1);
  200. lua_getglobal(L, "__uloop_fds");
  201. lua_pushvalue(L, -3);
  202. fd_ref = luaL_ref(L, -2);
  203. lua_pop(L, 1);
  204. ufd = ul_create_userdata(L, sizeof(*ufd), ufd_m, ul_ufd_delete);
  205. ufd->r = ref;
  206. ufd->fd.fd = fd;
  207. ufd->fd_r = fd_ref;
  208. ufd->fd.cb = ul_ufd_cb;
  209. if (flags)
  210. uloop_fd_add(&ufd->fd, flags);
  211. return 1;
  212. }
  213. static int ul_process_free(lua_State *L)
  214. {
  215. struct lua_uloop_process *proc = lua_touserdata(L, 1);
  216. /* obj.__index.__gc = nil , make sure executing only once*/
  217. lua_getfield(L, -1, "__index");
  218. lua_pushstring(L, "__gc");
  219. lua_pushnil(L);
  220. lua_settable(L, -3);
  221. if (proc->r != LUA_NOREF) {
  222. uloop_process_delete(&proc->p);
  223. lua_getglobal(state, "__uloop_cb");
  224. luaL_unref(state, -1, proc->r);
  225. lua_remove(state, -1);
  226. }
  227. return 1;
  228. }
  229. static int ul_process_pid(lua_State *L)
  230. {
  231. struct lua_uloop_process *proc = lua_touserdata(L, 1);
  232. if (proc->p.pid) {
  233. lua_pushnumber(L, proc->p.pid);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. static const luaL_Reg process_m[] = {
  239. { "delete", ul_process_free },
  240. { "pid", ul_process_pid },
  241. { NULL, NULL }
  242. };
  243. static void ul_process_cb(struct uloop_process *p, int ret)
  244. {
  245. struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
  246. lua_getglobal(state, "__uloop_cb");
  247. lua_rawgeti(state, -1, proc->r);
  248. luaL_unref(state, -2, proc->r);
  249. proc->r = LUA_NOREF;
  250. lua_remove(state, -2);
  251. lua_pushinteger(state, ret >> 8);
  252. lua_call(state, 1, 0);
  253. }
  254. static int ul_process(lua_State *L)
  255. {
  256. struct lua_uloop_process *proc;
  257. pid_t pid;
  258. int ref;
  259. if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
  260. !lua_istable(L, -3) || !lua_isstring(L, -4)) {
  261. lua_pushstring(L, "invalid arg list");
  262. lua_error(L);
  263. return 0;
  264. }
  265. pid = fork();
  266. if (pid == -1) {
  267. lua_pushstring(L, "failed to fork");
  268. lua_error(L);
  269. return 0;
  270. }
  271. if (pid == 0) {
  272. /* child */
  273. int argn = lua_objlen(L, -3);
  274. int envn = lua_objlen(L, -2);
  275. char** argp = malloc(sizeof(char*) * (argn + 2));
  276. char** envp = malloc(sizeof(char*) * (envn + 1));
  277. int i = 1;
  278. if (!argp || !envp)
  279. _exit(-1);
  280. argp[0] = (char*) lua_tostring(L, -4);
  281. for (i = 1; i <= argn; i++) {
  282. lua_rawgeti(L, -3, i);
  283. argp[i] = (char*) lua_tostring(L, -1);
  284. lua_pop(L, 1);
  285. }
  286. argp[i] = NULL;
  287. for (i = 1; i <= envn; i++) {
  288. lua_rawgeti(L, -2, i);
  289. envp[i - 1] = (char*) lua_tostring(L, -1);
  290. lua_pop(L, 1);
  291. }
  292. envp[i - 1] = NULL;
  293. execve(*argp, argp, envp);
  294. _exit(-1);
  295. }
  296. lua_getglobal(L, "__uloop_cb");
  297. lua_pushvalue(L, -2);
  298. ref = luaL_ref(L, -2);
  299. proc = ul_create_userdata(L, sizeof(*proc), process_m, ul_process_free);
  300. proc->r = ref;
  301. proc->p.pid = pid;
  302. proc->p.cb = ul_process_cb;
  303. uloop_process_add(&proc->p);
  304. return 1;
  305. }
  306. static int ul_init(lua_State *L)
  307. {
  308. uloop_init();
  309. lua_pushboolean(L, 1);
  310. return 1;
  311. }
  312. static int ul_run(lua_State *L)
  313. {
  314. uloop_run();
  315. lua_pushboolean(L, 1);
  316. return 1;
  317. }
  318. static int ul_end(lua_State *L)
  319. {
  320. uloop_end();
  321. return 1;
  322. }
  323. static luaL_reg uloop_func[] = {
  324. {"init", ul_init},
  325. {"run", ul_run},
  326. {"timer", ul_timer},
  327. {"process", ul_process},
  328. {"fd_add", ul_ufd_add},
  329. {"cancel", ul_end},
  330. {NULL, NULL},
  331. };
  332. /* avoid warnings about missing declarations */
  333. int luaopen_uloop(lua_State *L);
  334. int luaclose_uloop(lua_State *L);
  335. int luaopen_uloop(lua_State *L)
  336. {
  337. state = L;
  338. lua_createtable(L, 1, 0);
  339. lua_setglobal(L, "__uloop_cb");
  340. lua_createtable(L, 1, 0);
  341. lua_setglobal(L, "__uloop_fds");
  342. luaL_openlib(L, "uloop", uloop_func, 0);
  343. lua_pushstring(L, "_VERSION");
  344. lua_pushstring(L, "1.0");
  345. lua_rawset(L, -3);
  346. lua_pushstring(L, "ULOOP_READ");
  347. lua_pushinteger(L, ULOOP_READ);
  348. lua_rawset(L, -3);
  349. lua_pushstring(L, "ULOOP_WRITE");
  350. lua_pushinteger(L, ULOOP_WRITE);
  351. lua_rawset(L, -3);
  352. lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
  353. lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
  354. lua_rawset(L, -3);
  355. lua_pushstring(L, "ULOOP_BLOCKING");
  356. lua_pushinteger(L, ULOOP_BLOCKING);
  357. lua_rawset(L, -3);
  358. return 1;
  359. }
  360. int luaclose_uloop(lua_State *L)
  361. {
  362. lua_pushstring(L, "Called");
  363. return 1;
  364. }