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