/*************************************************************** SYS-SELECT.C This file contains Linux/i386/ILP32 implementation of __cpu_sys_select(). PART OF : CPU - library . USAGE : Internal only . Copyright (C) 2026 by Andrew V.Kosteltsev. All Rights Reserved. ***************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include __mpu_int64_t __cpu_sys_select( struct __cpu_select_entry *entries, __mpu_uint64_t count, const struct __cpu_timeval *timeout ) { struct pollfd *pfds = (struct pollfd *)0; struct timespec ts; struct timespec *pts = (struct timespec *)0; __mpu_uint64_t i; time_t sec; int ret; if( count && !entries ) return( -(__mpu_int64_t)__MCPU_EINVAL ); if( count > (__mpu_uint64_t)(size_t)-1 / sizeof( *pfds ) ) return( -(__mpu_int64_t)__MCPU_EINVAL ); if( timeout ) { if( timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999 ) return( -(__mpu_int64_t)__MCPU_EINVAL ); sec = (time_t)timeout->tv_sec; if( (__mpu_int64_t)sec != timeout->tv_sec ) return( -(__mpu_int64_t)__MCPU_EOVERFLOW ); ts.tv_sec = sec; ts.tv_nsec = (long)( timeout->tv_usec * 1000 ); pts = &ts; } if( count ) { pfds = (struct pollfd *)calloc( (size_t)count, sizeof( *pfds ) ); if( !pfds ) return( -(__mpu_int64_t)__MCPU_ENOMEM ); } for( i = 0; i < count; ++i ) { pfds[i].fd = entries[i].host_fd; pfds[i].events = 0; pfds[i].revents = 0; entries[i].revents = 0; if( entries[i].events & __CPU_SELECT_READ ) pfds[i].events |= POLLIN; if( entries[i].events & __CPU_SELECT_WRITE ) pfds[i].events |= POLLOUT; if( entries[i].events & __CPU_SELECT_EXCEPT ) pfds[i].events |= POLLPRI; } ret = ppoll( pfds, (nfds_t)count, pts, (const sigset_t *)0 ); if( ret < 0 ) { ret = -(__mpu_int64_t)__cpu_errno_from_host( errno ); free( pfds ); return( (__mpu_int64_t)ret ); } for( i = 0; i < count; ++i ) { short revents = pfds[i].revents; if( revents & POLLNVAL ) { free( pfds ); return( -(__mpu_int64_t)__MCPU_EBADF ); } if( ( entries[i].events & __CPU_SELECT_READ ) && ( revents & ( POLLIN | POLLHUP | POLLERR ) ) ) entries[i].revents |= __CPU_SELECT_READ; if( ( entries[i].events & __CPU_SELECT_WRITE ) && ( revents & ( POLLOUT | POLLERR ) ) ) entries[i].revents |= __CPU_SELECT_WRITE; if( ( entries[i].events & __CPU_SELECT_EXCEPT ) && ( revents & POLLPRI ) ) entries[i].revents |= __CPU_SELECT_EXCEPT; } free( pfds ); return( 0 ); } /* End of __cpu_sys_select() */