-
Notifications
You must be signed in to change notification settings - Fork 376
/
init.c
642 lines (595 loc) · 16 KB
/
init.c
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright 2010-2015, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "lua/init.h"
#include "lua/utils.h"
#include "main.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <libgen.h>
#endif
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <lj_cdata.h>
#include <luajit.h>
#include <fiber.h>
#include "version.h"
#include "coio.h"
#include "lua/fiber.h"
#include "lua/fiber_cond.h"
#include "lua/fiber_channel.h"
#include "lua/errno.h"
#include "lua/socket.h"
#include "lua/utils.h"
#include "third_party/lua-cjson/lua_cjson.h"
#include "third_party/lua-yaml/lyaml.h"
#include "lua/msgpack.h"
#include "lua/pickle.h"
#include "lua/fio.h"
#include "lua/httpc.h"
#include <small/ibuf.h>
#include <ctype.h>
#include <readline/readline.h>
#include <readline/history.h>
/**
* The single Lua state of the transaction processor (tx) thread.
*/
struct lua_State *tarantool_L;
static struct ibuf tarantool_lua_ibuf_body;
struct ibuf *tarantool_lua_ibuf = &tarantool_lua_ibuf_body;
/**
* The fiber running the startup Lua script
*/
struct fiber *script_fiber;
bool start_loop = true;
/* contents of src/lua/ files */
extern char strict_lua[],
uuid_lua[],
msgpack_lua[],
msgpackffi_lua[],
fun_lua[],
crypto_lua[],
digest_lua[],
init_lua[],
buffer_lua[],
errno_lua[],
fiber_lua[],
httpc_lua[],
log_lua[],
uri_lua[],
socket_lua[],
help_lua[],
help_en_US_lua[],
tap_lua[],
fio_lua[],
argparse_lua[],
iconv_lua[],
/* jit.* library */
vmdef_lua[],
bc_lua[],
bcsave_lua[],
dis_x86_lua[],
dis_x64_lua[],
dump_lua[],
csv_lua[],
v_lua[],
clock_lua[],
title_lua[],
env_lua[],
pwd_lua[],
table_lua[],
trigger_lua[],
string_lua[],
p_lua[], /* LuaJIT 2.1 profiler */
zone_lua[] /* LuaJIT 2.1 profiler */;
static const char *lua_modules[] = {
/* Make it first to affect load of all other modules */
"strict", strict_lua,
"fun", fun_lua,
"tarantool", init_lua,
"errno", errno_lua,
"fiber", fiber_lua,
"env", env_lua,
"string", string_lua,
"table", table_lua,
"buffer", buffer_lua,
"msgpack", msgpack_lua,
"msgpackffi", msgpackffi_lua,
"crypto", crypto_lua,
"digest", digest_lua,
"uuid", uuid_lua,
"log", log_lua,
"uri", uri_lua,
"fio", fio_lua,
"csv", csv_lua,
"clock", clock_lua,
"socket", socket_lua,
"title", title_lua,
"tap", tap_lua,
"help.en_US", help_en_US_lua,
"help", help_lua,
"internal.argparse", argparse_lua,
"internal.trigger", trigger_lua,
"pwd", pwd_lua,
"http.client", httpc_lua,
"iconv", iconv_lua,
/* jit.* library */
"jit.vmdef", vmdef_lua,
"jit.bc", bc_lua,
"jit.bcsave", bcsave_lua,
"jit.dis_x86", dis_x86_lua,
"jit.dis_x64", dis_x64_lua,
"jit.dump", dump_lua,
"jit.v", v_lua,
/* Profiler */
"jit.p", p_lua,
"jit.zone", zone_lua,
NULL
};
/*
* {{{ box Lua library: common functions
*/
/**
* Convert lua number or string to lua cdata 64bit number.
*/
static int
lbox_tonumber64(struct lua_State *L)
{
luaL_checkany(L, 1);
int base = luaL_optint(L, 2, -1);
luaL_argcheck(L, (2 <= base && base <= 36) || base == -1, 2,
"base out of range");
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
base = (base == -1 ? 10 : base);
if (base != 10)
return luaL_argerror(L, 1, "string expected");
lua_settop(L, 1); /* return original value as is */
return 1;
case LUA_TSTRING:
{
size_t argl = 0;
const char *arg = luaL_checklstring(L, 1, &argl);
/* Trim whitespaces at begin/end */
while (argl > 0 && isspace(arg[argl - 1])) {
argl--;
}
while (isspace(*arg)) {
arg++; argl--;
}
/*
* Check if we're parsing custom format:
* 1) '0x' or '0X' trim in case of base == 16 or base == -1
* 2) '0b' or '0B' trim in case of base == 2 or base == -1
* 3) '-' for negative numbers
*/
char negative = 0;
if (arg[0] == '-') {
arg++; argl--;
negative = 1;
}
if (argl > 2 && arg[0] == '0') {
if ((arg[1] == 'x' || arg[1] == 'X') &&
(base == 16 || base == -1)) {
base = 16; arg += 2; argl -= 2;
} else if ((arg[1] == 'b' || arg[1] == 'B') &&
(base == 2 || base == -1)) {
base = 2; arg += 2; argl -= 2;
}
}
base = (base == -1 ? 10 : base);
errno = 0;
char *arge;
unsigned long long result = strtoull(arg, &arge, base);
if (errno == 0 && arge == arg + argl) {
if (argl == 0) {
lua_pushnil(L);
} else if (negative) {
luaL_pushint64(L, -1 * (long long )result);
} else {
luaL_pushuint64(L, result);
}
return 1;
}
break;
} /* LUA_TSTRING */
case LUA_TCDATA:
{
base = (base == -1 ? 10 : base);
if (base != 10)
return luaL_argerror(L, 1, "string expected");
uint32_t ctypeid = 0;
luaL_checkcdata(L, 1, &ctypeid);
if (ctypeid >= CTID_INT8 && ctypeid <= CTID_DOUBLE) {
lua_pushvalue(L, 1);
return 1;
}
break;
} /* LUA_TCDATA */
}
lua_pushnil(L);
return 1;
}
/* }}} */
/**
* Original LuaJIT/Lua logic: <luajit/src/lib_package.c - function setpath>
*
* 1) If environment variable 'envname' is empty, it uses only <default value>
* 2) Otherwise:
* - If it contains ';;', then ';;' is replaced with ';'<default value>';'
* - Otherwise is uses only what's inside this value.
**/
static void
tarantool_lua_pushpath_env(struct lua_State *L, const char *envname)
{
const char *path = getenv(envname);
if (path != NULL) {
const char *def = lua_tostring(L, -1);
path = luaL_gsub(L, path, ";;", ";\1;");
luaL_gsub(L, path, "\1", def);
lua_remove(L, -2);
lua_remove(L, -2);
}
}
/**
* Prepend the variable list of arguments to the Lua
* package search path
*/
static void
tarantool_lua_setpaths(struct lua_State *L)
{
const char *home = getenv("HOME");
char cwd[PATH_MAX] = {'\0'};
getcwd(cwd, sizeof(cwd));
lua_getglobal(L, "package");
int top = lua_gettop(L);
if (home != NULL) {
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/share/lua/5.1/?.lua;");
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/share/lua/5.1/?/init.lua;");
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/share/lua/?.lua;");
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/share/lua/?/init.lua;");
}
lua_pushliteral(L, MODULE_LUAPATH ";");
/* overwrite standard paths */
lua_concat(L, lua_gettop(L) - top);
tarantool_lua_pushpath_env(L, "LUA_PATH");
lua_setfield(L, top, "path");
if (home != NULL) {
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/lib/lua/5.1/?" MODULE_LIBSUFFIX ";");
lua_pushstring(L, home);
lua_pushliteral(L, "/.luarocks/lib/lua/?" MODULE_LIBSUFFIX ";");
}
lua_pushliteral(L, MODULE_LIBPATH ";");
/* overwrite standard paths */
lua_concat(L, lua_gettop(L) - top);
tarantool_lua_pushpath_env(L, "LUA_CPATH");
lua_setfield(L, top, "cpath");
assert(lua_gettop(L) == top);
lua_pop(L, 1); /* package */
}
static int
luaopen_tarantool(lua_State *L)
{
/* Set _G._TARANTOOL (like _VERSION) */
lua_pushstring(L, tarantool_version());
lua_setfield(L, LUA_GLOBALSINDEX, "_TARANTOOL");
static const struct luaL_Reg initlib[] = {
{NULL, NULL}
};
luaL_register_module(L, "tarantool", initlib);
/* version */
lua_pushstring(L, tarantool_version());
lua_setfield(L, -2, "version");
/* build */
lua_pushstring(L, "build");
lua_newtable(L);
/* build.target */
lua_pushstring(L, "target");
lua_pushstring(L, BUILD_INFO);
lua_settable(L, -3);
/* build.options */
lua_pushstring(L, "options");
lua_pushstring(L, BUILD_OPTIONS);
lua_settable(L, -3);
/* build.compiler */
lua_pushstring(L, "compiler");
lua_pushstring(L, COMPILER_INFO);
lua_settable(L, -3);
/* build.mod_format */
lua_pushstring(L, "mod_format");
lua_pushstring(L, TARANTOOL_LIBEXT);
lua_settable(L, -3);
/* build.flags */
lua_pushstring(L, "flags");
lua_pushstring(L, TARANTOOL_C_FLAGS);
lua_settable(L, -3);
lua_settable(L, -3); /* box.info.build */
return 1;
}
void
tarantool_lua_init(const char *tarantool_bin, int argc, char **argv)
{
lua_State *L = luaL_newstate();
if (L == NULL) {
panic("failed to initialize Lua");
}
ibuf_create(tarantool_lua_ibuf, tarantool_lua_slab_cache(), 16000);
luaL_openlibs(L);
tarantool_lua_setpaths(L);
/* Initialize ffi to enable luaL_pushcdata/luaL_checkcdata functions */
luaL_loadstring(L, "return require('ffi')");
lua_call(L, 0, 0);
lua_register(L, "tonumber64", lbox_tonumber64);
tarantool_lua_utils_init(L);
tarantool_lua_fiber_init(L);
tarantool_lua_fiber_cond_init(L);
tarantool_lua_fiber_channel_init(L);
tarantool_lua_errno_init(L);
tarantool_lua_fio_init(L);
tarantool_lua_socket_init(L);
tarantool_lua_pickle_init(L);
luaopen_http_client_driver(L);
lua_pop(L, 1);
luaopen_msgpack(L);
lua_pop(L, 1);
luaopen_yaml(L);
lua_pop(L, 1);
luaopen_json(L);
lua_pop(L, 1);
#if defined(HAVE_GNU_READLINE)
/*
* Disable libreadline signals handlers. All signals are handled in
* main thread by libev watchers.
*/
rl_catch_signals = 0;
rl_catch_sigwinch = 0;
#endif
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
for (const char **s = lua_modules; *s; s += 2) {
const char *modname = *s;
const char *modsrc = *(s + 1);
const char *modfile = lua_pushfstring(L,
"@builtin/%s.lua", modname);
if (luaL_loadbuffer(L, modsrc, strlen(modsrc), modfile))
panic("Error loading Lua module %s...: %s",
modname, lua_tostring(L, -1));
lua_pushstring(L, modname);
lua_call(L, 1, 1);
if (!lua_isnil(L, -1)) {
lua_setfield(L, -3, modname); /* package.loaded.modname = t */
} else {
lua_pop(L, 1); /* nil */
}
lua_pop(L, 1); /* chunkname */
}
lua_pop(L, 1); /* _LOADED */
luaopen_tarantool(L);
lua_pop(L, 1);
lua_newtable(L);
lua_pushinteger(L, -1);
lua_pushstring(L, tarantool_bin);
lua_settable(L, -3);
for (int i = 0; i < argc; i++) {
lua_pushinteger(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
lua_setfield(L, LUA_GLOBALSINDEX, "arg");
#ifdef NDEBUG
/* Unload strict after boot in release mode */
if (luaL_dostring(L, "require('strict').off()") != 0)
panic("Failed to unload 'strict' Lua module");
#endif /* NDEBUG */
/* clear possible left-overs of init */
lua_settop(L, 0);
tarantool_L = L;
}
char *history = NULL;
struct slab_cache *
tarantool_lua_slab_cache()
{
return &cord()->slabc;
}
/**
* Push argument and call a function on the top of Lua stack
*/
static void
lua_main(lua_State *L, int argc, char **argv)
{
assert(lua_isfunction(L, -1));
lua_checkstack(L, argc - 1);
for (int i = 1; i < argc; i++)
lua_pushstring(L, argv[i]);
if (luaT_call(L, lua_gettop(L) - 1, 0) != 0) {
struct error *e = diag_last_error(&fiber()->diag);
panic("%s", e->errmsg);
}
/* clear the stack from return values. */
lua_settop(L, 0);
}
/**
* Execute start-up script.
*/
static int
run_script_f(va_list ap)
{
struct lua_State *L = va_arg(ap, struct lua_State *);
const char *path = va_arg(ap, const char *);
bool interactive = va_arg(ap, int);
int optc = va_arg(ap, int);
char **optv = va_arg(ap, char **);
int argc = va_arg(ap, int);
char **argv = va_arg(ap, char **);
struct diag *diag = &fiber()->diag;
/*
* Load libraries and execute chunks passed by -l and -e
* command line options
*/
for (int i = 0; i < optc; i += 2) {
assert(optv[i][0] == '-' && optv[i][2] == '\0');
switch (optv[i][1]) {
case 'l':
/*
* Load library
*/
lua_getglobal(L, "require");
lua_pushstring(L, optv[i + 1]);
if (luaT_call(L, 1, 1) != 0) {
struct error *e = diag_last_error(diag);
panic("%s", e->errmsg);
}
/* Non-standard: set name = require('name') */
lua_setglobal(L, optv[i + 1]);
lua_settop(L, 0);
break;
case 'e':
/*
* Execute chunk
*/
if (luaL_loadbuffer(L, optv[i + 1], strlen(optv[i + 1]),
"=(command line)") != 0) {
panic("%s", lua_tostring(L, -1));
}
if (luaT_call(L, 0, 0) != 0) {
struct error *e = diag_last_error(diag);
panic("%s", e->errmsg);
}
lua_settop(L, 0);
break;
default:
unreachable(); /* checked by getopt() in main() */
}
}
/*
* Return control to tarantool_lua_run_script.
* tarantool_lua_run_script then will start an auxiliary event
* loop and re-schedule this fiber.
*/
fiber_sleep(0.0);
if (path && strcmp(path, "-") != 0 && access(path, F_OK) == 0) {
/* Execute script. */
if (luaL_loadfile(L, path) != 0)
panic("%s", lua_tostring(L, -1));
lua_main(L, argc, argv);
} else if (!isatty(STDIN_FILENO) || (path && strcmp(path, "-") == 0)) {
/* Execute stdin */
if (luaL_loadfile(L, NULL) != 0)
panic("%s", lua_tostring(L, -1));
lua_main(L, argc, argv);
} else {
interactive = true;
}
/*
* Start interactive mode when it was explicitly requested
* by "-i" option or stdin is TTY or there are no script.
*/
if (interactive) {
say_crit("%s %s\ntype 'help' for interactive help",
tarantool_package(), tarantool_version());
/* get console.start from package.loaded */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, -1, "console");
lua_getfield(L, -1, "start");
lua_remove(L, -2); /* remove package.loaded.console */
lua_remove(L, -2); /* remove package.loaded */
start_loop = false;
lua_main(L, argc, argv);
}
/*
* Lua script finished. Stop the auxiliary event loop and
* return control back to tarantool_lua_run_script.
*/
ev_break(loop(), EVBREAK_ALL);
return 0;
}
void
tarantool_lua_run_script(char *path, bool interactive,
int optc, char **optv, int argc, char **argv)
{
const char *title = path ? basename(path) : "interactive";
/*
* init script can call box.fiber.yield (including implicitly via
* box.insert, box.update, etc...), but box.fiber.yield() today,
* when called from 'sched' fiber crashes the server.
* To work this problem around we must run init script in
* a separate fiber.
*/
script_fiber = fiber_new(title, run_script_f);
if (script_fiber == NULL)
panic("%s", diag_last_error(diag_get())->errmsg);
fiber_start(script_fiber, tarantool_L, path, interactive,
optc, optv, argc, argv);
/*
* Run an auxiliary event loop to re-schedule run_script fiber.
* When this fiber finishes, it will call ev_break to stop the loop.
*/
ev_run(loop(), 0);
/* The fiber running the startup script has ended. */
script_fiber = NULL;
}
void
tarantool_lua_free()
{
/*
* Some part of the start script panicked, and called
* exit(). The call stack in this case leads us back to
* luaL_call() in run_script(). Trying to free a Lua state
* from within luaL_call() is not the smartest idea (@sa
* gh-612).
*/
if (script_fiber)
return;
/*
* Got to be done prior to anything else, since GC
* handlers can refer to other subsystems (e.g. fibers).
*/
if (tarantool_L) {
/* collects garbage, invoking userdata gc */
lua_close(tarantool_L);
}
tarantool_L = NULL;
#if 0
/* Temporarily moved to tarantool_free(), tarantool_lua_free() not
* being called due to cleanup order issues
*/
if (isatty(STDIN_FILENO)) {
/*
* Restore terminal state. Doesn't hurt if exiting not
* due to a signal.
*/
rl_cleanup_after_signal();
}
#endif
}