Browse code

- open_fifo_server split into init_fifo_server and start_fifo_server - init_fifo_server called before do_suid - start_fifo_server called after do_suid and after all sockets are opened (to inherit them) - fixed init order fro non_fork_mode (this mode is only for debugging, shouldn't need fifo or unix server, but if it needs them try to init them before do_suid and start them after) - increased version no.

Andrei Pelinescu-Onciul authored on 09/03/2004 15:18:10
Showing 4 changed files
... ...
@@ -45,7 +45,7 @@ export makefile_defs
45 45
 VERSION = 0
46 46
 PATCHLEVEL = 8
47 47
 SUBLEVEL =   13
48
-EXTRAVERSION = -dev-26-unixsock
48
+EXTRAVERSION = -dev-27-unixsock
49 49
 
50 50
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
51 51
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -60,6 +60,7 @@
60 60
  *               reply fifo checks -- added fifo_check (andrei)
61 61
  *  2003-10-13  addef fifo_dir for reply fifos (andrei)
62 62
  *  2003-10-30  DB interface exported via FIFO (bogdan)
63
+ *  2004-03-09  open_fifo_server split into init_ and start_ (andrei)
63 64
  */
64 65
 
65 66
 
... ...
@@ -99,7 +100,7 @@ pid_t fifo_pid;
99 100
 /* file descriptors */
100 101
 static int fifo_read=0;
101 102
 static int fifo_write=0;
102
-static FILE *fifo_stream;
103
+static FILE *fifo_stream=0;
103 104
 
104 105
 /* list of fifo command */
105 106
 static struct fifo_command *cmd_list=0;
... ...
@@ -534,14 +535,12 @@ consume:
534 535
 	}
535 536
 }
536 537
 
537
-int open_fifo_server()
538
+int init_fifo_server()
538 539
 {
539 540
 	char *t;
540 541
 	struct stat filestat;
541 542
 	int n;
542
-#ifdef USE_TCP
543
-	int sockfd[2];
544
-#endif
543
+	long opt;
545 544
 
546 545
 	if (fifo==NULL) {
547 546
 		DBG("DBG: open_fifo_server: no fifo will be opened\n");
... ...
@@ -588,6 +587,49 @@ int open_fifo_server()
588 587
 		return -1;
589 588
 	}
590 589
 	memcpy(up_since_ctime,t,strlen(t)+1);
590
+	/* open it non-blocking or else wait here until someone
591
+	 * opens it for writting */
592
+	fifo_read=open(fifo, O_RDONLY|O_NONBLOCK, 0);
593
+	if (fifo_read<0) {
594
+		LOG(L_ERR, "ERROR: init_fifo_server: fifo_read did not open: %s\n",
595
+			strerror(errno));
596
+		return -1;
597
+	}
598
+	fifo_stream=fdopen(fifo_read, "r");
599
+	if (fifo_stream==NULL) {
600
+		LOG(L_ERR, "ERROR: init_fifo_server: fdopen failed: %s\n",
601
+			strerror(errno));
602
+		return -1;
603
+	}
604
+	/* make sure the read fifo will not close */
605
+	fifo_write=open(fifo, O_WRONLY|O_NONBLOCK, 0);
606
+	if (fifo_write<0) {
607
+		LOG(L_ERR, "ERROR: init_fifo_server: fifo_write did not open: %s\n",
608
+			strerror(errno));
609
+		return -1;
610
+	}
611
+	/* set read fifo blocking mode */
612
+	if ((opt=fcntl(fifo_read, F_GETFL))==-1){
613
+		LOG(L_ERR, "ERROR: init_fifo_server: fcntl(F_GETFL) failed: %s [%d]\n",
614
+				strerror(errno), errno);
615
+		return -1;
616
+	}
617
+	if (fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK))==-1){
618
+		LOG(L_ERR, "ERROR: init_fifo_server: fcntl(F_SETFL) failed: %s [%d]\n",
619
+				strerror(errno), errno);
620
+		return -1;
621
+	}
622
+	return 0;
623
+}
624
+
625
+
626
+
627
+int start_fifo_server()
628
+{
629
+#ifdef USE_TCP
630
+	int sockfd[2];
631
+#endif
632
+	if (fifo_stream==0) return 1; /* no error, we just don't start it */
591 633
 #ifdef USE_TCP
592 634
 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
593 635
 			LOG(L_ERR, "ERROR: open_fifo_server: socketpair failed: %s\n",
... ...
@@ -615,18 +657,6 @@ int open_fifo_server()
615 657
 			LOG(L_ERR, "ERROR: open_uac_fifo: init_child failed\n");
616 658
 			return -1;
617 659
 		}
618
-		fifo_read=open(fifo, O_RDONLY, 0);
619
-		if (fifo_read<0) {
620
-			LOG(L_ERR, "ERROR: open_uac_fifo: fifo_read did not open: %s\n",
621
-				strerror(errno));
622
-			return -1;
623
-		}
624
-		fifo_stream=fdopen(fifo_read, "r"	);
625
-		if (fifo_stream==NULL) {
626
-			LOG(L_ERR, "SER: open_uac_fifo: fdopen failed: %s\n",
627
-				strerror(errno));
628
-			return -1;
629
-		}
630 660
 		/* a real server doesn't die if writing to reply fifo fails */
631 661
 		signal(SIGPIPE, SIG_IGN);
632 662
 		LOG(L_INFO, "SER: open_uac_fifo: fifo server up at %s...\n",
... ...
@@ -641,13 +671,6 @@ int open_fifo_server()
641 671
 	pt[process_no].unix_sock=sockfd[0];
642 672
 	pt[process_no].idx=-1; /* this is not "tcp" process*/
643 673
 #endif
644
-	/* make sure the read fifo will not close */
645
-	fifo_write=open(fifo, O_WRONLY, 0);
646
-	if (fifo_write<0) {
647
-		LOG(L_ERR, "SER: open_uac_fifo: fifo_write did not open: %s\n",
648
-			strerror(errno));
649
-		return -1;
650
-	}
651 674
 	return 1;
652 675
 }
653 676
 
... ...
@@ -74,7 +74,8 @@ int read_line_set(char *buf, int max_len, FILE *fifo, int *len);
74 74
 /* consume a set of EoL-terminated lines terminated by a single dot line */
75 75
 int read_body(char *buf, int max_len, FILE *fifo, int *len);
76 76
 
77
-int open_fifo_server();
77
+int init_fifo_server();
78
+int start_fifo_server();
78 79
 
79 80
 /* regsiter core FIFO command set */
80 81
 int register_core_fifo();
... ...
@@ -670,6 +670,18 @@ int main_loop()
670 670
 			LOG(L_WARN, "WARNING: using only the first listen address"
671 671
 						" (no fork)\n");
672 672
 		}
673
+		/* intialize fifo server -- we need to open the fifo before
674
+		 * do_suid() and start the fifo server after all the socket 
675
+		 * are initialized, to inherit them*/
676
+		if (init_fifo_server()<0) {
677
+			LOG(L_ERR, "initializing fifo server failed\n");
678
+			goto error;
679
+		}
680
+		 /* Initialize Unix domain socket server */
681
+		if (init_unixsock_socket()<0) {
682
+			LOG(L_ERR, "Error while creating unix domain sockets\n");
683
+			goto error;
684
+		}
673 685
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
674 686
 		/* process_no now initialized to zero -- increase from now on
675 687
 		   as new processes are forked (while skipping 0 reserved for main 
... ...
@@ -710,17 +722,14 @@ int main_loop()
710 722
 				}
711 723
 		}
712 724
 
713
-		/* if configured to do so, start a server for accepting FIFO commands */
714
-		if (open_fifo_server()<0) {
715
-			LOG(L_ERR, "opening fifo server failed\n");
725
+		/* if configured, start a server for accepting FIFO commands,
726
+		 * we need to do it after all the sockets are intialized, to 
727
+		 * inherit them*/
728
+		if (start_fifo_server()<0) {
729
+			LOG(L_ERR, "starting fifo server failed\n");
716 730
 			goto error;
717 731
 		}
718 732
 
719
-		     /* Initialize Unix domain socket server */
720
-		if (init_unixsock_socket()<0) {
721
-			LOG(L_ERR, "ERror while creating unix domain sockets\n");
722
-			goto error;
723
-		}
724 733
 		if (init_unixsock_children()<0) {
725 734
 			LOG(L_ERR, "Error while initializing Unix domain socket server\n");
726 735
 			goto error;
... ...
@@ -803,6 +812,14 @@ int main_loop()
803 812
 #endif /* USE_TLS */
804 813
 #endif /* USE_TCP */
805 814
 
815
+		/* intialize fifo server -- we need to open the fifo before
816
+		 * do_suid() and start the fifo server after all the socket 
817
+		 * are initialized, to inherit them*/
818
+		if (init_fifo_server()<0) {
819
+			LOG(L_ERR, "initializing fifo server failed\n");
820
+			goto error;
821
+		}
822
+		 /* Initialize Unix domain socket server */
806 823
 		     /* Create the unix domain sockets */
807 824
 		if (init_unixsock_socket()<0) {
808 825
 			LOG(L_ERR, "ERROR: Could not create unix domain sockets\n");
... ...
@@ -813,6 +830,13 @@ int main_loop()
813 830
 			 * so we open all first*/
814 831
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
815 832
 
833
+		/* if configured, start a server for accepting FIFO commands,
834
+		 * we need to do it after all the sockets are intialized, to 
835
+		 * inherit them*/
836
+		if (start_fifo_server()<0) {
837
+			LOG(L_ERR, "starting fifo server failed\n");
838
+			goto error;
839
+		}
816 840
 		     /* Spawn children listening on unix domain socket if and only if
817 841
 		      * the unix domain socket server has not been disabled (i == 0)
818 842
 		      */
... ...
@@ -881,11 +905,6 @@ int main_loop()
881 905
 	/*this is the main process*/
882 906
 	bind_address=0;				/* main proc -> it shouldn't send anything, */
883 907
 	
884
-	/* if configured to do so, start a server for accepting FIFO commands */
885
-	if (open_fifo_server()<0) {
886
-		LOG(L_ERR, "opening fifo server failed\n");
887
-		goto error;
888
-	}
889 908
 
890 909
 #ifdef USE_TCP
891 910
 	/* if we are using tcp we always need the timer */