return 1;
}
-static int do_bind(int socket_fd, struct sockaddr_in *my_addr)
-{
- if (bind(socket_fd, (struct sockaddr *)my_addr,
- sizeof(struct sockaddr)) == -1)
- return -E_BIND;
- return 1;
-}
-
/**
* prepare a structure for \p AF_UNIX socket addresses
*
*/
int init_tcp_socket(int port)
{
- int sockfd, ret;
struct sockaddr_in my_addr;
+ int fd, ret = get_socket();
- if ((sockfd = get_socket()) < 0)
- return sockfd;
- ret = setserversockopts(sockfd);
if (ret < 0)
return ret;
- init_sockaddr(&my_addr, port, NULL);
- ret = do_bind(sockfd, &my_addr);
+ fd = ret;
+ ret = setserversockopts(fd);
if (ret < 0)
- return ret;
- if (listen(sockfd, BACKLOG) == -1)
- return -E_LISTEN;
- PARA_INFO_LOG("listening on port %d, fd %d\n", port, sockfd);
- return sockfd;
+ goto err;
+ init_sockaddr(&my_addr, port, NULL);
+ ret = -E_BIND;
+ if (bind(fd, (struct sockaddr *)&my_addr,
+ sizeof(struct sockaddr)) == -1) {
+ PARA_CRIT_LOG("bind error: %s\n", strerror(errno));
+ goto err;
+ }
+ ret = -E_LISTEN;
+ if (listen(fd, BACKLOG) == -1)
+ goto err;
+ PARA_INFO_LOG("listening on port %d, fd %d\n", port, fd);
+ return fd;
+err:
+ close(fd);
+ return ret;
}
/**