moonbridge

view moonbridge_io.c @ 94:de3982f17d05

Removed timeout option from socket:close(); Simulate shutdown for local sockets (Unix Domain Sockets)
author jbe
date Tue Apr 07 22:17:13 2015 +0200 (2015-04-07)
parents 111b2469b753
children 719f83c7fea4
line source
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/socket.h>
8 #include <sys/select.h>
9 #include <fcntl.h>
11 #include <lua.h>
12 #include <lauxlib.h>
13 #include <lualib.h>
15 #define MOONBR_IO_MAXSTRERRORLEN 80
16 #define MOONBR_IO_READBUFLEN 4096
17 #define MOONBR_IO_WRITEBUFLEN 4096
19 #define moonbr_io_errmsg() \
20 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
21 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
23 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
24 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
26 typedef struct {
27 int fd;
28 int useshutdown;
29 int finished;
30 int closed;
31 int nonblocking;
32 int readerr;
33 int readbufcnt;
34 int writeerr;
35 size_t writeleft;
36 #if LUA_VERSION_NUM >= 503
37 lua_Integer writeqin;
38 lua_Integer writeqout;
39 #else
40 int writeqin;
41 int writeqout;
42 #endif
43 size_t writeqoff;
44 int writebufcnt;
45 char readbuf[MOONBR_IO_READBUFLEN];
46 char writebuf[MOONBR_IO_WRITEBUFLEN];
47 } moonbr_io_handle_t;
49 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
50 if (handle->nonblocking != nonblocking) {
51 int flags;
52 flags = fcntl(handle->fd, F_GETFL, 0);
53 if (flags == -1) {
54 moonbr_io_errmsg();
55 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
56 }
57 if (nonblocking) flags |= O_NONBLOCK;
58 else flags &= ~O_NONBLOCK;
59 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
60 moonbr_io_errmsg();
61 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
62 }
63 }
64 }
66 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
67 struct linger lingerval = { 0, };
68 if (!handle->useshutdown) return;
69 if (timeout >= 0) {
70 lingerval.l_onoff = 1;
71 lingerval.l_linger = timeout;
72 }
73 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
74 moonbr_io_errmsg();
75 luaL_error(L, "Unexpected error in setsockopt call: %s", errmsg);
76 }
77 }
79 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
80 moonbr_io_handle_t *handle;
81 lua_Integer maxread;
82 const char *terminatorstr;
83 size_t terminatorlen;
84 char terminator;
85 luaL_Buffer luabuf;
86 size_t luabufcnt = 0;
87 int endcnt;
88 char *terminatorpos;
89 ssize_t result;
90 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
91 maxread = luaL_optinteger(L, 2, 0);
92 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
93 if (terminatorlen) {
94 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
95 terminator = terminatorstr[0];
96 }
97 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
98 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
99 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
100 if (handle->readerr) {
101 lua_pushnil(L);
102 lua_pushliteral(L, "Previous read error");
103 return 2;
104 }
105 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
106 if (!drain) luaL_buffinit(L, &luabuf);
107 while (1) {
108 endcnt = -1;
109 if (maxread > 0 && handle->readbufcnt >= maxread - luabufcnt) {
110 endcnt = maxread - luabufcnt;
111 } else if (terminatorlen) {
112 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
113 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
114 }
115 if (endcnt >= 0) {
116 if (!drain) {
117 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
118 luaL_pushresult(&luabuf);
119 } else {
120 luabufcnt += handle->readbufcnt;
121 lua_pushinteger(L, luabufcnt);
122 }
123 handle->readbufcnt -= endcnt;
124 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
125 return 1;
126 }
127 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
128 luabufcnt += handle->readbufcnt;
129 handle->readbufcnt = 0;
130 do {
131 result = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
132 } while (result < 0 && (errno == EINTR));
133 if (result == 0 || (nonblocking && result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
134 if (result < 0) {
135 moonbr_io_errmsg();
136 handle->readerr = 1;
137 lua_pushnil(L);
138 lua_pushstring(L, errmsg);
139 return 2;
140 }
141 handle->readbufcnt += result;
142 }
143 if (!drain) {
144 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
145 luaL_pushresult(&luabuf);
146 }
147 luabufcnt += handle->readbufcnt;
148 handle->readbufcnt = 0;
149 if (!drain) {
150 if (!luabufcnt && result == 0) {
151 moonbr_io_read_impl_eof:
152 lua_pushboolean(L, 0);
153 lua_pushliteral(L, "End of file");
154 return 2;
155 }
156 } else {
157 if (!luabufcnt && result == 0) lua_pushboolean(L, 1);
158 else lua_pushboolean(L, luabufcnt);
159 }
160 return 1;
161 }
163 static int moonbr_io_read(lua_State *L) {
164 return moonbr_io_read_impl(L, 0, 0);
165 }
167 static int moonbr_io_read_nb(lua_State *L) {
168 return moonbr_io_read_impl(L, 1, 0);
169 }
171 static int moonbr_io_drain(lua_State *L) {
172 return moonbr_io_read_impl(L, 0, 1);
173 }
175 static int moonbr_io_drain_nb(lua_State *L) {
176 return moonbr_io_read_impl(L, 1, 1);
177 }
179 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
180 moonbr_io_handle_t *handle;
181 int i, top;
182 const char *str;
183 size_t strlen;
184 size_t written;
185 ssize_t result;
186 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
187 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
188 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
189 if (handle->writeerr) {
190 lua_pushnil(L);
191 lua_pushliteral(L, "Previous write error");
192 return 2;
193 }
194 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
195 top = lua_gettop(L);
196 lua_getuservalue(L, 1);
197 lua_getfield(L, -1, "writebuf");
198 for (i=2; i<=top; i++) {
199 luaL_checklstring(L, i, &strlen);
200 lua_pushvalue(L, i);
201 lua_rawseti(L, -2, handle->writeqin++);
202 handle->writeleft += strlen;
203 }
204 while (handle->writeqout != handle->writeqin) {
205 lua_rawgeti(L, -1, handle->writeqout);
206 str = lua_tolstring(L, -1, &strlen);
207 while (handle->writeqoff < strlen) {
208 if (strlen - handle->writeqoff < MOONBR_IO_WRITEBUFLEN - handle->writebufcnt) {
209 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, strlen - handle->writeqoff);
210 handle->writebufcnt += strlen - handle->writeqoff;
211 break;
212 } else {
213 written = 0;
214 memcpy(handle->writebuf + handle->writebufcnt, str + handle->writeqoff, MOONBR_IO_WRITEBUFLEN - handle->writebufcnt);
215 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufcnt;
216 while (written < MOONBR_IO_WRITEBUFLEN) {
217 result = write(handle->fd, handle->writebuf + written, MOONBR_IO_WRITEBUFLEN - written);
218 if (result < 0) {
219 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
220 if (written) {
221 handle->writebufcnt -= written;
222 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
223 goto moonbr_io_write_impl_continue;
224 }
225 goto moonbr_io_write_impl_block;
226 } else if (errno != EINTR) {
227 moonbr_io_errmsg();
228 handle->writeerr = 1;
229 lua_pushnil(L);
230 lua_pushstring(L, errmsg);
231 return 2;
232 }
233 }
234 written += result;
235 handle->writeleft -= result;
236 }
237 handle->writebufcnt = 0;
238 }
239 moonbr_io_write_impl_continue:;
240 }
241 handle->writeqoff = 0;
242 lua_pop(L, 1);
243 lua_pushnil(L);
244 lua_rawseti(L, -2, handle->writeqout++);
245 }
246 if (flush) {
247 written = 0;
248 while (written < handle->writebufcnt) {
249 result = write(handle->fd, handle->writebuf + written, handle->writebufcnt - written);
250 if (result < 0) {
251 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
252 if (written) {
253 handle->writebufcnt -= written;
254 memmove(handle->writebuf, handle->writebuf + written, handle->writebufcnt);
255 }
256 goto moonbr_io_write_impl_block;
257 } else if (errno != EINTR) {
258 moonbr_io_errmsg();
259 handle->writeerr = -1;
260 lua_pushnil(L);
261 lua_pushstring(L, errmsg);
262 return 2;
263 }
264 } else {
265 written += result;
266 handle->writeleft -= result;
267 }
268 }
269 handle->writebufcnt = 0;
270 if (nonblocking) lua_pushinteger(L, 0);
271 } else {
272 if (nonblocking) lua_pushinteger(L, handle->writeleft);
273 }
274 if (!nonblocking) lua_pushvalue(L, 1);
275 return 1;
276 moonbr_io_write_impl_block:
277 lua_pushinteger(L, handle->writeleft);
278 return 1;
279 }
281 static int moonbr_io_write(lua_State *L) {
282 return moonbr_io_write_impl(L, 0, 0);
283 }
285 static int moonbr_io_write_nb(lua_State *L) {
286 return moonbr_io_write_impl(L, 1, 0);
287 }
289 static int moonbr_io_flush(lua_State *L) {
290 return moonbr_io_write_impl(L, 0, 1);
291 }
293 static int moonbr_io_flush_nb(lua_State *L) {
294 return moonbr_io_write_impl(L, 1, 1);
295 }
297 static int moonbr_io_finish(lua_State *L) {
298 moonbr_io_handle_t *handle;
299 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
300 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
301 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
302 if (handle->writeleft) {
303 lua_pushcfunction(L, moonbr_io_flush);
304 lua_pushvalue(L, 1);
305 lua_call(L, 1, 2);
306 if (!lua_toboolean(L, -2)) {
307 handle->finished = 1;
308 return 2;
309 }
310 }
311 handle->finished = 1;
312 if (handle->useshutdown) {
313 if (shutdown(handle->fd, SHUT_WR)) {
314 moonbr_io_errmsg();
315 lua_pushnil(L);
316 lua_pushstring(L, errmsg);
317 return 2;
318 }
319 } else {
320 if (close(handle->fd)) {
321 moonbr_io_errmsg();
322 handle->fd = -1;
323 lua_pushnil(L);
324 lua_pushstring(L, errmsg);
325 return 2;
326 }
327 handle->fd = -1; /* fake EOF on read */
328 }
329 lua_pushboolean(L, 1);
330 return 1;
331 }
333 static int moonbr_io_close_impl(lua_State *L, int reset) {
334 moonbr_io_handle_t *handle;
335 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
336 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
337 if (!reset) {
338 if (handle->writeleft) {
339 lua_pushcfunction(L, moonbr_io_flush);
340 lua_pushvalue(L, 1);
341 lua_call(L, 1, 2);
342 if (!lua_toboolean(L, -2)) {
343 close(handle->fd);
344 handle->fd = -1;
345 return 2;
346 }
347 }
348 moonbr_io_handle_set_linger(L, handle, -1);
349 }
350 if (handle->fd >= 0) {
351 if (close(handle->fd)) {
352 moonbr_io_errmsg();
353 handle->fd = -1;
354 lua_pushnil(L);
355 lua_pushstring(L, errmsg);
356 return 2;
357 }
358 }
359 handle->fd = -1;
360 lua_pushboolean(L, 1);
361 return 1;
363 }
365 static int moonbr_io_close(lua_State *L) {
366 return moonbr_io_close_impl(L, 0);
367 }
369 static int moonbr_io_reset(lua_State *L) {
370 return moonbr_io_close_impl(L, 1);
371 }
373 static int moonbr_io_gc(lua_State *L) {
374 moonbr_io_handle_t *handle;
375 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
376 if (handle->fd >= 0) {
377 lua_pushcfunction(L, moonbr_io_close);
378 lua_pushvalue(L, 1);
379 lua_pushinteger(L, 0);
380 lua_call(L, 2, 0);
381 }
382 return 0;
383 }
385 void moonbr_io_closehandle(lua_State *L, int idx, int timeout) {
386 moonbr_io_handle_t *handle;
387 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
388 if (handle->fd >= 0) {
389 lua_pushcfunction(L, moonbr_io_close);
390 lua_pushvalue(L, idx);
391 lua_pushinteger(L, timeout);
392 lua_call(L, 2, 0);
393 }
394 }
396 void moonbr_io_pushhandle(lua_State *L, int fd, int useshutdown) {
397 moonbr_io_handle_t *handle;
398 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
399 handle->fd = fd;
400 handle->useshutdown = useshutdown;
401 handle->finished = 0;
402 handle->closed = 0;
403 handle->nonblocking = -1;
404 handle->readerr = 0;
405 handle->readbufcnt = 0;
406 handle->writeerr = 0;
407 handle->writeleft = 0;
408 handle->writeqin = 0;
409 handle->writeqout = 0;
410 handle->writeqoff = 0;
411 handle->writebufcnt = 0;
412 moonbr_io_handle_set_linger(L, handle, 0);
413 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
414 lua_setmetatable(L, -2);
415 lua_newtable(L); // uservalue
416 lua_newtable(L);
417 lua_setfield(L, -2, "writebuf");
418 lua_newtable(L); // public
419 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
420 lua_setmetatable(L, -2);
421 lua_setfield(L, -2, "public");
422 lua_setuservalue(L, -2);
423 }
425 static int moonbr_io_handleindex(lua_State *L) {
426 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
427 lua_getuservalue(L, 1);
428 lua_getfield(L, -1, "public");
429 lua_pushvalue(L, 2);
430 lua_gettable(L, -2);
431 return 1;
432 }
434 static int moonbr_io_handlenewindex(lua_State *L) {
435 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
436 lua_getuservalue(L, 1);
437 lua_getfield(L, -1, "public");
438 lua_pushvalue(L, 2);
439 lua_pushvalue(L, 3);
440 lua_settable(L, -3);
441 return 0;
442 }
444 static const struct luaL_Reg moonbr_io_handle_methods[] = {
445 {"read", moonbr_io_read},
446 {"read_nb", moonbr_io_read_nb},
447 {"drain", moonbr_io_drain},
448 {"drain_nb", moonbr_io_drain_nb},
449 {"write", moonbr_io_write},
450 {"write_nb", moonbr_io_write_nb},
451 {"flush", moonbr_io_flush},
452 {"flush_nb", moonbr_io_flush_nb},
453 {"finish", moonbr_io_finish},
454 {"close", moonbr_io_close},
455 {"reset", moonbr_io_reset},
456 {NULL, NULL}
457 };
459 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
460 {"__index", moonbr_io_handleindex},
461 {"__newindex", moonbr_io_handlenewindex},
462 {"__gc", moonbr_io_gc},
463 {NULL, NULL}
464 };
466 static const struct luaL_Reg moonbr_io_module_funcs[] = {
467 {NULL, NULL}
468 };
470 int luaopen_moonbridge_io(lua_State *L) {
472 lua_newtable(L); // module
474 lua_newtable(L); // public metatable
475 lua_newtable(L); // handle methods
476 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
477 lua_pushvalue(L, -1);
478 lua_setfield(L, -4, "handle");
479 lua_setfield(L, -2, "__index");
480 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
482 lua_newtable(L); // handle metatable
483 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
484 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
486 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
487 return 1;
489 }

Impressum / About Us