Added proper POSIX signal handler.

This commit is contained in:
Jan Vidar Krey 2010-01-22 16:57:30 +01:00
parent 4711d26c11
commit 68a26e1160
1 changed files with 15 additions and 17 deletions

View File

@ -33,13 +33,13 @@ static const char* arg_pid = 0;
static int arg_log_syslog = 0; static int arg_log_syslog = 0;
#if !defined(WIN32) && defined(USE_LIBEVENT) #if !defined(WIN32)
void hub_handle_signal(int fd, short events, void* arg) extern struct hub_info* g_hub;
void hub_handle_signal(int sig)
{ {
struct hub_info* hub = (struct hub_info*) arg; struct hub_info* hub = g_hub;
int signal = fd;
switch (signal) switch (sig)
{ {
case SIGINT: case SIGINT:
LOG_INFO("Interrupted. Shutting down..."); LOG_INFO("Interrupted. Shutting down...");
@ -65,7 +65,6 @@ void hub_handle_signal(int fd, short events, void* arg)
} }
} }
static struct event signal_events[10];
static int signals[] = static int signals[] =
{ {
SIGINT, /* Interrupt the application */ SIGINT, /* Interrupt the application */
@ -77,11 +76,16 @@ static int signals[] =
void setup_signal_handlers(struct hub_info* hub) void setup_signal_handlers(struct hub_info* hub)
{ {
sigset_t sig_set;
sigemptyset(&sig_set);
struct sigaction act;
act.sa_mask = sig_set;
act.sa_flags = SA_ONSTACK | SA_RESTART;
act.sa_handler = hub_handle_signal;
int i = 0; int i = 0;
for (i = 0; signals[i]; i++) for (i = 0; signals[i]; i++)
{ {
signal_set(&signal_events[i], signals[i], hub_handle_signal, hub); if (sigaction(signals[i], &act, 0) != 0)
if (signal_add(&signal_events[i], NULL))
{ {
LOG_ERROR("Error setting signal handler %d", signals[i]); LOG_ERROR("Error setting signal handler %d", signals[i]);
} }
@ -90,14 +94,8 @@ void setup_signal_handlers(struct hub_info* hub)
void shutdown_signal_handlers(struct hub_info* hub) void shutdown_signal_handlers(struct hub_info* hub)
{ {
int i = 0;
for (i = 0; signals[i]; i++)
{
signal_del(&signal_events[i]);
}
} }
#endif /* !WIN32 */
#endif /* !WIN32 && USE_LIBEVENT*/
int main_loop() int main_loop()
@ -133,7 +131,7 @@ int main_loop()
hub = hub_start_service(&configuration); hub = hub_start_service(&configuration);
if (!hub) if (!hub)
return -1; return -1;
#if !defined(WIN32) && defined(USE_LIBEVENT) #if !defined(WIN32)
setup_signal_handlers(hub); setup_signal_handlers(hub);
#endif #endif
} }
@ -148,7 +146,7 @@ int main_loop()
} while (hub->status == hub_status_restart); } while (hub->status == hub_status_restart);
#if !defined(WIN32) && defined(USE_LIBEVENT) #if !defined(WIN32)
shutdown_signal_handlers(hub); shutdown_signal_handlers(hub);
#endif #endif