Browse code

Initialization if unix domain socket split

Jan Janak authored on 09/03/2004 11:10:59
Showing 3 changed files
... ...
@@ -717,7 +717,11 @@ int main_loop()
717 717
 		}
718 718
 
719 719
 		     /* Initialize Unix domain socket server */
720
-		if (init_unixsock_server()<0) {
720
+		if (init_unixsock_socket()<0) {
721
+			LOG(L_ERR, "ERror while creating unix domain sockets\n");
722
+			goto error;
723
+		}
724
+		if (init_unixsock_children()<0) {
721 725
 			LOG(L_ERR, "Error while initializing Unix domain socket server\n");
722 726
 			goto error;
723 727
 		}
... ...
@@ -798,15 +802,22 @@ int main_loop()
798 802
 		}
799 803
 #endif /* USE_TLS */
800 804
 #endif /* USE_TCP */
805
+
806
+		     /* Create the unix domain sockets */
807
+		if (init_unixsock_socket()<0) {
808
+			LOG(L_ERR, "ERROR: Could not create unix domain sockets\n");
809
+			goto error;
810
+		}
811
+
801 812
 			/* all procs should have access to all the sockets (for sending)
802 813
 			 * so we open all first*/
803 814
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
804 815
 
805
-		     /* Initialize Unix domain socket server before forking so that all
806
-		      * children inherit opened socket for sending and receiving
816
+		     /* Spawn children listening on unix domain socket if and only if
817
+		      * the unix domain socket server has not been disabled (i == 0)
807 818
 		      */
808
-		if (init_unixsock_server()<0) {
809
-			LOG(L_ERR, "Error while initializing Unix domain socket server\n");
819
+		if (init_unixsock_children()<0) {
820
+			LOG(L_ERR, "ERROR: Could not initialize unix domain socket server\n");
810 821
 			goto error;
811 822
 		}
812 823
 
... ...
@@ -295,55 +295,56 @@ static int register_core_commands(void)
295 295
 /*
296 296
  * Create and bind local socket
297 297
  */
298
-static int create_unix_socket(char* name)
298
+int init_unixsock_socket(void)
299 299
 {
300 300
 	struct sockaddr_un addr;
301 301
 	int len, flags;
302 302
 
303
-	if (name == 0) {
304
-		DBG("create_unix_socket: No unix domain socket"
303
+	if (unixsock_name == 0) {
304
+		DBG("init_unixsock_socket: No unix domain socket"
305 305
 		    " will be opened\n");
306 306
 		return 1;
307 307
 	}
308 308
 
309
-	len = strlen(name);
309
+	len = strlen(unixsock_name);
310 310
 	if (len == 0) {
311
-		DBG("create_unix_socket: Unix domain socket server disabled\n");
311
+		DBG("init_unixsock_socket: Unix domain socket server disabled\n");
312 312
 		return 1;
313 313
 	} else if (len > 107) {
314
-		LOG(L_ERR, "create_unix_socket: Socket name too long\n");
314
+		LOG(L_ERR, "init_unixsock_socket: Socket name too long\n");
315 315
 		return -1;
316 316
 	}
317 317
 
318
-	DBG("create_unix_socket: Initializing Unix domain socket server\n");
318
+	DBG("init_unixsock_socket: Initializing Unix domain socket server @ %s\n", 
319
+	    unixsock_name);
319 320
 
320
-	if (unlink(name) == -1) {
321
+	if (unlink(unixsock_name) == -1) {
321 322
 		if (errno != ENOENT) {
322
-			LOG(L_ERR, "create_unix_socket: Error while unlinking "
323
-			    "old socket (%s): %s\n", name, strerror(errno));
323
+			LOG(L_ERR, "init_unixsock_socket: Error while unlinking "
324
+			    "old socket (%s): %s\n", unixsock_name, strerror(errno));
324 325
 			return -1;
325 326
 		}
326 327
 	}
327 328
 
328 329
 	rx_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
329 330
 	if (rx_sock == -1) {
330
-		LOG(L_ERR, "create_unix_socket: Cannot create RX socket: %s\n", 
331
+		LOG(L_ERR, "init_unixsock_socket: Cannot create RX socket: %s\n", 
331 332
 		    strerror(errno));
332 333
 		return -1;
333 334
 	}
334 335
 
335 336
 	memset(&addr, 0, sizeof(addr));
336 337
 	addr.sun_family = PF_LOCAL;
337
-	memcpy(addr.sun_path, name, len);
338
+	memcpy(addr.sun_path, unixsock_name, len);
338 339
 
339 340
 	if (bind(rx_sock, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
340
-		LOG(L_ERR, "create_unix_socket: bind: %s\n", strerror(errno));
341
+		LOG(L_ERR, "init_unixsock_socket: bind: %s\n", strerror(errno));
341 342
 		goto err_rx;
342 343
 	}
343 344
 
344 345
 	tx_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
345 346
 	if (tx_sock == -1) {
346
-		LOG(L_ERR, "create_unix_socket: Cannot create TX socket: %s\n",
347
+		LOG(L_ERR, "init_unixsock_socket: Cannot create TX socket: %s\n",
347 348
 		    strerror(errno));
348 349
 		goto err_rx;
349 350
 	}
... ...
@@ -351,18 +352,18 @@ static int create_unix_socket(char* name)
351 352
 	     /* Turn non-blocking mode on */
352 353
 	flags = fcntl(tx_sock, F_GETFL);
353 354
 	if (flags == -1){
354
-		LOG(L_ERR, "create_unix_socket: fcntl failed: %s\n",
355
+		LOG(L_ERR, "init_unixsock_socket: fcntl failed: %s\n",
355 356
 		    strerror(errno));
356 357
 		goto err_both;
357 358
 	}
358 359
 		
359 360
 	if (fcntl(tx_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
360
-		LOG(L_ERR, "create_unix_socket: fcntl: set non-blocking failed:"
361
+		LOG(L_ERR, "init_unixsock_socket: fcntl: set non-blocking failed:"
361 362
 		    " %s\n", strerror(errno));
362 363
 		goto err_both;
363 364
 	}
364 365
 	
365
-	return 0;
366
+	return 1;
366 367
  err_both:
367 368
 	close(tx_sock);
368 369
  err_rx:
... ...
@@ -518,30 +519,24 @@ static int get_uptime(void)
518 519
 
519 520
 
520 521
 /*
521
- * Initialize Unix domain socket server
522
+ * Spawn listeners
522 523
  */
523
-int init_unixsock_server(void)
524
+int init_unixsock_children(void)
524 525
 {
525
-	int ret, i;
526
+	int i;
526 527
 	pid_t pid;
527 528
 #ifdef USE_TCP
528 529
 	int sockfd[2];
529 530
 #endif
530
-	
531
-	
532
-	ret = create_unix_socket(unixsock_name);
533
-	if (ret < 0) {
534
-		LOG(L_ERR, "init_unixsock_server: Error while creating "
535
-		    "local socket\n");
536
-		return -1;
537
-	} else if (ret > 0) {
531
+
532
+	if (!unixsock_name || *unixsock_name == '\0') {
538 533
 		return 1;
539 534
 	}
540 535
 
541 536
 	if (get_uptime() < 0) {
542 537
 		return -1;
543 538
 	}
544
-
539
+	
545 540
         if (register_core_commands() < 0) {
546 541
 		close(rx_sock);
547 542
 		close(tx_sock);
... ...
@@ -599,6 +594,8 @@ int init_unixsock_server(void)
599 594
 
600 595
 	}
601 596
 
597
+	DBG("init_unixsock_server: Unix domain socket server sucessfully initialized @ %s\n",
598
+	    unixsock_name);
602 599
 	return 1;
603 600
 }
604 601
 
... ...
@@ -49,7 +49,13 @@ struct unixsock_cmd {
49 49
 /*
50 50
  * Initialize Unix domain socket server
51 51
  */
52
-int init_unixsock_server(void);
52
+int init_unixsock_socket(void);
53
+
54
+
55
+/*
56
+ * Initialize Unix domain socket server
57
+ */
58
+int init_unixsock_children(void);
53 59
 
54 60
 
55 61
 /*