From eb7d0f4dd4545e52bcdbe67fd62b053932cc64a6 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 6 Mar 2017 23:27:46 +0100 Subject: [PATCH] Fix select(2) error and end of file handling. --- libc/sys/select/select.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/libc/sys/select/select.c b/libc/sys/select/select.c index e5bf3d05..3b64b115 100644 --- a/libc/sys/select/select.c +++ b/libc/sys/select/select.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2016 Jonas 'Sortie' Termansen. + * Copyright (c) 2013, 2016, 2017 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,9 +21,10 @@ #include #include +#include -static const int READ_EVENTS = POLLIN | POLLRDNORM; -static const int WRITE_EVENTS = POLLOUT | POLLWRNORM; +static const int READ_EVENTS = POLLIN | POLLRDNORM | POLLERR | POLLHUP; +static const int WRITE_EVENTS = POLLOUT | POLLWRNORM | POLLERR; static const int EXCEPT_EVENTS = POLLERR | POLLHUP; int select(int nfds, fd_set* restrict readfds, fd_set* restrict writefds, @@ -45,20 +46,11 @@ int select(int nfds, fd_set* restrict readfds, fd_set* restrict writefds, fds[fds_count].fd = i; fds[fds_count].events = fds[fds_count].revents = 0; if ( readfds && FD_ISSET(i, readfds) ) - { - FD_CLR(i, readfds); fds[fds_count].events |= READ_EVENTS; - } if ( writefds && FD_ISSET(i, writefds) ) - { - FD_CLR(i, writefds); fds[fds_count].events |= WRITE_EVENTS; - } if ( exceptfds && FD_ISSET(i, exceptfds) ) - { - FD_CLR(i, exceptfds); fds[fds_count].events |= EXCEPT_EVENTS; - } if ( fds[fds_count].events ) fds_count++; } @@ -73,6 +65,12 @@ int select(int nfds, fd_set* restrict readfds, fd_set* restrict writefds, int num_occur = ppoll(fds, fds_count, timeout_tsp, NULL); if ( num_occur < 0 ) return -1; + if ( readfds ) + memset(readfds, 0, sizeof(*readfds)); + if ( writefds ) + memset(writefds, 0, sizeof(*writefds)); + if ( exceptfds ) + memset(exceptfds, 0, sizeof(*exceptfds)); int ret = 0; for ( nfds_t i = 0; i < fds_count; i++ ) {