From 3041d8a5505221d1db278db63bf3c7f1371a70ce Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 5 Mar 2011 20:54:18 +0000 Subject: thread safe errno handling --- config | 2 +- src/luamq.c | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/config b/config index 1f3bd01..42364d3 100644 --- a/config +++ b/config @@ -19,7 +19,7 @@ V= 0.1 LIBNAME= luamq.so -WARN= -Wall -Wmissing-prototypes -Wmissing-declarations -ansi -pedantic -fPIC +WARN= -Wall -Wmissing-prototypes -Wmissing-declarations -pedantic -fPIC INCS= -I$(LUA_INC) CFLAGS= -O2 $(WARN) $(INCS) CC= gcc diff --git a/src/luamq.c b/src/luamq.c index 28d9e1e..53677fb 100644 --- a/src/luamq.c +++ b/src/luamq.c @@ -3,6 +3,7 @@ #include #include +#define _XOPEN_SOURCE 600 #include #include #include @@ -28,6 +29,18 @@ static int get_oflags(const char* flagstr) return O_RDWR; } +static void push_errno(lua_State *L) +{ + char msg[255]; + int ret; + + ret = strerror_r(errno, msg, sizeof(msg)); + if(!ret) + lua_pushstring(L, msg); + else + lua_pushstring(L, "unknown error"); +} + static int l_luamq_create(lua_State *L) { mqd_t id, *ptr; @@ -39,7 +52,7 @@ static int l_luamq_create(lua_State *L) id = mq_open(luaL_checkstring(L, 1), flags | O_CREAT, mode, NULL); if(id == (mqd_t)-1) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); @@ -57,7 +70,7 @@ static int l_luamq_open(lua_State *L) id = mq_open(luaL_checkstring(L, 1), flags); if(id == (mqd_t)-1) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t)); @@ -79,7 +92,7 @@ static int l_luamq_send(lua_State *L) prio = luaL_optint(L, 3, 0); if(mq_send(*id, str, len, prio)) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } @@ -98,7 +111,7 @@ static int l_luamq_receive(lua_State *L) id = luaL_checkudata(L, 1, MQD_TYPENAME); if(mq_getattr(*id, &attr)) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } @@ -114,7 +127,7 @@ static int l_luamq_receive(lua_State *L) if(len < 0) { free(str); lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } @@ -130,7 +143,7 @@ static int l_luamq_close(lua_State *L) id = luaL_checkudata(L, 1, MQD_TYPENAME); if(mq_close(*id)) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } @@ -142,7 +155,7 @@ static int l_luamq_unlink(lua_State *L) { if(mq_unlink(luaL_checkstring(L, 1))) { lua_pushnil(L); - lua_pushstring(L, strerror(errno)); /* TODO: thread safety */ + push_errno(L); return 2; } -- cgit v1.2.3