Clean signal handling

This commit is contained in:
Thomas Bernard 2012-05-24 18:17:31 +02:00
parent 489a178865
commit fe37de7e2c
8 changed files with 56 additions and 26 deletions

View File

@ -1,4 +1,7 @@
$Id: Changelog.txt,v 1.27 2012/05/18 10:35:11 nanard Exp $
$Id: Changelog.txt,v 1.29 2012/05/23 08:56:35 nanard Exp $
2012/05/21:
Clean signal handling
2012/05/18:
Improve ProcessInterfaceWatch() under BSD.

View File

@ -1,4 +1,4 @@
/* $Id: minissdpd.c,v 1.31 2012/05/02 10:28:25 nanard Exp $ */
/* $Id: minissdpd.c,v 1.35 2012/05/21 17:13:11 nanard Exp $ */
/* MiniUPnP project
* (c) 2007-2012 Thomas Bernard
* website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
@ -760,14 +760,21 @@ error:
return;
}
static volatile int quitting = 0;
static volatile sig_atomic_t quitting = 0;
/* SIGTERM signal handler */
static void
sigterm(int sig)
{
signal(sig, SIG_IGN);
(void)sig;
/*int save_errno = errno;*/
/*signal(sig, SIG_IGN);*/
#if 0
/* calling syslog() is forbidden in a signal handler according to
* signal(3) */
syslog(LOG_NOTICE, "received signal %d, good-bye", sig);
#endif
quitting = 1;
/*errno = save_errno;*/
}
/* main(): program entry point */
@ -974,8 +981,11 @@ int main(int argc, char * * argv)
if(select(FD_SETSIZE, &readfds, 0, 0, 0) < 0)
{
if(errno != EINTR)
{
syslog(LOG_ERR, "select: %m");
break;
break; /* quit */
}
continue; /* try again */
}
#ifdef ENABLE_IPV6
if((s_ssdp6 >= 0) && FD_ISSET(s_ssdp6, &readfds))

View File

@ -1,4 +1,4 @@
/* $Id: minihttptestserver.c,v 1.10 2012/05/01 16:24:36 nanard Exp $ */
/* $Id: minihttptestserver.c,v 1.11 2012/05/21 08:47:38 nanard Exp $ */
/* Project : miniUPnP
* Author : Thomas Bernard
* Copyright (c) 2011 Thomas Bernard
@ -19,8 +19,8 @@
#define CRAP_LENGTH (2048)
volatile int quit = 0;
volatile int child_to_wait_for = 0;
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t child_to_wait_for = 0;
/**
* signal handler for SIGCHLD (child status has changed)

View File

@ -1,4 +1,7 @@
$Id: Changelog.txt,v 1.282 2012/05/08 20:41:44 nanard Exp $
$Id: Changelog.txt,v 1.283 2012/05/21 15:57:17 nanard Exp $
2012/05/21:
Clean signal handling
2012/05/08:
Clean expired IPv6 pinholes correctly. and also with linux/netfilter.

View File

@ -1,4 +1,4 @@
/* $Id: ifacewatcher.c,v 1.3 2011/06/04 16:19:51 nanard Exp $ */
/* $Id: ifacewatcher.c,v 1.5 2012/05/21 08:55:10 nanard Exp $ */
/* Project MiniUPnP
* web : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2011 Thomas BERNARD
@ -13,6 +13,7 @@
#include <net/if.h>
#include <net/route.h>
#include <syslog.h>
#include <signal.h>
#if !defined(SA_LEN)
#define SA_LEN(sa) (sa)->sa_len
@ -24,7 +25,7 @@
#include "../upnputils.h"
#include "../upnpglobalvars.h"
extern volatile int should_send_public_address_change_notif;
extern volatile sig_atomic_t should_send_public_address_change_notif;
int
OpenAndConfInterfaceWatchSocket(void)

View File

@ -1,4 +1,4 @@
/* $Id: testifacewatcher.c,v 1.1 2011/05/20 09:34:25 nanard Exp $ */
/* $Id: testifacewatcher.c,v 1.2 2012/05/21 08:55:10 nanard Exp $ */
#include <syslog.h>
@ -9,7 +9,7 @@ void
ProcessInterfaceWatchNotify(int s);
const char * ext_if_name;
volatile int should_send_public_address_change_notif = 0;
volatile sig_atomic_t should_send_public_address_change_notif = 0;
int main(int argc, char * * argv)
{

View File

@ -1,4 +1,4 @@
/* $Id: ifacewatcher.c,v 1.3 2012/02/04 23:34:40 nanard Exp $ */
/* $Id: ifacewatcher.c,v 1.6 2012/05/22 22:09:51 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2012 Thomas Bernard
@ -46,6 +46,7 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include "../config.h"
@ -57,7 +58,7 @@
#include "../upnpglobalvars.h"
#include "../natpmp.h"
extern volatile int should_send_public_address_change_notif;
extern volatile sig_atomic_t should_send_public_address_change_notif;
int

View File

@ -1,4 +1,4 @@
/* $Id: miniupnpd.c,v 1.157 2012/05/09 10:41:16 nanard Exp $ */
/* $Id: miniupnpd.c,v 1.161 2012/05/21 15:50:03 nanard Exp $ */
/* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2012 Thomas Bernard
@ -49,6 +49,7 @@
#include <sys/un.h>
#endif
#include "macros.h"
#include "upnpglobalvars.h"
#include "upnphttp.h"
#include "upnpdescgen.h"
@ -99,8 +100,8 @@ int get_udp_dst_port (char *payload);
#endif
/* variables used by signals */
static volatile int quitting = 0;
volatile int should_send_public_address_change_notif = 0;
static volatile sig_atomic_t quitting = 0;
volatile sig_atomic_t should_send_public_address_change_notif = 0;
/* OpenAndConfHTTPSocket() :
* setup the socket used to handle incoming HTTP connections. */
@ -422,10 +423,16 @@ write_command_line(int fd, int argc, char * * argv)
static void
sigterm(int sig)
{
/*int save_errno = errno;*/
signal(sig, SIG_IGN); /* Ignore this signal while we are quitting */
UNUSED(sig);
/*int save_errno = errno; */
/*signal(sig, SIG_IGN);*/ /* Ignore this signal while we are quitting */
/* Note : isn't it useless ? */
#if 0
/* calling syslog() is forbidden in signal handler according to
* signal(3) */
syslog(LOG_NOTICE, "received signal %d, good-bye", sig);
#endif
quitting = 1;
/*errno = save_errno;*/
@ -435,7 +442,12 @@ sigterm(int sig)
static void
sigusr1(int sig)
{
UNUSED(sig);
#if 0
/* calling syslog() is forbidden in signal handler according to
* signal(3) */
syslog(LOG_INFO, "received signal %d, public ip address change", sig);
#endif
should_send_public_address_change_notif = 1;
}
@ -1041,23 +1053,23 @@ init(int argc, char * * argv, struct runtime_vars * v)
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = sigterm;
if (sigaction(SIGTERM, &sa, NULL))
if(sigaction(SIGTERM, &sa, NULL) < 0)
{
syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGTERM");
return 1;
}
if (sigaction(SIGINT, &sa, NULL))
if(sigaction(SIGINT, &sa, NULL) < 0)
{
syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGINT");
return 1;
}
if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
sa.sa_handler = SIG_IGN;
if(sigaction(SIGPIPE, &sa, NULL) < 0)
{
syslog(LOG_ERR, "Failed to ignore SIGPIPE signals");
}
sa.sa_handler = sigusr1;
if (sigaction(SIGUSR1, &sa, NULL))
if(sigaction(SIGUSR1, &sa, NULL) < 0)
{
syslog(LOG_NOTICE, "Failed to set %s handler", "SIGUSR1");
}