moonbridge

view moonbridge_io.c @ 213:43a077f2ab49

Safety catch for noncompliant strerror_r() implementation on GNU/Linux systems
author jbe
date Mon Jun 22 22:26:55 2015 +0200 (2015-06-22)
parents 453b7d1a7944
children 45a425c75643
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 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1187 if (fd < 0) {
1188 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1189 lua_pushboolean(L, 0);
1190 lua_pushliteral(L, "No incoming connection pending");
1191 return 2;
1192 } else if (errno != EINTR) {
1193 moonbr_io_errmsg();
1194 lua_pushnil(L);
1195 lua_pushstring(L, errmsg);
1196 return 2;
1198 } else {
1199 moonbr_io_pushhandle(L, fd);
1200 return 1;
1205 static int moonbr_io_accept(lua_State *L) {
1206 return moonbr_io_accept_impl(L, 0);
1209 static int moonbr_io_accept_nb(lua_State *L) {
1210 return moonbr_io_accept_impl(L, 1);
1213 static int moonbr_io_unlisten(lua_State *L) {
1214 moonbr_io_listener_t *listener;
1215 struct sockaddr_un addr;
1216 socklen_t addrlen;
1217 struct stat sb;
1218 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1219 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1220 addrlen = sizeof(addr);
1221 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1222 if (close(listener->fd)) {
1223 moonbr_io_errmsg();
1224 listener->fd = -1;
1225 if (addrlen && addrlen <= sizeof(addr)) {
1226 if (stat(addr.sun_path, &sb) == 0) {
1227 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1230 lua_pushnil(L);
1231 lua_pushstring(L, errmsg);
1232 return 2;
1234 listener->fd = -1;
1235 if (addrlen && addrlen <= sizeof(addr)) {
1236 if (stat(addr.sun_path, &sb) == 0) {
1237 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1240 lua_pushboolean(L, 1);
1241 return 1;
1244 static int moonbr_io_listenergc(lua_State *L) {
1245 moonbr_io_listener_t *listener;
1246 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1247 if (listener->fd >= 0) close(listener->fd);
1248 listener->fd = -1;
1249 return 0;
1252 static int moonbr_io_exec(lua_State *L) {
1253 char **argv;
1254 int i, argc;
1255 int sockin[2], sockout[2], sockerr[2];
1256 volatile int errorcond = 0;
1257 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
1258 moonbr_io_child_t *child;
1259 argc = lua_gettop(L);
1260 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
1261 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
1262 argv[argc] = NULL;
1263 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
1264 child->pid = 0;
1265 lua_newtable(L);
1266 lua_setuservalue(L, -2);
1267 luaL_getmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
1268 lua_setmetatable(L, -2);
1269 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
1270 moonbr_io_errmsg();
1271 lua_pushnil(L);
1272 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1273 return 2;
1275 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
1276 moonbr_io_errmsg();
1277 close(sockin[0]);
1278 close(sockin[1]);
1279 lua_pushnil(L);
1280 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1281 return 2;
1283 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
1284 moonbr_io_errmsg();
1285 close(sockin[0]);
1286 close(sockin[1]);
1287 close(sockout[0]);
1288 close(sockout[1]);
1289 lua_pushnil(L);
1290 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1291 return 2;
1293 child->pid = vfork();
1294 if (child->pid == -1) {
1295 moonbr_io_errmsg();
1296 close(sockin[0]);
1297 close(sockin[1]);
1298 close(sockout[0]);
1299 close(sockout[1]);
1300 close(sockerr[0]);
1301 close(sockerr[1]);
1302 lua_pushnil(L);
1303 lua_pushfstring(L, "Could not fork: %s", errmsg);
1304 return 2;
1306 if (!child->pid) {
1307 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
1308 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
1309 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
1310 closefrom(4);
1311 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1312 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1313 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1314 if (execvp(argv[0], argv)) {
1315 errorcond = 2;
1316 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1317 _exit(0);
1319 moonbr_io_exec_error1:
1320 errorcond = 1;
1321 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1322 _exit(0);
1324 close(sockin[1]);
1325 close(sockout[1]);
1326 close(sockerr[1]);
1327 if (errorcond) {
1328 int status;
1329 close(sockin[0]);
1330 close(sockout[0]);
1331 close(sockerr[0]);
1332 while (waitpid(child->pid, &status, 0) == -1) {
1333 if (errno != EINTR) {
1334 moonbr_io_errmsg();
1335 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
1338 child->pid = 0;
1339 lua_pushnil(L);
1340 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
1341 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
1342 return 2;
1344 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1345 lua_pushlightuserdata(L, &sockin[0]);
1346 if (lua_pcall(L, 1, 1, 0)) {
1347 if (sockin[0] != -1) close(sockin[0]);
1348 close(sockout[0]);
1349 close(sockerr[0]);
1350 goto moonbr_io_exec_error2;
1352 lua_setfield(L, -2, "stdin");
1353 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1354 lua_pushlightuserdata(L, &sockout[0]);
1355 if (lua_pcall(L, 1, 1, 0)) {
1356 if (sockout[0] != -1) close(sockout[0]);
1357 close(sockerr[0]);
1358 goto moonbr_io_exec_error2;
1360 lua_setfield(L, -2, "stdout");
1361 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1362 lua_pushlightuserdata(L, &sockerr[0]);
1363 if (lua_pcall(L, 1, 1, 0)) {
1364 if (sockerr[0] != -1) close(sockerr[0]);
1365 goto moonbr_io_exec_error2;
1367 lua_setfield(L, -2, "stderr");
1368 return 1;
1369 moonbr_io_exec_error2:
1371 int status;
1372 while (waitpid(child->pid, &status, 0) == -1) {
1373 if (errno != EINTR) {
1374 moonbr_io_errmsg();
1375 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
1379 child->pid = 0;
1380 return lua_error(L);
1383 static int moonbr_io_childindex(lua_State *L) {
1384 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1385 luaL_checkany(L, 2);
1386 lua_getuservalue(L, 1);
1387 lua_pushvalue(L, 2);
1388 lua_gettable(L, -2);
1389 if (lua_isnil(L, -1)) {
1390 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
1391 lua_pushvalue(L, 2);
1392 lua_gettable(L, -2);
1394 return 1;
1397 static int moonbr_io_childnewindex(lua_State *L) {
1398 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1399 luaL_checkany(L, 2);
1400 luaL_checkany(L, 3);
1401 lua_getuservalue(L, 1);
1402 lua_pushvalue(L, 2);
1403 lua_pushvalue(L, 3);
1404 lua_settable(L, -3);
1405 return 0;
1408 static int moonbr_io_childgc(lua_State *L) {
1409 moonbr_io_child_t *child;
1410 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1411 if (child->pid) {
1412 int status;
1413 if (kill(child->pid, SIGKILL)) {
1414 moonbr_io_errmsg();
1415 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
1417 while (waitpid(child->pid, &status, 0) == -1) {
1418 if (errno != EINTR) {
1419 moonbr_io_errmsg();
1420 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
1424 return 0;
1427 static int moonbr_io_kill(lua_State *L) {
1428 moonbr_io_child_t *child;
1429 int sig;
1430 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1431 sig = luaL_optinteger(L, 2, SIGTERM);
1432 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
1433 if (kill(child->pid, sig)) {
1434 moonbr_io_errmsg();
1435 luaL_error(L, "Error in kill call: %s", errmsg);
1437 lua_settop(L, 1);
1438 return 1;
1441 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
1442 moonbr_io_child_t *child;
1443 pid_t waitedpid;
1444 int status;
1445 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1446 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
1447 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
1448 if (errno != EINTR) {
1449 moonbr_io_errmsg();
1450 luaL_error(L, "Error in waitpid call: %s", errmsg);
1453 if (!waitedpid) {
1454 lua_pushnil(L);
1455 } else {
1456 child->pid = 0;
1457 if (WIFEXITED(status)) {
1458 lua_pushinteger(L, WEXITSTATUS(status));
1459 } else if (WIFSIGNALED(status)) {
1460 lua_pushinteger(L, -WTERMSIG(status));
1461 } else {
1462 luaL_error(L, "Unexpected status value returned by waitpid call");
1465 return 1;
1468 static int moonbr_io_wait(lua_State *L) {
1469 return moonbr_io_wait_impl(L, 0);
1472 static int moonbr_io_wait_nb(lua_State *L) {
1473 return moonbr_io_wait_impl(L, 1);
1476 #if LUA_VERSION_NUM >= 503
1477 static int moonbr_io_wait_cont(lua_State *L, int status, lua_KContext ctx) {
1478 #else
1479 static int moonbr_io_wait_cont(lua_State *L) {
1480 #endif
1481 #if !(LUA_VERSION_NUM >= 503)
1482 int ctx = 0;
1483 lua_getctx(L, &ctx);
1484 #endif
1485 while (1) {
1486 lua_pushcfunction(L, moonbr_io_wait_nb);
1487 lua_pushvalue(L, 1);
1488 lua_call(L, 1, 1);
1489 if (!lua_isnil(L, -1)) break;
1490 lua_pushvalue(L, 2);
1491 lua_callk(L, 0, 0, ctx, moonbr_io_wait_cont);
1493 return 1;
1496 static int moonbr_io_wait_call(lua_State *L) {
1497 lua_settop(L, 2);
1498 #if LUA_VERSION_NUM >= 503
1499 return moonbr_io_wait_cont(L, 0, 0);
1500 #else
1501 return moonbr_io_wait_cont(L);
1502 #endif
1505 moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
1507 static int moonbr_io_poll(lua_State *L) {
1508 moonbr_io_handle_t *handle;
1509 moonbr_io_listener_t *listener;
1510 int fd, isnum;
1511 int nfds = 0;
1512 fd_set readfds, writefds, exceptfds;
1513 struct timeval timeout = {0, };
1514 int status;
1515 FD_ZERO(&readfds);
1516 FD_ZERO(&writefds);
1517 FD_ZERO(&exceptfds);
1518 if (!lua_isnoneornil(L, 1)) {
1519 luaL_checktype(L, 1, LUA_TTABLE);
1520 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1521 if (lua_toboolean(L, -1)) {
1522 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1523 if (handle) {
1524 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1525 fd = handle->fd;
1526 if (
1527 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1528 handle->readbufin != handle->readbufout /* data pending in buffer */
1529 ) {
1530 lua_pushboolean(L, 1);
1531 return 1;
1533 } else {
1534 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1535 if (listener) {
1536 fd = listener->fd;
1537 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1538 } else {
1539 fd = lua_tointegerx(L, -2, &isnum);
1540 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1543 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1544 FD_SET(fd, &readfds);
1545 if (fd+1 > nfds) nfds = fd+1;
1549 if (!lua_isnoneornil(L, 2)) {
1550 luaL_checktype(L, 2, LUA_TTABLE);
1551 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1552 if (lua_toboolean(L, -1)) {
1553 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1554 if (handle) {
1555 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1556 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1557 fd = handle->fd;
1558 } else {
1559 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1560 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1561 fd = lua_tointegerx(L, -2, &isnum);
1562 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1564 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1565 FD_SET(fd, &writefds);
1566 if (fd+1 > nfds) nfds = fd+1;
1570 if (!lua_isnoneornil(L, 3)) {
1571 lua_Number n;
1572 n = lua_tonumberx(L, 3, &isnum);
1573 if (isnum && n<0) {
1574 lua_pushboolean(L, 0);
1575 lua_pushliteral(L, "Negative timeout");
1576 return 2;
1577 } else if (isnum && n>=0 && n<100000000) {
1578 timeout.tv_sec = n;
1579 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1580 } else {
1581 luaL_argcheck(L, 0, 3, "not a valid timeout");
1583 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1584 } else {
1585 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1587 if (status == -1) {
1588 if (errno == EINTR) {
1589 lua_pushnil(L);
1590 lua_pushliteral(L, "Signal received while polling file descriptors");
1591 return 2;
1592 } else {
1593 moonbr_io_errmsg();
1594 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1596 } else if (status == 0) {
1597 lua_pushboolean(L, 0);
1598 lua_pushliteral(L, "Timeout while polling file descriptors");
1599 return 2;
1600 } else {
1601 lua_pushboolean(L, 1);
1602 return 1;
1606 static int moonbr_io_timeref(lua_State *L) {
1607 lua_Number sub;
1608 struct timespec tp;
1609 sub = luaL_optnumber(L, 1, 0);
1610 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1611 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1613 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1614 return 1;
1617 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1618 {"read", moonbr_io_read},
1619 {"read_nb", moonbr_io_read_nb},
1620 {"read_call", moonbr_io_read_call},
1621 {"read_yield", moonbr_io_read_yield},
1622 {"drain", moonbr_io_drain},
1623 {"drain_nb", moonbr_io_drain_nb},
1624 {"drain_call", moonbr_io_drain_call},
1625 {"drain_yield", moonbr_io_drain_yield},
1626 {"write", moonbr_io_write},
1627 {"write_nb", moonbr_io_write_nb},
1628 {"write_call", moonbr_io_write_call},
1629 {"write_yield", moonbr_io_write_yield},
1630 {"flush", moonbr_io_flush},
1631 {"flush_nb", moonbr_io_flush_nb},
1632 {"flush_call", moonbr_io_flush_call},
1633 {"flush_yield", moonbr_io_flush_yield},
1634 {"finish", moonbr_io_finish},
1635 {"close", moonbr_io_close},
1636 {"reset", moonbr_io_reset},
1637 {NULL, NULL}
1638 };
1640 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1641 {"__index", moonbr_io_handleindex},
1642 {"__newindex", moonbr_io_handlenewindex},
1643 {"__gc", moonbr_io_handlegc},
1644 {NULL, NULL}
1645 };
1647 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1648 {"accept", moonbr_io_accept},
1649 {"accept_nb", moonbr_io_accept_nb},
1650 {"close", moonbr_io_unlisten},
1651 {NULL, NULL}
1652 };
1654 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1655 {"__gc", moonbr_io_listenergc},
1656 {NULL, NULL}
1657 };
1659 static const struct luaL_Reg moonbr_io_child_methods[] = {
1660 {"kill", moonbr_io_kill},
1661 {"wait", moonbr_io_wait},
1662 {"wait_nb", moonbr_io_wait_nb},
1663 {"wait_call", moonbr_io_wait_call},
1664 {"wait_yield", moonbr_io_wait_yield},
1665 {NULL, NULL}
1666 };
1668 static const struct luaL_Reg moonbr_io_child_metamethods[] = {
1669 {"__index", moonbr_io_childindex},
1670 {"__newindex", moonbr_io_childnewindex},
1671 {"__gc", moonbr_io_childgc},
1672 {NULL, NULL}
1673 };
1675 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1676 {"localconnect", moonbr_io_localconnect},
1677 {"localconnect_nb", moonbr_io_localconnect_nb},
1678 {"tcpconnect", moonbr_io_tcpconnect},
1679 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1680 {"locallisten", moonbr_io_locallisten},
1681 {"tcplisten", moonbr_io_tcplisten},
1682 {"exec", moonbr_io_exec},
1683 {"poll", moonbr_io_poll},
1684 {"timeref", moonbr_io_timeref},
1685 {NULL, NULL}
1686 };
1688 int luaopen_moonbridge_io(lua_State *L) {
1690 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1692 lua_newtable(L); // module
1694 lua_newtable(L); // public metatable
1695 lua_newtable(L); // handle methods
1696 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1697 lua_pushvalue(L, -1);
1698 lua_setfield(L, -4, "handle_pt");
1699 lua_setfield(L, -2, "__index");
1700 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1702 lua_newtable(L); // handle metatable
1703 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1704 lua_pushvalue(L, -1);
1705 lua_setfield(L, -3, "handle_mt");
1706 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1708 lua_newtable(L); // listener metatable
1709 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1710 lua_newtable(L); // listener methods
1711 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1712 lua_pushvalue(L, -1);
1713 lua_setfield(L, -4, "listener_pt");
1714 lua_setfield(L, -2, "__index");
1715 lua_pushvalue(L, -1);
1716 lua_setfield(L, -3, "listener_mt");
1717 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1719 lua_newtable(L); // child methods
1720 luaL_setfuncs(L, moonbr_io_child_methods, 0);
1721 lua_pushvalue(L, -1);
1722 lua_setfield(L, -3, "child_pt");
1723 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_PT_REGKEY);
1724 lua_newtable(L); // child metatable
1725 luaL_setfuncs(L, moonbr_io_child_metamethods, 0);
1726 lua_pushvalue(L, -1);
1727 lua_setfield(L, -3, "child_mt");
1728 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
1730 moonbr_io_pushhandle(L, 0);
1731 lua_setfield(L, -2, "stdin");
1732 moonbr_io_pushhandle(L, 1);
1733 lua_setfield(L, -2, "stdout");
1734 moonbr_io_pushhandle(L, 2);
1735 lua_setfield(L, -2, "stderr");
1737 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1738 return 1;

Impressum / About Us