diff options
Diffstat (limited to 'luxio_mq.c')
-rw-r--r-- | luxio_mq.c | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/luxio_mq.c b/luxio_mq.c deleted file mode 100644 index 4269237..0000000 --- a/luxio_mq.c +++ /dev/null @@ -1,222 +0,0 @@ -/* Light Unix I/O for Lua - * Copyright 2012 Rob Kendrick <rjek+luxio@rjek.com> - * - * Distributed under the same terms as Lua itself (MIT). - */ - -/* This implementation assumes that an mqd_t is representable as - * a number type. This sucks a bit. - */ - -#ifndef LUXIO_RELEASE -# error This file is not intended to be built stand-alone. -#endif - -#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; - } -} - -#define LUXIO_MQ_FUNCS_2001 \ - { "mq_timedsend", luxio_mq_timedsend }, \ - { "mq_timedreceive", luxio_mq_timedreceive }, - -#else -#define LUXIO_MQ_FUNCS_2001 -#endif /* POSIX.1-2001 check */ - -#define LUXIO_MQ_FUNCS \ - { "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 }, - -#else -# define LUXIO_MQ_FUNCS -#endif /* _POSIX_MESSAGE_PASSING */ - |