Browse code

- close(fd) fixes merged from testing_0_8_12

Andrei Pelinescu-Onciul authored on 20/11/2003 20:56:11
Showing 1 changed files
... ...
@@ -48,6 +48,8 @@
48 48
  *              does not inc refcnt by itself anymore (andrei)
49 49
  *  2003-11-07  different unix sockets are used for fd passing
50 50
  *              to/from readers/writers (andrei)
51
+ *  2003-11-17  handle_new_connect & tcp_connect will close the 
52
+ *              new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
51 53
  */
52 54
 
53 55
 
... ...
@@ -209,12 +211,13 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
209 211
 	union sockaddr_union my_name;
210 212
 	socklen_t my_name_len;
211 213
 	int optval;
214
+	struct tcp_connection* con;
212 215
 #ifdef DISABLE_NAGLE
213 216
 	int flag;
214 217
 #endif
215 218
 
216 219
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
217
-	if (s<0){
220
+	if (s==-1){
218 221
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
219 222
 				errno, strerror(errno));
220 223
 		goto error;
... ...
@@ -261,9 +264,16 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
261 264
 		else si=sendipv6_tcp;
262 265
 #endif
263 266
 	}
264
-	return tcpconn_new(s, server, si, type, S_CONN_CONNECT);
267
+	con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
268
+	if (con==0){
269
+		LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
270
+				 " socket\n");
271
+		goto error;
272
+	}
273
+	return con;
265 274
 	/*FIXME: set sock idx! */
266 275
 error:
276
+	if (s!=-1) close(s); /* close the opened socket */
267 277
 	return 0;
268 278
 }
269 279
 
... ...
@@ -570,7 +580,7 @@ get_fd:
570 580
 						  tmp, tmp->id, tmp->refcnt, tmp->state, n
571 581
 				   );
572 582
 				n=-1; /* fail */
573
-				goto release_c;
583
+				goto end;
574 584
 			}
575 585
 			DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
576 586
 		
... ...
@@ -808,7 +818,7 @@ static inline void handle_new_connect(struct socket_info* si,
808 818
 		su_len=sizeof(su);
809 819
 		new_sock=accept(si->socket, &(su.s), &su_len);
810 820
 		(*n)--;
811
-		if (new_sock<0){
821
+		if (new_sock==-1){
812 822
 			LOG(L_ERR,  "WARNING: tcp_main_loop: error while accepting"
813 823
 					" connection(%d): %s\n", errno, strerror(errno));
814 824
 			return;
... ...
@@ -834,6 +844,10 @@ static inline void handle_new_connect(struct socket_info* si,
834 844
 				}else tcpconn->timeout=0; /* force expire */
835 845
 				TCPCONN_UNLOCK;
836 846
 			}
847
+		}else{ /*tcpconn==0 */
848
+			LOG(L_ERR, "ERROR: tcp_main_loop: tcpconn_new failed, "
849
+					"closing socket\n");
850
+			close(new_sock);
837 851
 		}
838 852
 	}
839 853
 }