Browse code

- signals handlers are installed after daemonize to avoid catching our own SIGCHLD generated when becoming session leader; thanks to Daniel for diagnosticating this on RH 9.

Andrei Pelinescu-Onciul authored on 11/06/2003 13:49:44
Showing 3 changed files
... ...
@@ -40,7 +40,7 @@ export makefile_defs
40 40
 VERSION = 0
41 41
 PATCHLEVEL = 8
42 42
 SUBLEVEL =   11
43
-EXTRAVERSION = pre30
43
+EXTRAVERSION = pre31
44 44
 
45 45
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
46 46
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -34,8 +34,8 @@ x (different way) add request header bitmap field for the modules
34 34
 - ? variable number of params functions in script (no longer limited to 2)?
35 35
 - kill bind_idx
36 36
 x fix bind_address for tcp (in some  way)
37
-- add conflict in debs/rpms/etc (conflict w/ older ser-mysql, ser-jabber)
38
-- new packages ser-radius etc
37
+x add conflict in debs/rpms/etc (conflict w/ older ser-mysql, ser-jabber)
38
+x new packages ser-radius etc
39 39
 x tcp_destroy (called on ser exit)
40 40
 - BUG:?? ipv6 only and try to send to ipv4 => getsendsocket=>0 (send_ipv6=0)
41 41
   the reverse is also true
... ...
@@ -36,6 +36,10 @@
36 36
  *               init_shm_mallocs called after cmd. line parsing (andrei)
37 37
  *  2003-04-15  added tcp_disable support (andrei)
38 38
  *  2003-05-09  closelog() before openlog to force opening a new fd (needed on solaris) (andrei)
39
+ *  2003-06-11  moved all signal handlers init. in install_sigs and moved it
40
+ *               after daemonize (so that we won't catch anymore our own
41
+ *               SIGCHLD generated when becoming session leader) (andrei)
42
+ *              changed is_main default value to 1 (andrei)
39 43
  *
40 44
  */
41 45
 
... ...
@@ -350,7 +354,7 @@ extern FILE* yyin;
350 350
 extern int yyparse();
351 351
 
352 352
 
353
-int is_main=0; /* flag = is this the  "main" process? */
353
+int is_main=1; /* flag = is this the  "main" process? */
354 354
 
355 355
 char* pid_file = 0; /* filename as asked by use */
356 356
 
... ...
@@ -595,6 +599,101 @@ void handle_sigs()
595 595
 
596 596
 
597 597
 
598
+/* added by jku; allows for regular exit on a specific signal;
599
+   good for profiling which only works if exited regularly and
600
+   not by default signal handlers
601
+    - modified by andrei: moved most of the stuff to handle_sigs, 
602
+       made it safer for the "fork" case
603
+*/
604
+static void sig_usr(int signo)
605
+{
606
+
607
+
608
+	if (is_main){
609
+		if (sig_flag==0) sig_flag=signo;
610
+		else /*  previous sig. not processed yet, ignoring? */
611
+			return; ;
612
+		if (dont_fork) 
613
+				/* only one proc, doing everything from the sig handler,
614
+				unsafe, but this is only for debugging mode*/
615
+			handle_sigs();
616
+	}else{
617
+		/* process the important signals */
618
+		switch(signo){
619
+			case SIGPIPE:
620
+					LOG(L_INFO, "INFO: signal %d received\n", signo);
621
+				break;
622
+			case SIGINT:
623
+			case SIGTERM:
624
+					LOG(L_INFO, "INFO: signal %d received\n", signo);
625
+					/* print memory stats for non-main too */
626
+					#ifdef PKG_MALLOC
627
+					LOG(memlog, "Memory status (pkg):\n");
628
+					pkg_status();
629
+					#endif
630
+					exit(0);
631
+					break;
632
+			case SIGUSR1:
633
+				/* statistics, do nothing, printed only from the main proc */
634
+					break;
635
+				/* ignored*/
636
+			case SIGUSR2:
637
+			case SIGHUP:
638
+					break;
639
+			case SIGCHLD:
640
+#ifndef 			STOP_JIRIS_CHANGES
641
+					LOG(L_INFO, "INFO: SIGCHLD received: "
642
+						"we do not worry about grand-children\n");
643
+#else
644
+					exit(0); /* terminate if one child died */
645
+#endif
646
+		}
647
+	}
648
+}
649
+
650
+
651
+
652
+/* install the signal handlers, returns 0 on success, -1 on error */
653
+int install_sigs()
654
+{
655
+	/* added by jku: add exit handler */
656
+	if (signal(SIGINT, sig_usr) == SIG_ERR ) {
657
+		DPrint("ERROR: no SIGINT signal handler can be installed\n");
658
+		goto error;
659
+	}
660
+	/* if we debug and write to a pipe, we want to exit nicely too */
661
+	if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
662
+		DPrint("ERROR: no SIGINT signal handler can be installed\n");
663
+		goto error;
664
+	}
665
+	
666
+	if (signal(SIGUSR1, sig_usr)  == SIG_ERR ) {
667
+		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
668
+		goto error;
669
+	}
670
+	if (signal(SIGCHLD , sig_usr)  == SIG_ERR ) {
671
+		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
672
+		goto error;
673
+	}
674
+	if (signal(SIGTERM , sig_usr)  == SIG_ERR ) {
675
+		DPrint("ERROR: no SIGTERM signal handler can be installed\n");
676
+		goto error;
677
+	}
678
+	if (signal(SIGHUP , sig_usr)  == SIG_ERR ) {
679
+		DPrint("ERROR: no SIGHUP signal handler can be installed\n");
680
+		goto error;
681
+	}
682
+	if (signal(SIGUSR2 , sig_usr)  == SIG_ERR ) {
683
+		DPrint("ERROR: no SIGUSR2 signal handler can be installed\n");
684
+		goto error;
685
+	}
686
+	return 0;
687
+error:
688
+	return -1;
689
+}
690
+
691
+
692
+
598 693
 /* main loop */
599 694
 int main_loop()
600 695
 {
... ...
@@ -607,7 +706,7 @@ int main_loop()
607 607
 
608 608
 	/* one "main" process and n children handling i/o */
609 609
 
610
-
610
+	is_main=0;
611 611
 	if (dont_fork){
612 612
 #ifdef STATS
613 613
 		setstats( 0 );
... ...
@@ -906,61 +1005,6 @@ int main_loop()
906 906
 
907 907
 }
908 908
 
909
-
910
-/* added by jku; allows for regular exit on a specific signal;
911
-   good for profiling which only works if exited regularly and
912
-   not by default signal handlers
913
-    - modified by andrei: moved most of the stuff to handle_sigs, 
914
-       made it safer for the "fork" case
915
-*/
916
-static void sig_usr(int signo)
917
-{
918
-
919
-
920
-	if (is_main){
921
-		if (sig_flag==0) sig_flag=signo;
922
-		else /*  previous sig. not processed yet, ignoring? */
923
-			return; ;
924
-		if (dont_fork) 
925
-				/* only one proc, doing everything from the sig handler,
926
-				unsafe, but this is only for debugging mode*/
927
-			handle_sigs();
928
-	}else{
929
-		/* process the important signals */
930
-		switch(signo){
931
-			case SIGPIPE:
932
-					LOG(L_INFO, "INFO: signal %d received\n", signo);
933
-				break;
934
-			case SIGINT:
935
-			case SIGTERM:
936
-					LOG(L_INFO, "INFO: signal %d received\n", signo);
937
-					/* print memory stats for non-main too */
938
-					#ifdef PKG_MALLOC
939
-					LOG(memlog, "Memory status (pkg):\n");
940
-					pkg_status();
941
-					#endif
942
-					exit(0);
943
-					break;
944
-			case SIGUSR1:
945
-				/* statistics, do nothing, printed only from the main proc */
946
-					break;
947
-				/* ignored*/
948
-			case SIGUSR2:
949
-			case SIGHUP:
950
-					break;
951
-			case SIGCHLD:
952
-#ifndef 			STOP_JIRIS_CHANGES
953
-					LOG(L_INFO, "INFO: SIGCHLD received: "
954
-						"we do not worry about grand-children\n");
955
-#else
956
-					exit(0); /* terminate if one child died */
957
-#endif
958
-		}
959
-	}
960
-}
961
-
962
-
963
-
964 909
 /* add all family type addresses of interface if_name to the socket_info array
965 910
  * if if_name==0, adds all addresses on all interfaces
966 911
  * WARNING: it only works with ipv6 addresses on FreeBSD
... ...
@@ -1120,37 +1164,6 @@ int main(int argc, char** argv)
1120 1120
 	if (init_pkg_mallocs()==-1)
1121 1121
 		goto error;
1122 1122
 
1123
-	/* added by jku: add exit handler */
1124
-	if (signal(SIGINT, sig_usr) == SIG_ERR ) {
1125
-		DPrint("ERROR: no SIGINT signal handler can be installed\n");
1126
-		goto error;
1127
-	}
1128
-	/* if we debug and write to a pipe, we want to exit nicely too */
1129
-	if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
1130
-		DPrint("ERROR: no SIGINT signal handler can be installed\n");
1131
-		goto error;
1132
-	}
1133
-
1134
-	if (signal(SIGUSR1, sig_usr)  == SIG_ERR ) {
1135
-		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
1136
-		goto error;
1137
-	}
1138
-	if (signal(SIGCHLD , sig_usr)  == SIG_ERR ) {
1139
-		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
1140
-		goto error;
1141
-	}
1142
-	if (signal(SIGTERM , sig_usr)  == SIG_ERR ) {
1143
-		DPrint("ERROR: no SIGTERM signal handler can be installed\n");
1144
-		goto error;
1145
-	}
1146
-	if (signal(SIGHUP , sig_usr)  == SIG_ERR ) {
1147
-		DPrint("ERROR: no SIGHUP signal handler can be installed\n");
1148
-		goto error;
1149
-	}
1150
-	if (signal(SIGUSR2 , sig_usr)  == SIG_ERR ) {
1151
-		DPrint("ERROR: no SIGUSR2 signal handler can be installed\n");
1152
-		goto error;
1153
-	}
1154 1123
 #ifdef DBG_MSG_QA
1155 1124
 	fprintf(stderr, "WARNING: ser startup: "
1156 1125
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
... ...
@@ -1635,6 +1648,11 @@ try_again:
1635 1635
 	if (!dont_fork){
1636 1636
 		if ( daemonize(argv[0]) <0 ) goto error;
1637 1637
 	}
1638
+	if (install_sigs() != 0){
1639
+		fprintf(stderr, "ERROR: could not install the signal handlers\n");
1640
+		goto error;
1641
+	}
1642
+	
1638 1643
 	if (init_modules() != 0) {
1639 1644
 		fprintf(stderr, "ERROR: error while initializing modules\n");
1640 1645
 		goto error;