moonbridge

annotate moonbridge_io.c @ 238:73d832c4a9b5

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

Impressum / About Us