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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#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"
#include "lua-mq.h"
static const struct { char c; mode_t b; } M[] =
{
{'r', S_IRUSR}, {'w', S_IWUSR}, {'x', S_IXUSR},
{'r', S_IRGRP}, {'w', S_IWGRP}, {'x', S_IXGRP},
{'r', S_IROTH}, {'w', S_IWOTH}, {'x', S_IXOTH},
};
static int get_mode(mode_t *mode, const char* modestr)
{
int i;
*mode = S_IRWXU;
for (i=0; i<9; i++) {
if(*modestr == 0) break;
if(*modestr == M[i].c)
*mode |= M[i].b;
else if(*modestr == '-')
*mode &= ~M[i].b;
else if(*modestr == 's') {
switch(i) {
case 2: *mode |= S_ISUID | S_IXUSR; break;
case 5: *mode |= S_ISGID | S_IXGRP; break;
default: return -1; break;
}
}
modestr++;
}
return 0;
}
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_mq_create(lua_State *L)
{
mqd_t id, *ptr;
int flags, ret;
mode_t mode;
flags = get_oflags(luaL_optstring(L, 2, ""));
ret = get_mode(&mode, luaL_optstring(L, 3, ""));
if(ret) {
lua_pushnil(L);
lua_pushstring(L, "invalid mode specifier");
return 2;
}
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_mq_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_mq_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_mq_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_mq_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_mq_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 mq_funcs [] = {
{ "create", l_mq_create },
{ "open", l_mq_open },
{ "send", l_mq_send },
{ "receive", l_mq_receive },
{ "close", l_mq_close },
{ "unlink", l_mq_unlink },
{ NULL, NULL }
};
LUALIB_API int luaopen_mq(lua_State *L)
{
#if LUA_VERSION_NUM > 501
lua_newtable(L);
luaL_setfuncs(L, mq_funcs, 0);
/* since Lua 5.2 libs shouldn't do that anymore... */
/* lua_pushvalue(L, -1); */
/* lua_setglobal(L, "mq"); */
#else
luaL_register(L, "mq", mq_funcs);
#endif
return 1;
}
|