Преглед на файлове

- 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 преди 21 години
родител
ревизия
136e747830
променени са 4 файла, в които са добавени 82 реда и са изтрити 39 реда
  1. 1 1
      Makefile.defs
  2. 47 24
      fifo_server.c
  3. 2 1
      fifo_server.h
  4. 32 13
      main.c

+ 1 - 1
Makefile.defs

@@ -45,7 +45,7 @@ export makefile_defs
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL =   13
-EXTRAVERSION = -dev-26-unixsock
+EXTRAVERSION = -dev-27-unixsock
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")

+ 47 - 24
fifo_server.c

@@ -60,6 +60,7 @@
  *               reply fifo checks -- added fifo_check (andrei)
  *  2003-10-13  addef fifo_dir for reply fifos (andrei)
  *  2003-10-30  DB interface exported via FIFO (bogdan)
+ *  2004-03-09  open_fifo_server split into init_ and start_ (andrei)
  */
 
 
@@ -99,7 +100,7 @@ pid_t fifo_pid;
 /* file descriptors */
 static int fifo_read=0;
 static int fifo_write=0;
-static FILE *fifo_stream;
+static FILE *fifo_stream=0;
 
 /* list of fifo command */
 static struct fifo_command *cmd_list=0;
@@ -534,14 +535,12 @@ consume:
 	}
 }
 
-int open_fifo_server()
+int init_fifo_server()
 {
 	char *t;
 	struct stat filestat;
 	int n;
-#ifdef USE_TCP
-	int sockfd[2];
-#endif
+	long opt;
 
 	if (fifo==NULL) {
 		DBG("DBG: open_fifo_server: no fifo will be opened\n");
@@ -588,6 +587,49 @@ int open_fifo_server()
 		return -1;
 	}
 	memcpy(up_since_ctime,t,strlen(t)+1);
+	/* open it non-blocking or else wait here until someone
+	 * opens it for writting */
+	fifo_read=open(fifo, O_RDONLY|O_NONBLOCK, 0);
+	if (fifo_read<0) {
+		LOG(L_ERR, "ERROR: init_fifo_server: fifo_read did not open: %s\n",
+			strerror(errno));
+		return -1;
+	}
+	fifo_stream=fdopen(fifo_read, "r");
+	if (fifo_stream==NULL) {
+		LOG(L_ERR, "ERROR: init_fifo_server: fdopen failed: %s\n",
+			strerror(errno));
+		return -1;
+	}
+	/* make sure the read fifo will not close */
+	fifo_write=open(fifo, O_WRONLY|O_NONBLOCK, 0);
+	if (fifo_write<0) {
+		LOG(L_ERR, "ERROR: init_fifo_server: fifo_write did not open: %s\n",
+			strerror(errno));
+		return -1;
+	}
+	/* set read fifo blocking mode */
+	if ((opt=fcntl(fifo_read, F_GETFL))==-1){
+		LOG(L_ERR, "ERROR: init_fifo_server: fcntl(F_GETFL) failed: %s [%d]\n",
+				strerror(errno), errno);
+		return -1;
+	}
+	if (fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK))==-1){
+		LOG(L_ERR, "ERROR: init_fifo_server: fcntl(F_SETFL) failed: %s [%d]\n",
+				strerror(errno), errno);
+		return -1;
+	}
+	return 0;
+}
+
+
+
+int start_fifo_server()
+{
+#ifdef USE_TCP
+	int sockfd[2];
+#endif
+	if (fifo_stream==0) return 1; /* no error, we just don't start it */
 #ifdef USE_TCP
 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
 			LOG(L_ERR, "ERROR: open_fifo_server: socketpair failed: %s\n",
@@ -615,18 +657,6 @@ int open_fifo_server()
 			LOG(L_ERR, "ERROR: open_uac_fifo: init_child failed\n");
 			return -1;
 		}
-		fifo_read=open(fifo, O_RDONLY, 0);
-		if (fifo_read<0) {
-			LOG(L_ERR, "ERROR: open_uac_fifo: fifo_read did not open: %s\n",
-				strerror(errno));
-			return -1;
-		}
-		fifo_stream=fdopen(fifo_read, "r"	);
-		if (fifo_stream==NULL) {
-			LOG(L_ERR, "SER: open_uac_fifo: fdopen failed: %s\n",
-				strerror(errno));
-			return -1;
-		}
 		/* a real server doesn't die if writing to reply fifo fails */
 		signal(SIGPIPE, SIG_IGN);
 		LOG(L_INFO, "SER: open_uac_fifo: fifo server up at %s...\n",
@@ -641,13 +671,6 @@ int open_fifo_server()
 	pt[process_no].unix_sock=sockfd[0];
 	pt[process_no].idx=-1; /* this is not "tcp" process*/
 #endif
-	/* make sure the read fifo will not close */
-	fifo_write=open(fifo, O_WRONLY, 0);
-	if (fifo_write<0) {
-		LOG(L_ERR, "SER: open_uac_fifo: fifo_write did not open: %s\n",
-			strerror(errno));
-		return -1;
-	}
 	return 1;
 }
 

+ 2 - 1
fifo_server.h

@@ -74,7 +74,8 @@ int read_line_set(char *buf, int max_len, FILE *fifo, int *len);
 /* consume a set of EoL-terminated lines terminated by a single dot line */
 int read_body(char *buf, int max_len, FILE *fifo, int *len);
 
-int open_fifo_server();
+int init_fifo_server();
+int start_fifo_server();
 
 /* regsiter core FIFO command set */
 int register_core_fifo();

+ 32 - 13
main.c

@@ -670,6 +670,18 @@ int main_loop()
 			LOG(L_WARN, "WARNING: using only the first listen address"
 						" (no fork)\n");
 		}
+		/* intialize fifo server -- we need to open the fifo before
+		 * do_suid() and start the fifo server after all the socket 
+		 * are initialized, to inherit them*/
+		if (init_fifo_server()<0) {
+			LOG(L_ERR, "initializing fifo server failed\n");
+			goto error;
+		}
+		 /* Initialize Unix domain socket server */
+		if (init_unixsock_socket()<0) {
+			LOG(L_ERR, "Error while creating unix domain sockets\n");
+			goto error;
+		}
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
 		/* process_no now initialized to zero -- increase from now on
 		   as new processes are forked (while skipping 0 reserved for main 
@@ -710,17 +722,14 @@ int main_loop()
 				}
 		}
 
-		/* if configured to do so, start a server for accepting FIFO commands */
-		if (open_fifo_server()<0) {
-			LOG(L_ERR, "opening fifo server failed\n");
+		/* if configured, start a server for accepting FIFO commands,
+		 * we need to do it after all the sockets are intialized, to 
+		 * inherit them*/
+		if (start_fifo_server()<0) {
+			LOG(L_ERR, "starting fifo server failed\n");
 			goto error;
 		}
 
-		     /* Initialize Unix domain socket server */
-		if (init_unixsock_socket()<0) {
-			LOG(L_ERR, "ERror while creating unix domain sockets\n");
-			goto error;
-		}
 		if (init_unixsock_children()<0) {
 			LOG(L_ERR, "Error while initializing Unix domain socket server\n");
 			goto error;
@@ -803,6 +812,14 @@ int main_loop()
 #endif /* USE_TLS */
 #endif /* USE_TCP */
 
+		/* intialize fifo server -- we need to open the fifo before
+		 * do_suid() and start the fifo server after all the socket 
+		 * are initialized, to inherit them*/
+		if (init_fifo_server()<0) {
+			LOG(L_ERR, "initializing fifo server failed\n");
+			goto error;
+		}
+		 /* Initialize Unix domain socket server */
 		     /* Create the unix domain sockets */
 		if (init_unixsock_socket()<0) {
 			LOG(L_ERR, "ERROR: Could not create unix domain sockets\n");
@@ -813,6 +830,13 @@ int main_loop()
 			 * so we open all first*/
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
 
+		/* if configured, start a server for accepting FIFO commands,
+		 * we need to do it after all the sockets are intialized, to 
+		 * inherit them*/
+		if (start_fifo_server()<0) {
+			LOG(L_ERR, "starting fifo server failed\n");
+			goto error;
+		}
 		     /* Spawn children listening on unix domain socket if and only if
 		      * the unix domain socket server has not been disabled (i == 0)
 		      */
@@ -881,11 +905,6 @@ int main_loop()
 	/*this is the main process*/
 	bind_address=0;				/* main proc -> it shouldn't send anything, */
 	
-	/* if configured to do so, start a server for accepting FIFO commands */
-	if (open_fifo_server()<0) {
-		LOG(L_ERR, "opening fifo server failed\n");
-		goto error;
-	}
 
 #ifdef USE_TCP
 	/* if we are using tcp we always need the timer */