Browse code

- OS macro defined in Makefile.defs left unquoted and added OS_QUOTED. OS macro maybe used by 3rd party header in regular way, w/o quotes

- 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

Tomas Mandys authored on 15/05/2006 09:56:40
Showing 6 changed files
... ...
@@ -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 92
 		goto error;
93 93
 	}
94 94
 
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);
95
+	if (!dont_daemonize) {
96
+		/* fork to become!= group leader*/
97
+		if ((pid=fork())<0){
98
+			LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
99
+			goto error;
100
+		}else if (pid!=0){	
101
+			/*parent process => exit */
102
+			exit(0);
103
+		}
104
+		/* become session leader to drop the ctrl. terminal */
105
+		if (setsid()<0){
106
+			LOG(L_WARN, "setsid failed: %s\n",strerror(errno));
107
+		}else{
108
+			own_pgid=1;/* we have our own process group */
109
+		}
110
+		/* fork again to drop group  leadership */
111
+		if ((pid=fork())<0){
112
+			LOG(L_CRIT, "Cannot  fork:%s\n", strerror(errno));
113
+			goto error;
114
+		}else if (pid!=0){
115
+			/*parent process => exit */
116
+			exit(0);
117
+		}
116 118
 	}
117
-
118 119
 	/* added by noh: create a pid file for the main process */
119 120
 	if (pid_file!=0){
120 121
 		
... ...
@@ -91,6 +91,7 @@ extern int tls_disable;
91 91
 extern unsigned short tls_port_no;
92 92
 #endif
93 93
 extern int dont_fork;
94
+extern int dont_daemonize;
94 95
 extern int check_via;
95 96
 extern int received_dns;
96 97
 extern int syn_branch;
... ...
@@ -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 1210
 	unsigned int seed;
1211 1211
 	int rfd;
1212 1212
 	int debug_save, debug_flag = 0;
1213
+	int dont_fork_cnt = 0;
1213 1214
 
1214 1215
 	/*init*/
1215 1216
 	creator_pid = getpid();
... ...
@@ -1416,7 +1418,7 @@ try_again:
1416 1416
 					received_dns|=DO_REV_DNS;
1417 1417
 					break;
1418 1418
 			case 'D':
1419
-					dont_fork=1;
1419
+					dont_fork_cnt++;
1420 1420
 					break;
1421 1421
 			case 'T':
1422 1422
 				#ifdef USE_TCP
... ...
@@ -1477,6 +1479,13 @@ try_again:
1477 1477
 		}
1478 1478
 	}
1479 1479
 
1480
+	if (dont_fork_cnt)
1481
+		dont_fork = dont_fork_cnt;	/* override by command line */
1482
+
1483
+	if (dont_fork > 0) {
1484
+		dont_daemonize = dont_fork == 2;
1485
+		dont_fork = dont_fork == 1;
1486
+	}
1480 1487
 	/* init the resolver, before fixing the config */
1481 1488
 	resolv_init();
1482 1489
 	/* 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 186
 #define USE_SYSV_SEM_STR ""
187 187
 #endif
188 188
 
189
-
190 189
 #ifdef NOSMP
191 190
 #define NOSMP_STR "-NOSMP"
192 191
 #else