Skip to content

Commit

Permalink
add signal support
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Jul 10, 2024
1 parent ed4795f commit 228f784
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 72 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SRC_FILE = \
silly_queue.c \
silly_worker.c \
silly_timer.c \
silly_signal.c \
silly_run.c \
silly_daemon.c \
silly_malloc.c \
Expand Down
19 changes: 19 additions & 0 deletions lualib-src/lualib-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "silly_socket.h"
#include "silly_malloc.h"
#include "silly_timer.h"
#include "silly_signal.h"

static void
dispatch(lua_State *L, struct silly_message *sm)
Expand Down Expand Up @@ -74,6 +75,10 @@ dispatch(lua_State *L, struct silly_message *sm)
lua_pushinteger(L, tosocket(sm)->ud);
args += 3;
break;
case SILLY_SIGNAL:
lua_pushinteger(L, tosignal(sm)->signum);
args += 1;
break;
default:
silly_log_error("[silly.core] callback unknow message type:%d\n",
sm->type);
Expand Down Expand Up @@ -177,6 +182,19 @@ ltimercancel(lua_State *L)
return 1;
}

static int
lsignal(lua_State *L)
{
int signum = luaL_checkinteger(L, 1);
int err = silly_signal_watch(signum);
if (err != 0) {
lua_pushstring(L, strerror(err));
} else {
lua_pushnil(L);
}
return 1;
}

//socket
struct multicasthdr {
uint32_t ref;
Expand Down Expand Up @@ -509,6 +527,7 @@ luaopen_core_c(lua_State *L)
{"dispatch", ldispatch},
{"timeout", ltimeout},
{"timercancel", ltimercancel},
{"signal", lsignal},
{"genid", lgenid},
{"tostring", ltostring},
{"getpid", lgetpid},
Expand Down
9 changes: 9 additions & 0 deletions lualib-src/lualib-logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ llog(lua_State *L, enum silly_log_level log_level)
return 0;
}

static int
lopenfile(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
silly_log_openfile(path);
return 0;
}

static int
lgetlevel(lua_State *L)
{
Expand Down Expand Up @@ -229,6 +237,7 @@ int
luaopen_core_logger_c(lua_State *L)
{
luaL_Reg tbl[] = {
{"openfile", lopenfile},
{"getlevel", lgetlevel},
{"setlevel", lsetlevel},
{"debug", ldebug},
Expand Down
77 changes: 71 additions & 6 deletions lualib/core.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
local c = require "core.c"
local logger = require "core.logger"
local logger = require "core.logger.c"

local core = {}
local type = type
local pairs = pairs
local assert = assert
local xpcall = xpcall
local tostring = tostring
local tonumber = tonumber
local smatch = string.match
local sformat = string.format
local tremove = table.remove
local tpack = table.pack
local tunpack = table.unpack
Expand All @@ -22,12 +20,10 @@ local log_error = assert(logger.error)
local readctrl = assert(c.readctrl)
local trace_new = assert(c.trace_new)
local trace_set = assert(c.trace_set)
local trace_get = assert(c.trace_get)
local trace_span = assert(c.trace_span)

core.pid = c.getpid()
core.genid = c.genid
core.getpid = c.getpid
core.gitsha1 = c.gitsha1()
core.version = c.version()
core.tostring = c.tostring
Expand All @@ -37,6 +33,52 @@ core.socket_read_ctrl = function (sid, ctrl)
return readctrl(sid, ctrl == "enable")
end

--signal
local signal = c.signal
local signal_dispatch = {}
local signal_map = {}
do
local sig_list = {
SIGINT = 2, -- Interactive attention signal.
SIGILL = 4, -- Illegal instruction.
SIGABRT = 6, -- Abnormal termination.
SIGFPE = 8, -- Erroneous arithmetic operation.
SIGSEGV = 11, -- Invalid access to storage.
SIGTERM = 15, -- Termination request.

--Historical signals specified by POSIX.
SIGHUP = 1, -- Hangup.
SIGQUIT = 3, -- Quit.
SIGTRAP = 5, -- Trace/breakpoint trap.
SIGKILL = 9, -- Killed.
SIGBUS = 10, -- Bus error.
SIGSYS = 12, -- Bad system call.
SIGPIPE = 13, -- Broken pipe.
SIGALRM = 14, -- Alarm clock.

--New(er) POSIX signals (1003.1-2008, 1003.1-2013).

SIGURG = 16, -- Urgent data is available at a socket.
SIGSTOP = 17, -- Stop, unblockable.
SIGTSTP = 18, -- Keyboard stop.
SIGCONT = 19, -- Continue.
SIGCHLD = 20, -- Child terminated or stopped.
SIGTTIN = 21, -- Background read from control terminal.
SIGTTOU = 22, -- Background write to control terminal.
SIGPOLL = 23, -- Pollable event occurred (System V).
SIGXCPU = 24, -- CPU time limit exceeded.
SIGXFSZ = 25, -- File size limit exceeded.
SIGVTALRM = 26, -- Virtual timer expired.
SIGPROF = 27, -- Profiling timer expired.
SIGUSR1 = 30, -- User-defined signal 1.
SIGUSR2 = 31, -- User-defined signal 2.
}
for k, v in pairs(sig_list) do
signal_map[k] = v
signal_map[v] = k
end
end

--coroutine
--state migrate(RUN (WAIT->READY)/SLEEP RUN)
local task_status = setmetatable({}, weakmt)
Expand Down Expand Up @@ -260,6 +302,15 @@ function core.timercancel(session)
end
end

function core.signal(sig, f)
local s = assert(signal_map[sig], sig)
local err = signal(s)
assert(not err, err)
local old = signal_dispatch[s]
signal_dispatch[s] = f
return old
end

function core.start(func)
local t = task_create(func)
task_resume(t)
Expand Down Expand Up @@ -448,7 +499,17 @@ end,
else
log_info("[sys.core] SILLY_UDP fd:", fd, "closed")
end
end
end,
[7] = function(signum) --SILLY_ERROR = 7
local fn = signal_dispatch[signum]
if fn then
local t = task_create(fn)
task_resume(t, signal_map[signum])
return
end
log_info("[sys.core] signal", signum, "received")
core.exit(0)
end,
}

--fd, message, portid/errno, addr
Expand All @@ -460,5 +521,9 @@ end

c.dispatch(dispatch)

core.signal("SIGINT", function(_)
core.exit(0)
end)

return core

26 changes: 18 additions & 8 deletions lualib/core/logger.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
local core = require "core"
local env = require "core.env"
local c = require "core.logger.c"

local function nop()
end

local logger = {
--const from silly_log.h
DEBUG = 0,
Expand All @@ -8,10 +14,10 @@ local logger = {
--logger function export
getlevel = c.getlevel,
setlevel = nil,
debug = nil,
info = nil,
warn = nil,
error = nil,
debug = nop,
info = nop,
warn = nop,
error = nop,
}

local func_level = {
Expand All @@ -21,10 +27,6 @@ local func_level = {
error = logger.ERROR,
}

local function nop()
end


local function refresh(visiable_level)
for name, level in pairs(func_level) do
if level >= visiable_level then
Expand All @@ -43,4 +45,12 @@ function logger.setlevel(level)
c.setlevel(level)
end

core.signal("SIGUSR1", function(_)
local path = env.get("logpath")
if not path then
return
end
c.openfile(path)
end)

return logger
6 changes: 3 additions & 3 deletions silly-src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ int main(int argc, char *argv[])
print_help(config.selfname);
return -1;
}
silly_trace_init();
silly_log_init();
silly_timer_init();
strncpy(config.bootstrap, argv[1], ARRAY_SIZE(config.bootstrap) - 1);
parse_args(&config, argc, argv);
silly_trace_init();
silly_log_init(&config);
silly_timer_init();
status = silly_run(&config);
silly_log_info("%s exit, leak memory size:%zu\n",
argv[0], silly_memused());
Expand Down
17 changes: 12 additions & 5 deletions silly-src/silly.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define tocommon(msg) ((struct silly_message *)(msg))
#define totexpire(msg) ((struct silly_message_texpire *)(msg))
#define tosocket(msg) ((struct silly_message_socket *)(msg))
#define tosignal(msg) ((struct silly_message_signal *)(msg))
#define COMMONFIELD struct silly_message *next; enum silly_message_type type;


Expand All @@ -47,12 +48,13 @@ struct silly_config {


enum silly_message_type {
SILLY_TEXPIRE = 1,
SILLY_TEXPIRE = 1, //timer expire
SILLY_SACCEPT = 2, //new connetiong
SILLY_SCLOSE, //close from client
SILLY_SCONNECTED, //async connect result
SILLY_SDATA, //data packet(raw) from client
SILLY_SUDP, //data packet(raw) from client(udp)
SILLY_SCLOSE = 3, //close from client
SILLY_SCONNECTED = 4, //async connect result
SILLY_SDATA = 5, //data packet(raw) from client
SILLY_SUDP = 6, //data packet(raw) from client(udp)
SILLY_SIGNAL = 7, //signal
};

struct silly_message {
Expand All @@ -75,6 +77,11 @@ struct silly_message_socket { //socket accept
uint8_t *data;
};

struct silly_message_signal { //signal
COMMONFIELD
int signum;
};

static inline void
silly_message_free(struct silly_message *msg)
{
Expand Down
23 changes: 0 additions & 23 deletions silly-src/silly_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,6 @@ pidfile_delete(const struct silly_config *conf)
return ;
}

static inline void
logfileopen(const struct silly_config *conf)
{
int fd;
fd = open(conf->logpath, O_CREAT | O_WRONLY | O_APPEND, 00666);
if (fd >= 0) {
dup2(fd, 1);
dup2(fd, 2);
close(fd);
setvbuf(stdout, NULL, _IOFBF, LOG_BUF_SIZE);
setvbuf(stderr, NULL, _IOLBF, LOG_BUF_SIZE);

}
}

void
silly_daemon_start(const struct silly_config *conf)
{
Expand All @@ -93,14 +78,6 @@ silly_daemon_start(const struct silly_config *conf)
exit(0);
}
pidfile_write();
logfileopen(conf);
return ;
}

void
silly_daemon_sigusr1(const struct silly_config *conf)
{
logfileopen(conf);
return ;
}

Expand Down
1 change: 0 additions & 1 deletion silly-src/silly_daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define _SILLY_DAEMON_H

void silly_daemon_start(const struct silly_config *conf);
void silly_daemon_sigusr1(const struct silly_config *conf);
void silly_daemon_stop(const struct silly_config *conf);


Expand Down
Loading

0 comments on commit 228f784

Please sign in to comment.