Browse code

- hopefully solves the open tcp fd bug (now handle_new_connect and tcp_connect will close the socket if tcpconn_new fails -- e.g. when it's out of shared mem.)

Andrei Pelinescu-Onciul authored on 17/11/2003 11:20:11
Showing 1 changed files
... ...
@@ -46,6 +46,8 @@
46 46
  *              does not inc refcnt by itself anymore (andrei)
47 47
  *  2003-11-07  different unix sockets are used for fd passing
48 48
  *              to/from readers/writers (andrei)
49
+ *  2003-11-17  handle_new_connect & tcp_connect will close the 
50
+ *              new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
49 51
  */
50 52
 
51 53
 
... ...
@@ -205,12 +207,13 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
205 207
 	union sockaddr_union my_name;
206 208
 	socklen_t my_name_len;
207 209
 	int optval;
210
+	struct tcp_connection* con;
208 211
 #ifdef DISABLE_NAGLE
209 212
 	int flag;
210 213
 #endif
211 214
 
212 215
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
213
-	if (s<0){
216
+	if (s==-1){
214 217
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
215 218
 				errno, strerror(errno));
216 219
 		goto error;
... ...
@@ -257,9 +260,16 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
257 260
 		else si=sendipv6_tcp;
258 261
 #endif
259 262
 	}
260
-	return tcpconn_new(s, server, si, type, S_CONN_CONNECT);
263
+	con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
264
+	if (con==0){
265
+		LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
266
+				 " socket\n");
267
+		goto error;
268
+	}
269
+	return con;
261 270
 	/*FIXME: set sock idx! */
262 271
 error:
272
+	if (s!=-1) close(s); /* close the opened socket */
263 273
 	return 0;
264 274
 }
265 275
 
... ...
@@ -489,7 +499,7 @@ get_fd:
489 499
 						  tmp, tmp->id, tmp->refcnt, tmp->state, n
490 500
 				   );
491 501
 				n=-1; /* fail */
492
-				goto release_c;
502
+				goto end;
493 503
 			}
494 504
 			DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
495 505
 		
... ...
@@ -727,7 +737,7 @@ static inline void handle_new_connect(struct socket_info* si,
727 737
 		su_len=sizeof(su);
728 738
 		new_sock=accept(si->socket, &(su.s), &su_len);
729 739
 		(*n)--;
730
-		if (new_sock<0){
740
+		if (new_sock==-1){
731 741
 			LOG(L_ERR,  "WARNING: tcp_main_loop: error while accepting"
732 742
 					" connection(%d): %s\n", errno, strerror(errno));
733 743
 			return;
... ...
@@ -753,6 +763,10 @@ static inline void handle_new_connect(struct socket_info* si,
753 763
 				}else tcpconn->timeout=0; /* force expire */
754 764
 				TCPCONN_UNLOCK;
755 765
 			}
766
+		}else{ /*tcpconn==0 */
767
+			LOG(L_ERR, "ERROR: tcp_main_loop: tcpconn_new failed, "
768
+					"closing socket\n");
769
+			close(new_sock);
756 770
 		}
757 771
 	}
758 772
 }