Skip to content

Commit

Permalink
* ADD [broker] Add signal handler in broker
Browse files Browse the repository at this point in the history
Signed-off-by: Moi Ran <maoyi.ran@emqx.io>
  • Loading branch information
RanMaoyi committed Nov 15, 2023
1 parent a926e05 commit eef8b4c
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions nanomq/apps/broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#include <stdio.h>
#include <stdlib.h>

#if !defined(NANO_PLATFORM_WINDOWS)
#include <signal.h>
#endif

#include "nng/mqtt/mqtt_client.h"
#include "nng/protocol/mqtt/nmq_mqtt.h"
#include "nng/supplemental/tls/tls.h"
Expand Down Expand Up @@ -64,6 +60,38 @@
// be set in the thousands, each context consumes a couple of KB.) Recommend to
// set as the same as your CPU cores.

#if !defined(NANO_PLATFORM_WINDOWS)
#include <signal.h>
#include <errno.h>
static const all_signals[] = {

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable all_signals is never read.
#ifdef SIGHUP
SIGHUP,
#endif
#ifdef SIGQUIT
SIGQUIT,
#endif
#ifdef SIGTRAP
SIGTRAP,
#endif
#ifdef SIGIO
SIGIO,
#endif
SIGABRT,
SIGFPE,
SIGILL,
SIGINT,
SIGSEGV,
SIGTERM
};

int sig_handler(int signum)
{
log_error("signal caught!!!! signumber: %d\n", signum);
exit(EXIT_FAILURE);
}
#endif


enum options {
OPT_HELP = 1,
OPT_HOCONFILE = 2, /* Do not change this value, it is used beyond this file. */
Expand Down Expand Up @@ -147,22 +175,8 @@ static nng_optspec cmd_opts[] = {

#if (defined DEBUG) && (defined ASAN)
int keepRunning = 1;
void
intHandler(int dummy)
{
keepRunning = 0;
fprintf(stderr, "\nBroker exit(0).\n");
}
#endif

void
termHandler(int dummy)
{
fprintf(stderr, "\nReceive SIGTERM signal.");
fprintf(stderr, "\nBroker exit(0).\n");
exit(EXIT_FAILURE);
}

static inline bool
bridge_handler(nano_work *work)
{
Expand Down Expand Up @@ -1114,8 +1128,19 @@ broker(conf *nanomq_conf)

#if (defined DEBUG) && (defined ASAN)
#if !(defined NANO_PLATFORM_WINDOWS)
signal(SIGTERM, termHandler);
signal(SIGINT, intHandler);
struct sigaction act;
i = 0;

memset(&act, 0, sizeof act);
sigemptyset(&act.sa_mask);
act.sa_handler = sig_handler;
act.sa_flags = 0;

do {
if (sigaction(all_signals[i], &act, NULL)) {
fprintf(stderr, "Cannot install signal %d handler: %s.\n", all_signals[i], strerror(errno));
}
} while (all_signals[i++] != SIGTERM);
#endif

if (is_testing == true) {
Expand Down

0 comments on commit eef8b4c

Please sign in to comment.