--- libaitio/src/sock.c 2013/08/26 23:37:27 1.2.2.2 +++ libaitio/src/sock.c 2013/08/27 19:07:55 1.2.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: sock.c,v 1.2.2.2 2013/08/26 23:37:27 misho Exp $ +* $Id: sock.c,v 1.2.2.3 2013/08/27 19:07:55 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -202,6 +202,25 @@ ioUpSocket(sock_t * __restrict s, void *arg) return ret; } +static void +thrCliClean(void *arg) +{ + sock_cli_t *cli = (sock_cli_t*) arg; + + close(cli->cli_fd); + AIT_FREE_VAL(&cli->cli_buf); + e_free(cli); +} + +static void * +io_thrCliWrapper(void *arg) +{ + pthread_cleanup_push(thrCliClean, arg); + + pthread_cleanup_pop(42); + pthread_exit(NULL); +} + /* * ioAcceptSocket() - Accept clients * @@ -226,7 +245,8 @@ ioAcceptSocket(sock_t * __restrict s, sock_cb_t f, voi pfd->fd = s->sock_fd; pfd->events = POLLIN | POLLPRI; do { - if (poll(pfd, 1, -1) < 1) { + if (poll(pfd, 1, -1) < 1 || + pfd->revents & (POLLNVAL | POLLHUP || POLLERR)) { LOGERR; return -1; } else @@ -261,7 +281,7 @@ ioAcceptSocket(sock_t * __restrict s, sock_cb_t f, voi memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr); AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf)); - if (pthread_create(&cli->cli_tid, NULL, (void*(*)(void*)) f, cli) == -1) { + if (pthread_create(&cli->cli_tid, NULL, io_thrCliWrapper, cli) == -1) { LOGERR; close(c); AIT_FREE_VAL(&cli->cli_buf);