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 798
 		}
799 799
 #endif /* USE_TLS */
800 800
 #endif /* USE_TCP */
801
+
802
+		     /* Create the unix domain sockets */
803
+		if (init_unixsock_socket()<0) {
804
+			LOG(L_ERR, "ERROR: Could not create unix domain sockets\n");
805
+			goto error;
806
+		}
807
+
801 808
 			/* all procs should have access to all the sockets (for sending)
802 809
 			 * so we open all first*/
803 810
 		if (do_suid()==-1) goto error; /* try to drop priviledges */
804 811
 
805
-		     /* Initialize Unix domain socket server before forking so that all
806
-		      * children inherit opened socket for sending and receiving
812
+		     /* Spawn children listening on unix domain socket if and only if
813
+		      * the unix domain socket server has not been disabled (i == 0)
807 814
 		      */
808
-		if (init_unixsock_server()<0) {
809
-			LOG(L_ERR, "Error while initializing Unix domain socket server\n");
815
+		if (init_unixsock_children()<0) {
816
+			LOG(L_ERR, "ERROR: Could not initialize unix domain socket server\n");
810 817
 			goto error;
811 818
 		}
812 819
 
... ...
@@ -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 351
 	     /* Turn non-blocking mode on */
352 352
 	flags = fcntl(tx_sock, F_GETFL);
353 353
 	if (flags == -1){
354
-		LOG(L_ERR, "create_unix_socket: fcntl failed: %s\n",
354
+		LOG(L_ERR, "init_unixsock_socket: fcntl failed: %s\n",
355 355
 		    strerror(errno));
356 356
 		goto err_both;
357 357
 	}
358 358
 		
359 359
 	if (fcntl(tx_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
360
-		LOG(L_ERR, "create_unix_socket: fcntl: set non-blocking failed:"
360
+		LOG(L_ERR, "init_unixsock_socket: fcntl: set non-blocking failed:"
361 361
 		    " %s\n", strerror(errno));
362 362
 		goto err_both;
363 363
 	}
364 364
 	
365
-	return 0;
365
+	return 1;
366 366
  err_both:
367 367
 	close(tx_sock);
368 368
  err_rx:
... ...
@@ -518,30 +519,24 @@ static int get_uptime(void)
518 518
 
519 519
 
520 520
 /*
521
- * Initialize Unix domain socket server
521
+ * Spawn listeners
522 522
  */
523
-int init_unixsock_server(void)
523
+int init_unixsock_children(void)
524 524
 {
525
-	int ret, i;
525
+	int i;
526 526
 	pid_t pid;
527 527
 #ifdef USE_TCP
528 528
 	int sockfd[2];
529 529
 #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) {
530
+
531
+	if (!unixsock_name || *unixsock_name == '\0') {
538 532
 		return 1;
539 533
 	}
540 534
 
541 535
 	if (get_uptime() < 0) {
542 536
 		return -1;
543 537
 	}
544
-
538
+	
545 539
         if (register_core_commands() < 0) {
546 540
 		close(rx_sock);
547 541
 		close(tx_sock);
... ...
@@ -599,6 +594,8 @@ int init_unixsock_server(void)
599 599
 
600 600
 	}
601 601
 
602
+	DBG("init_unixsock_server: Unix domain socket server sucessfully initialized @ %s\n",
603
+	    unixsock_name);
602 604
 	return 1;
603 605
 }
604 606
 
... ...
@@ -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
 /*