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 205
 	union sockaddr_union my_name;
206 206
 	socklen_t my_name_len;
207 207
 	int optval;
208
+	struct tcp_connection* con;
208 209
 #ifdef DISABLE_NAGLE
209 210
 	int flag;
210 211
 #endif
211 212
 
212 213
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
213
-	if (s<0){
214
+	if (s==-1){
214 215
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
215 216
 				errno, strerror(errno));
216 217
 		goto error;
... ...
@@ -257,9 +260,16 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
257 257
 		else si=sendipv6_tcp;
258 258
 #endif
259 259
 	}
260
-	return tcpconn_new(s, server, si, type, S_CONN_CONNECT);
260
+	con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
261
+	if (con==0){
262
+		LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
263
+				 " socket\n");
264
+		goto error;
265
+	}
266
+	return con;
261 267
 	/*FIXME: set sock idx! */
262 268
 error:
269
+	if (s!=-1) close(s); /* close the opened socket */
263 270
 	return 0;
264 271
 }
265 272
 
... ...
@@ -489,7 +499,7 @@ get_fd:
489 489
 						  tmp, tmp->id, tmp->refcnt, tmp->state, n
490 490
 				   );
491 491
 				n=-1; /* fail */
492
-				goto release_c;
492
+				goto end;
493 493
 			}
494 494
 			DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
495 495
 		
... ...
@@ -727,7 +737,7 @@ static inline void handle_new_connect(struct socket_info* si,
727 727
 		su_len=sizeof(su);
728 728
 		new_sock=accept(si->socket, &(su.s), &su_len);
729 729
 		(*n)--;
730
-		if (new_sock<0){
730
+		if (new_sock==-1){
731 731
 			LOG(L_ERR,  "WARNING: tcp_main_loop: error while accepting"
732 732
 					" connection(%d): %s\n", errno, strerror(errno));
733 733
 			return;
... ...
@@ -753,6 +763,10 @@ static inline void handle_new_connect(struct socket_info* si,
753 753
 				}else tcpconn->timeout=0; /* force expire */
754 754
 				TCPCONN_UNLOCK;
755 755
 			}
756
+		}else{ /*tcpconn==0 */
757
+			LOG(L_ERR, "ERROR: tcp_main_loop: tcpconn_new failed, "
758
+					"closing socket\n");
759
+			close(new_sock);
756 760
 		}
757 761
 	}
758 762
 }