moonbridge

view moonbridge_io.c @ 233:8593a1f2c15d

Proper documentation of error handling of moonbridge_io.exec(...) function
author jbe
date Sat Jan 16 01:18:21 2016 +0100 (2016-01-16)
parents 6bab0621806a
children 1fd00eed96ee 73d832c4a9b5
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_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
786 lua_setmetatable(L, -2);
787 lua_newtable(L); // uservalue
788 lua_newtable(L);
789 lua_setfield(L, -2, "writequeue");
790 lua_newtable(L); // public
791 if (handle->addrfam == AF_INET6) {
792 struct sockaddr_in6 addr_in6;
793 char addrstrbuf[INET6_ADDRSTRLEN];
794 const char *addrstr;
795 addrlen = sizeof(addr_in6);
796 if (getsockname(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
797 moonbr_io_errmsg();
798 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
799 }
800 if (addrlen > sizeof(addr_in6)) {
801 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
802 }
803 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
804 if (!addrstr) {
805 moonbr_io_errmsg();
806 luaL_error(L, "Could not format local IP address: %s", errmsg);
807 } else {
808 lua_pushstring(L, addrstr);
809 lua_setfield(L, -2, "local_ip6");
810 }
811 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
812 lua_setfield(L, -2, "local_tcpport");
813 if (getpeername(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
814 moonbr_io_errmsg();
815 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
816 }
817 if (addrlen > sizeof(addr_in6)) {
818 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
819 }
820 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
821 if (!addrstr) {
822 moonbr_io_errmsg();
823 luaL_error(L, "Could not format remote IP address: %s", errmsg);
824 } else {
825 lua_pushstring(L, addrstr);
826 lua_setfield(L, -2, "remote_ip6");
827 }
828 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
829 lua_setfield(L, -2, "remote_tcpport");
830 } else if (handle->addrfam == AF_INET) {
831 struct sockaddr_in addr_in;
832 char addrstrbuf[INET_ADDRSTRLEN];
833 const char *addrstr;
834 addrlen = sizeof(addr_in);
835 if (getsockname(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
836 moonbr_io_errmsg();
837 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
838 }
839 if (addrlen > sizeof(addr_in)) {
840 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
841 }
842 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
843 if (!addrstr) {
844 moonbr_io_errmsg();
845 luaL_error(L, "Could not format local IP address: %s", errmsg);
846 } else {
847 lua_pushstring(L, addrstr);
848 lua_setfield(L, -2, "local_ip4");
849 }
850 lua_pushinteger(L, ntohs(addr_in.sin_port));
851 lua_setfield(L, -2, "local_tcpport");
852 if (getpeername(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
853 moonbr_io_errmsg();
854 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
855 }
856 if (addrlen > sizeof(addr_in)) {
857 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
858 }
859 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
860 if (!addrstr) {
861 moonbr_io_errmsg();
862 luaL_error(L, "Could not format remote IP address: %s", errmsg);
863 } else {
864 lua_pushstring(L, addrstr);
865 lua_setfield(L, -2, "remote_ip4");
866 }
867 lua_pushinteger(L, ntohs(addr_in.sin_port));
868 lua_setfield(L, -2, "remote_tcpport");
869 }
870 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
871 lua_setmetatable(L, -2);
872 lua_setfield(L, -2, "public");
873 lua_setuservalue(L, -2);
874 handle->fd = *fd;
875 *fd = -1; /* closing is now handled by garbage collection */
876 return 1;
877 }
879 void moonbr_io_pushhandle(lua_State *L, int fd) {
880 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
881 lua_pushlightuserdata(L, &fd);
882 if (lua_pcall(L, 1, 1, 0)) {
883 if (fd != -1) close(fd);
884 lua_error(L);
885 }
886 }
888 static int moonbr_io_handleindex(lua_State *L) {
889 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
890 luaL_checkany(L, 2);
891 lua_getuservalue(L, 1);
892 lua_getfield(L, -1, "public");
893 lua_pushvalue(L, 2);
894 lua_gettable(L, -2);
895 return 1;
896 }
898 static int moonbr_io_handlenewindex(lua_State *L) {
899 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
900 luaL_checkany(L, 2);
901 luaL_checkany(L, 3);
902 lua_getuservalue(L, 1);
903 lua_getfield(L, -1, "public");
904 lua_pushvalue(L, 2);
905 lua_pushvalue(L, 3);
906 lua_settable(L, -3);
907 return 0;
908 }
910 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
911 const char *path;
912 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
913 const int path_maxlen = sizeof(struct sockaddr_un) - (
914 (void *)sockaddr.sun_path - (void *)&sockaddr
915 ) - 1; /* one byte for termination */
916 int sock;
917 path = luaL_checkstring(L, 1);
918 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
919 strcpy(sockaddr.sun_path, path);
920 sock = socket(
921 PF_LOCAL,
922 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
923 0
924 );
925 if (sock < 0) {
926 moonbr_io_errmsg();
927 lua_pushnil(L);
928 lua_pushstring(L, errmsg);
929 return 2;
930 }
931 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
932 if (!nonblocking && errno == EINTR) {
933 moonbr_io_errmsg();
934 close(sock);
935 lua_pushnil(L);
936 lua_pushstring(L, errmsg);
937 return 2;
938 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
939 moonbr_io_errmsg();
940 lua_pushnil(L);
941 lua_pushstring(L, errmsg);
942 return 2;
943 }
944 }
945 moonbr_io_pushhandle(L, sock);
946 return 1;
947 }
949 static int moonbr_io_localconnect(lua_State *L) {
950 return moonbr_io_localconnect_impl(L, 0);
951 }
953 static int moonbr_io_localconnect_nb(lua_State *L) {
954 return moonbr_io_localconnect_impl(L, 1);
955 }
957 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
958 const char *host, *port;
959 struct addrinfo hints = { 0, };
960 struct addrinfo *res, *addrinfo;
961 int errcode;
962 int sock;
963 host = luaL_checkstring(L, 1);
964 port = luaL_checkstring(L, 2);
965 hints.ai_family = AF_UNSPEC;
966 hints.ai_socktype = SOCK_STREAM;
967 hints.ai_protocol = IPPROTO_TCP;
968 hints.ai_flags = AI_ADDRCONFIG;
969 errcode = getaddrinfo(host, port, &hints, &res);
970 if (errcode) {
971 freeaddrinfo(res);
972 if (errcode == EAI_SYSTEM) {
973 moonbr_io_errmsg();
974 lua_pushnil(L);
975 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
976 } else {
977 lua_pushnil(L);
978 lua_pushstring(L, gai_strerror(errcode));
979 }
980 return 2;
981 }
982 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
983 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
984 }
985 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
986 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
987 }
988 addrinfo = res;
989 moonbr_io_tcpconnect_found:
990 sock = socket(
991 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
992 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
993 addrinfo->ai_protocol
994 );
995 if (sock < 0) {
996 moonbr_io_errmsg();
997 freeaddrinfo(res);
998 lua_pushnil(L);
999 lua_pushstring(L, errmsg);
1000 return 2;
1002 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1003 freeaddrinfo(res);
1004 if (!nonblocking && errno == EINTR) {
1005 moonbr_io_errmsg();
1006 close(sock);
1007 lua_pushnil(L);
1008 lua_pushstring(L, errmsg);
1009 return 2;
1010 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
1011 moonbr_io_errmsg();
1012 lua_pushnil(L);
1013 lua_pushstring(L, errmsg);
1014 return 2;
1016 } else {
1017 freeaddrinfo(res);
1019 moonbr_io_pushhandle(L, sock);
1020 return 1;
1023 static int moonbr_io_tcpconnect(lua_State *L) {
1024 return moonbr_io_tcpconnect_impl(L, 0);
1027 static int moonbr_io_tcpconnect_nb(lua_State *L) {
1028 return moonbr_io_tcpconnect_impl(L, 1);
1031 static int moonbr_io_locallisten(lua_State *L) {
1032 moonbr_io_listener_t *listener;
1033 const char *path;
1034 struct stat sb;
1035 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
1036 const int path_maxlen = sizeof(struct sockaddr_un) - (
1037 (void *)sockaddr.sun_path - (void *)&sockaddr
1038 ) - 1; /* one byte for termination */
1039 int sock;
1040 path = luaL_checkstring(L, 1);
1041 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
1042 strcpy(sockaddr.sun_path, path);
1043 if (stat(path, &sb) == 0) {
1044 if (S_ISSOCK(sb.st_mode)) unlink(path);
1046 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1047 listener->fd = -1;
1048 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1049 sock = socket(
1050 PF_LOCAL,
1051 SOCK_STREAM | SOCK_CLOEXEC,
1053 );
1054 if (sock < 0) {
1055 moonbr_io_errmsg();
1056 lua_pushnil(L);
1057 lua_pushstring(L, errmsg);
1058 return 2;
1060 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
1061 moonbr_io_errmsg();
1062 close(sock);
1063 lua_pushnil(L);
1064 lua_pushstring(L, errmsg);
1065 return 2;
1067 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1068 moonbr_io_errmsg();
1069 close(sock);
1070 lua_pushnil(L);
1071 lua_pushstring(L, errmsg);
1072 return 2;
1074 listener->fd = sock;
1075 listener->addrfam = AF_LOCAL;
1076 listener->nonblocking = -1;
1077 return 1;
1080 static int moonbr_io_tcplisten(lua_State *L) {
1081 moonbr_io_listener_t *listener;
1082 const char *host, *port;
1083 struct addrinfo hints = { 0, };
1084 struct addrinfo *res, *addrinfo;
1085 int errcode;
1086 int sock;
1087 host = luaL_optstring(L, 1, NULL);
1088 port = luaL_checkstring(L, 2);
1089 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1090 listener->fd = -1;
1091 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1092 hints.ai_family = AF_UNSPEC;
1093 hints.ai_socktype = SOCK_STREAM;
1094 hints.ai_protocol = IPPROTO_TCP;
1095 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1096 errcode = getaddrinfo(host, port, &hints, &res);
1097 if (errcode) {
1098 freeaddrinfo(res);
1099 if (errcode == EAI_SYSTEM) {
1100 moonbr_io_errmsg();
1101 lua_pushnil(L);
1102 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
1103 } else {
1104 lua_pushnil(L);
1105 lua_pushstring(L, gai_strerror(errcode));
1107 return 2;
1109 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1110 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
1112 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1113 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
1115 addrinfo = res;
1116 moonbr_io_tcpconnect_found:
1117 listener->addrfam = addrinfo->ai_family;
1118 sock = socket(
1119 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
1120 addrinfo->ai_socktype | SOCK_CLOEXEC,
1121 addrinfo->ai_protocol
1122 );
1123 if (sock < 0) {
1124 moonbr_io_errmsg();
1125 freeaddrinfo(res);
1126 lua_pushnil(L);
1127 lua_pushstring(L, errmsg);
1128 return 2;
1131 static const int reuseval = 1;
1132 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1133 moonbr_io_errmsg();
1134 freeaddrinfo(res);
1135 close(sock);
1136 lua_pushnil(L);
1137 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1138 return 2;
1141 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1142 moonbr_io_errmsg();
1143 freeaddrinfo(res);
1144 close(sock);
1145 lua_pushnil(L);
1146 lua_pushstring(L, errmsg);
1147 return 2;
1149 freeaddrinfo(res);
1150 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1151 moonbr_io_errmsg();
1152 close(sock);
1153 lua_pushnil(L);
1154 lua_pushstring(L, errmsg);
1155 return 2;
1157 listener->fd = sock;
1158 listener->nonblocking = -1;
1159 return 1;
1162 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1163 moonbr_io_listener_t *listener;
1164 int fd;
1165 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1166 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1167 if (listener->nonblocking != nonblocking) {
1168 int flags;
1169 flags = fcntl(listener->fd, F_GETFL, 0);
1170 if (flags == -1) {
1171 moonbr_io_errmsg();
1172 close(listener->fd);
1173 listener->fd = -1;
1174 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1176 if (nonblocking) flags |= O_NONBLOCK;
1177 else flags &= ~O_NONBLOCK;
1178 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1179 moonbr_io_errmsg();
1180 close(listener->fd);
1181 listener->fd = -1;
1182 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1184 listener->nonblocking = nonblocking;
1186 while (1) {
1187 #if defined(__linux__) && !defined(_GNU_SOURCE)
1188 fd = accept(listener->fd, NULL, NULL);
1189 if (fd != -1) {
1190 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1191 moonbr_io_errmsg();
1192 close(listener->fd);
1193 listener->fd = -1;
1194 close(fd);
1195 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1198 #else
1199 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1200 #endif
1201 if (fd < 0) {
1202 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1203 lua_pushboolean(L, 0);
1204 lua_pushliteral(L, "No incoming connection pending");
1205 return 2;
1206 } else if (errno != EINTR) {
1207 moonbr_io_errmsg();
1208 lua_pushnil(L);
1209 lua_pushstring(L, errmsg);
1210 return 2;
1212 } else {
1213 moonbr_io_pushhandle(L, fd);
1214 return 1;
1219 static int moonbr_io_accept(lua_State *L) {
1220 return moonbr_io_accept_impl(L, 0);
1223 static int moonbr_io_accept_nb(lua_State *L) {
1224 return moonbr_io_accept_impl(L, 1);
1227 static int moonbr_io_unlisten(lua_State *L) {
1228 moonbr_io_listener_t *listener;
1229 struct sockaddr_un addr;
1230 socklen_t addrlen;
1231 struct stat sb;
1232 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1233 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1234 addrlen = sizeof(addr);
1235 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1236 if (close(listener->fd)) {
1237 moonbr_io_errmsg();
1238 listener->fd = -1;
1239 if (addrlen && addrlen <= sizeof(addr)) {
1240 if (stat(addr.sun_path, &sb) == 0) {
1241 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1244 lua_pushnil(L);
1245 lua_pushstring(L, errmsg);
1246 return 2;
1248 listener->fd = -1;
1249 if (addrlen && addrlen <= sizeof(addr)) {
1250 if (stat(addr.sun_path, &sb) == 0) {
1251 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1254 lua_pushboolean(L, 1);
1255 return 1;
1258 static int moonbr_io_listenergc(lua_State *L) {
1259 moonbr_io_listener_t *listener;
1260 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1261 if (listener->fd >= 0) close(listener->fd);
1262 listener->fd = -1;
1263 return 0;
1266 static int moonbr_io_exec(lua_State *L) {
1267 char **argv;
1268 int i, argc;
1269 int sockin[2], sockout[2], sockerr[2];
1270 volatile int errorcond = 0;
1271 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
1272 moonbr_io_child_t *child;
1273 argc = lua_gettop(L);
1274 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
1275 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
1276 argv[argc] = NULL;
1277 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
1278 child->pid = 0;
1279 lua_newtable(L);
1280 lua_setuservalue(L, -2);
1281 luaL_getmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
1282 lua_setmetatable(L, -2);
1283 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
1284 moonbr_io_errmsg();
1285 lua_pushnil(L);
1286 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1287 return 2;
1289 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
1290 moonbr_io_errmsg();
1291 close(sockin[0]);
1292 close(sockin[1]);
1293 lua_pushnil(L);
1294 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1295 return 2;
1297 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
1298 moonbr_io_errmsg();
1299 close(sockin[0]);
1300 close(sockin[1]);
1301 close(sockout[0]);
1302 close(sockout[1]);
1303 lua_pushnil(L);
1304 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
1305 return 2;
1307 child->pid = vfork();
1308 if (child->pid == -1) {
1309 moonbr_io_errmsg();
1310 close(sockin[0]);
1311 close(sockin[1]);
1312 close(sockout[0]);
1313 close(sockout[1]);
1314 close(sockerr[0]);
1315 close(sockerr[1]);
1316 lua_pushnil(L);
1317 lua_pushfstring(L, "Could not fork: %s", errmsg);
1318 return 2;
1320 if (!child->pid) {
1321 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
1322 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
1323 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
1324 closefrom(3);
1325 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1326 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1327 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
1328 if (execvp(argv[0], argv)) {
1329 errorcond = 2;
1330 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1331 _exit(0);
1333 moonbr_io_exec_error1:
1334 errorcond = 1;
1335 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
1336 _exit(0);
1338 close(sockin[1]);
1339 close(sockout[1]);
1340 close(sockerr[1]);
1341 if (errorcond) {
1342 int status;
1343 close(sockin[0]);
1344 close(sockout[0]);
1345 close(sockerr[0]);
1346 while (waitpid(child->pid, &status, 0) == -1) {
1347 if (errno != EINTR) {
1348 moonbr_io_errmsg();
1349 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
1352 child->pid = 0;
1353 lua_pushnil(L);
1354 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
1355 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
1356 return 2;
1358 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1359 lua_pushlightuserdata(L, &sockin[0]);
1360 if (lua_pcall(L, 1, 1, 0)) {
1361 if (sockin[0] != -1) close(sockin[0]);
1362 close(sockout[0]);
1363 close(sockerr[0]);
1364 goto moonbr_io_exec_error2;
1366 lua_setfield(L, -2, "stdin");
1367 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1368 lua_pushlightuserdata(L, &sockout[0]);
1369 if (lua_pcall(L, 1, 1, 0)) {
1370 if (sockout[0] != -1) close(sockout[0]);
1371 close(sockerr[0]);
1372 goto moonbr_io_exec_error2;
1374 lua_setfield(L, -2, "stdout");
1375 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
1376 lua_pushlightuserdata(L, &sockerr[0]);
1377 if (lua_pcall(L, 1, 1, 0)) {
1378 if (sockerr[0] != -1) close(sockerr[0]);
1379 goto moonbr_io_exec_error2;
1381 lua_setfield(L, -2, "stderr");
1382 return 1;
1383 moonbr_io_exec_error2:
1385 int status;
1386 while (waitpid(child->pid, &status, 0) == -1) {
1387 if (errno != EINTR) {
1388 moonbr_io_errmsg();
1389 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
1393 child->pid = 0;
1394 return lua_error(L);
1397 static int moonbr_io_childindex(lua_State *L) {
1398 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1399 luaL_checkany(L, 2);
1400 lua_getuservalue(L, 1);
1401 lua_pushvalue(L, 2);
1402 lua_gettable(L, -2);
1403 if (lua_isnil(L, -1)) {
1404 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
1405 lua_pushvalue(L, 2);
1406 lua_gettable(L, -2);
1408 return 1;
1411 static int moonbr_io_childnewindex(lua_State *L) {
1412 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1413 luaL_checkany(L, 2);
1414 luaL_checkany(L, 3);
1415 lua_getuservalue(L, 1);
1416 lua_pushvalue(L, 2);
1417 lua_pushvalue(L, 3);
1418 lua_settable(L, -3);
1419 return 0;
1422 static int moonbr_io_childgc(lua_State *L) {
1423 moonbr_io_child_t *child;
1424 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1425 if (child->pid) {
1426 int status;
1427 if (kill(child->pid, SIGKILL)) {
1428 moonbr_io_errmsg();
1429 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
1431 while (waitpid(child->pid, &status, 0) == -1) {
1432 if (errno != EINTR) {
1433 moonbr_io_errmsg();
1434 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
1438 return 0;
1441 static int moonbr_io_kill(lua_State *L) {
1442 moonbr_io_child_t *child;
1443 int sig;
1444 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1445 sig = luaL_optinteger(L, 2, SIGTERM);
1446 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
1447 if (kill(child->pid, sig)) {
1448 moonbr_io_errmsg();
1449 luaL_error(L, "Error in kill call: %s", errmsg);
1451 lua_settop(L, 1);
1452 return 1;
1455 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
1456 moonbr_io_child_t *child;
1457 pid_t waitedpid;
1458 int status;
1459 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
1460 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
1461 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
1462 if (errno != EINTR) {
1463 moonbr_io_errmsg();
1464 luaL_error(L, "Error in waitpid call: %s", errmsg);
1467 if (!waitedpid) {
1468 lua_pushboolean(L, 0);
1469 lua_pushliteral(L, "Process is still running");
1470 return 2;
1471 } else {
1472 child->pid = 0;
1473 if (WIFEXITED(status)) {
1474 lua_pushinteger(L, WEXITSTATUS(status));
1475 } else if (WIFSIGNALED(status)) {
1476 lua_pushinteger(L, -WTERMSIG(status));
1477 } else {
1478 luaL_error(L, "Unexpected status value returned by waitpid call");
1480 return 1;
1484 static int moonbr_io_wait(lua_State *L) {
1485 return moonbr_io_wait_impl(L, 0);
1488 static int moonbr_io_wait_nb(lua_State *L) {
1489 return moonbr_io_wait_impl(L, 1);
1492 #if LUA_VERSION_NUM >= 503
1493 static int moonbr_io_wait_cont(lua_State *L, int status, lua_KContext ctx) {
1494 #else
1495 static int moonbr_io_wait_cont(lua_State *L) {
1496 #endif
1497 #if !(LUA_VERSION_NUM >= 503)
1498 int ctx = 0;
1499 lua_getctx(L, &ctx);
1500 #endif
1501 while (1) {
1502 lua_pushcfunction(L, moonbr_io_wait_nb);
1503 lua_pushvalue(L, 1);
1504 lua_call(L, 1, 1);
1505 if (!lua_isnil(L, -1)) break;
1506 lua_pushvalue(L, 2);
1507 lua_callk(L, 0, 0, ctx, moonbr_io_wait_cont);
1509 return 1;
1512 static int moonbr_io_wait_call(lua_State *L) {
1513 lua_settop(L, 2);
1514 #if LUA_VERSION_NUM >= 503
1515 return moonbr_io_wait_cont(L, 0, 0);
1516 #else
1517 return moonbr_io_wait_cont(L);
1518 #endif
1521 moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
1523 static int moonbr_io_poll(lua_State *L) {
1524 moonbr_io_handle_t *handle;
1525 moonbr_io_listener_t *listener;
1526 int fd, isnum;
1527 int nfds = 0;
1528 fd_set readfds, writefds, exceptfds;
1529 struct timeval timeout = {0, };
1530 int status;
1531 FD_ZERO(&readfds);
1532 FD_ZERO(&writefds);
1533 FD_ZERO(&exceptfds);
1534 if (!lua_isnoneornil(L, 1)) {
1535 luaL_checktype(L, 1, LUA_TTABLE);
1536 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1537 if (lua_toboolean(L, -1)) {
1538 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1539 if (handle) {
1540 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1541 fd = handle->fd;
1542 if (
1543 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1544 handle->readbufin != handle->readbufout /* data pending in buffer */
1545 ) {
1546 lua_pushboolean(L, 1);
1547 return 1;
1549 } else {
1550 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1551 if (listener) {
1552 fd = listener->fd;
1553 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1554 } else {
1555 fd = lua_tointegerx(L, -2, &isnum);
1556 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1559 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1560 FD_SET(fd, &readfds);
1561 if (fd+1 > nfds) nfds = fd+1;
1565 if (!lua_isnoneornil(L, 2)) {
1566 luaL_checktype(L, 2, LUA_TTABLE);
1567 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1568 if (lua_toboolean(L, -1)) {
1569 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1570 if (handle) {
1571 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1572 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1573 fd = handle->fd;
1574 } else {
1575 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1576 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1577 fd = lua_tointegerx(L, -2, &isnum);
1578 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1580 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1581 FD_SET(fd, &writefds);
1582 if (fd+1 > nfds) nfds = fd+1;
1586 if (!lua_isnoneornil(L, 3)) {
1587 lua_Number n;
1588 n = lua_tonumberx(L, 3, &isnum);
1589 if (isnum && n<0) {
1590 lua_pushboolean(L, 0);
1591 lua_pushliteral(L, "Negative timeout");
1592 return 2;
1593 } else if (isnum && n>=0 && n<100000000) {
1594 timeout.tv_sec = n;
1595 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1596 } else {
1597 luaL_argcheck(L, 0, 3, "not a valid timeout");
1599 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1600 } else {
1601 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1603 if (status == -1) {
1604 if (errno == EINTR) {
1605 lua_pushnil(L);
1606 lua_pushliteral(L, "Signal received while polling file descriptors");
1607 return 2;
1608 } else {
1609 moonbr_io_errmsg();
1610 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1612 } else if (status == 0) {
1613 lua_pushboolean(L, 0);
1614 lua_pushliteral(L, "Timeout while polling file descriptors");
1615 return 2;
1616 } else {
1617 lua_pushboolean(L, 1);
1618 return 1;
1622 static int moonbr_io_timeref(lua_State *L) {
1623 lua_Number sub;
1624 struct timespec tp;
1625 sub = luaL_optnumber(L, 1, 0);
1626 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1627 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1629 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1630 return 1;
1633 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1634 {"read", moonbr_io_read},
1635 {"read_nb", moonbr_io_read_nb},
1636 {"read_call", moonbr_io_read_call},
1637 {"read_yield", moonbr_io_read_yield},
1638 {"drain", moonbr_io_drain},
1639 {"drain_nb", moonbr_io_drain_nb},
1640 {"drain_call", moonbr_io_drain_call},
1641 {"drain_yield", moonbr_io_drain_yield},
1642 {"write", moonbr_io_write},
1643 {"write_nb", moonbr_io_write_nb},
1644 {"write_call", moonbr_io_write_call},
1645 {"write_yield", moonbr_io_write_yield},
1646 {"flush", moonbr_io_flush},
1647 {"flush_nb", moonbr_io_flush_nb},
1648 {"flush_call", moonbr_io_flush_call},
1649 {"flush_yield", moonbr_io_flush_yield},
1650 {"finish", moonbr_io_finish},
1651 {"close", moonbr_io_close},
1652 {"reset", moonbr_io_reset},
1653 {NULL, NULL}
1654 };
1656 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1657 {"__index", moonbr_io_handleindex},
1658 {"__newindex", moonbr_io_handlenewindex},
1659 {"__gc", moonbr_io_handlegc},
1660 {NULL, NULL}
1661 };
1663 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1664 {"accept", moonbr_io_accept},
1665 {"accept_nb", moonbr_io_accept_nb},
1666 {"close", moonbr_io_unlisten},
1667 {NULL, NULL}
1668 };
1670 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1671 {"__gc", moonbr_io_listenergc},
1672 {NULL, NULL}
1673 };
1675 static const struct luaL_Reg moonbr_io_child_methods[] = {
1676 {"kill", moonbr_io_kill},
1677 {"wait", moonbr_io_wait},
1678 {"wait_nb", moonbr_io_wait_nb},
1679 {"wait_call", moonbr_io_wait_call},
1680 {"wait_yield", moonbr_io_wait_yield},
1681 {NULL, NULL}
1682 };
1684 static const struct luaL_Reg moonbr_io_child_metamethods[] = {
1685 {"__index", moonbr_io_childindex},
1686 {"__newindex", moonbr_io_childnewindex},
1687 {"__gc", moonbr_io_childgc},
1688 {NULL, NULL}
1689 };
1691 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1692 {"localconnect", moonbr_io_localconnect},
1693 {"localconnect_nb", moonbr_io_localconnect_nb},
1694 {"tcpconnect", moonbr_io_tcpconnect},
1695 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1696 {"locallisten", moonbr_io_locallisten},
1697 {"tcplisten", moonbr_io_tcplisten},
1698 {"exec", moonbr_io_exec},
1699 {"poll", moonbr_io_poll},
1700 {"timeref", moonbr_io_timeref},
1701 {NULL, NULL}
1702 };
1704 int luaopen_moonbridge_io(lua_State *L) {
1706 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1708 lua_newtable(L); // module
1710 lua_newtable(L); // public metatable
1711 lua_newtable(L); // handle methods
1712 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1713 lua_pushvalue(L, -1);
1714 lua_setfield(L, -4, "handle_pt");
1715 lua_setfield(L, -2, "__index");
1716 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1718 lua_newtable(L); // handle metatable
1719 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1720 lua_pushvalue(L, -1);
1721 lua_setfield(L, -3, "handle_mt");
1722 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1724 lua_newtable(L); // listener metatable
1725 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1726 lua_newtable(L); // listener methods
1727 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1728 lua_pushvalue(L, -1);
1729 lua_setfield(L, -4, "listener_pt");
1730 lua_setfield(L, -2, "__index");
1731 lua_pushvalue(L, -1);
1732 lua_setfield(L, -3, "listener_mt");
1733 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1735 lua_newtable(L); // child methods
1736 luaL_setfuncs(L, moonbr_io_child_methods, 0);
1737 lua_pushvalue(L, -1);
1738 lua_setfield(L, -3, "child_pt");
1739 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_PT_REGKEY);
1740 lua_newtable(L); // child metatable
1741 luaL_setfuncs(L, moonbr_io_child_metamethods, 0);
1742 lua_pushvalue(L, -1);
1743 lua_setfield(L, -3, "child_mt");
1744 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
1746 moonbr_io_pushhandle(L, 0);
1747 lua_setfield(L, -2, "stdin");
1748 moonbr_io_pushhandle(L, 1);
1749 lua_setfield(L, -2, "stdout");
1750 moonbr_io_pushhandle(L, 2);
1751 lua_setfield(L, -2, "stderr");
1753 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1754 return 1;

Impressum / About Us