diff options
Diffstat (limited to 'luxio.c')
-rw-r--r-- | luxio.c | 204 |
1 files changed, 200 insertions, 4 deletions
@@ -1720,7 +1720,194 @@ luxio_nanosleep(lua_State *L) /* 14.2.5 */ /* 15 Message passing ********************************************************/ -#include "luxio_mq.c" +#ifdef _POSIX_MESSAGE_PASSING + +#include <mqueue.h> + +static int +luxio_mq_open(lua_State *L) /* 15.2.1 */ +{ + const char *name = luaL_checkstring(L, 1); + int oflag = luaL_checkinteger(L, 2); + mode_t mode = luaL_optinteger(L, 3, INVALID_MODE); + mqd_t mq; + + if ((oflag & O_CREAT) && mode == INVALID_MODE) { + lua_pushstring(L, "mq_open with O_CREATE called with no mode"); + lua_error(L); + } + + if (oflag & O_CREAT) { + mq = mq_open(name, oflag, mode, NULL); + } else { + mq = mq_open(name, oflag); + } + + lua_pushinteger(L, mq); + lua_pushinteger(L, errno); + + return 2; +} + +static int +luxio_mq_close(lua_State *L) /* 15.2.2 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + + lua_pushinteger(L, mq_close(mq)); + lua_pushinteger(L, errno); + return 2; +} + +static int +luxio_mq_unlink(lua_State *L) /* 15.2.3 */ +{ + const char *name = luaL_checkstring(L, 1); + + lua_pushinteger(L, mq_unlink(name)); + lua_pushinteger(L, errno); + return 2; +} + +static int +luxio_mq_send(lua_State *L) /* 15.2.4 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + size_t msg_len; + const char *msg_ptr = luaL_checklstring(L, 2, &msg_len); + unsigned int msg_prio = luaL_checkinteger(L, 3); + + lua_pushinteger(L, mq_send(mq, msg_ptr, msg_len, msg_prio)); + lua_pushinteger(L, errno); + return 2; +} + +static int +luxio_mq_receive(lua_State *L) /* 15.2.5 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + unsigned int msg_prio; + struct mq_attr attr; + + /* Find out the maximum size of a message */ + if (mq_getattr(mq, &attr) == -1) { + lua_pushinteger(L, -1); + lua_pushinteger(L, errno); + return 2; + } else { + char msg_ptr[attr.mq_msgsize]; + int r = mq_receive(mq, msg_ptr, sizeof(msg_ptr), &msg_prio); + lua_pushinteger(L, r); + lua_pushinteger(L, errno); + if (r == -1) { + return 2; + } + lua_pushlstring(L, msg_ptr, r); + lua_pushinteger(L, msg_prio); + return 4; + } +} + +/* TODO: mq_notify() 15.2.6 */ + +static int +luxio_make_attr_table(lua_State *L, struct mq_attr *attr) +{ + int top = lua_gettop(L) + 1; + + lua_createtable(L, 0, 4); + +#define PUSH_ENTRY(e) do { lua_pushstring(L, "mq_" #e); \ + lua_pushinteger(L, attr->mq_##e); \ + lua_settable(L, top); } while (0) + + PUSH_ENTRY(flags); + PUSH_ENTRY(maxmsg); + PUSH_ENTRY(msgsize); + PUSH_ENTRY(curmsgs); + +#undef PUSH_ENTRY + + return 1; +} + +static int +luxio_mq_setattr(lua_State *L) /* 15.2.7 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + struct mq_attr mqstat = { luaL_checkinteger(L, 2), 0, 0, 0 }; + struct mq_attr omqstat = { 0, 0, 0, 0 }; + + lua_pushinteger(L, mq_setattr(mq, &mqstat, &omqstat)); + lua_pushinteger(L, errno); + luxio_make_attr_table(L, &omqstat); + + return 3; +} + +static int +luxio_mq_getattr(lua_State *L) /* 15.2.8 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + struct mq_attr mqstat; + + lua_pushinteger(L, mq_getattr(mq, &mqstat)); + lua_pushinteger(L, errno); + luxio_make_attr_table(L, &mqstat); + + return 3; +} + +#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L + +static int +luxio_mq_timedsend(lua_State *L) /* POSIX.1-2001 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + size_t msg_len; + const char *msg_ptr = luaL_checklstring(L, 2, &msg_len); + unsigned int msg_prio = luaL_checkinteger(L, 3); + time_t tv_secs = luaL_checkinteger(L, 4); + long tv_nsec = luaL_checkinteger(L, 5); + struct timespec abs_timeout = { tv_secs, tv_nsec }; + + lua_pushinteger(L, mq_timedsend(mq, msg_ptr, msg_len, msg_prio, + &abs_timeout)); + lua_pushinteger(L, errno); + return 2; +} + +static int +luxio_mq_timedreceive(lua_State *L) /* POSIX.1-2001 */ +{ + mqd_t mq = luaL_checkinteger(L, 1); + unsigned int msg_prio; + struct mq_attr attr; + time_t tv_secs = luaL_checkinteger(L, 2); + long tv_nsec = luaL_checkinteger(L, 3); + struct timespec abs_timeout = { tv_secs, tv_nsec }; + + /* Find out the maximum size of a message */ + if (mq_getattr(mq, &attr) == -1) { + lua_pushinteger(L, -1); + lua_pushinteger(L, errno); + return 2; + } else { + char msg_ptr[attr.mq_msgsize]; + int r = mq_timedreceive(mq, msg_ptr, sizeof(msg_ptr), &msg_prio, + &abs_timeout); + lua_pushinteger(L, r); + lua_pushinteger(L, errno); + if (r == -1) { + return 2; + } + lua_pushlstring(L, msg_ptr, r); + lua_pushinteger(L, msg_prio); + return 4; + } +} +#endif /* POSIX.1-2001 check */ +#endif /* _POSIX_MESSAGE_PASSING */ /* 16 Thread management ******************************************************/ @@ -2746,9 +2933,18 @@ luxio_functions[] = { { "nanosleep", luxio_nanosleep }, - LUXIO_MQ_FUNCS - LUXIO_MQ_FUNCS_2001 - +#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L + { "mq_timedsend", luxio_mq_timedsend }, + { "mq_timedreceive", luxio_mq_timedreceive }, +#endif + { "mq_open", luxio_mq_open }, + { "mq_close", luxio_mq_close }, + { "mq_unlink", luxio_mq_unlink }, + { "mq_send", luxio_mq_send }, + { "mq_receive", luxio_mq_receive }, + { "mq_setattr", luxio_mq_setattr }, + { "mq_getattr", luxio_mq_getattr }, + { NULL, NULL } }; |