added more error checks

This commit is contained in:
hyung-hwan 2018-03-23 10:02:08 +00:00
parent 8626c6c7aa
commit e7726ac3db
2 changed files with 44 additions and 4 deletions

View File

@ -1639,7 +1639,6 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto)
HCL_LOG0 (proto->hcl, SERVER_LOGMASK_ERROR, "Unexpected EOF without .END\n"); HCL_LOG0 (proto->hcl, SERVER_LOGMASK_ERROR, "Unexpected EOF without .END\n");
return -1; return -1;
} }
/* drop connection silently */ /* drop connection silently */
return 0; return 0;
@ -2193,7 +2192,6 @@ static void purge_all_workers (hcl_server_t* server, hcl_server_worker_state_t w
{ {
zap_worker_in_server (server, worker); zap_worker_in_server (server, worker);
worker->claimed = 1; worker->claimed = 1;
if (worker->sck >= 0) shutdown (worker->sck, SHUT_RDWR); if (worker->sck >= 0) shutdown (worker->sck, SHUT_RDWR);
} }
pthread_mutex_unlock (&server->worker_mutex); pthread_mutex_unlock (&server->worker_mutex);
@ -2367,6 +2365,10 @@ static int setup_listeners (hcl_server_t* server, const hcl_bch_t* addrs)
fcv = fcntl(srv_fd, F_GETFD, 0); fcv = fcntl(srv_fd, F_GETFD, 0);
if (fcv >= 0) fcntl(srv_fd, F_SETFD, fcv | O_CLOEXEC); if (fcv >= 0) fcntl(srv_fd, F_SETFD, fcv | O_CLOEXEC);
#endif #endif
#if defined(O_NONBLOCK)
fcv = fcntl(srv_fd, F_GETFL, 0);
if (fcv >= 0) fcntl(srv_fd, F_SETFL, fcv | O_NONBLOCK);
#endif
if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1) if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1)
{ {
@ -2492,6 +2494,13 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs)
{ {
if (server->stopreq) break; /* normal termination requested */ if (server->stopreq) break; /* normal termination requested */
if (errno == EINTR) continue; /* interrupted but no termination requested */ if (errno == EINTR) continue; /* interrupted but no termination requested */
#if defined(EWOULDBLOCK) && defined(EAGAIN) && (EWOULDBLOCK != EAGAIN)
if (errno == EWOULDBLOCK || errno == EAGAIN) continue;
#elif defined(EWOULDBLOCK)
if (errno == EWOULDBLOCK) continue;
#elif defined(EAGAIN)
if (errno == EAGAIN) continue;
#endif
set_err_with_syserr (server, errno, "unable to accept worker on server socket %d", evp->data.fd); set_err_with_syserr (server, errno, "unable to accept worker on server socket %d", evp->data.fd);
xret = -1; xret = -1;

View File

@ -49,6 +49,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -537,13 +538,32 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char*
sck = socket (sckfam, SOCK_STREAM, 0); sck = socket (sckfam, SOCK_STREAM, 0);
if (sck <= -1) if (sck <= -1)
{ {
fprintf (stderr, "cannot create a socket for %s\n", ipaddr); fprintf (stderr, "cannot create a socket for %s - %s\n", ipaddr, strerror(errno));
goto oops; goto oops;
} }
if (sckfam == AF_INET)
{
struct sockaddr_in anyaddr;
int opt = 1;
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
memset (&anyaddr, 0, HCL_SIZEOF(anyaddr));
anyaddr.sin_family = sckfam;
bind(sck, (struct sockaddr *)&anyaddr, scklen);
}
else if (sckfam == AF_INET6)
{
struct sockaddr_in6 anyaddr;
int opt = 1;
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
memset (&anyaddr, 0, HCL_SIZEOF(anyaddr));
anyaddr.sin6_family = sckfam;
bind(sck, (struct sockaddr *)&anyaddr, scklen);
}
if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1) if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1)
{ {
fprintf (stderr, "cannot connect to %s\n", ipaddr); fprintf (stderr, "cannot connect to %s - %s\n", ipaddr, strerror(errno));
goto oops; goto oops;
} }
@ -633,6 +653,17 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char*
} }
/* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */ /* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */
/*shutdown (sck, (shut_wr_after_req? SHUT_RD: SHUT_RDWR));*/
if (!shut_wr_after_req) shutdown (sck, SHUT_RDWR);
/*{
struct linger linger;
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt (sck, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(linger));
}*/
close (sck); close (sck);
return 0; return 0;