From 34a0dd6d93023de93ba29743f16379211d4aad75 Mon Sep 17 00:00:00 2001 From: "Rob Kendrick (monotony)" Date: Sun, 27 May 2012 15:13:21 +0100 Subject: Import MQ stuff directly into Luxio --- Makefile | 4 +- luxio.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- luxio_mq.c | 222 ------------------------------------------------------------- 3 files changed, 202 insertions(+), 228 deletions(-) delete mode 100644 luxio_mq.c diff --git a/Makefile b/Makefile index d02f7d4..0ca82ca 100644 --- a/Makefile +++ b/Makefile @@ -85,8 +85,8 @@ lua-5.2-install: lua-5.2 luxio-5.2.so: luxio-5.2.o $(CC) -shared -Wl,--no-as-needed -o luxio-5.2.so $(LUA52_LIB) -lrt $^ -luxio-5.1.o: luxio.c luxio_constants.h luxio_mq.c +luxio-5.1.o: luxio.c luxio_constants.h $(CC) $(CFLAGS) -fPIC $(LUA51_INC) -c $< -o $@ -luxio-5.2.o: luxio.c luxio_constants.h luxio_mq.c +luxio-5.2.o: luxio.c luxio_constants.h $(CC) $(CFLAGS) -fPIC $(LUA52_INC) -c $< -o $@ diff --git a/luxio.c b/luxio.c index 8fa5f3d..a87832d 100644 --- a/luxio.c +++ b/luxio.c @@ -1720,7 +1720,194 @@ luxio_nanosleep(lua_State *L) /* 14.2.5 */ /* 15 Message passing ********************************************************/ -#include "luxio_mq.c" +#ifdef _POSIX_MESSAGE_PASSING + +#include + +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 } }; 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 - * - * 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 - -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 */ - -- cgit v1.2.1