From 3669b94160a94c2f266ab065a11579a42e31365e Mon Sep 17 00:00:00 2001 From: Ove Kaaven Date: Sun, 7 Nov 1999 05:23:02 +0000 Subject: [PATCH] Handle socket creation errors. --- server/sock.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/server/sock.c b/server/sock.c index f3aa7bb551..fef1ae6818 100644 --- a/server/sock.c +++ b/server/sock.c @@ -297,10 +297,21 @@ static void sock_destroy( struct object *obj ) static struct object *create_socket( int family, int type, int protocol ) { struct sock *sock; + int sockfd; - if (!(sock = alloc_object( &sock_ops ))) + sockfd = socket( family, type, protocol ); + if (debug_level) + fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd); + if (sockfd == -1) { + sock_set_error(); return NULL; - sock->select.fd = socket(family,type,protocol); + } + fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */ + if (!(sock = alloc_object( &sock_ops ))) { + close( sockfd ); + return NULL; + } + sock->select.fd = sockfd; sock->select.func = sock_select_event; sock->select.private = sock; sock->state = (type!=SOCK_STREAM) ? WS_FD_READ|WS_FD_WRITE : 0; @@ -308,9 +319,6 @@ static struct object *create_socket( int family, int type, int protocol ) sock->hmask = 0; sock->pmask = 0; sock->event = NULL; - if (debug_level) - fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sock->select.fd); - fcntl(sock->select.fd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */ register_select_user( &sock->select ); sock_reselect( sock ); clear_error(); @@ -343,6 +351,7 @@ static struct object *accept_socket( int handle ) } if (!(acceptsock = alloc_object( &sock_ops ))) { + close( acceptfd ); release_object( sock ); return NULL; }