summaryrefslogtreecommitdiff
path: root/src/luamq.c
blob: 53677fbdd7e052276d304c59dea915c56d0d2cba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "lua.h"
#include "lauxlib.h"

#include <sys/stat.h>
#include <mqueue.h>
#define _XOPEN_SOURCE 600  
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#define MQD_TYPENAME "mqd_t"

LUALIB_API int luaopen_luamq(lua_State *L);

static mode_t get_mode(const char* modestr)
{
/* TODO: implement this!!! */
  return S_IRWXU | S_IRWXG;
}

static int get_oflags(const char* flagstr)
{
  if(!strcmp(flagstr, "ro"))
    return O_RDONLY;

  if(!strcmp(flagstr, "wo"))
    return O_WRONLY;

  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;
  int flags;
  mode_t mode;

  flags = get_oflags(luaL_optstring(L, 2, ""));
  mode = get_mode(luaL_optstring(L, 3, ""));
  id = mq_open(luaL_checkstring(L, 1), flags | O_CREAT, mode, NULL);
  if(id == (mqd_t)-1) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }
  ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t));
  *ptr = id;
  luaL_newmetatable(L, MQD_TYPENAME);
  lua_setmetatable(L, -2);
  return 1;
}

static int l_luamq_open(lua_State *L)
{
  mqd_t id, *ptr;
  int flags;
  flags = get_oflags(luaL_optlstring(L, 2, "", NULL));
  id = mq_open(luaL_checkstring(L, 1), flags);
  if(id == (mqd_t)-1) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }
  ptr = (mqd_t*)lua_newuserdata(L, sizeof(mqd_t));
  *ptr = id;
  luaL_newmetatable(L, MQD_TYPENAME);
  lua_setmetatable(L, -2);
  return 1;
}

static int l_luamq_send(lua_State *L)
{
  mqd_t* id;
  unsigned int prio;
  size_t len;
  const char* str;

  id = luaL_checkudata(L, 1, MQD_TYPENAME);
  str = luaL_checklstring(L, 2, &len);
  prio = luaL_optint(L, 3, 0);
  if(mq_send(*id, str, len, prio)) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }

  lua_pushboolean(L, 1);
  return 1;
}

static int l_luamq_receive(lua_State *L)
{
  mqd_t* id;
  unsigned int prio;
  struct mq_attr attr;
  ssize_t len;
  char* str;

  id = luaL_checkudata(L, 1, MQD_TYPENAME);
  if(mq_getattr(*id, &attr)) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }

  len = attr.mq_msgsize;
  str = malloc(len);
  if(!str) {
    lua_pushnil(L);
    lua_pushstring(L, "bad alloc");
    return 2;
  }

  len = mq_receive(*id, str, len, &prio);
  if(len < 0) {
    free(str);
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }

  lua_pushlstring(L, str, len);
  free(str);
  lua_pushinteger(L, prio);
  return 2;
}

static int l_luamq_close(lua_State *L)
{
  mqd_t* id;
  id = luaL_checkudata(L, 1, MQD_TYPENAME);
  if(mq_close(*id)) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }

  lua_pushboolean(L, 1);
  return 1;
}

static int l_luamq_unlink(lua_State *L)
{
  if(mq_unlink(luaL_checkstring(L, 1))) {
    lua_pushnil(L);
    push_errno(L);
    return 2;
  }

  lua_pushboolean(L, 1);
  return 1;
}

static const struct luaL_reg luamq_funcs [] = {
  { "create", l_luamq_create },
  { "open", l_luamq_open },
  { "send", l_luamq_send },
  { "receive", l_luamq_receive },
  { "close", l_luamq_close },
  { "unlink", l_luamq_unlink },
  { NULL, NULL }
};

LUALIB_API int luaopen_luamq(lua_State *L)
{
  luaL_register(L, "luamq", luamq_funcs);
  return 1;
}