- added dont_daemonize feature. When a process manager is responsible for running processes then is not happy when spawned process is terminated almost immediately. Manager will try restart ser in infinite loop.
Cmd line params (ser.cfg) (backward compatible):
-D ... don't fork (dont_fork=1)
-DD ... don't daemonize creator process (dont_fork=2)
-DDD | or no -D param ... normal daemonizetion (dont_fork>2 or 0)
Note there is problem when Ctrl-C in dont_daemonize mode, not all processes are terminated
Creator_pid variable..pid of creator process, may be terminated
... | ... |
@@ -340,10 +340,11 @@ endif |
340 | 340 |
# adding it to via, lumps a.s.o). WARNING: right now this option |
341 | 341 |
# is useless since the compression code doesn't exist yet. |
342 | 342 |
|
343 |
+# Sometimes is needes correct non-quoted $OS. HACK: gcc translates known OS to number ('linux'), so there is added underscore |
|
343 | 344 |
|
344 | 345 |
DEFS+= $(extra_defs) \ |
345 | 346 |
-DNAME='"$(MAIN_NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \ |
346 |
- -DOS='"$(OS)"' -DCOMPILER='"$(CC_VER)"' -D__CPU_$(ARCH) -D__OS_$(OS) \ |
|
347 |
+ -DOS='$(OS)_' -DOS_QUOTED='"$(OS)"' -DCOMPILER='"$(CC_VER)"' -D__CPU_$(ARCH) -D__OS_$(OS) \ |
|
347 | 348 |
-DSER_VER=$(SER_VER) \ |
348 | 349 |
-DCFG_DIR='"$(cfg-target)"'\ |
349 | 350 |
-DPKG_MALLOC \ |
... | ... |
@@ -77,11 +77,11 @@ |
77 | 77 |
#define CONTENT_LENGTH_LEN (sizeof(CONTENT_LENGTH)-1) |
78 | 78 |
|
79 | 79 |
#define USER_AGENT "User-Agent: Sip EXpress router"\ |
80 |
- "(" VERSION " (" ARCH "/" OS"))" |
|
80 |
+ "(" VERSION " (" ARCH "/" OS_QUOTED "))" |
|
81 | 81 |
#define USER_AGENT_LEN (sizeof(USER_AGENT)-1) |
82 | 82 |
|
83 | 83 |
#define SERVER_HDR "Server: Sip EXpress router "\ |
84 |
- "(" VERSION " (" ARCH "/" OS"))" |
|
84 |
+ "(" VERSION " (" ARCH "/" OS_QUOTED "))" |
|
85 | 85 |
#define SERVER_HDR_LEN (sizeof(SERVER_HDR)-1) |
86 | 86 |
|
87 | 87 |
#define MAX_WARNING_LEN 256 |
... | ... |
@@ -59,13 +59,12 @@ |
59 | 59 |
#include "daemonize.h" |
60 | 60 |
#include "globals.h" |
61 | 61 |
#include "dprint.h" |
62 |
+#include "signals.h" |
|
62 | 63 |
|
63 | 64 |
|
64 | 65 |
#define MAX_FD 32 /* maximum number of inherited open file descriptors, |
65 | 66 |
(normally it shouldn't be bigger than 3) */ |
66 | 67 |
|
67 |
- |
|
68 |
- |
|
69 | 68 |
/* daemon init, return 0 on success, -1 on error */ |
70 | 69 |
int daemonize(char* name) |
71 | 70 |
{ |
... | ... |
@@ -92,29 +91,30 @@ int daemonize(char* name) |
92 | 91 |
goto error; |
93 | 92 |
} |
94 | 93 |
|
95 |
- /* fork to become!= group leader*/ |
|
96 |
- if ((pid=fork())<0){ |
|
97 |
- LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno)); |
|
98 |
- goto error; |
|
99 |
- }else if (pid!=0){ |
|
100 |
- /* parent process => exit*/ |
|
101 |
- exit(0); |
|
102 |
- } |
|
103 |
- /* become session leader to drop the ctrl. terminal */ |
|
104 |
- if (setsid()<0){ |
|
105 |
- LOG(L_WARN, "setsid failed: %s\n",strerror(errno)); |
|
106 |
- }else{ |
|
107 |
- own_pgid=1;/* we have our own process group */ |
|
108 |
- } |
|
109 |
- /* fork again to drop group leadership */ |
|
110 |
- if ((pid=fork())<0){ |
|
111 |
- LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno)); |
|
112 |
- goto error; |
|
113 |
- }else if (pid!=0){ |
|
114 |
- /*parent process => exit */ |
|
115 |
- exit(0); |
|
94 |
+ if (!dont_daemonize) { |
|
95 |
+ /* fork to become!= group leader*/ |
|
96 |
+ if ((pid=fork())<0){ |
|
97 |
+ LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno)); |
|
98 |
+ goto error; |
|
99 |
+ }else if (pid!=0){ |
|
100 |
+ /*parent process => exit */ |
|
101 |
+ exit(0); |
|
102 |
+ } |
|
103 |
+ /* become session leader to drop the ctrl. terminal */ |
|
104 |
+ if (setsid()<0){ |
|
105 |
+ LOG(L_WARN, "setsid failed: %s\n",strerror(errno)); |
|
106 |
+ }else{ |
|
107 |
+ own_pgid=1;/* we have our own process group */ |
|
108 |
+ } |
|
109 |
+ /* fork again to drop group leadership */ |
|
110 |
+ if ((pid=fork())<0){ |
|
111 |
+ LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno)); |
|
112 |
+ goto error; |
|
113 |
+ }else if (pid!=0){ |
|
114 |
+ /*parent process => exit */ |
|
115 |
+ exit(0); |
|
116 |
+ } |
|
116 | 117 |
} |
117 |
- |
|
118 | 118 |
/* added by noh: create a pid file for the main process */ |
119 | 119 |
if (pid_file!=0){ |
120 | 120 |
|
... | ... |
@@ -162,7 +162,7 @@ Options:\n\ |
162 | 162 |
(to use both use `-rR`)\n\ |
163 | 163 |
-v Turn on \"via:\" host checking when forwarding replies\n\ |
164 | 164 |
-d Debugging mode (multiple -d increase the level)\n\ |
165 |
- -D Do not fork into daemon mode\n\ |
|
165 |
+ -D 1..do not fork (almost) anyway, 2..do not daemonize creator, 3..daemonize(default)\n\ |
|
166 | 166 |
-E Log to stderr\n" |
167 | 167 |
#ifdef USE_TCP |
168 | 168 |
" -T Disable tcp\n\ |
... | ... |
@@ -244,6 +244,7 @@ struct process_table *pt=0; /*array with children pids, 0= main proc, |
244 | 244 |
int sig_flag = 0; /* last signal received */ |
245 | 245 |
int debug = L_DEFAULT; /* print only msg. < L_WARN */ |
246 | 246 |
int dont_fork = 0; |
247 |
+int dont_daemonize = 0; |
|
247 | 248 |
int log_stderr = 0; |
248 | 249 |
pid_t creator_pid = (pid_t) -1; |
249 | 250 |
/* log facility (see syslog(3)) */ |
... | ... |
@@ -1210,6 +1211,7 @@ int main(int argc, char** argv) |
1210 | 1211 |
unsigned int seed; |
1211 | 1212 |
int rfd; |
1212 | 1213 |
int debug_save, debug_flag = 0; |
1214 |
+ int dont_fork_cnt = 0; |
|
1213 | 1215 |
|
1214 | 1216 |
/*init*/ |
1215 | 1217 |
creator_pid = getpid(); |
... | ... |
@@ -1416,7 +1418,7 @@ try_again: |
1416 | 1418 |
received_dns|=DO_REV_DNS; |
1417 | 1419 |
break; |
1418 | 1420 |
case 'D': |
1419 |
- dont_fork=1; |
|
1421 |
+ dont_fork_cnt++; |
|
1420 | 1422 |
break; |
1421 | 1423 |
case 'T': |
1422 | 1424 |
#ifdef USE_TCP |
... | ... |
@@ -1477,6 +1479,13 @@ try_again: |
1477 | 1479 |
} |
1478 | 1480 |
} |
1479 | 1481 |
|
1482 |
+ if (dont_fork_cnt) |
|
1483 |
+ dont_fork = dont_fork_cnt; /* override by command line */ |
|
1484 |
+ |
|
1485 |
+ if (dont_fork > 0) { |
|
1486 |
+ dont_daemonize = dont_fork == 2; |
|
1487 |
+ dont_fork = dont_fork == 1; |
|
1488 |
+ } |
|
1480 | 1489 |
/* init the resolver, before fixing the config */ |
1481 | 1490 |
resolv_init(); |
1482 | 1491 |
/* fix parameters */ |
... | ... |
@@ -31,8 +31,7 @@ |
31 | 31 |
#ifndef version_h |
32 | 32 |
#define version_h |
33 | 33 |
|
34 |
-#define SER_FULL_VERSION NAME " " VERSION " (" ARCH "/" OS ")" |
|
35 |
- |
|
34 |
+#define SER_FULL_VERSION NAME " " VERSION " (" ARCH "/" OS_QUOTED ")" |
|
36 | 35 |
|
37 | 36 |
#ifdef STATS |
38 | 37 |
#define STATS_STR "STATS: On" |
... | ... |
@@ -186,7 +185,6 @@ |
186 | 185 |
#define USE_SYSV_SEM_STR "" |
187 | 186 |
#endif |
188 | 187 |
|
189 |
- |
|
190 | 188 |
#ifdef NOSMP |
191 | 189 |
#define NOSMP_STR "-NOSMP" |
192 | 190 |
#else |