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 209
 	union sockaddr_union my_name;
210 210
 	socklen_t my_name_len;
211 211
 	int optval;
212
+	struct tcp_connection* con;
212 213
 #ifdef DISABLE_NAGLE
213 214
 	int flag;
214 215
 #endif
215 216
 
216 217
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
217
-	if (s<0){
218
+	if (s==-1){
218 219
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
219 220
 				errno, strerror(errno));
220 221
 		goto error;
... ...
@@ -261,9 +264,16 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
261 261
 		else si=sendipv6_tcp;
262 262
 #endif
263 263
 	}
264
-	return tcpconn_new(s, server, si, type, S_CONN_CONNECT);
264
+	con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
265
+	if (con==0){
266
+		LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
267
+				 " socket\n");
268
+		goto error;
269
+	}
270
+	return con;
265 271
 	/*FIXME: set sock idx! */
266 272
 error:
273
+	if (s!=-1) close(s); /* close the opened socket */
267 274
 	return 0;
268 275
 }
269 276
 
... ...
@@ -570,7 +580,7 @@ get_fd:
570 570
 						  tmp, tmp->id, tmp->refcnt, tmp->state, n
571 571
 				   );
572 572
 				n=-1; /* fail */
573
-				goto release_c;
573
+				goto end;
574 574
 			}
575 575
 			DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
576 576
 		
... ...
@@ -808,7 +818,7 @@ static inline void handle_new_connect(struct socket_info* si,
808 808
 		su_len=sizeof(su);
809 809
 		new_sock=accept(si->socket, &(su.s), &su_len);
810 810
 		(*n)--;
811
-		if (new_sock<0){
811
+		if (new_sock==-1){
812 812
 			LOG(L_ERR,  "WARNING: tcp_main_loop: error while accepting"
813 813
 					" connection(%d): %s\n", errno, strerror(errno));
814 814
 			return;
... ...
@@ -834,6 +844,10 @@ static inline void handle_new_connect(struct socket_info* si,
834 834
 				}else tcpconn->timeout=0; /* force expire */
835 835
 				TCPCONN_UNLOCK;
836 836
 			}
837
+		}else{ /*tcpconn==0 */
838
+			LOG(L_ERR, "ERROR: tcp_main_loop: tcpconn_new failed, "
839
+					"closing socket\n");
840
+			close(new_sock);
837 841
 		}
838 842
 	}
839 843
 }