Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* ADD [broker] Add signal handler in broker #1530

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 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,41 @@
// 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);

if (signum == SIGINT) {
exit(EXIT_FAILURE);
}
}
JaylinYu marked this conversation as resolved.
Show resolved Hide resolved
#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 +178,8 @@

#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 +1131,19 @@

#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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wanghaEMQ want broker exit while receving SIGINT

#endif

if (is_testing == true) {
Expand Down
Loading