From 1c2779e93cbb7e3e27ec0c822f1035e5744a0575 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Tue, 8 Dec 2020 08:54:40 -0600 Subject: add error handling --- P4/util.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'P4/util.c') diff --git a/P4/util.c b/P4/util.c index 5c5bdb2..3967767 100644 --- a/P4/util.c +++ b/P4/util.c @@ -67,15 +67,24 @@ void init(int port) { - if the return value is negative, the request should be ignored. ***********************************************/ int accept_connection(void) { - pthread_mutex_lock(&connection); + if (pthread_mutex_lock(&connection) != 0) { + perror("Cannot aquire lock"); + return -3; + } struct sockaddr_in address; int addrlen = sizeof(address), new_socket = 0; if ((new_socket = accept(sockfd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { - perror("Accept"); - pthread_mutex_unlock(&connection); + perror("Accept error"); + if (pthread_mutex_unlock(&connection) != 0) { + perror("Cannot unlock"); + return -2; + } return -1; } - pthread_mutex_unlock(&connection); + if (pthread_mutex_unlock(&connection) != 0) { + perror("Cannot unlock"); + return -2; + } return(new_socket); } -- cgit v1.2.3