summaryrefslogtreecommitdiff
path: root/0010-Improve-daemonizing.patch
blob: d2de7674c1cb13abca3c391aa3a5e3e50510bb30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
From 569e7078244470ac0fcc2af3947c2735338555ec Mon Sep 17 00:00:00 2001
From: Martin Sehnoutka <msehnout@redhat.com>
Date: Wed, 7 Sep 2016 11:29:29 +0200
Subject: [PATCH 10/59] Improve daemonizing

Init script gets correct return code if binding fails.
---
 standalone.c | 38 +++++++++++++++++++++++++++++++++++++-
 sysutil.c    | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sysutil.h    |  7 ++++++-
 3 files changed, 102 insertions(+), 2 deletions(-)

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