moonbridge

annotate moonbridge_io.c @ 266:48ffca6443d9

Added moonbridge_io.signalsocket(...) and moonbridge_io.getpid() functions; poll returns true on signal interruption (instead of nil)
author jbe
date Sun Jun 04 16:40:46 2017 +0200 (2017-06-04)
parents 6c634c96805b
children fab22173abc8
rev   line source
jbe@209 1
jbe@209 2 #ifndef __has_include
jbe@209 3 #define __has_include(x) 0
jbe@209 4 #endif
jbe@79 5
jbe@79 6 #include <stdlib.h>
jbe@135 7 #include <stdint.h>
jbe@135 8 #include <string.h>
jbe@135 9 #include <errno.h>
jbe@79 10 #include <unistd.h>
jbe@135 11 #include <signal.h>
jbe@135 12 #include <fcntl.h>
jbe@135 13 #include <sys/stat.h>
jbe@79 14 #include <sys/socket.h>
jbe@111 15 #include <sys/un.h>
jbe@95 16 #include <netinet/in.h>
jbe@95 17 #include <netinet/tcp.h>
jbe@135 18 #include <sys/select.h>
jbe@136 19 #include <time.h>
jbe@135 20 #include <netdb.h>
jbe@107 21 #include <arpa/inet.h>
jbe@205 22 #include <sys/types.h>
jbe@205 23 #include <sys/wait.h>
jbe@209 24 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
jbe@209 25 #include <bsd/unistd.h>
jbe@209 26 #endif
jbe@211 27
jbe@243 28 #ifdef MOONBR_IO_USE_TLS
jbe@243 29 #include <tls.h>
jbe@243 30 #endif
jbe@243 31
jbe@79 32 #include <lua.h>
jbe@79 33 #include <lauxlib.h>
jbe@79 34 #include <lualib.h>
jbe@79 35
jbe@149 36 #include <assert.h>
jbe@149 37
jbe@80 38 #define MOONBR_IO_MAXSTRERRORLEN 80
jbe@85 39 #define MOONBR_IO_READBUFLEN 4096
jbe@80 40 #define MOONBR_IO_WRITEBUFLEN 4096
jbe@80 41
jbe@108 42 #define MOONBR_IO_LISTEN_BACKLOG 1024
jbe@108 43
jbe@213 44 #define MOONBR_IO_STRERROR_R_MSG "Error detail unavailable due to noncompliant strerror_r() implementation"
jbe@257 45 #define moonbr_io_prepare_errmsg() \
jbe@213 46 char errmsg[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG; \
jbe@80 47 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
jbe@257 48 #define moonbr_io_return_prepared_errmsg() \
jbe@257 49 lua_pushnil(L); \
jbe@257 50 lua_pushstring(L, errmsg); \
jbe@257 51 return 2
jbe@257 52 #define moonbr_io_return_errmsg() \
jbe@257 53 do { \
jbe@257 54 moonbr_io_prepare_errmsg(); \
jbe@257 55 moonbr_io_return_prepared_errmsg(); \
jbe@257 56 } while (0)
jbe@80 57
jbe@266 58 static int moonbr_io_signalfds[2];
jbe@266 59 #define moonbr_io_signalfd_read moonbr_io_signalfds[0]
jbe@266 60 #define moonbr_io_signalfd_write moonbr_io_signalfds[1]
jbe@266 61
jbe@79 62 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
jbe@79 63 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
jbe@108 64 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
jbe@205 65 #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child"
jbe@205 66 #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt"
jbe@266 67 #define MOONBR_IO_SIGNALSOCKET_REGKEY "moonbridge_io_signalsocket"
jbe@79 68
jbe@243 69 #ifdef MOONBR_IO_USE_TLS
jbe@258 70
jbe@243 71 #define MOONBR_IO_TLSCONF_MT_REGKEY "moonbridge_io_tlsconf"
jbe@252 72
jbe@252 73 typedef struct {
jbe@252 74 struct tls_config *config;
jbe@252 75 int server;
jbe@252 76 } moonbr_io_tlsconf_t;
jbe@258 77
jbe@258 78 #endif /* MOONBR_IO_USE_TLS */
jbe@243 79
jbe@79 80 typedef struct {
jbe@79 81 int fd;
jbe@107 82 int issock;
jbe@107 83 sa_family_t addrfam;
jbe@94 84 int finished;
jbe@94 85 int closed;
jbe@81 86 int nonblocking;
jbe@95 87 int nopush;
jbe@85 88 int readerr;
jbe@105 89 int readbufin;
jbe@105 90 int readbufout;
jbe@81 91 int writeerr;
jbe@83 92 size_t writeleft;
jbe@132 93 size_t flushedleft;
jbe@83 94 #if LUA_VERSION_NUM >= 503
jbe@83 95 lua_Integer writeqin;
jbe@83 96 lua_Integer writeqout;
jbe@83 97 #else
jbe@83 98 int writeqin;
jbe@83 99 int writeqout;
jbe@83 100 #endif
jbe@83 101 size_t writeqoff;
jbe@103 102 int writebufin;
jbe@103 103 int writebufout;
jbe@85 104 char readbuf[MOONBR_IO_READBUFLEN];
jbe@80 105 char writebuf[MOONBR_IO_WRITEBUFLEN];
jbe@246 106 #ifdef MOONBR_IO_USE_TLS
jbe@250 107 struct tls *tls;
jbe@250 108 struct tls *servertls;
jbe@250 109 int tlshandshake;
jbe@261 110 int tlsclosing;
jbe@246 111 #endif
jbe@79 112 } moonbr_io_handle_t;
jbe@79 113
jbe@108 114 typedef struct {
jbe@108 115 int fd;
jbe@118 116 sa_family_t addrfam;
jbe@108 117 int nonblocking;
jbe@108 118 } moonbr_io_listener_t;
jbe@108 119
jbe@205 120 typedef struct {
jbe@205 121 pid_t pid;
jbe@205 122 } moonbr_io_child_t;
jbe@205 123
jbe@148 124 static int moonbr_io_yield(lua_State *L) {
jbe@149 125 return lua_yield(L, lua_gettop(L));
jbe@140 126 }
jbe@140 127
jbe@140 128 #if LUA_VERSION_NUM >= 503
jbe@140 129 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
jbe@140 130 #else
jbe@140 131 static int moonbr_io_cont_returnall(lua_State *L) {
jbe@140 132 #endif
jbe@140 133 return lua_gettop(L);
jbe@140 134 }
jbe@140 135
jbe@145 136 #define moonbr_io_yield_wrapper(yieldfunc, callfunc) \
jbe@145 137 static int yieldfunc(lua_State *L) { \
jbe@145 138 int args; \
jbe@145 139 lua_pushcfunction(L, callfunc); \
jbe@145 140 lua_insert(L, 1); \
jbe@145 141 args = lua_gettop(L); \
jbe@148 142 lua_pushcfunction(L, moonbr_io_yield); \
jbe@145 143 lua_insert(L, 3); \
jbe@145 144 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall); \
jbe@145 145 return lua_gettop(L); \
jbe@145 146 }
jbe@145 147
jbe@256 148 static int moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
jbe@95 149 int flags;
jbe@256 150 if (handle->nonblocking == nonblocking) return 0;
jbe@95 151 flags = fcntl(handle->fd, F_GETFL, 0);
jbe@256 152 if (flags == -1) return -1;
jbe@95 153 if (nonblocking) flags |= O_NONBLOCK;
jbe@95 154 else flags &= ~O_NONBLOCK;
jbe@256 155 if (fcntl(handle->fd, F_SETFL, flags) == -1) return -1;
jbe@95 156 handle->nonblocking = nonblocking;
jbe@256 157 return 0;
jbe@81 158 }
jbe@81 159
jbe@256 160 static int moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
jbe@87 161 struct linger lingerval = { 0, };
jbe@256 162 if (!handle->issock) return 0;
jbe@87 163 if (timeout >= 0) {
jbe@87 164 lingerval.l_onoff = 1;
jbe@87 165 lingerval.l_linger = timeout;
jbe@87 166 }
jbe@256 167 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) return -1;
jbe@256 168 return 0;
jbe@87 169 }
jbe@87 170
jbe@256 171 static inline int moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
jbe@96 172 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
jbe@107 173 if (
jbe@107 174 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
jbe@107 175 handle->nopush == nopush
jbe@256 176 ) return 0;
jbe@96 177 #if defined(TCP_NOPUSH)
jbe@256 178 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) return -1;
jbe@104 179 #elif defined(TCP_CORK)
jbe@256 180 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) return -1;
jbe@251 181 #endif
jbe@95 182 handle->nopush = nopush;
jbe@96 183 #else
jbe@96 184 #warning Neither TCP_NOPUSH nor TCP_CORK is available
jbe@96 185 #endif
jbe@256 186 return 0;
jbe@95 187 }
jbe@95 188
jbe@86 189 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
jbe@85 190 moonbr_io_handle_t *handle;
jbe@85 191 lua_Integer maxread;
jbe@85 192 const char *terminatorstr;
jbe@210 193 size_t terminatorlen;
jbe@210 194 char terminator = 0; /* initialize to avoid compiler warning */
jbe@85 195 luaL_Buffer luabuf;
jbe@85 196 size_t luabufcnt = 0;
jbe@105 197 int remaining;
jbe@85 198 char *terminatorpos;
jbe@103 199 ssize_t bytesread;
jbe@85 200 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@153 201 maxread = luaL_optinteger(L, 2, -1);
jbe@85 202 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
jbe@85 203 if (terminatorlen) {
jbe@85 204 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
jbe@85 205 terminator = terminatorstr[0];
jbe@85 206 }
jbe@86 207 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
jbe@94 208 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
jbe@85 209 if (handle->readerr) {
jbe@85 210 lua_pushnil(L);
jbe@85 211 lua_pushliteral(L, "Previous read error");
jbe@85 212 return 2;
jbe@85 213 }
jbe@143 214 if (handle->fd < 0) {
jbe@143 215 /* fake EOF to simulate shutdown */
jbe@143 216 if (!drain) lua_pushliteral(L, "");
jbe@143 217 else lua_pushinteger(L, 0);
jbe@143 218 lua_pushliteral(L, "eof");
jbe@143 219 return 2;
jbe@143 220 }
jbe@105 221 handle->readerr = 1;
jbe@257 222 if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
jbe@86 223 if (!drain) luaL_buffinit(L, &luabuf);
jbe@85 224 while (1) {
jbe@105 225 remaining = -1;
jbe@142 226 terminatorpos = NULL;
jbe@105 227 if (
jbe@153 228 maxread >= 0 &&
jbe@143 229 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
jbe@105 230 ) {
jbe@142 231 remaining = (size_t)maxread - luabufcnt;
jbe@193 232 if (terminatorlen) {
jbe@193 233 terminatorpos = memchr(
jbe@193 234 handle->readbuf + handle->readbufout,
jbe@193 235 terminator,
jbe@193 236 remaining
jbe@193 237 );
jbe@193 238 }
jbe@85 239 } else if (terminatorlen) {
jbe@105 240 terminatorpos = memchr(
jbe@105 241 handle->readbuf + handle->readbufout,
jbe@105 242 terminator,
jbe@105 243 handle->readbufin - handle->readbufout
jbe@105 244 );
jbe@85 245 }
jbe@142 246 if (terminatorpos) remaining = 1 + (
jbe@142 247 terminatorpos - (handle->readbuf + handle->readbufout)
jbe@142 248 );
jbe@105 249 if (remaining >= 0) {
jbe@86 250 if (!drain) {
jbe@105 251 luaL_addlstring(
jbe@105 252 &luabuf,
jbe@105 253 handle->readbuf + handle->readbufout,
jbe@105 254 remaining
jbe@105 255 );
jbe@86 256 luaL_pushresult(&luabuf);
jbe@90 257 } else {
jbe@105 258 lua_pushinteger(L, luabufcnt + remaining);
jbe@86 259 }
jbe@143 260 if (terminatorpos) lua_pushliteral(L, "term");
jbe@143 261 else lua_pushliteral(L, "maxlen");
jbe@105 262 handle->readbufout += remaining;
jbe@105 263 if (handle->readbufout == handle->readbufin) {
jbe@105 264 handle->readbufin = 0;
jbe@143 265 handle->readbufout = 0;
jbe@105 266 }
jbe@104 267 handle->readerr = 0;
jbe@143 268 return 2;
jbe@85 269 }
jbe@105 270 if (!drain) luaL_addlstring(
jbe@105 271 &luabuf,
jbe@105 272 handle->readbuf + handle->readbufout,
jbe@105 273 handle->readbufin - handle->readbufout
jbe@105 274 );
jbe@105 275 luabufcnt += handle->readbufin - handle->readbufout;
jbe@143 276 handle->readbufout = 0;
jbe@249 277 #ifdef MOONBR_IO_USE_TLS
jbe@249 278 if (handle->tls) {
jbe@250 279 do {
jbe@250 280 if (!handle->tlshandshake) {
jbe@250 281 do bytesread = tls_handshake(handle->tls);
jbe@250 282 while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT));
jbe@250 283 if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) {
jbe@250 284 handle->tlshandshake = bytesread;
jbe@250 285 errno = EAGAIN;
jbe@250 286 break;
jbe@250 287 }
jbe@250 288 if (bytesread < 0) {
jbe@250 289 lua_pushnil(L);
jbe@250 290 lua_pushstring(L, tls_error(handle->tls));
jbe@250 291 return 2;
jbe@250 292 }
jbe@250 293 handle->tlshandshake = 1;
jbe@250 294 }
jbe@250 295 do bytesread = tls_read(handle->tls, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@250 296 while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT));
jbe@250 297 if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) {
jbe@250 298 errno = EAGAIN;
jbe@250 299 break;
jbe@250 300 }
jbe@250 301 if (bytesread < 0) {
jbe@250 302 lua_pushnil(L);
jbe@250 303 lua_pushstring(L, tls_error(handle->tls));
jbe@250 304 return 2;
jbe@250 305 }
jbe@250 306 } while (0);
jbe@249 307 }
jbe@249 308 else
jbe@249 309 #endif
jbe@249 310 do bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@249 311 while (bytesread < 0 && (errno == EINTR));
jbe@105 312 if (
jbe@105 313 bytesread == 0 || (
jbe@105 314 nonblocking &&
jbe@105 315 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
jbe@105 316 )
jbe@105 317 ) {
jbe@105 318 handle->readbufin = 0;
jbe@143 319 if (!drain) luaL_pushresult(&luabuf);
jbe@143 320 else lua_pushinteger(L, luabufcnt);
jbe@143 321 if (bytesread == 0) lua_pushliteral(L, "eof");
jbe@143 322 else lua_pushliteral(L, "block");
jbe@105 323 handle->readerr = 0;
jbe@143 324 return 2;
jbe@105 325 }
jbe@257 326 if (bytesread < 0) moonbr_io_return_errmsg();
jbe@105 327 handle->readbufin = bytesread;
jbe@86 328 }
jbe@85 329 }
jbe@85 330
jbe@85 331 static int moonbr_io_read(lua_State *L) {
jbe@86 332 return moonbr_io_read_impl(L, 0, 0);
jbe@85 333 }
jbe@85 334
jbe@85 335 static int moonbr_io_read_nb(lua_State *L) {
jbe@86 336 return moonbr_io_read_impl(L, 1, 0);
jbe@86 337 }
jbe@86 338
jbe@86 339 static int moonbr_io_drain(lua_State *L) {
jbe@86 340 return moonbr_io_read_impl(L, 0, 1);
jbe@86 341 }
jbe@86 342
jbe@86 343 static int moonbr_io_drain_nb(lua_State *L) {
jbe@86 344 return moonbr_io_read_impl(L, 1, 1);
jbe@85 345 }
jbe@85 346
jbe@140 347 #if LUA_VERSION_NUM >= 503
jbe@140 348 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@140 349 #else
jbe@140 350 static int moonbr_io_read_cont(lua_State *L) {
jbe@140 351 #endif
jbe@143 352 lua_Integer remaining;
jbe@143 353 size_t len;
jbe@140 354 #if !(LUA_VERSION_NUM >= 503)
jbe@140 355 int ctx = 0;
jbe@140 356 lua_getctx(L, &ctx);
jbe@140 357 #endif
jbe@140 358 remaining = lua_tointeger(L, 3);
jbe@140 359 while (1) {
jbe@140 360 lua_pushcfunction(L, moonbr_io_read_nb);
jbe@140 361 lua_pushvalue(L, 1);
jbe@140 362 lua_pushvalue(L, 3);
jbe@140 363 lua_pushvalue(L, 4);
jbe@140 364 lua_call(L, 3, 2);
jbe@143 365 if (lua_isnil(L, -2)) return 2;
jbe@143 366 lua_insert(L, -2);
jbe@143 367 len = lua_rawlen(L, -1);
jbe@143 368 if (ctx == 0) {
jbe@143 369 lua_replace(L, 5);
jbe@143 370 ctx = 1;
jbe@143 371 } else if (ctx == 1) {
jbe@143 372 lua_pushvalue(L, 5);
jbe@143 373 lua_newtable(L);
jbe@143 374 lua_replace(L, 5);
jbe@143 375 lua_rawseti(L, 5, 2);
jbe@143 376 lua_rawseti(L, 5, 1);
jbe@143 377 ctx = 2;
jbe@140 378 } else {
jbe@143 379 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
jbe@140 380 }
jbe@144 381 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
jbe@144 382 lua_pop(L, 1);
jbe@144 383 if (remaining >= 0 && len) {
jbe@143 384 remaining -= len;
jbe@143 385 lua_pushinteger(L, remaining);
jbe@143 386 lua_replace(L, 3);
jbe@143 387 }
jbe@140 388 lua_pushvalue(L, 2);
jbe@152 389 lua_callk(L, 0, 0, ctx, moonbr_io_read_cont);
jbe@140 390 }
jbe@140 391 if (ctx == 1) {
jbe@140 392 lua_pushvalue(L, 5);
jbe@140 393 } else {
jbe@140 394 luaL_Buffer buf;
jbe@140 395 lua_Integer i, chunkcount;
jbe@140 396 chunkcount = lua_rawlen(L, 5);
jbe@140 397 luaL_buffinit(L, &buf);
jbe@140 398 for (i=1; i<=chunkcount && i>0; i++) {
jbe@140 399 lua_rawgeti(L, 5, i);
jbe@140 400 luaL_addvalue(&buf);
jbe@140 401 }
jbe@140 402 luaL_pushresult(&buf);
jbe@140 403 }
jbe@143 404 lua_pushvalue(L, -2);
jbe@143 405 return 2;
jbe@140 406 }
jbe@140 407
jbe@140 408 static int moonbr_io_read_call(lua_State *L) {
jbe@140 409 lua_settop(L, 4);
jbe@140 410 lua_pushnil(L);
jbe@140 411 #if LUA_VERSION_NUM >= 503
jbe@140 412 return moonbr_io_read_cont(L, 0, 0);
jbe@140 413 #else
jbe@140 414 return moonbr_io_read_cont(L);
jbe@140 415 #endif
jbe@140 416 }
jbe@140 417
jbe@145 418 moonbr_io_yield_wrapper(moonbr_io_read_yield, moonbr_io_read_call);
jbe@140 419
jbe@144 420 #if LUA_VERSION_NUM >= 503
jbe@144 421 static int moonbr_io_drain_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@144 422 #else
jbe@144 423 static int moonbr_io_drain_cont(lua_State *L) {
jbe@144 424 #endif
jbe@144 425 lua_Integer remaining, len;
jbe@144 426 size_t totallen = 0;
jbe@144 427 #if !(LUA_VERSION_NUM >= 503)
jbe@144 428 int ctx = 0;
jbe@144 429 lua_getctx(L, &ctx);
jbe@144 430 #endif
jbe@144 431 remaining = lua_tointeger(L, 3);
jbe@144 432 while (1) {
jbe@144 433 lua_pushcfunction(L, moonbr_io_drain_nb);
jbe@144 434 lua_pushvalue(L, 1);
jbe@144 435 lua_pushvalue(L, 3);
jbe@144 436 lua_pushvalue(L, 4);
jbe@144 437 lua_call(L, 3, 2);
jbe@144 438 if (lua_isnil(L, -2)) return 2;
jbe@144 439 lua_insert(L, -2);
jbe@144 440 len = lua_tointeger(L, -1);
jbe@144 441 lua_pop(L, 1);
jbe@144 442 totallen += len;
jbe@144 443 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
jbe@144 444 lua_pop(L, 1);
jbe@144 445 if (remaining >= 0 && len) {
jbe@144 446 remaining -= len;
jbe@144 447 lua_pushinteger(L, remaining);
jbe@144 448 lua_replace(L, 3);
jbe@144 449 }
jbe@144 450 lua_pushvalue(L, 2);
jbe@152 451 lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont);
jbe@144 452 }
jbe@144 453 lua_pushinteger(L, totallen);
jbe@144 454 lua_pushvalue(L, -2);
jbe@144 455 return 2;
jbe@144 456 }
jbe@144 457
jbe@144 458 static int moonbr_io_drain_call(lua_State *L) {
jbe@144 459 #if LUA_VERSION_NUM >= 503
jbe@144 460 return moonbr_io_drain_cont(L, 0, 0);
jbe@144 461 #else
jbe@144 462 return moonbr_io_drain_cont(L);
jbe@144 463 #endif
jbe@144 464 }
jbe@144 465
jbe@145 466 moonbr_io_yield_wrapper(moonbr_io_drain_yield, moonbr_io_drain_call);
jbe@144 467
jbe@250 468 #ifdef MOONBR_IO_USE_TLS
jbe@258 469
jbe@250 470 #define moonbr_io_write_tls(buf, buflen) \
jbe@250 471 if (handle->tls) { \
jbe@250 472 do { \
jbe@250 473 if (!handle->tlshandshake) { \
jbe@250 474 do written = tls_handshake(handle->tls); \
jbe@250 475 while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); \
jbe@250 476 if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { \
jbe@250 477 handle->tlshandshake = written; \
jbe@250 478 errno = EAGAIN; \
jbe@250 479 break; \
jbe@250 480 } \
jbe@250 481 if (written < 0) { \
jbe@250 482 lua_pushnil(L); \
jbe@250 483 lua_pushstring(L, tls_error(handle->tls)); \
jbe@250 484 return 2; \
jbe@250 485 } \
jbe@250 486 handle->tlshandshake = 1; \
jbe@250 487 } \
jbe@250 488 do written = tls_write(handle->tls, (buf), (buflen)); \
jbe@250 489 while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); \
jbe@250 490 if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { \
jbe@250 491 errno = EAGAIN; \
jbe@250 492 break; \
jbe@250 493 } \
jbe@250 494 if (written < 0) { \
jbe@250 495 lua_pushnil(L); \
jbe@250 496 lua_pushstring(L, tls_error(handle->tls)); \
jbe@250 497 return 2; \
jbe@250 498 } \
jbe@250 499 } while (0); \
jbe@250 500 } \
jbe@250 501 else
jbe@258 502
jbe@258 503 #endif /* MOONBR_IO_USE_TLS */
jbe@250 504
jbe@81 505 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
jbe@80 506 moonbr_io_handle_t *handle;
jbe@80 507 int i, top;
jbe@80 508 const char *str;
jbe@92 509 size_t strlen;
jbe@103 510 ssize_t written;
jbe@80 511 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 512 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
jbe@94 513 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
jbe@81 514 if (handle->writeerr) {
jbe@80 515 lua_pushnil(L);
jbe@80 516 lua_pushliteral(L, "Previous write error");
jbe@80 517 return 2;
jbe@80 518 }
jbe@103 519 handle->writeerr = 1;
jbe@257 520 if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
jbe@84 521 top = lua_gettop(L);
jbe@81 522 lua_getuservalue(L, 1);
jbe@103 523 lua_getfield(L, -1, "writequeue");
jbe@84 524 for (i=2; i<=top; i++) {
jbe@84 525 luaL_checklstring(L, i, &strlen);
jbe@84 526 lua_pushvalue(L, i);
jbe@84 527 lua_rawseti(L, -2, handle->writeqin++);
jbe@84 528 handle->writeleft += strlen;
jbe@81 529 }
jbe@132 530 if (flush) handle->flushedleft = handle->writeleft;
jbe@83 531 while (handle->writeqout != handle->writeqin) {
jbe@83 532 lua_rawgeti(L, -1, handle->writeqout);
jbe@81 533 str = lua_tolstring(L, -1, &strlen);
jbe@92 534 while (handle->writeqoff < strlen) {
jbe@103 535 if (
jbe@132 536 strlen - handle->writeqoff <
jbe@103 537 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 538 ) {
jbe@103 539 memcpy(
jbe@103 540 handle->writebuf + handle->writebufin,
jbe@103 541 str + handle->writeqoff,
jbe@103 542 strlen - handle->writeqoff
jbe@103 543 );
jbe@103 544 handle->writebufin += strlen - handle->writeqoff;
jbe@80 545 break;
jbe@80 546 } else {
jbe@103 547 memcpy(
jbe@103 548 handle->writebuf + handle->writebufin,
jbe@103 549 str + handle->writeqoff,
jbe@103 550 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 551 );
jbe@103 552 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
jbe@228 553 handle->writebufin = MOONBR_IO_WRITEBUFLEN;
jbe@103 554 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
jbe@257 555 if (moonbr_io_handle_set_nopush(L, handle, 1)) moonbr_io_return_errmsg();
jbe@249 556 #ifdef MOONBR_IO_USE_TLS
jbe@250 557 moonbr_io_write_tls(
jbe@250 558 handle->writebuf + handle->writebufout,
jbe@250 559 MOONBR_IO_WRITEBUFLEN - handle->writebufout
jbe@250 560 )
jbe@249 561 #endif
jbe@103 562 written = write(
jbe@103 563 handle->fd,
jbe@103 564 handle->writebuf + handle->writebufout,
jbe@103 565 MOONBR_IO_WRITEBUFLEN - handle->writebufout
jbe@103 566 );
jbe@103 567 if (written < 0) {
jbe@81 568 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 569 goto moonbr_io_write_impl_block;
jbe@257 570 } else if (errno != EINTR) moonbr_io_return_errmsg();
jbe@103 571 } else {
jbe@103 572 handle->writebufout += written;
jbe@103 573 handle->writeleft -= written;
jbe@133 574 if (handle->flushedleft) {
jbe@133 575 if (written >= handle->flushedleft) {
jbe@133 576 handle->flushedleft = 0;
jbe@257 577 if (moonbr_io_handle_set_nopush(L, handle, 0)) moonbr_io_return_errmsg();
jbe@133 578 } else {
jbe@133 579 handle->flushedleft -= written;
jbe@133 580 }
jbe@133 581 }
jbe@80 582 }
jbe@80 583 }
jbe@103 584 handle->writebufin = 0;
jbe@103 585 handle->writebufout = 0;
jbe@80 586 }
jbe@80 587 }
jbe@81 588 handle->writeqoff = 0;
jbe@81 589 lua_pop(L, 1);
jbe@81 590 lua_pushnil(L);
jbe@83 591 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 592 }
jbe@132 593 while (handle->flushedleft) {
jbe@257 594 if (moonbr_io_handle_set_nopush(L, handle, 1)) moonbr_io_return_errmsg();
jbe@249 595 #ifdef MOONBR_IO_USE_TLS
jbe@250 596 moonbr_io_write_tls(
jbe@250 597 handle->writebuf + handle->writebufout,
jbe@250 598 handle->writebufin - handle->writebufout
jbe@250 599 )
jbe@249 600 #endif
jbe@132 601 written = write(
jbe@132 602 handle->fd,
jbe@132 603 handle->writebuf + handle->writebufout,
jbe@132 604 handle->writebufin - handle->writebufout
jbe@132 605 );
jbe@132 606 if (written < 0) {
jbe@132 607 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@132 608 goto moonbr_io_write_impl_block;
jbe@257 609 } else if (errno != EINTR) moonbr_io_return_errmsg();
jbe@132 610 } else {
jbe@132 611 handle->writebufout += written;
jbe@132 612 handle->writeleft -= written;
jbe@133 613 if (handle->flushedleft) {
jbe@133 614 if (written >= handle->flushedleft) {
jbe@133 615 handle->flushedleft = 0;
jbe@257 616 if (moonbr_io_handle_set_nopush(L, handle, 0)) moonbr_io_return_errmsg();
jbe@133 617 } else {
jbe@133 618 handle->flushedleft -= written;
jbe@133 619 }
jbe@133 620 }
jbe@81 621 }
jbe@132 622 }
jbe@132 623 if (handle->writebufout == handle->writebufin) {
jbe@103 624 handle->writebufin = 0;
jbe@103 625 handle->writebufout = 0;
jbe@81 626 }
jbe@103 627 if (nonblocking) lua_pushinteger(L, 0);
jbe@103 628 else lua_pushvalue(L, 1);
jbe@103 629 handle->writeerr = 0;
jbe@80 630 return 1;
jbe@81 631 moonbr_io_write_impl_block:
jbe@91 632 lua_pushinteger(L, handle->writeleft);
jbe@103 633 handle->writeerr = 0;
jbe@81 634 return 1;
jbe@81 635 }
jbe@81 636
jbe@81 637 static int moonbr_io_write(lua_State *L) {
jbe@81 638 return moonbr_io_write_impl(L, 0, 0);
jbe@81 639 }
jbe@81 640
jbe@81 641 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 642 return moonbr_io_write_impl(L, 1, 0);
jbe@80 643 }
jbe@80 644
jbe@80 645 static int moonbr_io_flush(lua_State *L) {
jbe@81 646 return moonbr_io_write_impl(L, 0, 1);
jbe@81 647 }
jbe@81 648
jbe@81 649 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 650 return moonbr_io_write_impl(L, 1, 1);
jbe@80 651 }
jbe@80 652
jbe@145 653 #if LUA_VERSION_NUM >= 503
jbe@145 654 static int moonbr_io_write_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@145 655 #else
jbe@145 656 static int moonbr_io_write_cont(lua_State *L) {
jbe@145 657 #endif
jbe@145 658 while (1) {
jbe@145 659 lua_pushcfunction(L, moonbr_io_write_nb);
jbe@145 660 lua_pushvalue(L, 1);
jbe@145 661 lua_call(L, 1, 2);
jbe@145 662 if (lua_isnil(L, -2)) return 2;
jbe@145 663 if (!lua_tointeger(L, -2)) {
jbe@145 664 lua_pushvalue(L, 1);
jbe@145 665 return 1;
jbe@145 666 }
jbe@145 667 lua_pop(L, 2);
jbe@145 668 lua_pushvalue(L, 2);
jbe@152 669 lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
jbe@145 670 }
jbe@145 671 }
jbe@145 672
jbe@145 673 static int moonbr_io_write_call(lua_State *L) {
jbe@145 674 lua_pushcfunction(L, moonbr_io_write_nb);
jbe@145 675 lua_insert(L, 3);
jbe@145 676 lua_pushvalue(L, 1);
jbe@145 677 lua_insert(L, 4);
jbe@145 678 lua_call(L, lua_gettop(L) - 3, 2);
jbe@145 679 if (lua_isnil(L, -2)) return 2;
jbe@145 680 if (!lua_tointeger(L, -2)) {
jbe@145 681 lua_pushvalue(L, 1);
jbe@145 682 return 1;
jbe@145 683 }
jbe@145 684 #if LUA_VERSION_NUM >= 503
jbe@145 685 return moonbr_io_write_cont(L, 0, 0);
jbe@145 686 #else
jbe@145 687 return moonbr_io_write_cont(L);
jbe@145 688 #endif
jbe@145 689 }
jbe@145 690
jbe@145 691 moonbr_io_yield_wrapper(moonbr_io_write_yield, moonbr_io_write_call);
jbe@145 692
jbe@145 693 static int moonbr_io_flush_call(lua_State *L) {
jbe@145 694 lua_pushcfunction(L, moonbr_io_flush_nb);
jbe@145 695 lua_insert(L, 3);
jbe@145 696 lua_pushvalue(L, 1);
jbe@145 697 lua_insert(L, 4);
jbe@145 698 lua_call(L, lua_gettop(L) - 3, 2);
jbe@145 699 if (lua_isnil(L, -2)) return 2;
jbe@145 700 if (!lua_tointeger(L, -2)) {
jbe@145 701 lua_pushvalue(L, 1);
jbe@145 702 return 1;
jbe@145 703 }
jbe@145 704 #if LUA_VERSION_NUM >= 503
jbe@145 705 return moonbr_io_write_cont(L, 0, 0);
jbe@145 706 #else
jbe@145 707 return moonbr_io_write_cont(L);
jbe@145 708 #endif
jbe@145 709 }
jbe@145 710
jbe@145 711 moonbr_io_yield_wrapper(moonbr_io_flush_yield, moonbr_io_flush_call);
jbe@145 712
jbe@88 713 static int moonbr_io_finish(lua_State *L) {
jbe@88 714 moonbr_io_handle_t *handle;
jbe@88 715 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 716 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@94 717 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@94 718 if (handle->writeleft) {
jbe@94 719 lua_pushcfunction(L, moonbr_io_flush);
jbe@94 720 lua_pushvalue(L, 1);
jbe@116 721 if (lua_pcall(L, 1, 2, 0)) {
jbe@116 722 handle->finished = 1;
jbe@116 723 lua_error(L);
jbe@116 724 }
jbe@94 725 if (!lua_toboolean(L, -2)) {
jbe@94 726 handle->finished = 1;
jbe@94 727 return 2;
jbe@88 728 }
jbe@94 729 }
jbe@94 730 handle->finished = 1;
jbe@260 731 #ifdef MOONBR_IO_USE_TLS
jbe@260 732 if ((handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) && !handle->tls) {
jbe@260 733 #else
jbe@107 734 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
jbe@260 735 #endif
jbe@257 736 if (shutdown(handle->fd, SHUT_WR)) moonbr_io_return_errmsg();
jbe@94 737 } else {
jbe@251 738 #ifdef MOONBR_IO_USE_TLS
jbe@260 739 if (handle->tls) {
jbe@260 740 int status;
jbe@260 741 if (moonbr_io_handle_set_nonblocking(L, handle, 1)) moonbr_io_return_errmsg();
jbe@260 742 do status = tls_close(handle->tls);
jbe@260 743 while (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT);
jbe@260 744 if (status) {
jbe@260 745 close(handle->fd);
jbe@260 746 handle->fd = -1;
jbe@260 747 lua_pushnil(L);
jbe@260 748 lua_pushstring(L, tls_error(handle->tls));
jbe@260 749 return 2;
jbe@260 750 }
jbe@260 751 }
jbe@251 752 #endif
jbe@94 753 if (close(handle->fd)) {
jbe@94 754 handle->fd = -1;
jbe@257 755 moonbr_io_return_errmsg();
jbe@94 756 }
jbe@94 757 handle->fd = -1; /* fake EOF on read */
jbe@88 758 }
jbe@88 759 lua_pushboolean(L, 1);
jbe@88 760 return 1;
jbe@88 761 }
jbe@88 762
jbe@261 763 static int moonbr_io_close_impl(lua_State *L, int nonblocking, int reset) {
jbe@83 764 moonbr_io_handle_t *handle;
jbe@83 765 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 766 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@259 767 if (!reset && handle->fd >= 0) {
jbe@87 768 if (handle->writeleft) {
jbe@261 769 lua_pushcfunction(L, nonblocking ? moonbr_io_flush_nb : moonbr_io_flush);
jbe@87 770 lua_pushvalue(L, 1);
jbe@116 771 if (lua_pcall(L, 1, 2, 0)) {
jbe@129 772 handle->closed = 1;
jbe@116 773 close(handle->fd);
jbe@116 774 handle->fd = -1;
jbe@116 775 lua_error(L);
jbe@116 776 }
jbe@261 777 if (!nonblocking) handle->closed = 1; /* TODO: handle nonblocking case */
jbe@87 778 if (!lua_toboolean(L, -2)) {
jbe@87 779 close(handle->fd);
jbe@87 780 handle->fd = -1;
jbe@87 781 return 2;
jbe@87 782 }
jbe@261 783 #if LUA_VERSION_NUM >= 503
jbe@261 784 if (nonblocking && lua_tointeger(L, -2)) {
jbe@261 785 #else
jbe@261 786 if (nonblocking && lua_tonumber(L, -2)) {
jbe@261 787 #endif
jbe@261 788 lua_pushliteral(L, "flush");
jbe@261 789 lua_pushvalue(L, -3);
jbe@261 790 return 2;
jbe@261 791 }
jbe@129 792 } else {
jbe@129 793 handle->closed = 1;
jbe@83 794 }
jbe@260 795 #ifdef MOONBR_IO_USE_TLS
jbe@260 796 if (handle->tls) {
jbe@260 797 int status;
jbe@261 798 if (moonbr_io_handle_set_nonblocking(L, handle, nonblocking)) moonbr_io_return_errmsg();
jbe@260 799 do status = tls_close(handle->tls);
jbe@261 800 while (!nonblocking && (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT));
jbe@261 801 if (status == TLS_WANT_POLLIN || status == TLS_WANT_POLLOUT) {
jbe@261 802 handle->tlsclosing = status; /* TODO: handle polling */
jbe@261 803 lua_pushliteral(L, "close");
jbe@261 804 return 1;
jbe@261 805 }
jbe@260 806 if (status) {
jbe@260 807 close(handle->fd);
jbe@260 808 handle->fd = -1;
jbe@260 809 lua_pushnil(L);
jbe@260 810 lua_pushstring(L, tls_error(handle->tls));
jbe@260 811 return 2;
jbe@260 812 }
jbe@260 813 }
jbe@260 814 #endif
jbe@256 815 if (moonbr_io_handle_set_linger(L, handle, -1)) {
jbe@257 816 moonbr_io_prepare_errmsg();
jbe@256 817 close(handle->fd);
jbe@256 818 handle->fd = -1;
jbe@257 819 moonbr_io_return_prepared_errmsg();
jbe@256 820 }
jbe@129 821 } else {
jbe@129 822 handle->closed = 1;
jbe@83 823 }
jbe@94 824 if (handle->fd >= 0) {
jbe@94 825 if (close(handle->fd)) {
jbe@94 826 handle->fd = -1;
jbe@257 827 moonbr_io_return_errmsg();
jbe@94 828 }
jbe@104 829 handle->fd = -1;
jbe@83 830 }
jbe@83 831 lua_pushboolean(L, 1);
jbe@83 832 return 1;
jbe@84 833
jbe@83 834 }
jbe@83 835
jbe@94 836 static int moonbr_io_close(lua_State *L) {
jbe@261 837 return moonbr_io_close_impl(L, 0, 0);
jbe@94 838 }
jbe@94 839
jbe@84 840 static int moonbr_io_reset(lua_State *L) {
jbe@261 841 return moonbr_io_close_impl(L, 0, 1);
jbe@84 842 }
jbe@84 843
jbe@108 844 static int moonbr_io_handlegc(lua_State *L) {
jbe@88 845 moonbr_io_handle_t *handle;
jbe@88 846 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 847 if (handle->fd >= 0) {
jbe@218 848 lua_pushcfunction(L, moonbr_io_reset);
jbe@88 849 lua_pushvalue(L, 1);
jbe@88 850 lua_pushinteger(L, 0);
jbe@88 851 lua_call(L, 2, 0);
jbe@88 852 }
jbe@246 853 #ifdef MOONBR_IO_USE_TLS
jbe@246 854 if (handle->tls) {
jbe@246 855 tls_free(handle->tls);
jbe@246 856 handle->tls = NULL;
jbe@246 857 }
jbe@246 858 if (handle->servertls) {
jbe@246 859 tls_free(handle->servertls);
jbe@246 860 handle->servertls = NULL;
jbe@246 861 }
jbe@246 862 #endif
jbe@88 863 return 0;
jbe@88 864 }
jbe@88 865
jbe@100 866 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
jbe@88 867 moonbr_io_handle_t *handle;
jbe@88 868 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@100 869 if (!handle->closed) {
jbe@100 870 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
jbe@100 871 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
jbe@100 872 lua_call(L, 1, 0);
jbe@88 873 }
jbe@88 874 }
jbe@88 875
jbe@208 876 static int moonbr_io_pushhandle_impl(lua_State *L) {
jbe@208 877 int *fd;
jbe@79 878 moonbr_io_handle_t *handle;
jbe@107 879 struct sockaddr addr;
jbe@107 880 socklen_t addrlen;
jbe@208 881 fd = lua_touserdata(L, 1);
jbe@79 882 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@208 883 handle->fd = -1; /* avoid closing incomplete handle */
jbe@107 884 addrlen = sizeof(addr);
jbe@208 885 if (getsockname(*fd, &addr, &addrlen)) {
jbe@107 886 if (errno != ENOTSOCK) {
jbe@257 887 moonbr_io_prepare_errmsg();
jbe@107 888 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
jbe@107 889 }
jbe@107 890 handle->issock = 0;
jbe@107 891 } else {
jbe@107 892 handle->issock = 1;
jbe@107 893 handle->addrfam = addr.sa_family;
jbe@107 894 }
jbe@94 895 handle->finished = 0;
jbe@94 896 handle->closed = 0;
jbe@81 897 handle->nonblocking = -1;
jbe@95 898 handle->nopush = -1;
jbe@85 899 handle->readerr = 0;
jbe@105 900 handle->readbufin = 0;
jbe@105 901 handle->readbufout = 0;
jbe@81 902 handle->writeerr = 0;
jbe@81 903 handle->writeleft = 0;
jbe@132 904 handle->flushedleft = 0;
jbe@83 905 handle->writeqin = 0;
jbe@83 906 handle->writeqout = 0;
jbe@81 907 handle->writeqoff = 0;
jbe@103 908 handle->writebufin = 0;
jbe@103 909 handle->writebufout = 0;
jbe@246 910 #ifdef MOONBR_IO_USE_TLS
jbe@246 911 handle->tls = NULL;
jbe@246 912 handle->servertls = NULL;
jbe@250 913 handle->tlshandshake = 0;
jbe@261 914 handle->tlsclosing = 0;
jbe@246 915 #endif
jbe@208 916 handle->fd = *fd; /* required for set_linger call */
jbe@256 917 if (moonbr_io_handle_set_linger(L, handle, 0)) {
jbe@257 918 moonbr_io_prepare_errmsg();
jbe@256 919 handle->fd = -1;
jbe@256 920 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
jbe@256 921 }
jbe@208 922 handle->fd = -1; /* avoid closing incomplete handle */
jbe@238 923 luaL_setmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 924 lua_newtable(L); // uservalue
jbe@81 925 lua_newtable(L);
jbe@103 926 lua_setfield(L, -2, "writequeue");
jbe@79 927 lua_newtable(L); // public
jbe@107 928 if (handle->addrfam == AF_INET6) {
jbe@107 929 struct sockaddr_in6 addr_in6;
jbe@107 930 char addrstrbuf[INET6_ADDRSTRLEN];
jbe@107 931 const char *addrstr;
jbe@107 932 addrlen = sizeof(addr_in6);
jbe@208 933 if (getsockname(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@257 934 moonbr_io_prepare_errmsg();
jbe@107 935 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 936 }
jbe@107 937 if (addrlen > sizeof(addr_in6)) {
jbe@107 938 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 939 }
jbe@107 940 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 941 if (!addrstr) {
jbe@257 942 moonbr_io_prepare_errmsg();
jbe@107 943 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 944 } else {
jbe@107 945 lua_pushstring(L, addrstr);
jbe@107 946 lua_setfield(L, -2, "local_ip6");
jbe@107 947 }
jbe@107 948 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 949 lua_setfield(L, -2, "local_tcpport");
jbe@208 950 if (getpeername(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@257 951 moonbr_io_prepare_errmsg();
jbe@107 952 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 953 }
jbe@107 954 if (addrlen > sizeof(addr_in6)) {
jbe@107 955 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 956 }
jbe@107 957 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 958 if (!addrstr) {
jbe@257 959 moonbr_io_prepare_errmsg();
jbe@107 960 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 961 } else {
jbe@107 962 lua_pushstring(L, addrstr);
jbe@107 963 lua_setfield(L, -2, "remote_ip6");
jbe@107 964 }
jbe@107 965 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 966 lua_setfield(L, -2, "remote_tcpport");
jbe@107 967 } else if (handle->addrfam == AF_INET) {
jbe@107 968 struct sockaddr_in addr_in;
jbe@107 969 char addrstrbuf[INET_ADDRSTRLEN];
jbe@107 970 const char *addrstr;
jbe@107 971 addrlen = sizeof(addr_in);
jbe@208 972 if (getsockname(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@257 973 moonbr_io_prepare_errmsg();
jbe@107 974 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 975 }
jbe@107 976 if (addrlen > sizeof(addr_in)) {
jbe@107 977 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 978 }
jbe@107 979 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 980 if (!addrstr) {
jbe@257 981 moonbr_io_prepare_errmsg();
jbe@107 982 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 983 } else {
jbe@107 984 lua_pushstring(L, addrstr);
jbe@107 985 lua_setfield(L, -2, "local_ip4");
jbe@107 986 }
jbe@107 987 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 988 lua_setfield(L, -2, "local_tcpport");
jbe@208 989 if (getpeername(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@257 990 moonbr_io_prepare_errmsg();
jbe@107 991 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 992 }
jbe@107 993 if (addrlen > sizeof(addr_in)) {
jbe@107 994 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 995 }
jbe@107 996 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 997 if (!addrstr) {
jbe@257 998 moonbr_io_prepare_errmsg();
jbe@107 999 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 1000 } else {
jbe@107 1001 lua_pushstring(L, addrstr);
jbe@107 1002 lua_setfield(L, -2, "remote_ip4");
jbe@107 1003 }
jbe@107 1004 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 1005 lua_setfield(L, -2, "remote_tcpport");
jbe@107 1006 }
jbe@238 1007 luaL_setmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 1008 lua_setfield(L, -2, "public");
jbe@79 1009 lua_setuservalue(L, -2);
jbe@208 1010 handle->fd = *fd;
jbe@208 1011 *fd = -1; /* closing is now handled by garbage collection */
jbe@208 1012 return 1;
jbe@208 1013 }
jbe@208 1014
jbe@208 1015 void moonbr_io_pushhandle(lua_State *L, int fd) {
jbe@208 1016 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1017 lua_pushlightuserdata(L, &fd);
jbe@208 1018 if (lua_pcall(L, 1, 1, 0)) {
jbe@251 1019 if (fd != -1) close(fd); // TODO: correct to close file descriptor here?
jbe@208 1020 lua_error(L);
jbe@208 1021 }
jbe@79 1022 }
jbe@79 1023
jbe@79 1024 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 1025 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@205 1026 luaL_checkany(L, 2);
jbe@79 1027 lua_getuservalue(L, 1);
jbe@79 1028 lua_getfield(L, -1, "public");
jbe@79 1029 lua_pushvalue(L, 2);
jbe@79 1030 lua_gettable(L, -2);
jbe@79 1031 return 1;
jbe@79 1032 }
jbe@79 1033
jbe@79 1034 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 1035 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@205 1036 luaL_checkany(L, 2);
jbe@205 1037 luaL_checkany(L, 3);
jbe@79 1038 lua_getuservalue(L, 1);
jbe@79 1039 lua_getfield(L, -1, "public");
jbe@79 1040 lua_pushvalue(L, 2);
jbe@79 1041 lua_pushvalue(L, 3);
jbe@79 1042 lua_settable(L, -3);
jbe@79 1043 return 0;
jbe@79 1044 }
jbe@79 1045
jbe@111 1046 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
jbe@111 1047 const char *path;
jbe@111 1048 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@111 1049 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@111 1050 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@111 1051 ) - 1; /* one byte for termination */
jbe@111 1052 int sock;
jbe@111 1053 path = luaL_checkstring(L, 1);
jbe@111 1054 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@111 1055 strcpy(sockaddr.sun_path, path);
jbe@111 1056 sock = socket(
jbe@111 1057 PF_LOCAL,
jbe@111 1058 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@111 1059 0
jbe@111 1060 );
jbe@257 1061 if (sock < 0) moonbr_io_return_errmsg();
jbe@111 1062 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@111 1063 if (!nonblocking && errno == EINTR) {
jbe@257 1064 moonbr_io_prepare_errmsg();
jbe@111 1065 close(sock);
jbe@257 1066 moonbr_io_return_prepared_errmsg();
jbe@257 1067 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) moonbr_io_return_errmsg();
jbe@111 1068 }
jbe@111 1069 moonbr_io_pushhandle(L, sock);
jbe@111 1070 return 1;
jbe@111 1071 }
jbe@111 1072
jbe@111 1073 static int moonbr_io_localconnect(lua_State *L) {
jbe@111 1074 return moonbr_io_localconnect_impl(L, 0);
jbe@111 1075 }
jbe@111 1076
jbe@111 1077 static int moonbr_io_localconnect_nb(lua_State *L) {
jbe@111 1078 return moonbr_io_localconnect_impl(L, 1);
jbe@111 1079 }
jbe@111 1080
jbe@99 1081 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
jbe@98 1082 const char *host, *port;
jbe@98 1083 struct addrinfo hints = { 0, };
jbe@98 1084 struct addrinfo *res, *addrinfo;
jbe@98 1085 int errcode;
jbe@98 1086 int sock;
jbe@98 1087 host = luaL_checkstring(L, 1);
jbe@98 1088 port = luaL_checkstring(L, 2);
jbe@98 1089 hints.ai_family = AF_UNSPEC;
jbe@98 1090 hints.ai_socktype = SOCK_STREAM;
jbe@98 1091 hints.ai_protocol = IPPROTO_TCP;
jbe@98 1092 hints.ai_flags = AI_ADDRCONFIG;
jbe@98 1093 errcode = getaddrinfo(host, port, &hints, &res);
jbe@98 1094 if (errcode) {
jbe@102 1095 freeaddrinfo(res);
jbe@98 1096 if (errcode == EAI_SYSTEM) {
jbe@257 1097 moonbr_io_prepare_errmsg();
jbe@98 1098 lua_pushnil(L);
jbe@98 1099 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@98 1100 } else {
jbe@98 1101 lua_pushnil(L);
jbe@98 1102 lua_pushstring(L, gai_strerror(errcode));
jbe@98 1103 }
jbe@98 1104 return 2;
jbe@98 1105 }
jbe@98 1106 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1107 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@98 1108 }
jbe@98 1109 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1110 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@98 1111 }
jbe@98 1112 addrinfo = res;
jbe@98 1113 moonbr_io_tcpconnect_found:
jbe@99 1114 sock = socket(
jbe@134 1115 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@99 1116 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@99 1117 addrinfo->ai_protocol
jbe@99 1118 );
jbe@98 1119 if (sock < 0) {
jbe@257 1120 moonbr_io_prepare_errmsg();
jbe@108 1121 freeaddrinfo(res);
jbe@257 1122 moonbr_io_return_prepared_errmsg();
jbe@98 1123 }
jbe@98 1124 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@108 1125 freeaddrinfo(res);
jbe@99 1126 if (!nonblocking && errno == EINTR) {
jbe@257 1127 moonbr_io_prepare_errmsg();
jbe@99 1128 close(sock);
jbe@257 1129 moonbr_io_return_prepared_errmsg();
jbe@257 1130 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) moonbr_io_return_errmsg();
jbe@108 1131 } else {
jbe@108 1132 freeaddrinfo(res);
jbe@98 1133 }
jbe@107 1134 moonbr_io_pushhandle(L, sock);
jbe@98 1135 return 1;
jbe@98 1136 }
jbe@98 1137
jbe@99 1138 static int moonbr_io_tcpconnect(lua_State *L) {
jbe@99 1139 return moonbr_io_tcpconnect_impl(L, 0);
jbe@99 1140 }
jbe@99 1141
jbe@99 1142 static int moonbr_io_tcpconnect_nb(lua_State *L) {
jbe@99 1143 return moonbr_io_tcpconnect_impl(L, 1);
jbe@99 1144 }
jbe@99 1145
jbe@112 1146 static int moonbr_io_locallisten(lua_State *L) {
jbe@112 1147 moonbr_io_listener_t *listener;
jbe@112 1148 const char *path;
jbe@118 1149 struct stat sb;
jbe@112 1150 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@112 1151 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@112 1152 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@112 1153 ) - 1; /* one byte for termination */
jbe@112 1154 int sock;
jbe@112 1155 path = luaL_checkstring(L, 1);
jbe@112 1156 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@112 1157 strcpy(sockaddr.sun_path, path);
jbe@118 1158 if (stat(path, &sb) == 0) {
jbe@118 1159 if (S_ISSOCK(sb.st_mode)) unlink(path);
jbe@118 1160 }
jbe@112 1161 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 1162 listener->fd = -1;
jbe@112 1163 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@112 1164 sock = socket(
jbe@112 1165 PF_LOCAL,
jbe@112 1166 SOCK_STREAM | SOCK_CLOEXEC,
jbe@112 1167 0
jbe@112 1168 );
jbe@257 1169 if (sock < 0) moonbr_io_return_errmsg();
jbe@112 1170 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@257 1171 moonbr_io_prepare_errmsg();
jbe@112 1172 close(sock);
jbe@257 1173 moonbr_io_return_prepared_errmsg();
jbe@112 1174 }
jbe@112 1175 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@257 1176 moonbr_io_prepare_errmsg();
jbe@112 1177 close(sock);
jbe@257 1178 moonbr_io_return_prepared_errmsg();
jbe@112 1179 }
jbe@112 1180 listener->fd = sock;
jbe@118 1181 listener->addrfam = AF_LOCAL;
jbe@112 1182 listener->nonblocking = -1;
jbe@112 1183 return 1;
jbe@112 1184 }
jbe@112 1185
jbe@108 1186 static int moonbr_io_tcplisten(lua_State *L) {
jbe@108 1187 moonbr_io_listener_t *listener;
jbe@108 1188 const char *host, *port;
jbe@108 1189 struct addrinfo hints = { 0, };
jbe@108 1190 struct addrinfo *res, *addrinfo;
jbe@108 1191 int errcode;
jbe@108 1192 int sock;
jbe@108 1193 host = luaL_optstring(L, 1, NULL);
jbe@108 1194 port = luaL_checkstring(L, 2);
jbe@108 1195 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 1196 listener->fd = -1;
jbe@108 1197 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1198 hints.ai_family = AF_UNSPEC;
jbe@108 1199 hints.ai_socktype = SOCK_STREAM;
jbe@108 1200 hints.ai_protocol = IPPROTO_TCP;
jbe@108 1201 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
jbe@108 1202 errcode = getaddrinfo(host, port, &hints, &res);
jbe@108 1203 if (errcode) {
jbe@108 1204 freeaddrinfo(res);
jbe@108 1205 if (errcode == EAI_SYSTEM) {
jbe@257 1206 moonbr_io_prepare_errmsg();
jbe@108 1207 lua_pushnil(L);
jbe@108 1208 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@108 1209 } else {
jbe@108 1210 lua_pushnil(L);
jbe@108 1211 lua_pushstring(L, gai_strerror(errcode));
jbe@108 1212 }
jbe@108 1213 return 2;
jbe@108 1214 }
jbe@108 1215 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1216 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@108 1217 }
jbe@108 1218 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1219 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@108 1220 }
jbe@108 1221 addrinfo = res;
jbe@108 1222 moonbr_io_tcpconnect_found:
jbe@118 1223 listener->addrfam = addrinfo->ai_family;
jbe@108 1224 sock = socket(
jbe@134 1225 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@108 1226 addrinfo->ai_socktype | SOCK_CLOEXEC,
jbe@108 1227 addrinfo->ai_protocol
jbe@108 1228 );
jbe@108 1229 if (sock < 0) {
jbe@257 1230 moonbr_io_prepare_errmsg();
jbe@108 1231 freeaddrinfo(res);
jbe@257 1232 moonbr_io_return_prepared_errmsg();
jbe@108 1233 }
jbe@120 1234 {
jbe@120 1235 static const int reuseval = 1;
jbe@120 1236 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
jbe@257 1237 moonbr_io_prepare_errmsg();
jbe@120 1238 freeaddrinfo(res);
jbe@120 1239 close(sock);
jbe@120 1240 lua_pushnil(L);
jbe@120 1241 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
jbe@120 1242 return 2;
jbe@120 1243 }
jbe@120 1244 }
jbe@108 1245 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@257 1246 moonbr_io_prepare_errmsg();
jbe@108 1247 freeaddrinfo(res);
jbe@108 1248 close(sock);
jbe@257 1249 moonbr_io_return_prepared_errmsg();
jbe@108 1250 }
jbe@108 1251 freeaddrinfo(res);
jbe@108 1252 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@257 1253 moonbr_io_prepare_errmsg();
jbe@108 1254 close(sock);
jbe@257 1255 moonbr_io_return_prepared_errmsg();
jbe@108 1256 }
jbe@108 1257 listener->fd = sock;
jbe@108 1258 listener->nonblocking = -1;
jbe@108 1259 return 1;
jbe@108 1260 }
jbe@108 1261
jbe@108 1262 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
jbe@108 1263 moonbr_io_listener_t *listener;
jbe@108 1264 int fd;
jbe@108 1265 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1266 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
jbe@108 1267 if (listener->nonblocking != nonblocking) {
jbe@108 1268 int flags;
jbe@108 1269 flags = fcntl(listener->fd, F_GETFL, 0);
jbe@108 1270 if (flags == -1) {
jbe@257 1271 moonbr_io_prepare_errmsg();
jbe@108 1272 close(listener->fd);
jbe@108 1273 listener->fd = -1;
jbe@108 1274 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1275 }
jbe@108 1276 if (nonblocking) flags |= O_NONBLOCK;
jbe@108 1277 else flags &= ~O_NONBLOCK;
jbe@108 1278 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
jbe@257 1279 moonbr_io_prepare_errmsg();
jbe@108 1280 close(listener->fd);
jbe@108 1281 listener->fd = -1;
jbe@108 1282 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1283 }
jbe@108 1284 listener->nonblocking = nonblocking;
jbe@108 1285 }
jbe@108 1286 while (1) {
jbe@214 1287 #if defined(__linux__) && !defined(_GNU_SOURCE)
jbe@215 1288 fd = accept(listener->fd, NULL, NULL);
jbe@214 1289 if (fd != -1) {
jbe@214 1290 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
jbe@257 1291 moonbr_io_prepare_errmsg();
jbe@214 1292 close(listener->fd);
jbe@214 1293 listener->fd = -1;
jbe@214 1294 close(fd);
jbe@214 1295 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@214 1296 }
jbe@214 1297 }
jbe@214 1298 #else
jbe@108 1299 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
jbe@214 1300 #endif
jbe@108 1301 if (fd < 0) {
jbe@108 1302 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@108 1303 lua_pushboolean(L, 0);
jbe@108 1304 lua_pushliteral(L, "No incoming connection pending");
jbe@108 1305 return 2;
jbe@257 1306 } else if (errno != EINTR) moonbr_io_return_errmsg();
jbe@108 1307 } else {
jbe@108 1308 moonbr_io_pushhandle(L, fd);
jbe@108 1309 return 1;
jbe@108 1310 }
jbe@108 1311 }
jbe@108 1312 }
jbe@108 1313
jbe@108 1314 static int moonbr_io_accept(lua_State *L) {
jbe@108 1315 return moonbr_io_accept_impl(L, 0);
jbe@108 1316 }
jbe@108 1317
jbe@108 1318 static int moonbr_io_accept_nb(lua_State *L) {
jbe@108 1319 return moonbr_io_accept_impl(L, 1);
jbe@108 1320 }
jbe@108 1321
jbe@108 1322 static int moonbr_io_unlisten(lua_State *L) {
jbe@108 1323 moonbr_io_listener_t *listener;
jbe@118 1324 struct sockaddr_un addr;
jbe@118 1325 socklen_t addrlen;
jbe@118 1326 struct stat sb;
jbe@108 1327 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1328 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
jbe@118 1329 addrlen = sizeof(addr);
jbe@118 1330 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
jbe@108 1331 if (close(listener->fd)) {
jbe@257 1332 moonbr_io_prepare_errmsg();
jbe@108 1333 listener->fd = -1;
jbe@118 1334 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1335 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1336 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1337 }
jbe@118 1338 }
jbe@257 1339 moonbr_io_return_prepared_errmsg();
jbe@108 1340 }
jbe@108 1341 listener->fd = -1;
jbe@118 1342 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1343 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1344 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1345 }
jbe@118 1346 }
jbe@108 1347 lua_pushboolean(L, 1);
jbe@108 1348 return 1;
jbe@108 1349 }
jbe@108 1350
jbe@108 1351 static int moonbr_io_listenergc(lua_State *L) {
jbe@108 1352 moonbr_io_listener_t *listener;
jbe@108 1353 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@117 1354 if (listener->fd >= 0) close(listener->fd);
jbe@108 1355 listener->fd = -1;
jbe@108 1356 return 0;
jbe@108 1357 }
jbe@108 1358
jbe@205 1359 static int moonbr_io_exec(lua_State *L) {
jbe@205 1360 char **argv;
jbe@205 1361 int i, argc;
jbe@205 1362 int sockin[2], sockout[2], sockerr[2];
jbe@205 1363 volatile int errorcond = 0;
jbe@213 1364 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
jbe@205 1365 moonbr_io_child_t *child;
jbe@205 1366 argc = lua_gettop(L);
jbe@205 1367 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
jbe@205 1368 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
jbe@205 1369 argv[argc] = NULL;
jbe@208 1370 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
jbe@208 1371 child->pid = 0;
jbe@208 1372 lua_newtable(L);
jbe@208 1373 lua_setuservalue(L, -2);
jbe@238 1374 luaL_setmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
jbe@206 1375 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
jbe@257 1376 moonbr_io_prepare_errmsg();
jbe@205 1377 lua_pushnil(L);
jbe@205 1378 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1379 return 2;
jbe@205 1380 }
jbe@206 1381 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
jbe@257 1382 moonbr_io_prepare_errmsg();
jbe@205 1383 close(sockin[0]);
jbe@205 1384 close(sockin[1]);
jbe@205 1385 lua_pushnil(L);
jbe@205 1386 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1387 return 2;
jbe@205 1388 }
jbe@206 1389 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
jbe@257 1390 moonbr_io_prepare_errmsg();
jbe@205 1391 close(sockin[0]);
jbe@205 1392 close(sockin[1]);
jbe@205 1393 close(sockout[0]);
jbe@205 1394 close(sockout[1]);
jbe@205 1395 lua_pushnil(L);
jbe@205 1396 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1397 return 2;
jbe@205 1398 }
jbe@205 1399 child->pid = vfork();
jbe@205 1400 if (child->pid == -1) {
jbe@257 1401 moonbr_io_prepare_errmsg();
jbe@205 1402 close(sockin[0]);
jbe@205 1403 close(sockin[1]);
jbe@205 1404 close(sockout[0]);
jbe@205 1405 close(sockout[1]);
jbe@205 1406 close(sockerr[0]);
jbe@205 1407 close(sockerr[1]);
jbe@205 1408 lua_pushnil(L);
jbe@205 1409 lua_pushfstring(L, "Could not fork: %s", errmsg);
jbe@205 1410 return 2;
jbe@205 1411 }
jbe@205 1412 if (!child->pid) {
jbe@208 1413 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1414 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
jbe@208 1415 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
jbe@232 1416 closefrom(3);
jbe@208 1417 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1418 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1419 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@205 1420 if (execvp(argv[0], argv)) {
jbe@205 1421 errorcond = 2;
jbe@205 1422 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
jbe@205 1423 _exit(0);
jbe@205 1424 }
jbe@208 1425 moonbr_io_exec_error1:
jbe@206 1426 errorcond = 1;
jbe@206 1427 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
jbe@206 1428 _exit(0);
jbe@205 1429 }
jbe@205 1430 close(sockin[1]);
jbe@205 1431 close(sockout[1]);
jbe@205 1432 close(sockerr[1]);
jbe@205 1433 if (errorcond) {
jbe@205 1434 int status;
jbe@205 1435 close(sockin[0]);
jbe@205 1436 close(sockout[0]);
jbe@205 1437 close(sockerr[0]);
jbe@205 1438 while (waitpid(child->pid, &status, 0) == -1) {
jbe@205 1439 if (errno != EINTR) {
jbe@257 1440 moonbr_io_prepare_errmsg();
jbe@205 1441 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
jbe@205 1442 }
jbe@205 1443 }
jbe@208 1444 child->pid = 0;
jbe@205 1445 lua_pushnil(L);
jbe@205 1446 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
jbe@205 1447 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
jbe@205 1448 return 2;
jbe@205 1449 }
jbe@208 1450 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1451 lua_pushlightuserdata(L, &sockin[0]);
jbe@208 1452 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1453 if (sockin[0] != -1) close(sockin[0]);
jbe@208 1454 close(sockout[0]);
jbe@208 1455 close(sockerr[0]);
jbe@208 1456 goto moonbr_io_exec_error2;
jbe@208 1457 }
jbe@205 1458 lua_setfield(L, -2, "stdin");
jbe@208 1459 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1460 lua_pushlightuserdata(L, &sockout[0]);
jbe@208 1461 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1462 if (sockout[0] != -1) close(sockout[0]);
jbe@208 1463 close(sockerr[0]);
jbe@208 1464 goto moonbr_io_exec_error2;
jbe@208 1465 }
jbe@205 1466 lua_setfield(L, -2, "stdout");
jbe@208 1467 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1468 lua_pushlightuserdata(L, &sockerr[0]);
jbe@208 1469 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1470 if (sockerr[0] != -1) close(sockerr[0]);
jbe@208 1471 goto moonbr_io_exec_error2;
jbe@208 1472 }
jbe@205 1473 lua_setfield(L, -2, "stderr");
jbe@205 1474 return 1;
jbe@208 1475 moonbr_io_exec_error2:
jbe@208 1476 {
jbe@208 1477 int status;
jbe@208 1478 while (waitpid(child->pid, &status, 0) == -1) {
jbe@208 1479 if (errno != EINTR) {
jbe@257 1480 moonbr_io_prepare_errmsg();
jbe@208 1481 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
jbe@208 1482 }
jbe@208 1483 }
jbe@208 1484 }
jbe@208 1485 child->pid = 0;
jbe@208 1486 return lua_error(L);
jbe@205 1487 }
jbe@205 1488
jbe@205 1489 static int moonbr_io_childindex(lua_State *L) {
jbe@205 1490 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1491 luaL_checkany(L, 2);
jbe@205 1492 lua_getuservalue(L, 1);
jbe@205 1493 lua_pushvalue(L, 2);
jbe@205 1494 lua_gettable(L, -2);
jbe@205 1495 if (lua_isnil(L, -1)) {
jbe@205 1496 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
jbe@205 1497 lua_pushvalue(L, 2);
jbe@205 1498 lua_gettable(L, -2);
jbe@205 1499 }
jbe@205 1500 return 1;
jbe@205 1501 }
jbe@205 1502
jbe@205 1503 static int moonbr_io_childnewindex(lua_State *L) {
jbe@205 1504 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1505 luaL_checkany(L, 2);
jbe@205 1506 luaL_checkany(L, 3);
jbe@205 1507 lua_getuservalue(L, 1);
jbe@205 1508 lua_pushvalue(L, 2);
jbe@205 1509 lua_pushvalue(L, 3);
jbe@205 1510 lua_settable(L, -3);
jbe@205 1511 return 0;
jbe@205 1512 }
jbe@205 1513
jbe@205 1514 static int moonbr_io_childgc(lua_State *L) {
jbe@205 1515 moonbr_io_child_t *child;
jbe@205 1516 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1517 if (child->pid) {
jbe@205 1518 int status;
jbe@205 1519 if (kill(child->pid, SIGKILL)) {
jbe@257 1520 moonbr_io_prepare_errmsg();
jbe@205 1521 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
jbe@205 1522 }
jbe@205 1523 while (waitpid(child->pid, &status, 0) == -1) {
jbe@205 1524 if (errno != EINTR) {
jbe@257 1525 moonbr_io_prepare_errmsg();
jbe@205 1526 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
jbe@205 1527 }
jbe@205 1528 }
jbe@205 1529 }
jbe@205 1530 return 0;
jbe@205 1531 }
jbe@205 1532
jbe@205 1533 static int moonbr_io_kill(lua_State *L) {
jbe@205 1534 moonbr_io_child_t *child;
jbe@205 1535 int sig;
jbe@205 1536 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1537 sig = luaL_optinteger(L, 2, SIGTERM);
jbe@205 1538 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
jbe@205 1539 if (kill(child->pid, sig)) {
jbe@257 1540 moonbr_io_prepare_errmsg();
jbe@205 1541 luaL_error(L, "Error in kill call: %s", errmsg);
jbe@205 1542 }
jbe@205 1543 lua_settop(L, 1);
jbe@205 1544 return 1;
jbe@205 1545 }
jbe@205 1546
jbe@205 1547 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
jbe@205 1548 moonbr_io_child_t *child;
jbe@205 1549 pid_t waitedpid;
jbe@205 1550 int status;
jbe@205 1551 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1552 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
jbe@205 1553 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
jbe@205 1554 if (errno != EINTR) {
jbe@257 1555 moonbr_io_prepare_errmsg();
jbe@205 1556 luaL_error(L, "Error in waitpid call: %s", errmsg);
jbe@205 1557 }
jbe@205 1558 }
jbe@205 1559 if (!waitedpid) {
jbe@219 1560 lua_pushboolean(L, 0);
jbe@219 1561 lua_pushliteral(L, "Process is still running");
jbe@219 1562 return 2;
jbe@205 1563 } else {
jbe@205 1564 child->pid = 0;
jbe@205 1565 if (WIFEXITED(status)) {
jbe@205 1566 lua_pushinteger(L, WEXITSTATUS(status));
jbe@205 1567 } else if (WIFSIGNALED(status)) {
jbe@205 1568 lua_pushinteger(L, -WTERMSIG(status));
jbe@205 1569 } else {
jbe@205 1570 luaL_error(L, "Unexpected status value returned by waitpid call");
jbe@205 1571 }
jbe@219 1572 return 1;
jbe@205 1573 }
jbe@205 1574 }
jbe@205 1575
jbe@205 1576 static int moonbr_io_wait(lua_State *L) {
jbe@205 1577 return moonbr_io_wait_impl(L, 0);
jbe@205 1578 }
jbe@205 1579
jbe@205 1580 static int moonbr_io_wait_nb(lua_State *L) {
jbe@205 1581 return moonbr_io_wait_impl(L, 1);
jbe@205 1582 }
jbe@205 1583
jbe@205 1584 #if LUA_VERSION_NUM >= 503
jbe@205 1585 static int moonbr_io_wait_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@205 1586 #else
jbe@205 1587 static int moonbr_io_wait_cont(lua_State *L) {
jbe@205 1588 #endif
jbe@205 1589 #if !(LUA_VERSION_NUM >= 503)
jbe@205 1590 int ctx = 0;
jbe@205 1591 lua_getctx(L, &ctx);
jbe@205 1592 #endif
jbe@205 1593 while (1) {
jbe@205 1594 lua_pushcfunction(L, moonbr_io_wait_nb);
jbe@205 1595 lua_pushvalue(L, 1);
jbe@205 1596 lua_call(L, 1, 1);
jbe@205 1597 if (!lua_isnil(L, -1)) break;
jbe@205 1598 lua_pushvalue(L, 2);
jbe@205 1599 lua_callk(L, 0, 0, ctx, moonbr_io_wait_cont);
jbe@205 1600 }
jbe@205 1601 return 1;
jbe@205 1602 }
jbe@205 1603
jbe@205 1604 static int moonbr_io_wait_call(lua_State *L) {
jbe@205 1605 lua_settop(L, 2);
jbe@205 1606 #if LUA_VERSION_NUM >= 503
jbe@205 1607 return moonbr_io_wait_cont(L, 0, 0);
jbe@205 1608 #else
jbe@205 1609 return moonbr_io_wait_cont(L);
jbe@205 1610 #endif
jbe@205 1611 }
jbe@205 1612
jbe@205 1613 moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
jbe@205 1614
jbe@266 1615 static void moonbr_io_signalhandler(int sig) {
jbe@266 1616 int errno2 = errno;
jbe@266 1617 char buf[1] = {sig};
jbe@266 1618 write(moonbr_io_signalfd_write, buf, 1);
jbe@266 1619 errno = errno2;
jbe@266 1620 }
jbe@266 1621
jbe@266 1622 static int moonbr_io_signalsocket(lua_State *L) {
jbe@266 1623 int i, argc, sig;
jbe@266 1624 if (moonbr_io_signalfd_read < 0) {
jbe@266 1625 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, moonbr_io_signalfds)) {
jbe@266 1626 luaL_error(L, "Could not create socket pair for signal queueing");
jbe@266 1627 }
jbe@266 1628 }
jbe@266 1629 argc = lua_gettop(L);
jbe@266 1630 for (i=1; i<=argc; i++) {
jbe@266 1631 sig = luaL_checkinteger(L, i);
jbe@266 1632 errno = 0;
jbe@266 1633 signal(sig, moonbr_io_signalhandler);
jbe@266 1634 if (errno) luaL_error(L, "Could not install signal handler (invalid signal number?)");
jbe@266 1635 }
jbe@266 1636 lua_settop(L, 0);
jbe@266 1637 lua_getfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKET_REGKEY);
jbe@266 1638 if (lua_isnil(L, -1)) {
jbe@266 1639 int fd = dup(moonbr_io_signalfd_read); /* TODO: determine how mooonbr_io_pushhandle acts in case of error */
jbe@266 1640 if (fd < 0) luaL_error(L, "Error in dup() system call");
jbe@266 1641 moonbr_io_pushhandle(L, fd);
jbe@266 1642 lua_pushvalue(L, -1);
jbe@266 1643 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKET_REGKEY);
jbe@266 1644 }
jbe@266 1645 return 1;
jbe@266 1646 }
jbe@266 1647
jbe@266 1648 static int moonbr_io_getpid(lua_State *L) {
jbe@266 1649 lua_pushinteger(L, getpid());
jbe@266 1650 return 1;
jbe@266 1651 }
jbe@266 1652
jbe@250 1653 #ifdef MOONBR_IO_USE_TLS
jbe@258 1654
jbe@250 1655 #define moonbr_io_poll_tls() \
jbe@250 1656 if (!handle->tlshandshake) { \
jbe@250 1657 lua_pushboolean(L, 1); \
jbe@250 1658 return 1; \
jbe@250 1659 } \
jbe@250 1660 if (handle->tlshandshake == TLS_WANT_POLLIN) { \
jbe@250 1661 if (fd < 0) { \
jbe@250 1662 lua_pushboolean(L, 1); \
jbe@250 1663 return 1; \
jbe@250 1664 } \
jbe@250 1665 FD_SET(fd, &readfds); \
jbe@250 1666 if (fd+1 > nfds) nfds = fd+1; \
jbe@250 1667 continue; \
jbe@250 1668 } \
jbe@250 1669 if (handle->tlshandshake == TLS_WANT_POLLOUT) { \
jbe@250 1670 if (fd < 0) { \
jbe@250 1671 lua_pushboolean(L, 1); \
jbe@250 1672 return 1; \
jbe@250 1673 } \
jbe@250 1674 FD_SET(fd, &writefds); \
jbe@250 1675 if (fd+1 > nfds) nfds = fd+1; \
jbe@250 1676 continue; \
jbe@250 1677 } \
jbe@250 1678 while (0)
jbe@258 1679
jbe@258 1680 #endif /* MOONBR_IO_USE_TLS */
jbe@250 1681
jbe@106 1682 static int moonbr_io_poll(lua_State *L) {
jbe@106 1683 moonbr_io_handle_t *handle;
jbe@108 1684 moonbr_io_listener_t *listener;
jbe@106 1685 int fd, isnum;
jbe@106 1686 int nfds = 0;
jbe@106 1687 fd_set readfds, writefds, exceptfds;
jbe@106 1688 struct timeval timeout = {0, };
jbe@106 1689 int status;
jbe@106 1690 FD_ZERO(&readfds);
jbe@106 1691 FD_ZERO(&writefds);
jbe@106 1692 FD_ZERO(&exceptfds);
jbe@106 1693 if (!lua_isnoneornil(L, 1)) {
jbe@106 1694 luaL_checktype(L, 1, LUA_TTABLE);
jbe@106 1695 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
jbe@106 1696 if (lua_toboolean(L, -1)) {
jbe@106 1697 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1698 if (handle) {
jbe@109 1699 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@106 1700 fd = handle->fd;
jbe@247 1701 #if MOONBR_IO_USE_TLS
jbe@250 1702 moonbr_io_poll_tls();
jbe@247 1703 #endif
jbe@122 1704 if (
jbe@122 1705 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
jbe@122 1706 handle->readbufin != handle->readbufout /* data pending in buffer */
jbe@122 1707 ) {
jbe@109 1708 lua_pushboolean(L, 1);
jbe@109 1709 return 1;
jbe@109 1710 }
jbe@106 1711 } else {
jbe@108 1712 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1713 if (listener) {
jbe@108 1714 fd = listener->fd;
jbe@108 1715 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
jbe@108 1716 } else {
jbe@108 1717 fd = lua_tointegerx(L, -2, &isnum);
jbe@108 1718 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
jbe@108 1719 }
jbe@106 1720 }
jbe@138 1721 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1722 FD_SET(fd, &readfds);
jbe@106 1723 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1724 }
jbe@106 1725 }
jbe@106 1726 }
jbe@106 1727 if (!lua_isnoneornil(L, 2)) {
jbe@106 1728 luaL_checktype(L, 2, LUA_TTABLE);
jbe@106 1729 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
jbe@106 1730 if (lua_toboolean(L, -1)) {
jbe@106 1731 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1732 if (handle) {
jbe@109 1733 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@109 1734 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
jbe@106 1735 fd = handle->fd;
jbe@247 1736 #if MOONBR_IO_USE_TLS
jbe@250 1737 moonbr_io_poll_tls();
jbe@247 1738 #endif
jbe@106 1739 } else {
jbe@108 1740 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@109 1741 if (listener) luaL_error(L, "Attempt to write-poll a listener");
jbe@109 1742 fd = lua_tointegerx(L, -2, &isnum);
jbe@109 1743 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
jbe@106 1744 }
jbe@138 1745 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1746 FD_SET(fd, &writefds);
jbe@106 1747 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1748 }
jbe@106 1749 }
jbe@106 1750 }
jbe@106 1751 if (!lua_isnoneornil(L, 3)) {
jbe@106 1752 lua_Number n;
jbe@106 1753 n = lua_tonumberx(L, 3, &isnum);
jbe@146 1754 if (isnum && n<0) {
jbe@146 1755 lua_pushboolean(L, 0);
jbe@146 1756 lua_pushliteral(L, "Negative timeout");
jbe@146 1757 return 2;
jbe@146 1758 } else if (isnum && n>=0 && n<100000000) {
jbe@106 1759 timeout.tv_sec = n;
jbe@106 1760 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
jbe@106 1761 } else {
jbe@106 1762 luaL_argcheck(L, 0, 3, "not a valid timeout");
jbe@106 1763 }
jbe@106 1764 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
jbe@106 1765 } else {
jbe@106 1766 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
jbe@106 1767 }
jbe@106 1768 if (status == -1) {
jbe@106 1769 if (errno == EINTR) {
jbe@266 1770 lua_pushboolean(L, 1);
jbe@266 1771 return 1;
jbe@106 1772 } else {
jbe@257 1773 moonbr_io_prepare_errmsg();
jbe@106 1774 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
jbe@106 1775 }
jbe@106 1776 } else if (status == 0) {
jbe@106 1777 lua_pushboolean(L, 0);
jbe@106 1778 lua_pushliteral(L, "Timeout while polling file descriptors");
jbe@106 1779 return 2;
jbe@106 1780 } else {
jbe@106 1781 lua_pushboolean(L, 1);
jbe@106 1782 return 1;
jbe@106 1783 }
jbe@106 1784 }
jbe@106 1785
jbe@115 1786 static int moonbr_io_timeref(lua_State *L) {
jbe@115 1787 lua_Number sub;
jbe@115 1788 struct timespec tp;
jbe@115 1789 sub = luaL_optnumber(L, 1, 0);
jbe@115 1790 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
jbe@115 1791 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
jbe@115 1792 }
jbe@115 1793 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
jbe@115 1794 return 1;
jbe@115 1795 }
jbe@115 1796
jbe@243 1797 #ifdef MOONBR_IO_USE_TLS
jbe@243 1798
jbe@243 1799 #define moonbr_io_tlsconf_string(name, field, func) \
jbe@243 1800 /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
jbe@243 1801 lua_getfield(L, 1, (field)); \
jbe@243 1802 valuetype = lua_type(L, -1); \
jbe@243 1803 if (valuetype != LUA_TNIL) { \
jbe@243 1804 luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
jbe@243 1805 value = lua_tostring(L, -1); \
jbe@252 1806 if (func(tlsconf->config, value)) { \
jbe@243 1807 lua_pushnil(L); \
jbe@243 1808 lua_pushfstring(L, "Could not set " name " \"%s\"", value); \
jbe@243 1809 return 2; \
jbe@243 1810 } \
jbe@243 1811 } \
jbe@243 1812 lua_pop(L, 1);
jbe@243 1813
jbe@243 1814 #define moonbr_io_tlsconf_binary(name, field, func) \
jbe@243 1815 /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
jbe@243 1816 lua_getfield(L, 1, (field)); \
jbe@243 1817 valuetype = lua_type(L, -1); \
jbe@243 1818 if (valuetype != LUA_TNIL) { \
jbe@243 1819 luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
jbe@243 1820 value = lua_tolstring(L, -1, &valuelen); \
jbe@252 1821 if (func(tlsconf->config, (void *)value, valuelen)) { \
jbe@243 1822 lua_pushnil(L); \
jbe@243 1823 lua_pushliteral(L, "Could not set " name); \
jbe@243 1824 return 2; \
jbe@243 1825 } \
jbe@243 1826 } \
jbe@243 1827 lua_pop(L, 1);
jbe@243 1828
jbe@243 1829 static int moonbr_io_tlsconf(lua_State *L) {
jbe@252 1830 moonbr_io_tlsconf_t *tlsconf;
jbe@243 1831 int valuetype;
jbe@243 1832 const char *value;
jbe@243 1833 size_t valuelen;
jbe@243 1834 luaL_checktype(L, 1, LUA_TTABLE);
jbe@252 1835 tlsconf = lua_newuserdata(L, sizeof(moonbr_io_tlsconf_t));
jbe@252 1836 tlsconf->config = tls_config_new();
jbe@252 1837 if (!tlsconf->config) {
jbe@243 1838 return luaL_error(L, "Could not allocate memory for TLS configuration");
jbe@243 1839 }
jbe@243 1840 luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@245 1841 lua_getfield(L, 1, "mode");
jbe@252 1842 value = lua_tostring(L, -1);
jbe@252 1843 if (value && !strcmp(value, "server")) tlsconf->server = 1;
jbe@252 1844 else if (value && !strcmp(value, "client")) tlsconf->server = 0;
jbe@252 1845 else luaL_argcheck(L, 0, 1, "field \"mode\" must be set to \"server\" or \"client\"");
jbe@245 1846 lua_pop(L, 1);
jbe@243 1847 moonbr_io_tlsconf_string("CA file", "ca_file", tls_config_set_ca_file);
jbe@243 1848 moonbr_io_tlsconf_string("CA path", "ca_path", tls_config_set_ca_path);
jbe@243 1849 moonbr_io_tlsconf_binary("CA", "ca_mem", tls_config_set_ca_mem);
jbe@243 1850 moonbr_io_tlsconf_string("certificate file", "cert_file", tls_config_set_cert_file);
jbe@243 1851 moonbr_io_tlsconf_binary("certificate", "cert_mem", tls_config_set_cert_mem);
jbe@243 1852 moonbr_io_tlsconf_string("key file", "key_file", tls_config_set_key_file);
jbe@243 1853 moonbr_io_tlsconf_binary("key", "key_mem", tls_config_set_key_mem);
jbe@245 1854 #if LUA_VERSION_NUM >= 503
jbe@245 1855 valuetype = lua_getfield(L, 1, "verify_client");
jbe@245 1856 #else
jbe@244 1857 lua_getfield(L, 1, "verify_client");
jbe@245 1858 #endif
jbe@244 1859 if (lua_toboolean(L, -1)) {
jbe@252 1860 value = lua_tostring(L, -1);
jbe@252 1861 if (value && !strcmp(value, "required")) {
jbe@252 1862 tls_config_verify_client(tlsconf->config);
jbe@252 1863 } else if (value && !strcmp(value, "optional")) {
jbe@252 1864 tls_config_verify_client_optional(tlsconf->config);
jbe@244 1865 } else {
jbe@244 1866 luaL_argcheck(L, 0, 1, "field \"verify_client\" must be set to \"required\", \"optional\", or be false or nil");
jbe@244 1867 }
jbe@244 1868 }
jbe@244 1869 lua_pop(L, 1);
jbe@254 1870 // TODO: configurable legacy support
jbe@254 1871 // tls_config_set_protocols(tlsconf->config, TLS_PROTOCOLS_ALL);
jbe@254 1872 // tls_config_set_ciphers(tlsconf->config, "legacy");
jbe@243 1873 return 1;
jbe@243 1874 }
jbe@243 1875
jbe@252 1876 static int moonbr_io_tlsconfgc(lua_State *L) {
jbe@252 1877 moonbr_io_tlsconf_t *tlsconf;
jbe@243 1878 tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@252 1879 if (tlsconf->config) tls_config_free(tlsconf->config);
jbe@252 1880 tlsconf->config = NULL;
jbe@243 1881 return 0;
jbe@243 1882 }
jbe@243 1883
jbe@246 1884 static int moonbr_io_starttls(lua_State *L) {
jbe@246 1885 moonbr_io_handle_t *handle;
jbe@252 1886 moonbr_io_tlsconf_t *tlsconf;
jbe@246 1887 const char *servername;
jbe@246 1888 struct tls *tls, *tls2;
jbe@246 1889 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@246 1890 if (lua_type(L, 2) == LUA_TTABLE) {
jbe@246 1891 lua_pushcfunction(L, moonbr_io_tlsconf);
jbe@246 1892 lua_pushvalue(L, 2);
jbe@246 1893 lua_call(L, 1, 2);
jbe@246 1894 if (lua_isnil(L, -2)) return 2;
jbe@246 1895 lua_pop(L, 1);
jbe@246 1896 lua_replace(L, 2);
jbe@246 1897 }
jbe@246 1898 tlsconf = luaL_checkudata(L, 2, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@246 1899 if (handle->closed) return luaL_error(L, "Attempt to start TLS on a closed I/O handle");
jbe@246 1900 if (handle->finished) return luaL_error(L, "Attempt to start TLS on a finished I/O handle");
jbe@247 1901 if (handle->tls) return luaL_error(L, "Attempt to start TLS twice");
jbe@246 1902 if (handle->readbufin || handle->writebufin) {
jbe@246 1903 return luaL_error(L, "Attempt to start TLS on an I/O handle with non-empty buffers");
jbe@246 1904 }
jbe@252 1905 if (tlsconf->server) tls = tls_server();
jbe@252 1906 else {
jbe@246 1907 servername = luaL_checkstring(L, 3);
jbe@246 1908 tls = tls_client();
jbe@246 1909 }
jbe@246 1910 if (!tls) {
jbe@246 1911 return luaL_error(L, "Could not allocate memory for TLS context");
jbe@246 1912 }
jbe@252 1913 if (tls_configure(tls, tlsconf->config)) goto moonbr_io_starttls_error;
jbe@252 1914 if (tlsconf->server) {
jbe@246 1915 if (tls_accept_socket(tls, &tls2, handle->fd)) goto moonbr_io_starttls_error;
jbe@246 1916 handle->servertls = tls;
jbe@246 1917 handle->tls = tls2;
jbe@246 1918 } else {
jbe@246 1919 if (tls_connect_socket(tls, handle->fd, servername)) goto moonbr_io_starttls_error;
jbe@246 1920 handle->tls = tls;
jbe@246 1921 }
jbe@246 1922 lua_settop(L, 1);
jbe@246 1923 return 1;
jbe@246 1924 moonbr_io_starttls_error:
jbe@246 1925 lua_pushnil(L);
jbe@246 1926 lua_pushstring(L, tls_error(tls));
jbe@246 1927 tls_free(tls);
jbe@246 1928 return 2;
jbe@246 1929 }
jbe@246 1930
jbe@258 1931 #endif /* MOONBR_IO_USE_TLS */
jbe@243 1932
jbe@79 1933 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 1934 {"read", moonbr_io_read},
jbe@85 1935 {"read_nb", moonbr_io_read_nb},
jbe@140 1936 {"read_call", moonbr_io_read_call},
jbe@140 1937 {"read_yield", moonbr_io_read_yield},
jbe@86 1938 {"drain", moonbr_io_drain},
jbe@86 1939 {"drain_nb", moonbr_io_drain_nb},
jbe@144 1940 {"drain_call", moonbr_io_drain_call},
jbe@144 1941 {"drain_yield", moonbr_io_drain_yield},
jbe@80 1942 {"write", moonbr_io_write},
jbe@81 1943 {"write_nb", moonbr_io_write_nb},
jbe@145 1944 {"write_call", moonbr_io_write_call},
jbe@145 1945 {"write_yield", moonbr_io_write_yield},
jbe@80 1946 {"flush", moonbr_io_flush},
jbe@81 1947 {"flush_nb", moonbr_io_flush_nb},
jbe@145 1948 {"flush_call", moonbr_io_flush_call},
jbe@145 1949 {"flush_yield", moonbr_io_flush_yield},
jbe@88 1950 {"finish", moonbr_io_finish},
jbe@87 1951 {"close", moonbr_io_close},
jbe@85 1952 {"reset", moonbr_io_reset},
jbe@246 1953 #ifdef MOONBR_IO_USE_TLS
jbe@246 1954 {"starttls", moonbr_io_starttls},
jbe@246 1955 #endif
jbe@79 1956 {NULL, NULL}
jbe@79 1957 };
jbe@79 1958
jbe@79 1959 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 1960 {"__index", moonbr_io_handleindex},
jbe@79 1961 {"__newindex", moonbr_io_handlenewindex},
jbe@108 1962 {"__gc", moonbr_io_handlegc},
jbe@108 1963 {NULL, NULL}
jbe@108 1964 };
jbe@108 1965
jbe@108 1966 static const struct luaL_Reg moonbr_io_listener_methods[] = {
jbe@108 1967 {"accept", moonbr_io_accept},
jbe@108 1968 {"accept_nb", moonbr_io_accept_nb},
jbe@108 1969 {"close", moonbr_io_unlisten},
jbe@108 1970 {NULL, NULL}
jbe@108 1971 };
jbe@108 1972
jbe@108 1973 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
jbe@108 1974 {"__gc", moonbr_io_listenergc},
jbe@79 1975 {NULL, NULL}
jbe@79 1976 };
jbe@79 1977
jbe@205 1978 static const struct luaL_Reg moonbr_io_child_methods[] = {
jbe@205 1979 {"kill", moonbr_io_kill},
jbe@205 1980 {"wait", moonbr_io_wait},
jbe@205 1981 {"wait_nb", moonbr_io_wait_nb},
jbe@205 1982 {"wait_call", moonbr_io_wait_call},
jbe@205 1983 {"wait_yield", moonbr_io_wait_yield},
jbe@205 1984 {NULL, NULL}
jbe@205 1985 };
jbe@205 1986
jbe@205 1987 static const struct luaL_Reg moonbr_io_child_metamethods[] = {
jbe@205 1988 {"__index", moonbr_io_childindex},
jbe@205 1989 {"__newindex", moonbr_io_childnewindex},
jbe@205 1990 {"__gc", moonbr_io_childgc},
jbe@205 1991 {NULL, NULL}
jbe@205 1992 };
jbe@205 1993
jbe@79 1994 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@111 1995 {"localconnect", moonbr_io_localconnect},
jbe@111 1996 {"localconnect_nb", moonbr_io_localconnect_nb},
jbe@98 1997 {"tcpconnect", moonbr_io_tcpconnect},
jbe@99 1998 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
jbe@112 1999 {"locallisten", moonbr_io_locallisten},
jbe@108 2000 {"tcplisten", moonbr_io_tcplisten},
jbe@205 2001 {"exec", moonbr_io_exec},
jbe@266 2002 {"signalsocket", moonbr_io_signalsocket},
jbe@266 2003 {"getpid", moonbr_io_getpid},
jbe@106 2004 {"poll", moonbr_io_poll},
jbe@115 2005 {"timeref", moonbr_io_timeref},
jbe@243 2006 #ifdef MOONBR_IO_USE_TLS
jbe@243 2007 {"tlsconf", moonbr_io_tlsconf},
jbe@243 2008 #endif
jbe@79 2009 {NULL, NULL}
jbe@79 2010 };
jbe@79 2011
jbe@243 2012 #ifdef MOONBR_IO_USE_TLS
jbe@258 2013
jbe@243 2014 static const struct luaL_Reg moonbr_io_tlsconf_metamethods[] = {
jbe@243 2015 {"__gc", moonbr_io_tlsconfgc},
jbe@243 2016 {NULL, NULL}
jbe@243 2017 };
jbe@258 2018
jbe@258 2019 #endif /* MOONBR_IO_USE_TLS */
jbe@243 2020
jbe@79 2021 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 2022
jbe@113 2023 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
jbe@266 2024 moonbr_io_signalfd_read = -1;
jbe@266 2025 moonbr_io_signalfd_write = -1;
jbe@113 2026
jbe@80 2027 lua_newtable(L); // module
jbe@80 2028
jbe@79 2029 lua_newtable(L); // public metatable
jbe@79 2030 lua_newtable(L); // handle methods
jbe@79 2031 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 2032 lua_pushvalue(L, -1);
jbe@146 2033 lua_setfield(L, -4, "handle_pt");
jbe@79 2034 lua_setfield(L, -2, "__index");
jbe@79 2035 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 2036
jbe@79 2037 lua_newtable(L); // handle metatable
jbe@79 2038 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@146 2039 lua_pushvalue(L, -1);
jbe@146 2040 lua_setfield(L, -3, "handle_mt");
jbe@79 2041 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 2042
jbe@108 2043 lua_newtable(L); // listener metatable
jbe@108 2044 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
jbe@108 2045 lua_newtable(L); // listener methods
jbe@108 2046 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
jbe@108 2047 lua_pushvalue(L, -1);
jbe@146 2048 lua_setfield(L, -4, "listener_pt");
jbe@108 2049 lua_setfield(L, -2, "__index");
jbe@146 2050 lua_pushvalue(L, -1);
jbe@146 2051 lua_setfield(L, -3, "listener_mt");
jbe@108 2052 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 2053
jbe@205 2054 lua_newtable(L); // child methods
jbe@205 2055 luaL_setfuncs(L, moonbr_io_child_methods, 0);
jbe@205 2056 lua_pushvalue(L, -1);
jbe@205 2057 lua_setfield(L, -3, "child_pt");
jbe@205 2058 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_PT_REGKEY);
jbe@205 2059 lua_newtable(L); // child metatable
jbe@205 2060 luaL_setfuncs(L, moonbr_io_child_metamethods, 0);
jbe@205 2061 lua_pushvalue(L, -1);
jbe@205 2062 lua_setfield(L, -3, "child_mt");
jbe@205 2063 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 2064
jbe@243 2065 #ifdef MOONBR_IO_USE_TLS
jbe@243 2066 if(tls_init()) {
jbe@243 2067 return luaL_error(L, "Could not initialize TLS library");
jbe@243 2068 }
jbe@243 2069 lua_newtable(L); // tlsconf metatable
jbe@243 2070 luaL_setfuncs(L, moonbr_io_tlsconf_metamethods, 0);
jbe@243 2071 lua_pushvalue(L, -1);
jbe@243 2072 lua_setfield(L, -3, "tlsconf_mt");
jbe@243 2073 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@243 2074 #endif
jbe@243 2075
jbe@205 2076 moonbr_io_pushhandle(L, 0);
jbe@205 2077 lua_setfield(L, -2, "stdin");
jbe@205 2078 moonbr_io_pushhandle(L, 1);
jbe@205 2079 lua_setfield(L, -2, "stdout");
jbe@205 2080 moonbr_io_pushhandle(L, 2);
jbe@205 2081 lua_setfield(L, -2, "stderr");
jbe@205 2082
jbe@79 2083 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 2084 return 1;
jbe@79 2085
jbe@79 2086 }
jbe@79 2087

Impressum / About Us