moonbridge

view moonbridge_io.c @ 242:63466f65af56

Removed experimental work on libtls integration
author jbe
date Sun Aug 21 01:42:18 2016 +0200 (2016-08-21)
parents 58e442fbc5a3
children 1dbc5c27b279
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 handle->writebufin = MOONBR_IO_WRITEBUFLEN;
480 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
481 moonbr_io_handle_set_nopush(L, handle, 1);
482 written = write(
483 handle->fd,
484 handle->writebuf + handle->writebufout,
485 MOONBR_IO_WRITEBUFLEN - handle->writebufout
486 );
487 if (written < 0) {
488 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
489 goto moonbr_io_write_impl_block;
490 } else if (errno != EINTR) {
491 moonbr_io_errmsg();
492 lua_pushnil(L);
493 lua_pushstring(L, errmsg);
494 return 2;
495 }
496 } else {
497 handle->writebufout += written;
498 handle->writeleft -= written;
499 if (handle->flushedleft) {
500 if (written >= handle->flushedleft) {
501 handle->flushedleft = 0;
502 moonbr_io_handle_set_nopush(L, handle, 0);
503 } else {
504 handle->flushedleft -= written;
505 }
506 }
507 }
508 }
509 handle->writebufin = 0;
510 handle->writebufout = 0;
511 }
512 }
513 handle->writeqoff = 0;
514 lua_pop(L, 1);
515 lua_pushnil(L);
516 lua_rawseti(L, -2, handle->writeqout++);
517 }
518 while (handle->flushedleft) {
519 moonbr_io_handle_set_nopush(L, handle, 1);
520 written = write(
521 handle->fd,
522 handle->writebuf + handle->writebufout,
523 handle->writebufin - handle->writebufout
524 );
525 if (written < 0) {
526 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
527 goto moonbr_io_write_impl_block;
528 } else if (errno != EINTR) {
529 moonbr_io_errmsg();
530 lua_pushnil(L);
531 lua_pushstring(L, errmsg);
532 return 2;
533 }
534 } else {
535 handle->writebufout += written;
536 handle->writeleft -= written;
537 if (handle->flushedleft) {
538 if (written >= handle->flushedleft) {
539 handle->flushedleft = 0;
540 moonbr_io_handle_set_nopush(L, handle, 0);
541 } else {
542 handle->flushedleft -= written;
543 }
544 }
545 }
546 }
547 if (handle->writebufout == handle->writebufin) {
548 handle->writebufin = 0;
549 handle->writebufout = 0;
550 }
551 if (nonblocking) lua_pushinteger(L, 0);
552 else lua_pushvalue(L, 1);
553 handle->writeerr = 0;
554 return 1;
555 moonbr_io_write_impl_block:
556 lua_pushinteger(L, handle->writeleft);
557 handle->writeerr = 0;
558 return 1;
559 }
561 static int moonbr_io_write(lua_State *L) {
562 return moonbr_io_write_impl(L, 0, 0);
563 }
565 static int moonbr_io_write_nb(lua_State *L) {
566 return moonbr_io_write_impl(L, 1, 0);
567 }
569 static int moonbr_io_flush(lua_State *L) {
570 return moonbr_io_write_impl(L, 0, 1);
571 }
573 static int moonbr_io_flush_nb(lua_State *L) {
574 return moonbr_io_write_impl(L, 1, 1);
575 }
577 #if LUA_VERSION_NUM >= 503
578 static int moonbr_io_write_cont(lua_State *L, int status, lua_KContext ctx) {
579 #else
580 static int moonbr_io_write_cont(lua_State *L) {
581 #endif
582 while (1) {
583 lua_pushcfunction(L, moonbr_io_write_nb);
584 lua_pushvalue(L, 1);
585 lua_call(L, 1, 2);
586 if (lua_isnil(L, -2)) return 2;
587 if (!lua_tointeger(L, -2)) {
588 lua_pushvalue(L, 1);
589 return 1;
590 }
591 lua_pop(L, 2);
592 lua_pushvalue(L, 2);
593 lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
594 }
595 }
597 static int moonbr_io_write_call(lua_State *L) {
598 lua_pushcfunction(L, moonbr_io_write_nb);
599 lua_insert(L, 3);
600 lua_pushvalue(L, 1);
601 lua_insert(L, 4);
602 lua_call(L, lua_gettop(L) - 3, 2);
603 if (lua_isnil(L, -2)) return 2;
604 if (!lua_tointeger(L, -2)) {
605 lua_pushvalue(L, 1);
606 return 1;
607 }
608 #if LUA_VERSION_NUM >= 503
609 return moonbr_io_write_cont(L, 0, 0);
610 #else
611 return moonbr_io_write_cont(L);
612 #endif
613 }
615 moonbr_io_yield_wrapper(moonbr_io_write_yield, moonbr_io_write_call);
617 static int moonbr_io_flush_call(lua_State *L) {
618 lua_pushcfunction(L, moonbr_io_flush_nb);
619 lua_insert(L, 3);
620 lua_pushvalue(L, 1);
621 lua_insert(L, 4);
622 lua_call(L, lua_gettop(L) - 3, 2);
623 if (lua_isnil(L, -2)) return 2;
624 if (!lua_tointeger(L, -2)) {
625 lua_pushvalue(L, 1);
626 return 1;
627 }
628 #if LUA_VERSION_NUM >= 503
629 return moonbr_io_write_cont(L, 0, 0);
630 #else
631 return moonbr_io_write_cont(L);
632 #endif
633 }
635 moonbr_io_yield_wrapper(moonbr_io_flush_yield, moonbr_io_flush_call);
637 static int moonbr_io_finish(lua_State *L) {
638 moonbr_io_handle_t *handle;
639 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
640 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
641 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
642 if (handle->writeleft) {
643 lua_pushcfunction(L, moonbr_io_flush);
644 lua_pushvalue(L, 1);
645 if (lua_pcall(L, 1, 2, 0)) {
646 handle->finished = 1;
647 lua_error(L);
648 }
649 if (!lua_toboolean(L, -2)) {
650 handle->finished = 1;
651 return 2;
652 }
653 }
654 handle->finished = 1;
655 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
656 if (shutdown(handle->fd, SHUT_WR)) {
657 moonbr_io_errmsg();
658 lua_pushnil(L);
659 lua_pushstring(L, errmsg);
660 return 2;
661 }
662 } else {
663 if (close(handle->fd)) {
664 moonbr_io_errmsg();
665 handle->fd = -1;
666 lua_pushnil(L);
667 lua_pushstring(L, errmsg);
668 return 2;
669 }
670 handle->fd = -1; /* fake EOF on read */
671 }
672 lua_pushboolean(L, 1);
673 return 1;
674 }
676 static int moonbr_io_close_impl(lua_State *L, int reset) {
677 moonbr_io_handle_t *handle;
678 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
679 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
680 if (!reset) {
681 if (handle->writeleft) {
682 lua_pushcfunction(L, moonbr_io_flush);
683 lua_pushvalue(L, 1);
684 if (lua_pcall(L, 1, 2, 0)) {
685 handle->closed = 1;
686 close(handle->fd);
687 handle->fd = -1;
688 lua_error(L);
689 }
690 handle->closed = 1;
691 if (!lua_toboolean(L, -2)) {
692 close(handle->fd);
693 handle->fd = -1;
694 return 2;
695 }
696 } else {
697 handle->closed = 1;
698 moonbr_io_handle_set_linger(L, handle, -1);
699 }
700 } else {
701 handle->closed = 1;
702 }
703 if (handle->fd >= 0) {
704 if (close(handle->fd)) {
705 moonbr_io_errmsg();
706 handle->fd = -1;
707 lua_pushnil(L);
708 lua_pushstring(L, errmsg);
709 return 2;
710 }
711 handle->fd = -1;
712 }
713 lua_pushboolean(L, 1);
714 return 1;
716 }
718 static int moonbr_io_close(lua_State *L) {
719 return moonbr_io_close_impl(L, 0);
720 }
722 static int moonbr_io_reset(lua_State *L) {
723 return moonbr_io_close_impl(L, 1);
724 }
726 static int moonbr_io_handlegc(lua_State *L) {
727 moonbr_io_handle_t *handle;
728 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
729 if (handle->fd >= 0) {
730 lua_pushcfunction(L, moonbr_io_reset);
731 lua_pushvalue(L, 1);
732 lua_pushinteger(L, 0);
733 lua_call(L, 2, 0);
734 }
735 return 0;
736 }
738 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
739 moonbr_io_handle_t *handle;
740 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
741 if (!handle->closed) {
742 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
743 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
744 lua_call(L, 1, 0);
745 }
746 }
748 static int moonbr_io_pushhandle_impl(lua_State *L) {
749 int *fd;
750 moonbr_io_handle_t *handle;
751 struct sockaddr addr;
752 socklen_t addrlen;
753 fd = lua_touserdata(L, 1);
754 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
755 handle->fd = -1; /* avoid closing incomplete handle */
756 addrlen = sizeof(addr);
757 if (getsockname(*fd, &addr, &addrlen)) {
758 if (errno != ENOTSOCK) {
759 moonbr_io_errmsg();
760 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
761 }
762 handle->issock = 0;
763 } else {
764 handle->issock = 1;
765 handle->addrfam = addr.sa_family;
766 }
767 handle->finished = 0;
768 handle->closed = 0;
769 handle->nonblocking = -1;
770 handle->nopush = -1;
771 handle->readerr = 0;
772 handle->readbufin = 0;
773 handle->readbufout = 0;
774 handle->writeerr = 0;
775 handle->writeleft = 0;
776 handle->flushedleft = 0;
777 handle->writeqin = 0;
778 handle->writeqout = 0;
779 handle->writeqoff = 0;
780 handle->writebufin = 0;
781 handle->writebufout = 0;
782 handle->fd = *fd; /* required for set_linger call */
783 moonbr_io_handle_set_linger(L, handle, 0);
784 handle->fd = -1; /* avoid closing incomplete handle */
785 luaL_setmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
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_setmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
870 lua_setfield(L, -2, "public");
871 lua_setuservalue(L, -2);
872 handle->fd = *fd;
873 *fd = -1; /* closing is now handled by garbage collection */
874 return 1;
875 }
877 void moonbr_io_pushhandle(lua_State *L, int fd) {
878 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
879 lua_pushlightuserdata(L, &fd);
880 if (lua_pcall(L, 1, 1, 0)) {
881 if (fd != -1) close(fd);
882 lua_error(L);
883 }
884 }
886 static int moonbr_io_handleindex(lua_State *L) {
887 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
888 luaL_checkany(L, 2);
889 lua_getuservalue(L, 1);
890 lua_getfield(L, -1, "public");
891 lua_pushvalue(L, 2);
892 lua_gettable(L, -2);
893 return 1;
894 }
896 static int moonbr_io_handlenewindex(lua_State *L) {
897 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
898 luaL_checkany(L, 2);
899 luaL_checkany(L, 3);
900 lua_getuservalue(L, 1);
901 lua_getfield(L, -1, "public");
902 lua_pushvalue(L, 2);
903 lua_pushvalue(L, 3);
904 lua_settable(L, -3);
905 return 0;
906 }
908 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
909 const char *path;
910 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
911 const int path_maxlen = sizeof(struct sockaddr_un) - (
912 (void *)sockaddr.sun_path - (void *)&sockaddr
913 ) - 1; /* one byte for termination */
914 int sock;
915 path = luaL_checkstring(L, 1);
916 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
917 strcpy(sockaddr.sun_path, path);
918 sock = socket(
919 PF_LOCAL,
920 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
921 0
922 );
923 if (sock < 0) {
924 moonbr_io_errmsg();
925 lua_pushnil(L);
926 lua_pushstring(L, errmsg);
927 return 2;
928 }
929 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
930 if (!nonblocking && errno == EINTR) {
931 moonbr_io_errmsg();
932 close(sock);
933 lua_pushnil(L);
934 lua_pushstring(L, errmsg);
935 return 2;
936 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
937 moonbr_io_errmsg();
938 lua_pushnil(L);
939 lua_pushstring(L, errmsg);
940 return 2;
941 }
942 }
943 moonbr_io_pushhandle(L, sock);
944 return 1;
945 }
947 static int moonbr_io_localconnect(lua_State *L) {
948 return moonbr_io_localconnect_impl(L, 0);
949 }
951 static int moonbr_io_localconnect_nb(lua_State *L) {
952 return moonbr_io_localconnect_impl(L, 1);
953 }
955 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
956 const char *host, *port;
957 struct addrinfo hints = { 0, };
958 struct addrinfo *res, *addrinfo;
959 int errcode;
960 int sock;
961 host = luaL_checkstring(L, 1);
962 port = luaL_checkstring(L, 2);
963 hints.ai_family = AF_UNSPEC;
964 hints.ai_socktype = SOCK_STREAM;
965 hints.ai_protocol = IPPROTO_TCP;
966 hints.ai_flags = AI_ADDRCONFIG;
967 errcode = getaddrinfo(host, port, &hints, &res);
968 if (errcode) {
969 freeaddrinfo(res);
970 if (errcode == EAI_SYSTEM) {
971 moonbr_io_errmsg();
972 lua_pushnil(L);
973 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
974 } else {
975 lua_pushnil(L);
976 lua_pushstring(L, gai_strerror(errcode));
977 }
978 return 2;
979 }
980 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
981 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
982 }
983 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
984 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
985 }
986 addrinfo = res;
987 moonbr_io_tcpconnect_found:
988 sock = socket(
989 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
990 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
991 addrinfo->ai_protocol
992 );
993 if (sock < 0) {
994 moonbr_io_errmsg();
995 freeaddrinfo(res);
996 lua_pushnil(L);
997 lua_pushstring(L, errmsg);
998 return 2;
999 }
1000 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1001 freeaddrinfo(res);
1002 if (!nonblocking && errno == EINTR) {
1003 moonbr_io_errmsg();
1004 close(sock);
1005 lua_pushnil(L);
1006 lua_pushstring(L, errmsg);
1007 return 2;
1008 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
1009 moonbr_io_errmsg();
1010 lua_pushnil(L);
1011 lua_pushstring(L, errmsg);
1012 return 2;
1014 } else {
1015 freeaddrinfo(res);
1017 moonbr_io_pushhandle(L, sock);
1018 return 1;
1021 static int moonbr_io_tcpconnect(lua_State *L) {
1022 return moonbr_io_tcpconnect_impl(L, 0);
1025 static int moonbr_io_tcpconnect_nb(lua_State *L) {
1026 return moonbr_io_tcpconnect_impl(L, 1);
1029 static int moonbr_io_locallisten(lua_State *L) {
1030 moonbr_io_listener_t *listener;
1031 const char *path;
1032 struct stat sb;
1033 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
1034 const int path_maxlen = sizeof(struct sockaddr_un) - (
1035 (void *)sockaddr.sun_path - (void *)&sockaddr
1036 ) - 1; /* one byte for termination */
1037 int sock;
1038 path = luaL_checkstring(L, 1);
1039 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
1040 strcpy(sockaddr.sun_path, path);
1041 if (stat(path, &sb) == 0) {
1042 if (S_ISSOCK(sb.st_mode)) unlink(path);
1044 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1045 listener->fd = -1;
1046 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1047 sock = socket(
1048 PF_LOCAL,
1049 SOCK_STREAM | SOCK_CLOEXEC,
1051 );
1052 if (sock < 0) {
1053 moonbr_io_errmsg();
1054 lua_pushnil(L);
1055 lua_pushstring(L, errmsg);
1056 return 2;
1058 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
1059 moonbr_io_errmsg();
1060 close(sock);
1061 lua_pushnil(L);
1062 lua_pushstring(L, errmsg);
1063 return 2;
1065 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1066 moonbr_io_errmsg();
1067 close(sock);
1068 lua_pushnil(L);
1069 lua_pushstring(L, errmsg);
1070 return 2;
1072 listener->fd = sock;
1073 listener->addrfam = AF_LOCAL;
1074 listener->nonblocking = -1;
1075 return 1;
1078 static int moonbr_io_tcplisten(lua_State *L) {
1079 moonbr_io_listener_t *listener;
1080 const char *host, *port;
1081 struct addrinfo hints = { 0, };
1082 struct addrinfo *res, *addrinfo;
1083 int errcode;
1084 int sock;
1085 host = luaL_optstring(L, 1, NULL);
1086 port = luaL_checkstring(L, 2);
1087 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1088 listener->fd = -1;
1089 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1090 hints.ai_family = AF_UNSPEC;
1091 hints.ai_socktype = SOCK_STREAM;
1092 hints.ai_protocol = IPPROTO_TCP;
1093 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1094 errcode = getaddrinfo(host, port, &hints, &res);
1095 if (errcode) {
1096 freeaddrinfo(res);
1097 if (errcode == EAI_SYSTEM) {
1098 moonbr_io_errmsg();
1099 lua_pushnil(L);
1100 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
1101 } else {
1102 lua_pushnil(L);
1103 lua_pushstring(L, gai_strerror(errcode));
1105 return 2;
1107 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1108 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
1110 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1111 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
1113 addrinfo = res;
1114 moonbr_io_tcpconnect_found:
1115 listener->addrfam = addrinfo->ai_family;
1116 sock = socket(
1117 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
1118 addrinfo->ai_socktype | SOCK_CLOEXEC,
1119 addrinfo->ai_protocol
1120 );
1121 if (sock < 0) {
1122 moonbr_io_errmsg();
1123 freeaddrinfo(res);
1124 lua_pushnil(L);
1125 lua_pushstring(L, errmsg);
1126 return 2;
1129 static const int reuseval = 1;
1130 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1131 moonbr_io_errmsg();
1132 freeaddrinfo(res);
1133 close(sock);
1134 lua_pushnil(L);
1135 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1136 return 2;
1139 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1140 moonbr_io_errmsg();
1141 freeaddrinfo(res);
1142 close(sock);
1143 lua_pushnil(L);
1144 lua_pushstring(L, errmsg);
1145 return 2;
1147 freeaddrinfo(res);
1148 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1149 moonbr_io_errmsg();
1150 close(sock);
1151 lua_pushnil(L);
1152 lua_pushstring(L, errmsg);
1153 return 2;
1155 listener->fd = sock;
1156 listener->nonblocking = -1;
1157 return 1;
1160 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1161 moonbr_io_listener_t *listener;
1162 int fd;
1163 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1164 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1165 if (listener->nonblocking != nonblocking) {
1166 int flags;
1167 flags = fcntl(listener->fd, F_GETFL, 0);
1168 if (flags == -1) {
1169 moonbr_io_errmsg();
1170 close(listener->fd);
1171 listener->fd = -1;
1172 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1174 if (nonblocking) flags |= O_NONBLOCK;
1175 else flags &= ~O_NONBLOCK;
1176 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1177 moonbr_io_errmsg();
1178 close(listener->fd);
1179 listener->fd = -1;
1180 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1182 listener->nonblocking = nonblocking;
1184 while (1) {
1185 #if defined(__linux__) && !defined(_GNU_SOURCE)
1186 fd = accept(listener->fd, NULL, NULL);
1187 if (fd != -1) {
1188 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1189 moonbr_io_errmsg();
1190 close(listener->fd);
1191 listener->fd = -1;
1192 close(fd);
1193 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1196 #else
1197 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1198 #endif
1199 if (fd < 0) {
1200 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1201 lua_pushboolean(L, 0);
1202 lua_pushliteral(L, "No incoming connection pending");
1203 return 2;
1204 } else if (errno != EINTR) {
1205 moonbr_io_errmsg();
1206 lua_pushnil(L);
1207 lua_pushstring(L, errmsg);
1208 return 2;
1210 } else {
1211 moonbr_io_pushhandle(L, fd);
1212 return 1;
1217 static int moonbr_io_accept(lua_State *L) {
1218 return moonbr_io_accept_impl(L, 0);
1221 static int moonbr_io_accept_nb(lua_State *L) {
1222 return moonbr_io_accept_impl(L, 1);
1225 static int moonbr_io_unlisten(lua_State *L) {
1226 moonbr_io_listener_t *listener;
1227 struct sockaddr_un addr;
1228 socklen_t addrlen;
1229 struct stat sb;
1230 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1231 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1232 addrlen = sizeof(addr);
1233 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1234 if (close(listener->fd)) {
1235 moonbr_io_errmsg();
1236 listener->fd = -1;
1237 if (addrlen && addrlen <= sizeof(addr)) {
1238 if (stat(addr.sun_path, &sb) == 0) {
1239 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1242 lua_pushnil(L);
1243 lua_pushstring(L, errmsg);
1244 return 2;
1246 listener->fd = -1;
1247 if (addrlen && addrlen <= sizeof(addr)) {
1248 if (stat(addr.sun_path, &sb) == 0) {
1249 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1252 lua_pushboolean(L, 1);
1253 return 1;
1256 static int moonbr_io_listenergc(lua_State *L) {
1257 moonbr_io_listener_t *listener;
1258 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1259 if (listener->fd >= 0) close(listener->fd);
1260 listener->fd = -1;
1261 return 0;
1264 static int moonbr_io_exec(lua_State *L) {
1265 char **argv;
1266 int i, argc;
1267 int sockin[2], sockout[2], sockerr[2];
1268 volatile int errorcond = 0;
1269 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
1270 moonbr_io_child_t *child;
1271 argc = lua_gettop(L);
1272 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
1273 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
1274 argv[argc] = NULL;
1275 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
1276 child->pid = 0;
1277 lua_newtable(L);
1278 lua_setuservalue(L, -2);
1279 luaL_setmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
1280 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
1281 moonbr_io_errmsg();
1282 lua_pushnil(L);
1283 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1284 return 2;
1286 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
1287 moonbr_io_errmsg();
1288 close(sockin[0]);
1289 close(sockin[1]);
1290 lua_pushnil(L);
1291 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1292 return 2;
1294 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
1295 moonbr_io_errmsg();
1296 close(sockin[0]);
1297 close(sockin[1]);
1298 close(sockout[0]);
1299 close(sockout[1]);
1300 lua_pushnil(L);
1301 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1302 return 2;
1304 child->pid = vfork();
1305 if (child->pid == -1) {
1306 moonbr_io_errmsg();
1307 close(sockin[0]);
1308 close(sockin[1]);
1309 close(sockout[0]);
1310 close(sockout[1]);
1311 close(sockerr[0]);
1312 close(sockerr[1]);
1313 lua_pushnil(L);
1314 lua_pushfstring(L, "Could not fork: %s", errmsg);
1315 return 2;
1317 if (!child->pid) {
1318 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
1319 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
1320 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
1321 closefrom(3);
1322 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1323 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1324 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1325 if (execvp(argv[0], argv)) {
1326 errorcond = 2;
1327 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1328 _exit(0);
1330 moonbr_io_exec_error1:
1331 errorcond = 1;
1332 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1333 _exit(0);
1335 close(sockin[1]);
1336 close(sockout[1]);
1337 close(sockerr[1]);
1338 if (errorcond) {
1339 int status;
1340 close(sockin[0]);
1341 close(sockout[0]);
1342 close(sockerr[0]);
1343 while (waitpid(child->pid, &status, 0) == -1) {
1344 if (errno != EINTR) {
1345 moonbr_io_errmsg();
1346 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
1349 child->pid = 0;
1350 lua_pushnil(L);
1351 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
1352 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
1353 return 2;
1355 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1356 lua_pushlightuserdata(L, &sockin[0]);
1357 if (lua_pcall(L, 1, 1, 0)) {
1358 if (sockin[0] != -1) close(sockin[0]);
1359 close(sockout[0]);
1360 close(sockerr[0]);
1361 goto moonbr_io_exec_error2;
1363 lua_setfield(L, -2, "stdin");
1364 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1365 lua_pushlightuserdata(L, &sockout[0]);
1366 if (lua_pcall(L, 1, 1, 0)) {
1367 if (sockout[0] != -1) close(sockout[0]);
1368 close(sockerr[0]);
1369 goto moonbr_io_exec_error2;
1371 lua_setfield(L, -2, "stdout");
1372 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1373 lua_pushlightuserdata(L, &sockerr[0]);
1374 if (lua_pcall(L, 1, 1, 0)) {
1375 if (sockerr[0] != -1) close(sockerr[0]);
1376 goto moonbr_io_exec_error2;
1378 lua_setfield(L, -2, "stderr");
1379 return 1;
1380 moonbr_io_exec_error2:
1382 int status;
1383 while (waitpid(child->pid, &status, 0) == -1) {
1384 if (errno != EINTR) {
1385 moonbr_io_errmsg();
1386 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
1390 child->pid = 0;
1391 return lua_error(L);
1394 static int moonbr_io_childindex(lua_State *L) {
1395 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1396 luaL_checkany(L, 2);
1397 lua_getuservalue(L, 1);
1398 lua_pushvalue(L, 2);
1399 lua_gettable(L, -2);
1400 if (lua_isnil(L, -1)) {
1401 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
1402 lua_pushvalue(L, 2);
1403 lua_gettable(L, -2);
1405 return 1;
1408 static int moonbr_io_childnewindex(lua_State *L) {
1409 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1410 luaL_checkany(L, 2);
1411 luaL_checkany(L, 3);
1412 lua_getuservalue(L, 1);
1413 lua_pushvalue(L, 2);
1414 lua_pushvalue(L, 3);
1415 lua_settable(L, -3);
1416 return 0;
1419 static int moonbr_io_childgc(lua_State *L) {
1420 moonbr_io_child_t *child;
1421 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1422 if (child->pid) {
1423 int status;
1424 if (kill(child->pid, SIGKILL)) {
1425 moonbr_io_errmsg();
1426 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
1428 while (waitpid(child->pid, &status, 0) == -1) {
1429 if (errno != EINTR) {
1430 moonbr_io_errmsg();
1431 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
1435 return 0;
1438 static int moonbr_io_kill(lua_State *L) {
1439 moonbr_io_child_t *child;
1440 int sig;
1441 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1442 sig = luaL_optinteger(L, 2, SIGTERM);
1443 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
1444 if (kill(child->pid, sig)) {
1445 moonbr_io_errmsg();
1446 luaL_error(L, "Error in kill call: %s", errmsg);
1448 lua_settop(L, 1);
1449 return 1;
1452 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
1453 moonbr_io_child_t *child;
1454 pid_t waitedpid;
1455 int status;
1456 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1457 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
1458 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
1459 if (errno != EINTR) {
1460 moonbr_io_errmsg();
1461 luaL_error(L, "Error in waitpid call: %s", errmsg);
1464 if (!waitedpid) {
1465 lua_pushboolean(L, 0);
1466 lua_pushliteral(L, "Process is still running");
1467 return 2;
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");
1477 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