moonbridge

annotate moonbridge_io.c @ 208:77dc29ce7948

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

Impressum / About Us