blob: 45ad5d85c473bf6b553a4583a4b4612e1b01c708 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/***************************************************************
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 <config.h>
#endif
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <libmcpu.h>
#include <cpu-sysdeps.h>
__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() */
|