summaryrefslogtreecommitdiff
path: root/ninfod
diff options
context:
space:
mode:
authorYOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>2013-02-15 13:28:22 +0900
committerYOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>2013-02-15 14:04:52 +0900
commit7b2b88ac124bd60d495852a81feac33d9c158fa3 (patch)
tree097619b75e2d0b31b2bbafbf9d44c4d15baf65ab /ninfod
parentb39a4fe05109364e8a08db1661b7cbc139c7f916 (diff)
downloadiputils-7b2b88ac124bd60d495852a81feac33d9c158fa3.tar.gz
ninfod: Clean up signal/logfile handling
- Do real work outside the signal handler. - Setup logfile early. - Do not try unlinking pid file if filename is NULL (this is not a bug, because filename is alway set non-NULL by default.) Signed-off-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Diffstat (limited to 'ninfod')
-rw-r--r--ninfod/ninfod.c86
1 files changed, 52 insertions, 34 deletions
diff --git a/ninfod/ninfod.c b/ninfod/ninfod.c
index d1b99d9..ac7c21f 100644
--- a/ninfod/ninfod.c
+++ b/ninfod/ninfod.c
@@ -139,6 +139,7 @@ char *appname;
static int opt_d = 0; /* debug */
static int opt_h = 0; /* help */
static char *opt_p = NINFOD_PIDFILE; /* pidfile */
+static int got_signal = 0; /* loop unless true */
int opt_v = 0; /* verbose */
static uid_t opt_u;
@@ -391,25 +392,53 @@ int ni_send(struct packetcontext *p)
/* --------- */
static void sig_handler(int sig)
{
+ if (!got_signal)
+ DEBUG(LOG_INFO, "singnal(%d) received, quitting.\n", sig);
+ got_signal = 1;
+}
+
+static void setup_sighandlers(void)
+{
+ struct sigaction act;
+ sigset_t smask;
+ sigemptyset(&smask);
+ sigaddset(&smask, SIGHUP);
+ sigaddset(&smask, SIGINT);
+ sigaddset(&smask, SIGQUIT);
+ sigaddset(&smask, SIGTERM);
+
+ memset(&act, 0, sizeof(act));
+ act.sa_handler = sig_handler;
+ act.sa_mask = smask;
+
+ sigaction(SIGHUP, &act, NULL);
+ sigaction(SIGINT, &act, NULL);
+ sigaction(SIGQUIT, &act, NULL);
+ sigaction(SIGTERM, &act, NULL);
+}
+
+static void set_logfile(void)
+{
+ setbuf(stderr, NULL);
+#if ENABLE_DEBUG
+ openlog(NINFOD, 0, LOG_USER);
+#endif
+}
+
+static void cleanup_pidfile(void)
+{
int err;
- DEBUG(LOG_INFO, "singnal(%d) received, quit.\n", sig);
- err = unlink(opt_p);
- if (err < 0) {
+ if (daemonized && opt_p) {
+ err = unlink(opt_p);
DEBUG(LOG_ERR, "failed to unlink file '%s' : %s\n",
opt_p, strerror(errno));
- exit(1);
}
- /* closelog() */
-
- exit(0);
}
static void do_daemonize(void)
{
FILE *fp = NULL;
- struct sigaction act;
- sigset_t smask;
pid_t pid;
if (opt_p) {
@@ -427,21 +456,6 @@ static void do_daemonize(void)
}
}
- sigemptyset(&smask);
- sigaddset(&smask, SIGHUP);
- sigaddset(&smask, SIGINT);
- sigaddset(&smask, SIGQUIT);
- sigaddset(&smask, SIGTERM);
-
- memset(&act, 0, sizeof(act));
- act.sa_handler = sig_handler;
- act.sa_mask = smask;
-
- sigaction(SIGHUP, &act, NULL);
- sigaction(SIGINT, &act, NULL);
- sigaction(SIGQUIT, &act, NULL);
- sigaction(SIGTERM, &act, NULL);
-
fp = fopen(opt_p, "w+");
if (!fp) {
DEBUG(LOG_ERR, "failed to open file '%s': %s\n",
@@ -455,9 +469,6 @@ static void do_daemonize(void)
unlink(opt_p);
exit(1);
}
-#if ENABLE_DEBUG
- openlog(NINFOD, 0, LOG_USER);
-#endif
daemonized = 1;
if (fp) {
@@ -634,8 +645,10 @@ static void print_usage(void) {
int main (int argc, char **argv)
{
int sock_errno = 0;
+ int ret;
appname = argv[0];
+ set_logfile();
limit_capabilities();
@@ -659,19 +672,18 @@ int main (int argc, char **argv)
exit(1);
}
- setbuf(stderr, NULL);
-
- if (!opt_d)
- do_daemonize();
-
/* initialize */
if (init_sock(sock) < 0)
exit(1);
+ setup_sighandlers();
+ if (!opt_d)
+ do_daemonize();
+
init_core(1);
/* main loop */
- while(1) {
+ while (!got_signal) {
struct packetcontext *p;
struct icmp6_hdr *icmph;
#if ENABLE_DEBUG
@@ -689,11 +701,13 @@ int main (int argc, char **argv)
continue;
}
- while (1) {
+ while (!got_signal) {
memset(p, 0, sizeof(*p));
p->sock = sock;
if (ni_recv(p) < 0) {
+ if (got_signal)
+ break;
if (errno == EAGAIN || errno == EINTR)
continue;
/* XXX: syslog */
@@ -736,5 +750,9 @@ int main (int argc, char **argv)
pr_nodeinfo(p); /* this frees p */
}
+
+ cleanup_pidfile();
+
+ exit(0);
}