Browse code

Additional socket in non-blocking mode created for sending replies

Jan Janak authored on 04/03/2004 12:23:05
Showing 2 changed files
... ...
@@ -40,6 +40,7 @@
40 40
 #include <signal.h>
41 41
 #include <stdarg.h>
42 42
 #include <time.h>
43
+#include <fcntl.h>
43 44
 #include "config.h"
44 45
 #include "ut.h"
45 46
 #include "globals.h"
... ...
@@ -56,7 +57,7 @@
56 57
 char* unixsock_name = 0;
57 58
 int unixsock_children = 1;
58 59
 
59
-static int sock;
60
+static int rx_sock, tx_sock;
60 61
 static struct unixsock_cmd* cmd_list;
61 62
 static char reply_buf[UNIXSOCK_BUF_SIZE];
62 63
 static str reply_pos;
... ...
@@ -295,7 +296,7 @@ static int register_core_commands(void)
295 296
 static int create_unix_socket(char* name)
296 297
 {
297 298
 	struct sockaddr_un addr;
298
-	int len;
299
+	int len, flags;
299 300
 
300 301
 	if (name == 0) {
301 302
 		DBG("create_unix_socket: No unix domain socket"
... ...
@@ -322,9 +323,9 @@ static int create_unix_socket(char* name)
322 323
 		}
323 324
 	}
324 325
 
325
-	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
326
-	if (sock == -1) {
327
-		LOG(L_ERR, "create_unix_socket: Cannot create socket: %s\n", 
326
+	rx_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
327
+	if (rx_sock == -1) {
328
+		LOG(L_ERR, "create_unix_socket: Cannot create RX socket: %s\n", 
328 329
 		    strerror(errno));
329 330
 		return -1;
330 331
 	}
... ...
@@ -333,13 +334,38 @@ static int create_unix_socket(char* name)
333 334
 	addr.sun_family = PF_LOCAL;
334 335
 	memcpy(addr.sun_path, name, len);
335 336
 
336
-	if (bind(sock, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
337
+	if (bind(rx_sock, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
337 338
 		LOG(L_ERR, "create_unix_socket: bind: %s\n", strerror(errno));
338
-		close(sock);
339
-		return -1;
339
+		goto err_rx;
340
+	}
341
+
342
+	tx_sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
343
+	if (tx_sock == -1) {
344
+		LOG(L_ERR, "create_unix_socket: Cannot create TX socket: %s\n",
345
+		    strerror(errno));
346
+		goto err_rx;
340 347
 	}
341 348
 
349
+	     /* Turn non-blocking mode on */
350
+	flags = fcntl(tx_sock, F_GETFL);
351
+	if (flags == -1){
352
+		LOG(L_ERR, "create_unix_socket: fcntl failed: %s\n",
353
+		    strerror(errno));
354
+		goto err_both;
355
+	}
356
+		
357
+	if (fcntl(tx_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
358
+		LOG(L_ERR, "create_unix_socket: fcntl: set non-blocking failed:"
359
+		    " %s\n", strerror(errno));
360
+		goto err_both;
361
+	}
362
+	
342 363
 	return 0;
364
+ err_both:
365
+	close(tx_sock);
366
+ err_rx:
367
+	close(rx_sock);
368
+	return -1;
343 369
 }
344 370
 
345 371
 
... ...
@@ -424,7 +450,7 @@ static void unix_server_loop(void)
424 450
 	
425 451
 	while(1) {
426 452
 		reply_addr_len = sizeof(reply_addr);
427
-		ret = recvfrom(sock, buffer.s, UNIXSOCK_BUF_SIZE, 0, 
453
+		ret = recvfrom(rx_sock, buffer.s, UNIXSOCK_BUF_SIZE, 0, 
428 454
 			       (struct sockaddr*)&reply_addr, &reply_addr_len);
429 455
 		if (ret == -1) {
430 456
 			LOG(L_ERR, "unix_server_loop: recvfrom: (%d) %s\n", 
... ...
@@ -515,7 +541,8 @@ int init_unixsock_server(void)
515 541
 	}
516 542
 
517 543
         if (register_core_commands() < 0) {
518
-		close(sock);
544
+		close(rx_sock);
545
+		close(tx_sock);
519 546
 		return -1;
520 547
 	}
521 548
 
... ...
@@ -534,7 +561,8 @@ int init_unixsock_server(void)
534 561
 		if (pid < 0) {
535 562
 			LOG(L_ERR, "init_unixsock_server: Unable to fork: %s\n",
536 563
 			    strerror(errno));
537
-			close(sock);
564
+			close(rx_sock);
565
+			close(tx_sock);
538 566
 			return -1;
539 567
 		} else if (pid == 0) { /* child */
540 568
 #ifdef USE_TCP
... ...
@@ -546,7 +574,8 @@ int init_unixsock_server(void)
546 574
 			if (init_child(PROC_UNIXSOCK) < 0) {
547 575
 				LOG(L_ERR, "init_unixsock_server: Error in "
548 576
 				    "init_child\n");
549
-				close(sock);
577
+				close(rx_sock);
578
+				close(tx_sock);
550 579
 				return -1;
551 580
 			}
552 581
 
... ...
@@ -578,7 +607,8 @@ int init_unixsock_server(void)
578 607
 void close_unixsock_server(void)
579 608
 {
580 609
 	struct unixsock_cmd* c;
581
-	close(sock);
610
+	close(rx_sock);
611
+	close(tx_sock);
582 612
 
583 613
 	while(cmd_list) {
584 614
 		c = cmd_list;
... ...
@@ -643,7 +673,7 @@ ssize_t unixsock_reply_send(void)
643 673
 {
644 674
 	int ret;
645 675
 
646
-	ret = sendto(sock, reply_buf, reply_pos.s - reply_buf, MSG_DONTWAIT, 
676
+	ret = sendto(tx_sock, reply_buf, reply_pos.s - reply_buf, MSG_DONTWAIT, 
647 677
 		     (struct sockaddr*)&reply_addr, reply_addr_len);
648 678
 
649 679
 	if (ret == -1) {
... ...
@@ -667,7 +697,7 @@ ssize_t unixsock_reply_sendto(struct sockaddr_un* to)
667 697
 		return -1;
668 698
 	}
669 699
 
670
-	ret = sendto(sock, reply_buf, reply_pos.s - reply_buf, MSG_DONTWAIT, 
700
+	ret = sendto(tx_sock, reply_buf, reply_pos.s - reply_buf, MSG_DONTWAIT, 
671 701
 		     (struct sockaddr*)to, SUN_LEN(to));
672 702
 
673 703
 	if (ret == -1) {
... ...
@@ -951,7 +981,7 @@ int unixsock_reply_printf(char* fmt, ...)
951 981
 /*
952 982
  * Return the address of the sender
953 983
  */
954
-struct sockaddr_un* unix_sender_address(void)
984
+struct sockaddr_un* unix_sender_addr(void)
955 985
 {
956 986
 	return &reply_addr;
957 987
 }
... ...
@@ -129,7 +129,7 @@ int unixsock_read_lineset(str* lineset, str* source);
129 129
 /*
130 130
  * Return the address of the sender
131 131
  */
132
-struct sockaddr_un* unixsock_sender_address(void);
132
+struct sockaddr_un* unixsock_sender_addr(void);
133 133
 
134 134
 
135 135
 #endif /* _UNIXSOCK_SERVER_H */