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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
** Lua eXtended system library
*/
#define lxsyslib_c
#define LUA_LIB
#include <lua/lprefix.h>
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <lua/lua.h>
#include <lua/lauxlib.h>
#include <lua/lualib.h>
#include "lxlib.h"
static int sys_dbg_print(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
sc_dbg_print(str);
return 0;
}
static int sys_yield(lua_State *L) {
sc_yield();
return 0;
}
static int sys_exit(lua_State *L) {
int code = luaL_checkinteger(L, 1);
sc_exit(code);
return 0;
}
static int sys_usleep(lua_State *L) {
int usecs = luaL_checkinteger(L, 1);
sc_usleep(usecs);
return 0;
}
/*
Missing syscalls :
- sys_new_thread, exit_thread -> into specific threading library
- mmap, mmap_file, mchmap, munmap -> into specific memory-related library ??
- ioctl, fctl -> into ioctl library
*/
static int sys_create(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
int type = luaL_checkinteger(L, 2);
lua_pushboolean(L, sc_create(name, type));
return 1;
}
static int sys_delete(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
lua_pushboolean(L, sc_delete(name));
return 1;
}
static int sys_move(lua_State *L) {
const char *oldname = luaL_checkstring(L, 1);
const char *newname = luaL_checkstring(L, 2);
lua_pushboolean(L, sc_move(oldname, newname));
return 1;
}
static int sys_stat(lua_State *L) {
const char* name = luaL_checkstring(L, 1);
stat_t s;
if (sc_stat(name, &s)) {
lua_createtable(L, 0, 3);
setintfield(L, "type", s.type);
setintfield(L, "access", s.access);
setintfield(L, "size", s.size);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_open(lua_State *L) {
const char* name = luaL_checkstring(L, 1);
int mode = luaL_checkinteger(L, 2);
fd_t fh = sc_open(name, mode);
if (fh) {
lua_pushinteger(L, fh);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_close(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
sc_close(fd);
return 0;
}
static int sys_read(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
int offset = luaL_checkinteger(L, 2);
int len = luaL_checkinteger(L, 3);
luaL_Buffer b;
char* bufptr = luaL_buffinitsize(L, &b, len);
int ret = sc_read(fd, offset, len, bufptr);
luaL_pushresultsize(&b, ret);
lua_pushinteger(L, ret);
return 2;
}
static int sys_write(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
int offset = luaL_checkinteger(L, 2);
const char* str = luaL_checkstring(L, 3);
int len = luaL_len(L, 3);
int ret = sc_write(fd, offset, len, str);
lua_pushinteger(L, ret);
return 1;
}
static int sys_readdir(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
int ent_no = luaL_checkinteger(L, 2);
dirent_t d;
bool ret = sc_readdir(fd, ent_no, &d);
if (ret) {
lua_createtable(L, 0, 4);
setstrfield(L, "name", d.name);
setintfield(L, "type", d.st.type);
setintfield(L, "access", d.st.access);
setintfield(L, "size", d.st.size);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_stat_open(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
stat_t s;
if (sc_stat_open(fd, &s)) {
lua_createtable(L, 0, 3);
setintfield(L, "type", s.type);
setintfield(L, "access", s.access);
setintfield(L, "size", s.size);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_select(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
int nfds = luaL_len(L, 1);
int timeout = luaL_checkinteger(L, 2);
sel_fd_t *fds = (sel_fd_t*)malloc(nfds*sizeof(sel_fd_t));
if (fds == NULL)
luaL_error(L, "Out of memory, could not allocate %d sel_fd_t", nfds);
for (int i = 0; i < nfds; i++) {
lua_rawgeti(L, 1, i+1);
luaL_checktype(L, -1, LUA_TTABLE);
lua_rawgeti(L, -1, 1);
fds[i].fd = luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 2);
fds[i].req_flags = luaL_checkinteger(L, -1);
lua_pop(L, 2);
}
bool result = sc_select(fds, nfds, timeout);
for (int i = 0; i < nfds; i++) {
lua_rawgeti(L, 1, i+1);
lua_pushinteger(L, fds[i].got_flags);
lua_rawseti(L, -2, 3);
lua_pop(L, 1);
}
free(fds);
lua_pushboolean(L, result);
return 1;
}
static int sys_make_channel(lua_State *L) {
bool blocking = lx_checkboolean(L, 1);
fd_pair_t pair = sc_make_channel(blocking);
lua_pushinteger(L, pair.a);
lua_pushinteger(L, pair.b);
return 2;
}
static int sys_make_shm(lua_State *L) {
int size = luaL_checkinteger(L, 1);
int fd = sc_make_shm(size);
if (fd) {
lua_pushinteger(L, fd);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_gen_token(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
token_t t;
if (sc_gen_token(fd, &t)) {
lua_pushlstring(L, t.bytes, TOKEN_LENGTH);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_use_token(lua_State *L) {
const char* tok = luaL_checkstring(L, 1);
if (!(luaL_len(L, 1) == TOKEN_LENGTH)) {
luaL_argerror(L, 1, "invalid token length");
}
token_t t;
memcpy(t.bytes, tok, TOKEN_LENGTH);
fd_t f = sc_use_token(&t);
if (f) {
lua_pushinteger(L, f);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_make_fs(lua_State *L) {
const char* name = luaL_checkstring(L, 1);
const char* driver = luaL_checkstring(L, 2);
int source = luaL_checkinteger(L, 3);
const char* options = luaL_checkstring(L, 4);
lua_pushboolean(L, sc_make_fs(name, driver, source, options));
return 1;
}
static int sys_fs_add_source(lua_State *L) {
const char* fs = luaL_checkstring(L, 1);
int source = luaL_checkinteger(L, 2);
const char* options = luaL_checkstring(L, 3);
lua_pushboolean(L, sc_fs_add_source(fs, source, options));
return 1;
}
static int sys_fs_subfs(lua_State *L) {
const char* name = luaL_checkstring(L, 1);
const char* orig_fs = luaL_checkstring(L, 2);
const char* root = luaL_checkstring(L, 3);
int ok_modes = luaL_checkinteger(L, 4);
lua_pushboolean(L, sc_fs_subfs(name, orig_fs, root, ok_modes));
return 1;
}
static int sys_fs_remove(lua_State *L) {
const char* name = luaL_checkstring(L, 1);
sc_fs_remove(name);
return 0;
}
static int sys_new_proc(lua_State *L) {
lua_pushinteger(L, sc_new_proc());
return 1;
}
static int sys_bind_fs(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
const char* new_name = luaL_checkstring(L, 2);
const char* name = luaL_checkstring(L, 3);
lua_pushboolean(L, sc_bind_fs(pid, new_name, name));
return 1;
}
static int sys_bind_subfs(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
const char* new_name = luaL_checkstring(L, 2);
const char* fs = luaL_checkstring(L, 3);
const char* root = luaL_checkstring(L, 4);
int ok_modes = luaL_checkinteger(L, 5);
lua_pushboolean(L, sc_bind_subfs(pid, new_name, fs, root, ok_modes));
return 1;
}
static int sys_bind_make_fs(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
const char* new_name = luaL_checkstring(L, 2);
const char* driver = luaL_checkstring(L, 3);
fd_t source = luaL_checkinteger(L, 4);
const char* options = luaL_checkstring(L, 5);
lua_pushboolean(L, sc_bind_make_fs(pid, new_name, driver, source, options));
return 1;
}
static int sys_bind_fd(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
int new_fd = luaL_checkinteger(L, 2);
int fd = luaL_checkinteger(L, 3);
lua_pushboolean(L, sc_bind_fd(pid, new_fd, fd));
return 1;
}
static int sys_proc_exec(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
const char *name = luaL_checkstring(L, 2);
lua_pushboolean(L, sc_proc_exec(pid, name));
return 1;
}
static int sys_proc_status(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
proc_status_t s;
if (sc_proc_status(pid, &s)) {
lua_createtable(L, 0, 3);
setintfield(L, "pid", s.pid);
setintfield(L, "status",s.status);
setintfield(L, "exit_code", s.exit_code);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_proc_kill(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
proc_status_t s;
if (sc_proc_kill(pid, &s)) {
lua_createtable(L, 0, 3);
setintfield(L, "pid", s.pid);
setintfield(L, "status",s.status);
setintfield(L, "exit_code", s.exit_code);
} else {
lua_pushnil(L);
}
return 1;
}
static int sys_proc_wait(lua_State *L) {
int pid = luaL_checkinteger(L, 1);
bool wait = lx_checkboolean(L, 2);
proc_status_t s;
sc_proc_wait(pid, wait, &s);
lua_createtable(L, 0, 3);
setintfield(L, "pid", s.pid);
setintfield(L, "status",s.status);
setintfield(L, "exit_code", s.exit_code);
return 1;
}
/* }====================================================== */
static const luaL_Reg syslib[] = {
{"dbg_print", sys_dbg_print},
{"yield", sys_yield},
{"exit", sys_exit},
{"usleep", sys_usleep},
{"create", sys_create},
{"delete", sys_delete},
{"move", sys_move},
{"stat", sys_stat},
{"open", sys_open},
{"close", sys_close},
{"read", sys_read},
{"write", sys_write},
{"readdir", sys_readdir},
{"stat_open", sys_stat_open},
{"select", sys_select},
{"make_channel",sys_make_channel},
{"make_shm", sys_make_shm},
{"gen_token", sys_gen_token},
{"use_token", sys_use_token},
{"make_fs", sys_make_fs},
{"fs_add_source",sys_fs_add_source},
{"fs_subfs", sys_fs_subfs},
{"fs_remove", sys_fs_remove},
{"new_proc", sys_new_proc},
{"bind_fs", sys_bind_fs},
{"bind_subfs",sys_bind_subfs},
{"bind_make_fs",sys_bind_make_fs},
{"bind_fd", sys_bind_fd},
{"proc_exec", sys_proc_exec},
{"proc_status",sys_proc_status},
{"proc_kill", sys_proc_kill},
{"proc_wait", sys_proc_wait},
{NULL, NULL}
};
LUAMOD_API int lx_open_sys (lua_State *L) {
luaL_newlib(L, syslib);
return 1;
}
/* vim: set sts=2 ts=2 sw=2 tw=0 et :*/
|