Skip to content
Snippets Groups Projects
Commit 2bc82ed3 authored by Dmitry E. Oboukhov's avatar Dmitry E. Oboukhov
Browse files

It is possible to use symlinks to tarantoolctl from /etc/init.d.

parent 0808a001
No related branches found
No related tags found
No related merge requests found
......@@ -77,13 +77,19 @@ returns code C<0>.
Return code != 0 in other cases. Can complain in log (stderr) if pid file
exists and socket doesn't, etc.
=head2 separate instances control
If You use SysV init, You can use symlink from
C<dist.lua> to C</etc/init.d/instance_name[.lua]>.
C<dist.lua> detects if it is started by symlink and uses
instance_name as C<`basename $0 .lua`>.
=head1 COPYRIGHT
Copyright (C) 2010-2013 Tarantool AUTHORS:
please see AUTHORS file.
=cut
]]
......@@ -96,16 +102,56 @@ local console = require 'console'
local socket = require 'socket'
local ffi = require 'ffi'
local os = require 'os'
local fiber = require 'fiber'
ffi.cdef[[ int kill(int pid, int sig); ]]
if arg[1] == nil or arg[2] == nil then
log.error("Usage: dist.lua {start|stop|logrotate} instance")
os.exit(-1)
local available_commands = {
'start',
'stop',
'logrotate',
'status',
'enter',
'restart'
}
local function usage()
log.error("Usage: %s {%s} instance_name",
arg[0], table.concat(available_commands, '|'))
os.exit(1)
end
local cmd = arg[1]
local instance = fio.basename(arg[2], '.lua')
local valid_cmd = false
for _, vcmd in pairs(available_commands) do
if cmd == vcmd then
valid_cmd = true
break
end
end
if not valid_cmd then
usage()
end
local instance
if arg[2] == nil then
local istat = fio.lstat(arg[0])
if istat == nil then
log.error("Can't stat %s: %s", arg[0], errno.strerror())
os.exit(1)
end
if not istat:is_link() then
usage()
end
instance = fio.basename(arg[0], '.lua')
arg[2] = instance
else
instance = fio.basename(arg[2], '.lua')
end
-- shift argv to remove 'tarantoolctl' from arg[0]
for i = 0, 128 do
......@@ -215,20 +261,18 @@ wrapper_cfg = function(cfg)
return res
end
if cmd == 'start' then
box.cfg = wrapper_cfg
dofile(instance_lua)
elseif cmd == 'stop' then
function stop()
log.info("Stopping instance...")
if fio.stat(force_cfg.pid_file) == nil then
log.error("Process is not running (pid: %s)", force_cfg.pid_file)
os.exit(-1)
return 0
end
local f = fio.open(force_cfg.pid_file, 'O_RDONLY')
if f == nil then
log.error("Can't read pid file %s: %s",
force_cfg.pid_file, errno.strerror())
return -1
end
local str = f:read(64)
......@@ -239,14 +283,34 @@ elseif cmd == 'stop' then
if pid == nil or pid <= 0 then
log.error("Broken pid file %s", force_cfg.pid_file)
fio.unlink(force_cfg.pid_file)
os.exit(-1)
return -1
end
if ffi.C.kill(pid, 15) < 0 then
log.error("Can't kill process %d: %s", pid, errno.strerror())
fio.unlink(force_cfg.pid_file)
return -1
end
os.exit(-1)
return 0
end
function start()
log.info("Starting instance...")
box.cfg = wrapper_cfg
dofile(instance_lua)
end
if cmd == 'start' then
os.exit(start())
elseif cmd == 'stop' then
os.exit(stop())
elseif cmd == 'restart' then
stop()
fiber.sleep(1)
start()
elseif cmd == 'logrotate' then
if fio.stat(console_sock) == nil then
......
......@@ -292,6 +292,32 @@ lbox_fio_pushtimespec(struct lua_State *L, const struct timespec *ts)
lua_settable(L, -3); \
}
#define DEF_STAT_METHOD(method_name, macro_name) \
static int \
lbox_fio_stat_##method_name(struct lua_State *L) \
{ \
if (lua_gettop(L) < 1 || !lua_istable(L, 1)) \
luaL_error(L, "usage: stat:" #method_name "()"); \
lua_pushliteral(L, "mode"); \
lua_gettable(L, 1); \
int mode = lua_tointeger(L, -1); \
lua_pop(L, 1); \
lua_pushboolean(L, macro_name(mode) ? 1 : 0); \
return 1; \
}
DEF_STAT_METHOD(is_reg, S_ISREG);
DEF_STAT_METHOD(is_dir, S_ISDIR);
DEF_STAT_METHOD(is_chr, S_ISCHR);
DEF_STAT_METHOD(is_blk, S_ISBLK);
DEF_STAT_METHOD(is_fifo, S_ISFIFO);
#ifdef S_ISLNK
DEF_STAT_METHOD(is_link, S_ISLNK);
#endif
#ifdef S_ISSOCK
DEF_STAT_METHOD(is_sock, S_ISSOCK);
#endif
static int
lbox_fio_pushstat(struct lua_State *L, const struct stat *stat)
{
......@@ -316,6 +342,34 @@ lbox_fio_pushstat(struct lua_State *L, const struct stat *stat)
PUSHTABLE("mtime", lbox_fio_pushtimespec, &stat->st_mtim);
PUSHTABLE("atime", lbox_fio_pushtimespec, &stat->st_atim);
#endif
int top = lua_gettop(L);
/* metatable for tables *stat */
lua_newtable(L);
lua_pushliteral(L, "__index");
lua_newtable(L);
static const struct luaL_Reg stat_methods[] = {
{ "is_reg", lbox_fio_stat_is_reg },
{ "is_dir", lbox_fio_stat_is_dir },
{ "is_chr", lbox_fio_stat_is_chr },
{ "is_blk", lbox_fio_stat_is_blk },
{ "is_fifo", lbox_fio_stat_is_fifo },
#ifdef S_ISLNK
{ "is_link", lbox_fio_stat_is_link },
#endif
#ifdef S_ISSOCK
{ "is_sock", lbox_fio_stat_is_sock },
#endif
{ NULL, NULL }
};
luaL_register(L, NULL, stat_methods);
lua_settable(L, -3);
lua_setmetatable(L, top);
lua_settop(L, top);
return 1;
}
......@@ -525,6 +579,9 @@ lbox_fio_close(struct lua_State *L)
return 1;
}
void
tarantool_lua_fio_init(struct lua_State *L)
{
......@@ -649,6 +706,7 @@ tarantool_lua_fio_init(struct lua_State *L)
lua_settable(L, -3);
lua_pushliteral(L, "seek");
lua_newtable(L);
PUSHTABLE("SEEK_SET", lua_pushinteger, SEEK_SET);
......
......@@ -74,10 +74,45 @@ fh1 ~= nil
---
- true
...
fh1:stat().size
f1s = fh1:stat()
---
...
f1s.size
---
- 0
...
f1s.is_reg()
---
- error: 'usage: stat:is_reg()'
...
f1s:is_reg()
---
- true
...
f1s:is_dir()
---
- false
...
f1s:is_link()
---
- false
...
f1s:is_sock()
---
- false
...
f1s:is_fifo()
---
- false
...
f1s:is_chr()
---
- false
...
f1s:is_blk()
---
- false
...
fh1:seek(121)
---
- 121
......
......@@ -32,7 +32,18 @@ file4 = fio.pathjoin(tmpdir, 'file.4')
fh1 = fio.open(file1, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
fh1 ~= nil
fh1:stat().size
f1s = fh1:stat()
f1s.size
f1s.is_reg()
f1s:is_reg()
f1s:is_dir()
f1s:is_link()
f1s:is_sock()
f1s:is_fifo()
f1s:is_chr()
f1s:is_blk()
fh1:seek(121)
fh1:stat().size
fh1:write("Hello, world")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment