Blame SOURCES/0010-Improve-daemonizing.patch

bd689f
From 569e7078244470ac0fcc2af3947c2735338555ec Mon Sep 17 00:00:00 2001
bd689f
From: Martin Sehnoutka <msehnout@redhat.com>
bd689f
Date: Wed, 7 Sep 2016 11:29:29 +0200
bd689f
Subject: [PATCH 10/59] Improve daemonizing
bd689f
bd689f
Init script gets correct return code if binding fails.
bd689f
---
bd689f
 standalone.c | 38 +++++++++++++++++++++++++++++++++++++-
bd689f
 sysutil.c    | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bd689f
 sysutil.h    |  7 ++++++-
bd689f
 3 files changed, 102 insertions(+), 2 deletions(-)
bd689f
bd689f
diff --git a/standalone.c b/standalone.c
bd689f
index e0f2d5b..3b65ea2 100644
bd689f
--- a/standalone.c
bd689f
+++ b/standalone.c
bd689f
@@ -26,6 +26,8 @@ static unsigned int s_ipaddr_size;
bd689f
 
bd689f
 static void handle_sigchld(void*  duff);
bd689f
 static void handle_sighup(void*  duff);
bd689f
+static void handle_sigusr1(int sig);
bd689f
+static void handle_sigalrm(int sig);
bd689f
 static void prepare_child(int sockfd);
bd689f
 static unsigned int handle_ip_count(void* p_raw_addr);
bd689f
 static void drop_ip_count(void* p_raw_addr);
bd689f
@@ -46,11 +48,23 @@ vsf_standalone_main(void)
bd689f
   }
bd689f
   if (tunable_background)
bd689f
   {
bd689f
+    vsf_sysutil_sigaction(kVSFSysUtilSigALRM, handle_sigalrm);
bd689f
+    vsf_sysutil_sigaction(kVSFSysUtilSigUSR1, handle_sigusr1);
bd689f
+
bd689f
     int forkret = vsf_sysutil_fork();
bd689f
     if (forkret > 0)
bd689f
     {
bd689f
       /* Parent, just exit */
bd689f
-      vsf_sysutil_exit(0);
bd689f
+      vsf_sysutil_set_alarm(3);
bd689f
+      vsf_sysutil_pause();
bd689f
+
bd689f
+      vsf_sysutil_exit(1);
bd689f
+    }
bd689f
+    else if (forkret == 0)
bd689f
+    {
bd689f
+      // Son, restore original signal handler
bd689f
+      vsf_sysutil_sigaction(kVSFSysUtilSigALRM, 0L);
bd689f
+      vsf_sysutil_sigaction(kVSFSysUtilSigUSR1, 0L);
bd689f
     }
bd689f
     /* Son, close standard FDs to avoid SSH hang-on-exit */
bd689f
     vsf_sysutil_reopen_standard_fds();
bd689f
@@ -99,6 +113,10 @@ vsf_standalone_main(void)
bd689f
     {
bd689f
       die("could not bind listening IPv4 socket");
bd689f
     }
bd689f
+    if (tunable_background)
bd689f
+    {
bd689f
+      vsf_sysutil_kill(vsf_sysutil_getppid(), kVSFSysUtilSigUSR1);
bd689f
+    }
bd689f
   }
bd689f
   else
bd689f
   {
bd689f
@@ -129,6 +147,10 @@ vsf_standalone_main(void)
bd689f
     {
bd689f
       die("could not bind listening IPv6 socket");
bd689f
     }
bd689f
+    if (tunable_background)
bd689f
+    {
bd689f
+      vsf_sysutil_kill(vsf_sysutil_getppid(), kVSFSysUtilSigUSR1);
bd689f
+    }
bd689f
   }
bd689f
   vsf_sysutil_close(0);
bd689f
   vsf_sysutil_close(1);
bd689f
@@ -268,6 +290,20 @@ handle_sighup(void* duff)
bd689f
   vsf_parseconf_load_file(0, 0);
bd689f
 }
bd689f
 
bd689f
+static void
bd689f
+handle_sigalrm(int sig)
bd689f
+{
bd689f
+  (void)sig; // avoid unused parameter error
bd689f
+  vsf_sysutil_exit(1);
bd689f
+}
bd689f
+
bd689f
+static void
bd689f
+handle_sigusr1(int sig)
bd689f
+{
bd689f
+  (void)sig; // avoid unused parameter error
bd689f
+  vsf_sysutil_exit(0);
bd689f
+}
bd689f
+
bd689f
 static unsigned int
bd689f
 hash_ip(unsigned int buckets, void* p_key)
bd689f
 {
bd689f
diff --git a/sysutil.c b/sysutil.c
bd689f
index 428a34a..c848356 100644
bd689f
--- a/sysutil.c
bd689f
+++ b/sysutil.c
bd689f
@@ -201,6 +201,9 @@ vsf_sysutil_translate_sig(const enum EVSFSysUtilSignal sig)
bd689f
     case kVSFSysUtilSigHUP:
bd689f
       realsig = SIGHUP;
bd689f
       break;
bd689f
+    case kVSFSysUtilSigUSR1:
bd689f
+      realsig = SIGUSR1;
bd689f
+      break;
bd689f
     default:
bd689f
       bug("unknown signal in vsf_sysutil_translate_sig");
bd689f
       break;
bd689f
@@ -549,6 +552,12 @@ vsf_sysutil_getpid(void)
bd689f
   return (unsigned int) s_current_pid;
bd689f
 }
bd689f
 
bd689f
+unsigned int
bd689f
+vsf_sysutil_getppid(void)
bd689f
+{
bd689f
+  return (unsigned int)getppid();
bd689f
+}
bd689f
+
bd689f
 int
bd689f
 vsf_sysutil_fork(void)
bd689f
 {
bd689f
@@ -2871,3 +2880,53 @@ vsf_sysutil_post_fork()
bd689f
     s_sig_details[i].pending = 0;
bd689f
   }
bd689f
 }
bd689f
+
bd689f
+static struct sigaction sigalr, sigusr1;
bd689f
+
bd689f
+void
bd689f
+vsf_sysutil_sigaction(const enum EVSFSysUtilSignal sig, void (*p_handlefunc)(int))
bd689f
+{
bd689f
+  int realsig = vsf_sysutil_translate_sig(sig);
bd689f
+  int retval;
bd689f
+  struct sigaction sigact, *origsigact=NULL;
bd689f
+  if (realsig==SIGALRM)
bd689f
+  {
bd689f
+    origsigact = &sigalr;
bd689f
+  }
bd689f
+  else if (realsig==SIGUSR1)
bd689f
+  {
bd689f
+    origsigact = &sigusr1;
bd689f
+  }
bd689f
+  vsf_sysutil_memclr(&sigact, sizeof(sigact));
bd689f
+  if (p_handlefunc != NULL)
bd689f
+  {
bd689f
+    sigact.sa_handler = p_handlefunc;
bd689f
+    retval = sigfillset(&sigact.sa_mask);
bd689f
+    if (retval != 0)
bd689f
+    {
bd689f
+      die("sigfillset");
bd689f
+    }
bd689f
+    retval = sigaction(realsig, &sigact, origsigact);
bd689f
+  }
bd689f
+  else
bd689f
+  {
bd689f
+    retval = sigaction(realsig, origsigact, NULL);
bd689f
+  }
bd689f
+  if (retval != 0)
bd689f
+  {
bd689f
+    die("sigaction");
bd689f
+  }
bd689f
+}
bd689f
+
bd689f
+int
bd689f
+vsf_sysutil_kill(int pid, int sig)
bd689f
+{
bd689f
+  int realsig = vsf_sysutil_translate_sig(sig);
bd689f
+  return kill(pid, realsig);
bd689f
+}
bd689f
+
bd689f
+int
bd689f
+vsf_sysutil_pause()
bd689f
+{
bd689f
+  return pause();
bd689f
+}
bd689f
diff --git a/sysutil.h b/sysutil.h
bd689f
index c2ddd15..bfc92cb 100644
bd689f
--- a/sysutil.h
bd689f
+++ b/sysutil.h
bd689f
@@ -30,7 +30,8 @@ enum EVSFSysUtilSignal
bd689f
   kVSFSysUtilSigCHLD,
bd689f
   kVSFSysUtilSigPIPE,
bd689f
   kVSFSysUtilSigURG,
bd689f
-  kVSFSysUtilSigHUP
bd689f
+  kVSFSysUtilSigHUP,
bd689f
+  kVSFSysUtilSigUSR1
bd689f
 };
bd689f
 enum EVSFSysUtilInterruptContext
bd689f
 {
bd689f
@@ -165,6 +166,7 @@ void vsf_sysutil_free(void* p_ptr);
bd689f
 
bd689f
 /* Process creation/exit/process handling */
bd689f
 unsigned int vsf_sysutil_getpid(void);
bd689f
+unsigned int vsf_sysutil_getppid(void);
bd689f
 void vsf_sysutil_post_fork(void);
bd689f
 int vsf_sysutil_fork(void);
bd689f
 int vsf_sysutil_fork_failok(void);
bd689f
@@ -182,6 +184,9 @@ int vsf_sysutil_wait_exited_normally(
bd689f
   const struct vsf_sysutil_wait_retval* p_waitret);
bd689f
 int vsf_sysutil_wait_get_exitcode(
bd689f
   const struct vsf_sysutil_wait_retval* p_waitret);
bd689f
+void vsf_sysutil_sigaction(const enum EVSFSysUtilSignal sig, void (*p_handlefunc)(int));
bd689f
+int vsf_sysutil_kill(int pid, int sig);
bd689f
+int vsf_sysutil_pause();
bd689f
 
bd689f
 /* Various string functions */
bd689f
 unsigned int vsf_sysutil_strlen(const char* p_text);
bd689f
-- 
bd689f
2.14.4
bd689f