moonbridge

view moonbridge_io.c @ 217:0b6555732047

Changed version number to 1.0.0
author jbe
date Tue Jun 23 02:05:46 2015 +0200 (2015-06-23)
parents b124f8f3ca79
children 35ea31e45b43
line source
2 #ifndef __has_include
3 #define __has_include(x) 0
4 #endif
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netinet/in.h>
17 #include <netinet/tcp.h>
18 #include <sys/select.h>
19 #include <time.h>
20 #include <netdb.h>
21 #include <arpa/inet.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
25 #include <bsd/unistd.h>
26 #endif
28 #include <lua.h>
29 #include <lauxlib.h>
30 #include <lualib.h>
32 #include <assert.h>
34 #define MOONBR_IO_MAXSTRERRORLEN 80
35 #define MOONBR_IO_READBUFLEN 4096
36 #define MOONBR_IO_WRITEBUFLEN 4096
38 #define MOONBR_IO_LISTEN_BACKLOG 1024
40 #define MOONBR_IO_STRERROR_R_MSG "Error detail unavailable due to noncompliant strerror_r() implementation"
41 #define moonbr_io_errmsg() \
42 char errmsg[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG; \
43 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
45 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
46 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
47 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
48 #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child"
49 #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt"
51 typedef struct {
52 int fd;
53 int issock;
54 sa_family_t addrfam;
55 int finished;
56 int closed;
57 int nonblocking;
58 int nopush;
59 int readerr;
60 int readbufin;
61 int readbufout;
62 int writeerr;
63 size_t writeleft;
64 size_t flushedleft;
65 #if LUA_VERSION_NUM >= 503
66 lua_Integer writeqin;
67 lua_Integer writeqout;
68 #else
69 int writeqin;
70 int writeqout;
71 #endif
72 size_t writeqoff;
73 int writebufin;
74 int writebufout;
75 char readbuf[MOONBR_IO_READBUFLEN];
76 char writebuf[MOONBR_IO_WRITEBUFLEN];
77 } moonbr_io_handle_t;
79 typedef struct {
80 int fd;
81 sa_family_t addrfam;
82 int nonblocking;
83 } moonbr_io_listener_t;
85 typedef struct {
86 pid_t pid;
87 } moonbr_io_child_t;
89 static int moonbr_io_yield(lua_State *L) {
90 return lua_yield(L, lua_gettop(L));
91 }
93 #if LUA_VERSION_NUM >= 503
94 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
95 #else
96 static int moonbr_io_cont_returnall(lua_State *L) {
97 #endif
98 return lua_gettop(L);
99 }
101 #define moonbr_io_yield_wrapper(yieldfunc, callfunc) \
102 static int yieldfunc(lua_State *L) { \
103 int args; \
104 lua_pushcfunction(L, callfunc); \
105 lua_insert(L, 1); \
106 args = lua_gettop(L); \
107 lua_pushcfunction(L, moonbr_io_yield); \
108 lua_insert(L, 3); \
109 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall); \
110 return lua_gettop(L); \
111 }
113 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
114 int flags;
115 if (handle->nonblocking == nonblocking) return;
116 flags = fcntl(handle->fd, F_GETFL, 0);
117 if (flags == -1) {
118 moonbr_io_errmsg();
119 close(handle->fd);
120 handle->fd = -1;
121 handle->closed = 1;
122 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
123 }
124 if (nonblocking) flags |= O_NONBLOCK;
125 else flags &= ~O_NONBLOCK;
126 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
127 moonbr_io_errmsg();
128 close(handle->fd);
129 handle->fd = -1;
130 handle->closed = 1;
131 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
132 }
133 handle->nonblocking = nonblocking;
134 }
136 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
137 struct linger lingerval = { 0, };
138 if (!handle->issock) return;
139 if (timeout >= 0) {
140 lingerval.l_onoff = 1;
141 lingerval.l_linger = timeout;
142 }
143 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
144 moonbr_io_errmsg();
145 close(handle->fd);
146 handle->fd = -1;
147 handle->closed = 1;
148 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
149 }
150 }
152 static inline void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
153 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
154 if (
155 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
156 handle->nopush == nopush
157 ) return;
158 #if defined(TCP_NOPUSH)
159 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
160 #elif defined(TCP_CORK)
161 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
162 #endif
163 moonbr_io_errmsg();
164 close(handle->fd);
165 handle->fd = -1;
166 handle->closed = 1;
167 #if defined(TCP_NOPUSH)
168 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
169 #elif defined(TCP_CORK)
170 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
171 #endif
172 }
173 handle->nopush = nopush;
174 #else
175 #warning Neither TCP_NOPUSH nor TCP_CORK is available
176 #endif
177 }
179 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
180 moonbr_io_handle_t *handle;
181 lua_Integer maxread;
182 const char *terminatorstr;
183 size_t terminatorlen;
184 char terminator = 0; /* initialize to avoid compiler warning */
185 luaL_Buffer luabuf;
186 size_t luabufcnt = 0;
187 int remaining;
188 char *terminatorpos;
189 ssize_t bytesread;
190 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
191 maxread = luaL_optinteger(L, 2, -1);
192 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
193 if (terminatorlen) {
194 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
195 terminator = terminatorstr[0];
196 }
197 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
198 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
199 if (handle->readerr) {
200 lua_pushnil(L);
201 lua_pushliteral(L, "Previous read error");
202 return 2;
203 }
204 if (handle->fd < 0) {
205 /* fake EOF to simulate shutdown */
206 if (!drain) lua_pushliteral(L, "");
207 else lua_pushinteger(L, 0);
208 lua_pushliteral(L, "eof");
209 return 2;
210 }
211 handle->readerr = 1;
212 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
213 if (!drain) luaL_buffinit(L, &luabuf);
214 while (1) {
215 remaining = -1;
216 terminatorpos = NULL;
217 if (
218 maxread >= 0 &&
219 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
220 ) {
221 remaining = (size_t)maxread - luabufcnt;
222 if (terminatorlen) {
223 terminatorpos = memchr(
224 handle->readbuf + handle->readbufout,
225 terminator,
226 remaining
227 );
228 }
229 } else if (terminatorlen) {
230 terminatorpos = memchr(
231 handle->readbuf + handle->readbufout,
232 terminator,
233 handle->readbufin - handle->readbufout
234 );
235 }
236 if (terminatorpos) remaining = 1 + (
237 terminatorpos - (handle->readbuf + handle->readbufout)
238 );
239 if (remaining >= 0) {
240 if (!drain) {
241 luaL_addlstring(
242 &luabuf,
243 handle->readbuf + handle->readbufout,
244 remaining
245 );
246 luaL_pushresult(&luabuf);
247 } else {
248 lua_pushinteger(L, luabufcnt + remaining);
249 }
250 if (terminatorpos) lua_pushliteral(L, "term");
251 else lua_pushliteral(L, "maxlen");
252 handle->readbufout += remaining;
253 if (handle->readbufout == handle->readbufin) {
254 handle->readbufin = 0;
255 handle->readbufout = 0;
256 }
257 handle->readerr = 0;
258 return 2;
259 }
260 if (!drain) luaL_addlstring(
261 &luabuf,
262 handle->readbuf + handle->readbufout,
263 handle->readbufin - handle->readbufout
264 );
265 luabufcnt += handle->readbufin - handle->readbufout;
266 handle->readbufout = 0;
267 do {
268 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
269 } while (bytesread < 0 && (errno == EINTR));
270 if (
271 bytesread == 0 || (
272 nonblocking &&
273 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
274 )
275 ) {
276 handle->readbufin = 0;
277 if (!drain) luaL_pushresult(&luabuf);
278 else lua_pushinteger(L, luabufcnt);
279 if (bytesread == 0) lua_pushliteral(L, "eof");
280 else lua_pushliteral(L, "block");
281 handle->readerr = 0;
282 return 2;
283 }
284 if (bytesread < 0) {
285 moonbr_io_errmsg();
286 lua_pushnil(L);
287 lua_pushstring(L, errmsg);
288 return 2;
289 }
290 handle->readbufin = bytesread;
291 }
292 }
294 static int moonbr_io_read(lua_State *L) {
295 return moonbr_io_read_impl(L, 0, 0);
296 }
298 static int moonbr_io_read_nb(lua_State *L) {
299 return moonbr_io_read_impl(L, 1, 0);
300 }
302 static int moonbr_io_drain(lua_State *L) {
303 return moonbr_io_read_impl(L, 0, 1);
304 }
306 static int moonbr_io_drain_nb(lua_State *L) {
307 return moonbr_io_read_impl(L, 1, 1);
308 }
310 #if LUA_VERSION_NUM >= 503
311 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
312 #else
313 static int moonbr_io_read_cont(lua_State *L) {
314 #endif
315 lua_Integer remaining;
316 size_t len;
317 #if !(LUA_VERSION_NUM >= 503)
318 int ctx = 0;
319 lua_getctx(L, &ctx);
320 #endif
321 remaining = lua_tointeger(L, 3);
322 while (1) {
323 lua_pushcfunction(L, moonbr_io_read_nb);
324 lua_pushvalue(L, 1);
325 lua_pushvalue(L, 3);
326 lua_pushvalue(L, 4);
327 lua_call(L, 3, 2);
328 if (lua_isnil(L, -2)) return 2;
329 lua_insert(L, -2);
330 len = lua_rawlen(L, -1);
331 if (ctx == 0) {
332 lua_replace(L, 5);
333 ctx = 1;
334 } else if (ctx == 1) {
335 lua_pushvalue(L, 5);
336 lua_newtable(L);
337 lua_replace(L, 5);
338 lua_rawseti(L, 5, 2);
339 lua_rawseti(L, 5, 1);
340 ctx = 2;
341 } else {
342 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
343 }
344 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
345 lua_pop(L, 1);
346 if (remaining >= 0 && len) {
347 remaining -= len;
348 lua_pushinteger(L, remaining);
349 lua_replace(L, 3);
350 }
351 lua_pushvalue(L, 2);
352 lua_callk(L, 0, 0, ctx, moonbr_io_read_cont);
353 }
354 if (ctx == 1) {
355 lua_pushvalue(L, 5);
356 } else {
357 luaL_Buffer buf;
358 lua_Integer i, chunkcount;
359 chunkcount = lua_rawlen(L, 5);
360 luaL_buffinit(L, &buf);
361 for (i=1; i<=chunkcount && i>0; i++) {
362 lua_rawgeti(L, 5, i);
363 luaL_addvalue(&buf);
364 }
365 luaL_pushresult(&buf);
366 }
367 lua_pushvalue(L, -2);
368 return 2;
369 }
371 static int moonbr_io_read_call(lua_State *L) {
372 lua_settop(L, 4);
373 lua_pushnil(L);
374 #if LUA_VERSION_NUM >= 503
375 return moonbr_io_read_cont(L, 0, 0);
376 #else
377 return moonbr_io_read_cont(L);
378 #endif
379 }
381 moonbr_io_yield_wrapper(moonbr_io_read_yield, moonbr_io_read_call);
383 #if LUA_VERSION_NUM >= 503
384 static int moonbr_io_drain_cont(lua_State *L, int status, lua_KContext ctx) {
385 #else
386 static int moonbr_io_drain_cont(lua_State *L) {
387 #endif
388 lua_Integer remaining, len;
389 size_t totallen = 0;
390 #if !(LUA_VERSION_NUM >= 503)
391 int ctx = 0;
392 lua_getctx(L, &ctx);
393 #endif
394 remaining = lua_tointeger(L, 3);
395 while (1) {
396 lua_pushcfunction(L, moonbr_io_drain_nb);
397 lua_pushvalue(L, 1);
398 lua_pushvalue(L, 3);
399 lua_pushvalue(L, 4);
400 lua_call(L, 3, 2);
401 if (lua_isnil(L, -2)) return 2;
402 lua_insert(L, -2);
403 len = lua_tointeger(L, -1);
404 lua_pop(L, 1);
405 totallen += len;
406 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
407 lua_pop(L, 1);
408 if (remaining >= 0 && len) {
409 remaining -= len;
410 lua_pushinteger(L, remaining);
411 lua_replace(L, 3);
412 }
413 lua_pushvalue(L, 2);
414 lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont);
415 }
416 lua_pushinteger(L, totallen);
417 lua_pushvalue(L, -2);
418 return 2;
419 }
421 static int moonbr_io_drain_call(lua_State *L) {
422 #if LUA_VERSION_NUM >= 503
423 return moonbr_io_drain_cont(L, 0, 0);
424 #else
425 return moonbr_io_drain_cont(L);
426 #endif
427 }
429 moonbr_io_yield_wrapper(moonbr_io_drain_yield, moonbr_io_drain_call);
431 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
432 moonbr_io_handle_t *handle;
433 int i, top;
434 const char *str;
435 size_t strlen;
436 ssize_t written;
437 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
438 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
439 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
440 if (handle->writeerr) {
441 lua_pushnil(L);
442 lua_pushliteral(L, "Previous write error");
443 return 2;
444 }
445 handle->writeerr = 1;
446 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
447 top = lua_gettop(L);
448 lua_getuservalue(L, 1);
449 lua_getfield(L, -1, "writequeue");
450 for (i=2; i<=top; i++) {
451 luaL_checklstring(L, i, &strlen);
452 lua_pushvalue(L, i);
453 lua_rawseti(L, -2, handle->writeqin++);
454 handle->writeleft += strlen;
455 }
456 if (flush) handle->flushedleft = handle->writeleft;
457 while (handle->writeqout != handle->writeqin) {
458 lua_rawgeti(L, -1, handle->writeqout);
459 str = lua_tolstring(L, -1, &strlen);
460 while (handle->writeqoff < strlen) {
461 if (
462 strlen - handle->writeqoff <
463 MOONBR_IO_WRITEBUFLEN - handle->writebufin
464 ) {
465 memcpy(
466 handle->writebuf + handle->writebufin,
467 str + handle->writeqoff,
468 strlen - handle->writeqoff
469 );
470 handle->writebufin += strlen - handle->writeqoff;
471 break;
472 } else {
473 memcpy(
474 handle->writebuf + handle->writebufin,
475 str + handle->writeqoff,
476 MOONBR_IO_WRITEBUFLEN - handle->writebufin
477 );
478 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
479 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
480 moonbr_io_handle_set_nopush(L, handle, 1);
481 written = write(
482 handle->fd,
483 handle->writebuf + handle->writebufout,
484 MOONBR_IO_WRITEBUFLEN - handle->writebufout
485 );
486 if (written < 0) {
487 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
488 goto moonbr_io_write_impl_block;
489 } else if (errno != EINTR) {
490 moonbr_io_errmsg();
491 lua_pushnil(L);
492 lua_pushstring(L, errmsg);
493 return 2;
494 }
495 } else {
496 handle->writebufout += written;
497 handle->writeleft -= written;
498 if (handle->flushedleft) {
499 if (written >= handle->flushedleft) {
500 handle->flushedleft = 0;
501 moonbr_io_handle_set_nopush(L, handle, 0);
502 } else {
503 handle->flushedleft -= written;
504 }
505 }
506 }
507 }
508 handle->writebufin = 0;
509 handle->writebufout = 0;
510 }
511 }
512 handle->writeqoff = 0;
513 lua_pop(L, 1);
514 lua_pushnil(L);
515 lua_rawseti(L, -2, handle->writeqout++);
516 }
517 while (handle->flushedleft) {
518 moonbr_io_handle_set_nopush(L, handle, 1);
519 written = write(
520 handle->fd,
521 handle->writebuf + handle->writebufout,
522 handle->writebufin - handle->writebufout
523 );
524 if (written < 0) {
525 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
526 goto moonbr_io_write_impl_block;
527 } else if (errno != EINTR) {
528 moonbr_io_errmsg();
529 lua_pushnil(L);
530 lua_pushstring(L, errmsg);
531 return 2;
532 }
533 } else {
534 handle->writebufout += written;
535 handle->writeleft -= written;
536 if (handle->flushedleft) {
537 if (written >= handle->flushedleft) {
538 handle->flushedleft = 0;
539 moonbr_io_handle_set_nopush(L, handle, 0);
540 } else {
541 handle->flushedleft -= written;
542 }
543 }
544 }
545 }
546 if (handle->writebufout == handle->writebufin) {
547 handle->writebufin = 0;
548 handle->writebufout = 0;
549 }
550 if (nonblocking) lua_pushinteger(L, 0);
551 else lua_pushvalue(L, 1);
552 handle->writeerr = 0;
553 return 1;
554 moonbr_io_write_impl_block:
555 lua_pushinteger(L, handle->writeleft);
556 handle->writeerr = 0;
557 return 1;
558 }
560 static int moonbr_io_write(lua_State *L) {
561 return moonbr_io_write_impl(L, 0, 0);
562 }
564 static int moonbr_io_write_nb(lua_State *L) {
565 return moonbr_io_write_impl(L, 1, 0);
566 }
568 static int moonbr_io_flush(lua_State *L) {
569 return moonbr_io_write_impl(L, 0, 1);
570 }
572 static int moonbr_io_flush_nb(lua_State *L) {
573 return moonbr_io_write_impl(L, 1, 1);
574 }
576 #if LUA_VERSION_NUM >= 503
577 static int moonbr_io_write_cont(lua_State *L, int status, lua_KContext ctx) {
578 #else
579 static int moonbr_io_write_cont(lua_State *L) {
580 #endif
581 while (1) {
582 lua_pushcfunction(L, moonbr_io_write_nb);
583 lua_pushvalue(L, 1);
584 lua_call(L, 1, 2);
585 if (lua_isnil(L, -2)) return 2;
586 if (!lua_tointeger(L, -2)) {
587 lua_pushvalue(L, 1);
588 return 1;
589 }
590 lua_pop(L, 2);
591 lua_pushvalue(L, 2);
592 lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
593 }
594 }
596 static int moonbr_io_write_call(lua_State *L) {
597 lua_pushcfunction(L, moonbr_io_write_nb);
598 lua_insert(L, 3);
599 lua_pushvalue(L, 1);
600 lua_insert(L, 4);
601 lua_call(L, lua_gettop(L) - 3, 2);
602 if (lua_isnil(L, -2)) return 2;
603 if (!lua_tointeger(L, -2)) {
604 lua_pushvalue(L, 1);
605 return 1;
606 }
607 #if LUA_VERSION_NUM >= 503
608 return moonbr_io_write_cont(L, 0, 0);
609 #else
610 return moonbr_io_write_cont(L);
611 #endif
612 }
614 moonbr_io_yield_wrapper(moonbr_io_write_yield, moonbr_io_write_call);
616 static int moonbr_io_flush_call(lua_State *L) {
617 lua_pushcfunction(L, moonbr_io_flush_nb);
618 lua_insert(L, 3);
619 lua_pushvalue(L, 1);
620 lua_insert(L, 4);
621 lua_call(L, lua_gettop(L) - 3, 2);
622 if (lua_isnil(L, -2)) return 2;
623 if (!lua_tointeger(L, -2)) {
624 lua_pushvalue(L, 1);
625 return 1;
626 }
627 #if LUA_VERSION_NUM >= 503
628 return moonbr_io_write_cont(L, 0, 0);
629 #else
630 return moonbr_io_write_cont(L);
631 #endif
632 }
634 moonbr_io_yield_wrapper(moonbr_io_flush_yield, moonbr_io_flush_call);
636 static int moonbr_io_finish(lua_State *L) {
637 moonbr_io_handle_t *handle;
638 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
639 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
640 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
641 if (handle->writeleft) {
642 lua_pushcfunction(L, moonbr_io_flush);
643 lua_pushvalue(L, 1);
644 if (lua_pcall(L, 1, 2, 0)) {
645 handle->finished = 1;
646 lua_error(L);
647 }
648 if (!lua_toboolean(L, -2)) {
649 handle->finished = 1;
650 return 2;
651 }
652 }
653 handle->finished = 1;
654 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
655 if (shutdown(handle->fd, SHUT_WR)) {
656 moonbr_io_errmsg();
657 lua_pushnil(L);
658 lua_pushstring(L, errmsg);
659 return 2;
660 }
661 } else {
662 if (close(handle->fd)) {
663 moonbr_io_errmsg();
664 handle->fd = -1;
665 lua_pushnil(L);
666 lua_pushstring(L, errmsg);
667 return 2;
668 }
669 handle->fd = -1; /* fake EOF on read */
670 }
671 lua_pushboolean(L, 1);
672 return 1;
673 }
675 static int moonbr_io_close_impl(lua_State *L, int reset) {
676 moonbr_io_handle_t *handle;
677 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
678 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
679 if (!reset) {
680 if (handle->writeleft) {
681 lua_pushcfunction(L, moonbr_io_flush);
682 lua_pushvalue(L, 1);
683 if (lua_pcall(L, 1, 2, 0)) {
684 handle->closed = 1;
685 close(handle->fd);
686 handle->fd = -1;
687 lua_error(L);
688 }
689 handle->closed = 1;
690 if (!lua_toboolean(L, -2)) {
691 close(handle->fd);
692 handle->fd = -1;
693 return 2;
694 }
695 } else {
696 handle->closed = 1;
697 moonbr_io_handle_set_linger(L, handle, -1);
698 }
699 } else {
700 handle->closed = 1;
701 }
702 if (handle->fd >= 0) {
703 if (close(handle->fd)) {
704 moonbr_io_errmsg();
705 handle->fd = -1;
706 lua_pushnil(L);
707 lua_pushstring(L, errmsg);
708 return 2;
709 }
710 handle->fd = -1;
711 }
712 lua_pushboolean(L, 1);
713 return 1;
715 }
717 static int moonbr_io_close(lua_State *L) {
718 return moonbr_io_close_impl(L, 0);
719 }
721 static int moonbr_io_reset(lua_State *L) {
722 return moonbr_io_close_impl(L, 1);
723 }
725 static int moonbr_io_handlegc(lua_State *L) {
726 moonbr_io_handle_t *handle;
727 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
728 if (handle->fd >= 0) {
729 lua_pushcfunction(L, moonbr_io_close);
730 lua_pushvalue(L, 1);
731 lua_pushinteger(L, 0);
732 lua_call(L, 2, 0);
733 }
734 return 0;
735 }
737 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
738 moonbr_io_handle_t *handle;
739 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
740 if (!handle->closed) {
741 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
742 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
743 lua_call(L, 1, 0);
744 }
745 }
747 static int moonbr_io_pushhandle_impl(lua_State *L) {
748 int *fd;
749 moonbr_io_handle_t *handle;
750 struct sockaddr addr;
751 socklen_t addrlen;
752 fd = lua_touserdata(L, 1);
753 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
754 handle->fd = -1; /* avoid closing incomplete handle */
755 addrlen = sizeof(addr);
756 if (getsockname(*fd, &addr, &addrlen)) {
757 if (errno != ENOTSOCK) {
758 moonbr_io_errmsg();
759 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
760 }
761 handle->issock = 0;
762 } else {
763 handle->issock = 1;
764 handle->addrfam = addr.sa_family;
765 }
766 handle->finished = 0;
767 handle->closed = 0;
768 handle->nonblocking = -1;
769 handle->nopush = -1;
770 handle->readerr = 0;
771 handle->readbufin = 0;
772 handle->readbufout = 0;
773 handle->writeerr = 0;
774 handle->writeleft = 0;
775 handle->flushedleft = 0;
776 handle->writeqin = 0;
777 handle->writeqout = 0;
778 handle->writeqoff = 0;
779 handle->writebufin = 0;
780 handle->writebufout = 0;
781 handle->fd = *fd; /* required for set_linger call */
782 moonbr_io_handle_set_linger(L, handle, 0);
783 handle->fd = -1; /* avoid closing incomplete handle */
784 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
785 lua_setmetatable(L, -2);
786 lua_newtable(L); // uservalue
787 lua_newtable(L);
788 lua_setfield(L, -2, "writequeue");
789 lua_newtable(L); // public
790 if (handle->addrfam == AF_INET6) {
791 struct sockaddr_in6 addr_in6;
792 char addrstrbuf[INET6_ADDRSTRLEN];
793 const char *addrstr;
794 addrlen = sizeof(addr_in6);
795 if (getsockname(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
796 moonbr_io_errmsg();
797 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
798 }
799 if (addrlen > sizeof(addr_in6)) {
800 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
801 }
802 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
803 if (!addrstr) {
804 moonbr_io_errmsg();
805 luaL_error(L, "Could not format local IP address: %s", errmsg);
806 } else {
807 lua_pushstring(L, addrstr);
808 lua_setfield(L, -2, "local_ip6");
809 }
810 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
811 lua_setfield(L, -2, "local_tcpport");
812 if (getpeername(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
813 moonbr_io_errmsg();
814 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
815 }
816 if (addrlen > sizeof(addr_in6)) {
817 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
818 }
819 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
820 if (!addrstr) {
821 moonbr_io_errmsg();
822 luaL_error(L, "Could not format remote IP address: %s", errmsg);
823 } else {
824 lua_pushstring(L, addrstr);
825 lua_setfield(L, -2, "remote_ip6");
826 }
827 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
828 lua_setfield(L, -2, "remote_tcpport");
829 } else if (handle->addrfam == AF_INET) {
830 struct sockaddr_in addr_in;
831 char addrstrbuf[INET_ADDRSTRLEN];
832 const char *addrstr;
833 addrlen = sizeof(addr_in);
834 if (getsockname(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
835 moonbr_io_errmsg();
836 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
837 }
838 if (addrlen > sizeof(addr_in)) {
839 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
840 }
841 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
842 if (!addrstr) {
843 moonbr_io_errmsg();
844 luaL_error(L, "Could not format local IP address: %s", errmsg);
845 } else {
846 lua_pushstring(L, addrstr);
847 lua_setfield(L, -2, "local_ip4");
848 }
849 lua_pushinteger(L, ntohs(addr_in.sin_port));
850 lua_setfield(L, -2, "local_tcpport");
851 if (getpeername(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
852 moonbr_io_errmsg();
853 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
854 }
855 if (addrlen > sizeof(addr_in)) {
856 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
857 }
858 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
859 if (!addrstr) {
860 moonbr_io_errmsg();
861 luaL_error(L, "Could not format remote IP address: %s", errmsg);
862 } else {
863 lua_pushstring(L, addrstr);
864 lua_setfield(L, -2, "remote_ip4");
865 }
866 lua_pushinteger(L, ntohs(addr_in.sin_port));
867 lua_setfield(L, -2, "remote_tcpport");
868 }
869 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
870 lua_setmetatable(L, -2);
871 lua_setfield(L, -2, "public");
872 lua_setuservalue(L, -2);
873 handle->fd = *fd;
874 *fd = -1; /* closing is now handled by garbage collection */
875 return 1;
876 }
878 void moonbr_io_pushhandle(lua_State *L, int fd) {
879 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
880 lua_pushlightuserdata(L, &fd);
881 if (lua_pcall(L, 1, 1, 0)) {
882 if (fd != -1) close(fd);
883 lua_error(L);
884 }
885 }
887 static int moonbr_io_handleindex(lua_State *L) {
888 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
889 luaL_checkany(L, 2);
890 lua_getuservalue(L, 1);
891 lua_getfield(L, -1, "public");
892 lua_pushvalue(L, 2);
893 lua_gettable(L, -2);
894 return 1;
895 }
897 static int moonbr_io_handlenewindex(lua_State *L) {
898 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
899 luaL_checkany(L, 2);
900 luaL_checkany(L, 3);
901 lua_getuservalue(L, 1);
902 lua_getfield(L, -1, "public");
903 lua_pushvalue(L, 2);
904 lua_pushvalue(L, 3);
905 lua_settable(L, -3);
906 return 0;
907 }
909 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
910 const char *path;
911 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
912 const int path_maxlen = sizeof(struct sockaddr_un) - (
913 (void *)sockaddr.sun_path - (void *)&sockaddr
914 ) - 1; /* one byte for termination */
915 int sock;
916 path = luaL_checkstring(L, 1);
917 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
918 strcpy(sockaddr.sun_path, path);
919 sock = socket(
920 PF_LOCAL,
921 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
922 0
923 );
924 if (sock < 0) {
925 moonbr_io_errmsg();
926 lua_pushnil(L);
927 lua_pushstring(L, errmsg);
928 return 2;
929 }
930 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
931 if (!nonblocking && errno == EINTR) {
932 moonbr_io_errmsg();
933 close(sock);
934 lua_pushnil(L);
935 lua_pushstring(L, errmsg);
936 return 2;
937 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
938 moonbr_io_errmsg();
939 lua_pushnil(L);
940 lua_pushstring(L, errmsg);
941 return 2;
942 }
943 }
944 moonbr_io_pushhandle(L, sock);
945 return 1;
946 }
948 static int moonbr_io_localconnect(lua_State *L) {
949 return moonbr_io_localconnect_impl(L, 0);
950 }
952 static int moonbr_io_localconnect_nb(lua_State *L) {
953 return moonbr_io_localconnect_impl(L, 1);
954 }
956 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
957 const char *host, *port;
958 struct addrinfo hints = { 0, };
959 struct addrinfo *res, *addrinfo;
960 int errcode;
961 int sock;
962 host = luaL_checkstring(L, 1);
963 port = luaL_checkstring(L, 2);
964 hints.ai_family = AF_UNSPEC;
965 hints.ai_socktype = SOCK_STREAM;
966 hints.ai_protocol = IPPROTO_TCP;
967 hints.ai_flags = AI_ADDRCONFIG;
968 errcode = getaddrinfo(host, port, &hints, &res);
969 if (errcode) {
970 freeaddrinfo(res);
971 if (errcode == EAI_SYSTEM) {
972 moonbr_io_errmsg();
973 lua_pushnil(L);
974 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
975 } else {
976 lua_pushnil(L);
977 lua_pushstring(L, gai_strerror(errcode));
978 }
979 return 2;
980 }
981 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
982 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
983 }
984 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
985 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
986 }
987 addrinfo = res;
988 moonbr_io_tcpconnect_found:
989 sock = socket(
990 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
991 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
992 addrinfo->ai_protocol
993 );
994 if (sock < 0) {
995 moonbr_io_errmsg();
996 freeaddrinfo(res);
997 lua_pushnil(L);
998 lua_pushstring(L, errmsg);
999 return 2;
1001 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1002 freeaddrinfo(res);
1003 if (!nonblocking && errno == EINTR) {
1004 moonbr_io_errmsg();
1005 close(sock);
1006 lua_pushnil(L);
1007 lua_pushstring(L, errmsg);
1008 return 2;
1009 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
1010 moonbr_io_errmsg();
1011 lua_pushnil(L);
1012 lua_pushstring(L, errmsg);
1013 return 2;
1015 } else {
1016 freeaddrinfo(res);
1018 moonbr_io_pushhandle(L, sock);
1019 return 1;
1022 static int moonbr_io_tcpconnect(lua_State *L) {
1023 return moonbr_io_tcpconnect_impl(L, 0);
1026 static int moonbr_io_tcpconnect_nb(lua_State *L) {
1027 return moonbr_io_tcpconnect_impl(L, 1);
1030 static int moonbr_io_locallisten(lua_State *L) {
1031 moonbr_io_listener_t *listener;
1032 const char *path;
1033 struct stat sb;
1034 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
1035 const int path_maxlen = sizeof(struct sockaddr_un) - (
1036 (void *)sockaddr.sun_path - (void *)&sockaddr
1037 ) - 1; /* one byte for termination */
1038 int sock;
1039 path = luaL_checkstring(L, 1);
1040 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
1041 strcpy(sockaddr.sun_path, path);
1042 if (stat(path, &sb) == 0) {
1043 if (S_ISSOCK(sb.st_mode)) unlink(path);
1045 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1046 listener->fd = -1;
1047 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1048 sock = socket(
1049 PF_LOCAL,
1050 SOCK_STREAM | SOCK_CLOEXEC,
1052 );
1053 if (sock < 0) {
1054 moonbr_io_errmsg();
1055 lua_pushnil(L);
1056 lua_pushstring(L, errmsg);
1057 return 2;
1059 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
1060 moonbr_io_errmsg();
1061 close(sock);
1062 lua_pushnil(L);
1063 lua_pushstring(L, errmsg);
1064 return 2;
1066 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1067 moonbr_io_errmsg();
1068 close(sock);
1069 lua_pushnil(L);
1070 lua_pushstring(L, errmsg);
1071 return 2;
1073 listener->fd = sock;
1074 listener->addrfam = AF_LOCAL;
1075 listener->nonblocking = -1;
1076 return 1;
1079 static int moonbr_io_tcplisten(lua_State *L) {
1080 moonbr_io_listener_t *listener;
1081 const char *host, *port;
1082 struct addrinfo hints = { 0, };
1083 struct addrinfo *res, *addrinfo;
1084 int errcode;
1085 int sock;
1086 host = luaL_optstring(L, 1, NULL);
1087 port = luaL_checkstring(L, 2);
1088 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1089 listener->fd = -1;
1090 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1091 hints.ai_family = AF_UNSPEC;
1092 hints.ai_socktype = SOCK_STREAM;
1093 hints.ai_protocol = IPPROTO_TCP;
1094 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1095 errcode = getaddrinfo(host, port, &hints, &res);
1096 if (errcode) {
1097 freeaddrinfo(res);
1098 if (errcode == EAI_SYSTEM) {
1099 moonbr_io_errmsg();
1100 lua_pushnil(L);
1101 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
1102 } else {
1103 lua_pushnil(L);
1104 lua_pushstring(L, gai_strerror(errcode));
1106 return 2;
1108 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1109 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
1111 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1112 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
1114 addrinfo = res;
1115 moonbr_io_tcpconnect_found:
1116 listener->addrfam = addrinfo->ai_family;
1117 sock = socket(
1118 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
1119 addrinfo->ai_socktype | SOCK_CLOEXEC,
1120 addrinfo->ai_protocol
1121 );
1122 if (sock < 0) {
1123 moonbr_io_errmsg();
1124 freeaddrinfo(res);
1125 lua_pushnil(L);
1126 lua_pushstring(L, errmsg);
1127 return 2;
1130 static const int reuseval = 1;
1131 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1132 moonbr_io_errmsg();
1133 freeaddrinfo(res);
1134 close(sock);
1135 lua_pushnil(L);
1136 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1137 return 2;
1140 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1141 moonbr_io_errmsg();
1142 freeaddrinfo(res);
1143 close(sock);
1144 lua_pushnil(L);
1145 lua_pushstring(L, errmsg);
1146 return 2;
1148 freeaddrinfo(res);
1149 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1150 moonbr_io_errmsg();
1151 close(sock);
1152 lua_pushnil(L);
1153 lua_pushstring(L, errmsg);
1154 return 2;
1156 listener->fd = sock;
1157 listener->nonblocking = -1;
1158 return 1;
1161 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1162 moonbr_io_listener_t *listener;
1163 int fd;
1164 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1165 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1166 if (listener->nonblocking != nonblocking) {
1167 int flags;
1168 flags = fcntl(listener->fd, F_GETFL, 0);
1169 if (flags == -1) {
1170 moonbr_io_errmsg();
1171 close(listener->fd);
1172 listener->fd = -1;
1173 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1175 if (nonblocking) flags |= O_NONBLOCK;
1176 else flags &= ~O_NONBLOCK;
1177 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1178 moonbr_io_errmsg();
1179 close(listener->fd);
1180 listener->fd = -1;
1181 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1183 listener->nonblocking = nonblocking;
1185 while (1) {
1186 #if defined(__linux__) && !defined(_GNU_SOURCE)
1187 fd = accept(listener->fd, NULL, NULL);
1188 if (fd != -1) {
1189 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1190 moonbr_io_errmsg();
1191 close(listener->fd);
1192 listener->fd = -1;
1193 close(fd);
1194 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1197 #else
1198 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1199 #endif
1200 if (fd < 0) {
1201 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1202 lua_pushboolean(L, 0);
1203 lua_pushliteral(L, "No incoming connection pending");
1204 return 2;
1205 } else if (errno != EINTR) {
1206 moonbr_io_errmsg();
1207 lua_pushnil(L);
1208 lua_pushstring(L, errmsg);
1209 return 2;
1211 } else {
1212 moonbr_io_pushhandle(L, fd);
1213 return 1;
1218 static int moonbr_io_accept(lua_State *L) {
1219 return moonbr_io_accept_impl(L, 0);
1222 static int moonbr_io_accept_nb(lua_State *L) {
1223 return moonbr_io_accept_impl(L, 1);
1226 static int moonbr_io_unlisten(lua_State *L) {
1227 moonbr_io_listener_t *listener;
1228 struct sockaddr_un addr;
1229 socklen_t addrlen;
1230 struct stat sb;
1231 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1232 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1233 addrlen = sizeof(addr);
1234 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1235 if (close(listener->fd)) {
1236 moonbr_io_errmsg();
1237 listener->fd = -1;
1238 if (addrlen && addrlen <= sizeof(addr)) {
1239 if (stat(addr.sun_path, &sb) == 0) {
1240 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1243 lua_pushnil(L);
1244 lua_pushstring(L, errmsg);
1245 return 2;
1247 listener->fd = -1;
1248 if (addrlen && addrlen <= sizeof(addr)) {
1249 if (stat(addr.sun_path, &sb) == 0) {
1250 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1253 lua_pushboolean(L, 1);
1254 return 1;
1257 static int moonbr_io_listenergc(lua_State *L) {
1258 moonbr_io_listener_t *listener;
1259 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1260 if (listener->fd >= 0) close(listener->fd);
1261 listener->fd = -1;
1262 return 0;
1265 static int moonbr_io_exec(lua_State *L) {
1266 char **argv;
1267 int i, argc;
1268 int sockin[2], sockout[2], sockerr[2];
1269 volatile int errorcond = 0;
1270 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
1271 moonbr_io_child_t *child;
1272 argc = lua_gettop(L);
1273 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
1274 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
1275 argv[argc] = NULL;
1276 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
1277 child->pid = 0;
1278 lua_newtable(L);
1279 lua_setuservalue(L, -2);
1280 luaL_getmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
1281 lua_setmetatable(L, -2);
1282 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
1283 moonbr_io_errmsg();
1284 lua_pushnil(L);
1285 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1286 return 2;
1288 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
1289 moonbr_io_errmsg();
1290 close(sockin[0]);
1291 close(sockin[1]);
1292 lua_pushnil(L);
1293 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1294 return 2;
1296 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
1297 moonbr_io_errmsg();
1298 close(sockin[0]);
1299 close(sockin[1]);
1300 close(sockout[0]);
1301 close(sockout[1]);
1302 lua_pushnil(L);
1303 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1304 return 2;
1306 child->pid = vfork();
1307 if (child->pid == -1) {
1308 moonbr_io_errmsg();
1309 close(sockin[0]);
1310 close(sockin[1]);
1311 close(sockout[0]);
1312 close(sockout[1]);
1313 close(sockerr[0]);
1314 close(sockerr[1]);
1315 lua_pushnil(L);
1316 lua_pushfstring(L, "Could not fork: %s", errmsg);
1317 return 2;
1319 if (!child->pid) {
1320 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
1321 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
1322 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
1323 closefrom(4);
1324 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1325 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1326 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1327 if (execvp(argv[0], argv)) {
1328 errorcond = 2;
1329 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1330 _exit(0);
1332 moonbr_io_exec_error1:
1333 errorcond = 1;
1334 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1335 _exit(0);
1337 close(sockin[1]);
1338 close(sockout[1]);
1339 close(sockerr[1]);
1340 if (errorcond) {
1341 int status;
1342 close(sockin[0]);
1343 close(sockout[0]);
1344 close(sockerr[0]);
1345 while (waitpid(child->pid, &status, 0) == -1) {
1346 if (errno != EINTR) {
1347 moonbr_io_errmsg();
1348 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
1351 child->pid = 0;
1352 lua_pushnil(L);
1353 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
1354 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
1355 return 2;
1357 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1358 lua_pushlightuserdata(L, &sockin[0]);
1359 if (lua_pcall(L, 1, 1, 0)) {
1360 if (sockin[0] != -1) close(sockin[0]);
1361 close(sockout[0]);
1362 close(sockerr[0]);
1363 goto moonbr_io_exec_error2;
1365 lua_setfield(L, -2, "stdin");
1366 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1367 lua_pushlightuserdata(L, &sockout[0]);
1368 if (lua_pcall(L, 1, 1, 0)) {
1369 if (sockout[0] != -1) close(sockout[0]);
1370 close(sockerr[0]);
1371 goto moonbr_io_exec_error2;
1373 lua_setfield(L, -2, "stdout");
1374 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1375 lua_pushlightuserdata(L, &sockerr[0]);
1376 if (lua_pcall(L, 1, 1, 0)) {
1377 if (sockerr[0] != -1) close(sockerr[0]);
1378 goto moonbr_io_exec_error2;
1380 lua_setfield(L, -2, "stderr");
1381 return 1;
1382 moonbr_io_exec_error2:
1384 int status;
1385 while (waitpid(child->pid, &status, 0) == -1) {
1386 if (errno != EINTR) {
1387 moonbr_io_errmsg();
1388 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
1392 child->pid = 0;
1393 return lua_error(L);
1396 static int moonbr_io_childindex(lua_State *L) {
1397 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1398 luaL_checkany(L, 2);
1399 lua_getuservalue(L, 1);
1400 lua_pushvalue(L, 2);
1401 lua_gettable(L, -2);
1402 if (lua_isnil(L, -1)) {
1403 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
1404 lua_pushvalue(L, 2);
1405 lua_gettable(L, -2);
1407 return 1;
1410 static int moonbr_io_childnewindex(lua_State *L) {
1411 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1412 luaL_checkany(L, 2);
1413 luaL_checkany(L, 3);
1414 lua_getuservalue(L, 1);
1415 lua_pushvalue(L, 2);
1416 lua_pushvalue(L, 3);
1417 lua_settable(L, -3);
1418 return 0;
1421 static int moonbr_io_childgc(lua_State *L) {
1422 moonbr_io_child_t *child;
1423 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1424 if (child->pid) {
1425 int status;
1426 if (kill(child->pid, SIGKILL)) {
1427 moonbr_io_errmsg();
1428 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
1430 while (waitpid(child->pid, &status, 0) == -1) {
1431 if (errno != EINTR) {
1432 moonbr_io_errmsg();
1433 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
1437 return 0;
1440 static int moonbr_io_kill(lua_State *L) {
1441 moonbr_io_child_t *child;
1442 int sig;
1443 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1444 sig = luaL_optinteger(L, 2, SIGTERM);
1445 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
1446 if (kill(child->pid, sig)) {
1447 moonbr_io_errmsg();
1448 luaL_error(L, "Error in kill call: %s", errmsg);
1450 lua_settop(L, 1);
1451 return 1;
1454 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
1455 moonbr_io_child_t *child;
1456 pid_t waitedpid;
1457 int status;
1458 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1459 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
1460 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
1461 if (errno != EINTR) {
1462 moonbr_io_errmsg();
1463 luaL_error(L, "Error in waitpid call: %s", errmsg);
1466 if (!waitedpid) {
1467 lua_pushnil(L);
1468 } else {
1469 child->pid = 0;
1470 if (WIFEXITED(status)) {
1471 lua_pushinteger(L, WEXITSTATUS(status));
1472 } else if (WIFSIGNALED(status)) {
1473 lua_pushinteger(L, -WTERMSIG(status));
1474 } else {
1475 luaL_error(L, "Unexpected status value returned by waitpid call");
1478 return 1;
1481 static int moonbr_io_wait(lua_State *L) {
1482 return moonbr_io_wait_impl(L, 0);
1485 static int moonbr_io_wait_nb(lua_State *L) {
1486 return moonbr_io_wait_impl(L, 1);
1489 #if LUA_VERSION_NUM >= 503
1490 static int moonbr_io_wait_cont(lua_State *L, int status, lua_KContext ctx) {
1491 #else
1492 static int moonbr_io_wait_cont(lua_State *L) {
1493 #endif
1494 #if !(LUA_VERSION_NUM >= 503)
1495 int ctx = 0;
1496 lua_getctx(L, &ctx);
1497 #endif
1498 while (1) {
1499 lua_pushcfunction(L, moonbr_io_wait_nb);
1500 lua_pushvalue(L, 1);
1501 lua_call(L, 1, 1);
1502 if (!lua_isnil(L, -1)) break;
1503 lua_pushvalue(L, 2);
1504 lua_callk(L, 0, 0, ctx, moonbr_io_wait_cont);
1506 return 1;
1509 static int moonbr_io_wait_call(lua_State *L) {
1510 lua_settop(L, 2);
1511 #if LUA_VERSION_NUM >= 503
1512 return moonbr_io_wait_cont(L, 0, 0);
1513 #else
1514 return moonbr_io_wait_cont(L);
1515 #endif
1518 moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
1520 static int moonbr_io_poll(lua_State *L) {
1521 moonbr_io_handle_t *handle;
1522 moonbr_io_listener_t *listener;
1523 int fd, isnum;
1524 int nfds = 0;
1525 fd_set readfds, writefds, exceptfds;
1526 struct timeval timeout = {0, };
1527 int status;
1528 FD_ZERO(&readfds);
1529 FD_ZERO(&writefds);
1530 FD_ZERO(&exceptfds);
1531 if (!lua_isnoneornil(L, 1)) {
1532 luaL_checktype(L, 1, LUA_TTABLE);
1533 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1534 if (lua_toboolean(L, -1)) {
1535 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1536 if (handle) {
1537 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1538 fd = handle->fd;
1539 if (
1540 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1541 handle->readbufin != handle->readbufout /* data pending in buffer */
1542 ) {
1543 lua_pushboolean(L, 1);
1544 return 1;
1546 } else {
1547 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1548 if (listener) {
1549 fd = listener->fd;
1550 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1551 } else {
1552 fd = lua_tointegerx(L, -2, &isnum);
1553 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1556 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1557 FD_SET(fd, &readfds);
1558 if (fd+1 > nfds) nfds = fd+1;
1562 if (!lua_isnoneornil(L, 2)) {
1563 luaL_checktype(L, 2, LUA_TTABLE);
1564 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1565 if (lua_toboolean(L, -1)) {
1566 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1567 if (handle) {
1568 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1569 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1570 fd = handle->fd;
1571 } else {
1572 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1573 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1574 fd = lua_tointegerx(L, -2, &isnum);
1575 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1577 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1578 FD_SET(fd, &writefds);
1579 if (fd+1 > nfds) nfds = fd+1;
1583 if (!lua_isnoneornil(L, 3)) {
1584 lua_Number n;
1585 n = lua_tonumberx(L, 3, &isnum);
1586 if (isnum && n<0) {
1587 lua_pushboolean(L, 0);
1588 lua_pushliteral(L, "Negative timeout");
1589 return 2;
1590 } else if (isnum && n>=0 && n<100000000) {
1591 timeout.tv_sec = n;
1592 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1593 } else {
1594 luaL_argcheck(L, 0, 3, "not a valid timeout");
1596 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1597 } else {
1598 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1600 if (status == -1) {
1601 if (errno == EINTR) {
1602 lua_pushnil(L);
1603 lua_pushliteral(L, "Signal received while polling file descriptors");
1604 return 2;
1605 } else {
1606 moonbr_io_errmsg();
1607 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1609 } else if (status == 0) {
1610 lua_pushboolean(L, 0);
1611 lua_pushliteral(L, "Timeout while polling file descriptors");
1612 return 2;
1613 } else {
1614 lua_pushboolean(L, 1);
1615 return 1;
1619 static int moonbr_io_timeref(lua_State *L) {
1620 lua_Number sub;
1621 struct timespec tp;
1622 sub = luaL_optnumber(L, 1, 0);
1623 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1624 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1626 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1627 return 1;
1630 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1631 {"read", moonbr_io_read},
1632 {"read_nb", moonbr_io_read_nb},
1633 {"read_call", moonbr_io_read_call},
1634 {"read_yield", moonbr_io_read_yield},
1635 {"drain", moonbr_io_drain},
1636 {"drain_nb", moonbr_io_drain_nb},
1637 {"drain_call", moonbr_io_drain_call},
1638 {"drain_yield", moonbr_io_drain_yield},
1639 {"write", moonbr_io_write},
1640 {"write_nb", moonbr_io_write_nb},
1641 {"write_call", moonbr_io_write_call},
1642 {"write_yield", moonbr_io_write_yield},
1643 {"flush", moonbr_io_flush},
1644 {"flush_nb", moonbr_io_flush_nb},
1645 {"flush_call", moonbr_io_flush_call},
1646 {"flush_yield", moonbr_io_flush_yield},
1647 {"finish", moonbr_io_finish},
1648 {"close", moonbr_io_close},
1649 {"reset", moonbr_io_reset},
1650 {NULL, NULL}
1651 };
1653 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1654 {"__index", moonbr_io_handleindex},
1655 {"__newindex", moonbr_io_handlenewindex},
1656 {"__gc", moonbr_io_handlegc},
1657 {NULL, NULL}
1658 };
1660 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1661 {"accept", moonbr_io_accept},
1662 {"accept_nb", moonbr_io_accept_nb},
1663 {"close", moonbr_io_unlisten},
1664 {NULL, NULL}
1665 };
1667 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1668 {"__gc", moonbr_io_listenergc},
1669 {NULL, NULL}
1670 };
1672 static const struct luaL_Reg moonbr_io_child_methods[] = {
1673 {"kill", moonbr_io_kill},
1674 {"wait", moonbr_io_wait},
1675 {"wait_nb", moonbr_io_wait_nb},
1676 {"wait_call", moonbr_io_wait_call},
1677 {"wait_yield", moonbr_io_wait_yield},
1678 {NULL, NULL}
1679 };
1681 static const struct luaL_Reg moonbr_io_child_metamethods[] = {
1682 {"__index", moonbr_io_childindex},
1683 {"__newindex", moonbr_io_childnewindex},
1684 {"__gc", moonbr_io_childgc},
1685 {NULL, NULL}
1686 };
1688 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1689 {"localconnect", moonbr_io_localconnect},
1690 {"localconnect_nb", moonbr_io_localconnect_nb},
1691 {"tcpconnect", moonbr_io_tcpconnect},
1692 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1693 {"locallisten", moonbr_io_locallisten},
1694 {"tcplisten", moonbr_io_tcplisten},
1695 {"exec", moonbr_io_exec},
1696 {"poll", moonbr_io_poll},
1697 {"timeref", moonbr_io_timeref},
1698 {NULL, NULL}
1699 };
1701 int luaopen_moonbridge_io(lua_State *L) {
1703 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1705 lua_newtable(L); // module
1707 lua_newtable(L); // public metatable
1708 lua_newtable(L); // handle methods
1709 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1710 lua_pushvalue(L, -1);
1711 lua_setfield(L, -4, "handle_pt");
1712 lua_setfield(L, -2, "__index");
1713 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1715 lua_newtable(L); // handle metatable
1716 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1717 lua_pushvalue(L, -1);
1718 lua_setfield(L, -3, "handle_mt");
1719 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1721 lua_newtable(L); // listener metatable
1722 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1723 lua_newtable(L); // listener methods
1724 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1725 lua_pushvalue(L, -1);
1726 lua_setfield(L, -4, "listener_pt");
1727 lua_setfield(L, -2, "__index");
1728 lua_pushvalue(L, -1);
1729 lua_setfield(L, -3, "listener_mt");
1730 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1732 lua_newtable(L); // child methods
1733 luaL_setfuncs(L, moonbr_io_child_methods, 0);
1734 lua_pushvalue(L, -1);
1735 lua_setfield(L, -3, "child_pt");
1736 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_PT_REGKEY);
1737 lua_newtable(L); // child metatable
1738 luaL_setfuncs(L, moonbr_io_child_metamethods, 0);
1739 lua_pushvalue(L, -1);
1740 lua_setfield(L, -3, "child_mt");
1741 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
1743 moonbr_io_pushhandle(L, 0);
1744 lua_setfield(L, -2, "stdin");
1745 moonbr_io_pushhandle(L, 1);
1746 lua_setfield(L, -2, "stdout");
1747 moonbr_io_pushhandle(L, 2);
1748 lua_setfield(L, -2, "stderr");
1750 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1751 return 1;

Impressum / About Us