summaryrefslogtreecommitdiff
path: root/nopsyncd/l_pipe.c
diff options
context:
space:
mode:
Diffstat (limited to 'nopsyncd/l_pipe.c')
-rw-r--r--nopsyncd/l_pipe.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/nopsyncd/l_pipe.c b/nopsyncd/l_pipe.c
index 5a99809..6b769a5 100644
--- a/nopsyncd/l_pipe.c
+++ b/nopsyncd/l_pipe.c
@@ -43,16 +43,19 @@ void pipe_close()
static int l_pipe_signal(lua_State *L)
{
- char data = 1;
- int ret = 0;
+ size_t len = 0, written = 0;
+ const char* data = luaL_checklstring(L, 1, &len);
+ len++; // also send trailing zero
+ int ret = 0;
for(;;) {
- ret = write(pipefds_[1], &data, 1);
- if(ret == 1) break;
-
+ ret = write(pipefds_[1], &(data[written]), len - written);
if(!ret) continue;
if(ret == -1 && (errno == EAGAIN || errno == EINTR)) continue;
+ written += ret;
+ if(written == len) break;
+
break;
}
@@ -93,19 +96,30 @@ static int l_pipe_getreadfd(lua_State *L)
static int l_pipe_consume(lua_State *L)
{
- char data;
- int ret = 0;
+ char data[17]; // 17 should be sufficient
+ size_t len = 0;
+ int ret = 0;
for(;;) {
- ret = read(pipefds_[0], &data, 1);
- if(ret == 1 || ret == 0) break;
-
+ ret = read(pipefds_[0], &(data[len]), 1);
+ if(ret == 0) break;
if(ret == -1 && (errno == EAGAIN || errno == EINTR)) continue;
- break;
+ if(data[len] == 0)
+ break;
+
+ len += ret;
+ if(len >= sizeof(data)) {
+ ret = 0;
+ break;
+ }
}
- lua_pushinteger(L, ret);
+ if(ret)
+ lua_pushstring(L, data);
+ else
+ lua_pushnil(L);
+
return 1;
}