diff --git a/src/dnode/src/dnodeSystem.c b/src/dnode/src/dnodeSystem.c index a16ff826bf498f4fc56838a36f2ba0539b7ac5e1..bdaecf2e0f211d8c63bc35037190d5c7392e77aa 100644 --- a/src/dnode/src/dnodeSystem.c +++ b/src/dnode/src/dnodeSystem.c @@ -19,8 +19,10 @@ #include "tconfig.h" #include "dnodeMain.h" -static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context); static tsem_t exitSem; +static void siguser1Handler(int32_t signum); +static void siguser2Handler(int32_t signum); +static void sigintHandler(int32_t signum); int32_t main(int32_t argc, char *argv[]) { int dump_config = 0; @@ -28,7 +30,7 @@ int32_t main(int32_t argc, char *argv[]) { // Set global configuration file for (int32_t i = 1; i < argc; ++i) { if (strcmp(argv[i], "-c") == 0) { - if (i < argc - 1) { + if (i < argc - 1) { if (strlen(argv[++i]) >= TSDB_FILENAME_LEN) { printf("config file path overflow"); exit(EXIT_FAILURE); @@ -80,8 +82,8 @@ int32_t main(int32_t argc, char *argv[]) { taosSetRandomFileFailOutput(NULL); } } else if (strcmp(argv[i], "--random-file-fail-factor") == 0) { - if ( (i+1) < argc ) { - int factor = atoi(argv[i+1]); + if ((i + 1) < argc) { + int factor = atoi(argv[i + 1]); printf("The factor of random failure is %d\n", factor); taosSetRandomFileFailFactor(factor); } else { @@ -93,7 +95,7 @@ int32_t main(int32_t argc, char *argv[]) { } if (0 != dump_config) { - tscEmbedded = 1; + tscEmbedded = 1; taosInitGlobalCfg(); taosReadGlobalLogCfg(); @@ -112,14 +114,12 @@ int32_t main(int32_t argc, char *argv[]) { } /* Set termination handler. */ - struct sigaction act = {{0}}; - act.sa_flags = SA_SIGINFO; - act.sa_sigaction = signal_handler; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGHUP, &act, NULL); - sigaction(SIGINT, &act, NULL); - sigaction(SIGUSR1, &act, NULL); - sigaction(SIGUSR2, &act, NULL); + taosSetSignal(SIGUSR1, siguser1Handler); + taosSetSignal(SIGUSR2, siguser2Handler); + taosSetSignal(SIGTERM, sigintHandler); + taosSetSignal(SIGHUP, sigintHandler); + taosSetSignal(SIGINT, sigintHandler); + taosSetSignal(SIGABRT, sigintHandler); // Open /var/log/syslog file to record information. openlog("TDengine:", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL1); @@ -147,32 +147,25 @@ int32_t main(int32_t argc, char *argv[]) { return EXIT_SUCCESS; } -static void signal_handler(int32_t signum, siginfo_t *sigInfo, void *context) { - if (signum == SIGUSR1) { - taosCfgDynamicOptions("debugFlag 143"); - return; - } - if (signum == SIGUSR2) { - taosCfgDynamicOptions("resetlog"); - return; - } - - syslog(LOG_INFO, "Shut down signal is %d", signum); - syslog(LOG_INFO, "Shutting down TDengine service..."); - // clean the system. - dInfo("shut down signal is %d, sender PID:%d cmdline:%s", signum, sigInfo->si_pid, taosGetCmdlineByPID(sigInfo->si_pid)); - - // protect the application from receive another signal - struct sigaction act = {{0}}; -#ifndef WINDOWS - act.sa_handler = SIG_IGN; -#endif - sigaction(SIGTERM, &act, NULL); - sigaction(SIGHUP, &act, NULL); - sigaction(SIGINT, &act, NULL); - sigaction(SIGUSR1, &act, NULL); - sigaction(SIGUSR2, &act, NULL); - - // inform main thread to exit - tsem_post(&exitSem); +static void siguser1Handler(int32_t signum) { taosCfgDynamicOptions("debugFlag 143"); } + +static void siguser2Handler(int32_t signum) { taosCfgDynamicOptions("resetlog"); } + +static void sigintHandler(int32_t signum) { + // clean the system. + dInfo("shut down signal is %d", signum); + + syslog(LOG_INFO, "Shut down signal is %d", signum); + syslog(LOG_INFO, "Shutting down TDengine service..."); + + // protect the application from receive another signal + taosIgnSignal(SIGUSR1); + taosIgnSignal(SIGUSR2); + taosIgnSignal(SIGTERM); + taosIgnSignal(SIGHUP); + taosIgnSignal(SIGINT); + taosIgnSignal(SIGABRT); + + // inform main thread to exit + tsem_post(&exitSem); } \ No newline at end of file diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index cd801ef07a3f7a0000e76a1601ce7d6372864cac..c7a0dab2dd013608daac5691f5b8b2bc94104e0a 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -21,7 +21,7 @@ pthread_t pid; static tsem_t cancelSem; -void shellQueryInterruptHandler(int32_t signum, siginfo_t *sigInfo, void *context) { +void shellQueryInterruptHandler(int32_t signum) { tsem_post(&cancelSem); } @@ -130,12 +130,10 @@ int main(int argc, char* argv[]) { pthread_create(&spid, NULL, cancelHandler, NULL); /* Interrupt handler. */ - struct sigaction act; - memset(&act, 0, sizeof(struct sigaction)); - - act.sa_handler = shellQueryInterruptHandler; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGINT, &act, NULL); + taosSetSignal(SIGTERM, shellQueryInterruptHandler); + taosSetSignal(SIGINT, shellQueryInterruptHandler); + taosSetSignal(SIGHUP, shellQueryInterruptHandler); + taosSetSignal(SIGABRT, shellQueryInterruptHandler); /* Get grant information */ shellGetGrantInfo(con); diff --git a/src/os/inc/os.h b/src/os/inc/os.h index 9383ae48dc5c6151aea0d9e8a2641603b63da144..8312b74a5015aeb0991b303f0ea8f207cb2cf3eb 100644 --- a/src/os/inc/os.h +++ b/src/os/inc/os.h @@ -62,6 +62,7 @@ extern "C" { #include "osMemory.h" #include "osRand.h" #include "osSemphone.h" +#include "osSignal.h" #include "osSocket.h" #include "osString.h" #include "osSysinfo.h" diff --git a/src/os/inc/osSignal.h b/src/os/inc/osSignal.h new file mode 100644 index 0000000000000000000000000000000000000000..8b047b9e258122b91f1462fc244c1e58a44997b1 --- /dev/null +++ b/src/os/inc/osSignal.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef TDENGINE_OS_SIGNAL_H +#define TDENGINE_OS_SIGNAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "os.h" +#include "taosdef.h" +#include + +#ifndef SIGALRM + #define SIGALRM 1234 +#endif + +#ifndef SIGHUP + #define SIGHUP 1234 +#endif + +#ifndef SIGCHLD + #define SIGCHLD 1234 +#endif + +#ifndef SIGUSR1 + #define SIGUSR1 1234 +#endif + +#ifndef SIGUSR2 + #define SIGUSR2 1234 +#endif + +typedef void (*FSignalHandler)(int32_t signum); +void taosSetSignal(int32_t signum, FSignalHandler sigfp); +void taosIgnSignal(int32_t signum); +void taosDflSignal(int32_t signum); + +#ifdef __cplusplus +} +#endif + +#endif // TDENGINE_TTIME_H diff --git a/src/os/inc/osWindows.h b/src/os/inc/osWindows.h index 15b26d208d0a549facd751ceb1ccbb36b4f9c217..1b28cfa90f3201a655500f8661f3ff613c443392 100644 --- a/src/os/inc/osWindows.h +++ b/src/os/inc/osWindows.h @@ -191,24 +191,6 @@ int gettimeofday(struct timeval *ptv, void *pTimeZone); #define PATH_MAX 256 #endif -//for signal, not dispose -#define SIGALRM 1234 -#define SIGHUP 1234 -#define SIGUSR1 1234 -#define SIGUSR2 1234 -#define SA_SIGINFO 1234 - -typedef int sigset_t; -typedef struct siginfo_t { - int si_pid; -} siginfo_t; -struct sigaction { - int sa_flags; - void (*sa_handler)(int32_t signum, siginfo_t *sigInfo, void *context); - void (*sa_sigaction)(int32_t signum, siginfo_t *sigInfo, void *context); -}; -int sigaction(int, struct sigaction *, void *); - typedef struct { int we_wordc; char **we_wordv; diff --git a/src/os/src/detail/osSignal.c b/src/os/src/detail/osSignal.c new file mode 100644 index 0000000000000000000000000000000000000000..c97a3343de133b9ff6f6bab7d5647d2e3459d0b5 --- /dev/null +++ b/src/os/src/detail/osSignal.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#define _DEFAULT_SOURCE +#include "os.h" +#include "tconfig.h" +#include "tglobal.h" +#include "tulog.h" + +#ifndef TAOS_OS_FUNC_SIGNAL + +void taosSetSignal(int32_t signum, FSignalHandler sigfp) { + struct sigaction act = {{0}}; + act.sa_handler = sigfp; + sigaction(signum, &act, NULL); +} + +void taosIgnSignal(int32_t signum) { + signal(signum, SIG_IGN); +} + +void taosDflSignal(int32_t signum) { + signal(signum, SIG_DFL); +} + +#endif diff --git a/src/os/src/linux/linuxEnv.c b/src/os/src/linux/linuxEnv.c index e3eadbc94bcf71e7cf09e70901e4de0aed427c54..abcdabcfd503929d9e38aa9704d4bc7e4585ac1e 100644 --- a/src/os/src/linux/linuxEnv.c +++ b/src/os/src/linux/linuxEnv.c @@ -43,6 +43,7 @@ void osInit() { char cmdline[1024]; char* taosGetCmdlineByPID(int pid) { +#if 0 sprintf(cmdline, "/proc/%d/cmdline", pid); FILE* f = fopen(cmdline, "r"); if (f) { @@ -54,4 +55,7 @@ char* taosGetCmdlineByPID(int pid) { fclose(f); } return cmdline; +#else + return ""; +#endif } diff --git a/src/os/src/windows/wSysinfo.c b/src/os/src/windows/wSysinfo.c index c0245212193f580437b491f5d727ced8de9f6148..9a79f5a6bfb271057d20dfa9e185646a28e8b1c4 100644 --- a/src/os/src/windows/wSysinfo.c +++ b/src/os/src/windows/wSysinfo.c @@ -235,8 +235,6 @@ int taosSystem(const char *cmd) { int flock(int fd, int option) { return 0; } -int sigaction(int sig, struct sigaction *d, void *p) { return 0; } - LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) { typedef BOOL(WINAPI * FxMiniDumpWriteDump)(IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile, IN MINIDUMP_TYPE DumpType, diff --git a/src/sync/src/syncArbitrator.c b/src/sync/src/syncArbitrator.c index 06b4a61d6f9132f190af3c446bf1db72b4ca43cc..4fc5e2196c3e9f47f77dcb79e903e7c53feac710 100644 --- a/src/sync/src/syncArbitrator.c +++ b/src/sync/src/syncArbitrator.c @@ -27,7 +27,7 @@ #include "syncInt.h" #include "syncTcp.h" -static void arbSignalHandler(int32_t signum, siginfo_t *sigInfo, void *context); +static void arbSignalHandler(int32_t signum); static void arbProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp); static void arbProcessBrokenLink(int64_t rid); static int32_t arbProcessPeerMsg(int64_t rid, void *buffer); @@ -69,14 +69,10 @@ int32_t main(int32_t argc, char *argv[]) { } /* Set termination handler. */ - struct sigaction act = {{0}}; - act.sa_flags = SA_SIGINFO; - act.sa_sigaction = arbSignalHandler; - - act.sa_handler = arbSignalHandler; - sigaction(SIGTERM, &act, NULL); - sigaction(SIGHUP, &act, NULL); - sigaction(SIGINT, &act, NULL); + taosSetSignal(SIGTERM, arbSignalHandler); + taosSetSignal(SIGINT, arbSignalHandler); + taosSetSignal(SIGHUP, arbSignalHandler); + taosSetSignal(SIGABRT, arbSignalHandler); tsAsyncLog = 0; strcat(arbLogPath, "/arbitrator.log"); @@ -174,16 +170,13 @@ static int32_t arbProcessPeerMsg(int64_t rid, void *buffer) { return 0; } -static void arbSignalHandler(int32_t signum, siginfo_t *sigInfo, void *context) { - struct sigaction act = {{0}}; -#ifndef WINDOWS - act.sa_handler = SIG_IGN; -#endif - sigaction(SIGTERM, &act, NULL); - sigaction(SIGHUP, &act, NULL); - sigaction(SIGINT, &act, NULL); +static void arbSignalHandler(int32_t signum) { + taosIgnSignal(SIGTERM); + taosIgnSignal(SIGINT); + taosIgnSignal(SIGABRT); + taosIgnSignal(SIGHUP); - sInfo("shut down signal is %d, sender PID:%d", signum, sigInfo->si_pid); + sInfo("shut down signal is %d", signum); // inform main thread to exit tsem_post(&tsArbSem); diff --git a/tests/tsim/src/simExe.c b/tests/tsim/src/simExe.c index a23dff13be07c1a56da6d3051a4384ccfb96d9bb..10e87eefe6af5932ef42437bb6570403136d6383 100644 --- a/tests/tsim/src/simExe.c +++ b/tests/tsim/src/simExe.c @@ -314,9 +314,7 @@ bool simExecuteSystemCmd(SScript *script, char *option) { simError("script:%s, failed to execute %s , code %d, errno:%d %s, repeatTimes:%d", script->fileName, buf, code, errno, strerror(errno), repeatTimes); taosMsleep(1000); -#ifdef LINUX - signal(SIGCHLD, SIG_DFL); -#endif + taosDflSignal(SIGCHLD); if (repeatTimes++ >= 10) { exit(0); } diff --git a/tests/tsim/src/simMain.c b/tests/tsim/src/simMain.c index 8f13254f68165a9e26ecac2406538f8cba905120..de560901aaaf26478503132a1b5a84efe8dfcb5e 100644 --- a/tests/tsim/src/simMain.c +++ b/tests/tsim/src/simMain.c @@ -51,7 +51,7 @@ int32_t main(int32_t argc, char *argv[]) { } simInfo("simulator is running ..."); - signal(SIGINT, simHandleSignal); + taosSetSignal(SIGINT, simHandleSignal); SScript *script = simParseScript(scriptFile); if (script == NULL) {