add minimal poll(2) implementation for Windows
This provides sufficient functionality to run openssl(1) from a Windows
console. This is based on the original select-based version from from
songdongsheng@live.cn. Changes:
* use nfds_t directly for iterating the fds.
* add WSAGetLastError -> errno mappings
* handle POLLHUP and the OOB data cases for revents
* handle sparse arrays of fds correctly
* KNF style updates
* teach poll how to handle file handles as well as sockets
This handles the socket/non-socket issue by alternating a loop between
WaitForMultipleObjects for non-sockets and and select for sockets. One
would think this would be terrible for performance, but as of this
writing, poll consumes about 6% of the time doing a bulk transfer
between a Linux box and 'openssl.exe s_server'.
I tried to implement this all in terms of WaitForMultipleObjects with a
select 'poll' at the end to get extra specific socket status. However,
the cost of setting up an event handle for each socket, setting the
WSAEventSelect attributes, and cleaning them up reliably was pretty
high. Since the event handle associated with a socket is also global,
creating a new one cancels the previous one or can be disabled
externally.
In addition, the 'FD_WRITE' status of a socket event handle does not
behave in an expected fashion, being triggered by an edge on a write
event rather than being level triggered.
Another fun horror story is how stdin in windows might be a console, it
might be a pipe, it might be something else. If these all worked in the
same way, it would be great. But, since a console-stdin can also signal
on a mouse or window event, it means we can easily get stuck in a
blocking read (you can't make stdin non-blocking) if the non-character
events are not filtered out. So, poll does that too.
See here for various additional horror stories:
http://www.postgresql.org/message-id/4351.1336927207@sss.pgh.pa.us
2014-11-20 07:24:20 +01:00
|
|
|
/*
|
|
|
|
* Public domain
|
2014-11-19 14:43:07 +01:00
|
|
|
*
|
|
|
|
* poll(2) emulation for Windows
|
|
|
|
*
|
|
|
|
* This emulates just-enough poll functionality on Windows to work in the
|
|
|
|
* context of the openssl(1) program. This is not a replacement for
|
|
|
|
* POSIX.1-2001 poll(2).
|
|
|
|
*
|
|
|
|
* Dongsheng Song <dongsheng.song@gmail.com>
|
|
|
|
* Brent Cook <bcook@openbsd.org>
|
add minimal poll(2) implementation for Windows
This provides sufficient functionality to run openssl(1) from a Windows
console. This is based on the original select-based version from from
songdongsheng@live.cn. Changes:
* use nfds_t directly for iterating the fds.
* add WSAGetLastError -> errno mappings
* handle POLLHUP and the OOB data cases for revents
* handle sparse arrays of fds correctly
* KNF style updates
* teach poll how to handle file handles as well as sockets
This handles the socket/non-socket issue by alternating a loop between
WaitForMultipleObjects for non-sockets and and select for sockets. One
would think this would be terrible for performance, but as of this
writing, poll consumes about 6% of the time doing a bulk transfer
between a Linux box and 'openssl.exe s_server'.
I tried to implement this all in terms of WaitForMultipleObjects with a
select 'poll' at the end to get extra specific socket status. However,
the cost of setting up an event handle for each socket, setting the
WSAEventSelect attributes, and cleaning them up reliably was pretty
high. Since the event handle associated with a socket is also global,
creating a new one cancels the previous one or can be disabled
externally.
In addition, the 'FD_WRITE' status of a socket event handle does not
behave in an expected fashion, being triggered by an edge on a write
event rather than being level triggered.
Another fun horror story is how stdin in windows might be a console, it
might be a pipe, it might be something else. If these all worked in the
same way, it would be great. But, since a console-stdin can also signal
on a mouse or window event, it means we can easily get stuck in a
blocking read (you can't make stdin non-blocking) if the non-character
events are not filtered out. So, poll does that too.
See here for various additional horror stories:
http://www.postgresql.org/message-id/4351.1336927207@sss.pgh.pa.us
2014-11-20 07:24:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LIBCRYPTOCOMPAT_POLL_H
|
|
|
|
#define LIBCRYPTOCOMPAT_POLL_H
|
|
|
|
|
2014-07-11 05:06:10 +02:00
|
|
|
#ifndef _WIN32
|
add minimal poll(2) implementation for Windows
This provides sufficient functionality to run openssl(1) from a Windows
console. This is based on the original select-based version from from
songdongsheng@live.cn. Changes:
* use nfds_t directly for iterating the fds.
* add WSAGetLastError -> errno mappings
* handle POLLHUP and the OOB data cases for revents
* handle sparse arrays of fds correctly
* KNF style updates
* teach poll how to handle file handles as well as sockets
This handles the socket/non-socket issue by alternating a loop between
WaitForMultipleObjects for non-sockets and and select for sockets. One
would think this would be terrible for performance, but as of this
writing, poll consumes about 6% of the time doing a bulk transfer
between a Linux box and 'openssl.exe s_server'.
I tried to implement this all in terms of WaitForMultipleObjects with a
select 'poll' at the end to get extra specific socket status. However,
the cost of setting up an event handle for each socket, setting the
WSAEventSelect attributes, and cleaning them up reliably was pretty
high. Since the event handle associated with a socket is also global,
creating a new one cancels the previous one or can be disabled
externally.
In addition, the 'FD_WRITE' status of a socket event handle does not
behave in an expected fashion, being triggered by an edge on a write
event rather than being level triggered.
Another fun horror story is how stdin in windows might be a console, it
might be a pipe, it might be something else. If these all worked in the
same way, it would be great. But, since a console-stdin can also signal
on a mouse or window event, it means we can easily get stuck in a
blocking read (you can't make stdin non-blocking) if the non-character
events are not filtered out. So, poll does that too.
See here for various additional horror stories:
http://www.postgresql.org/message-id/4351.1336927207@sss.pgh.pa.us
2014-11-20 07:24:20 +01:00
|
|
|
#include_next <poll.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
|
|
|
/* Type used for the number of file descriptors. */
|
|
|
|
typedef unsigned long int nfds_t;
|
|
|
|
|
|
|
|
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0600)
|
|
|
|
/* Data structure describing a polling request. */
|
|
|
|
struct pollfd {
|
|
|
|
int fd; /* file descriptor */
|
|
|
|
short events; /* requested events */
|
|
|
|
short revents; /* returned events */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Event types that can be polled */
|
|
|
|
#define POLLIN 0x001 /* There is data to read. */
|
|
|
|
#define POLLPRI 0x002 /* There is urgent data to read. */
|
|
|
|
#define POLLOUT 0x004 /* Writing now will not block. */
|
|
|
|
|
|
|
|
# define POLLRDNORM 0x040 /* Normal data may be read. */
|
|
|
|
# define POLLRDBAND 0x080 /* Priority data may be read. */
|
|
|
|
# define POLLWRNORM 0x100 /* Writing now will not block. */
|
|
|
|
# define POLLWRBAND 0x200 /* Priority data may be written. */
|
|
|
|
|
|
|
|
/* Event types always implicitly polled. */
|
|
|
|
#define POLLERR 0x008 /* Error condition. */
|
|
|
|
#define POLLHUP 0x010 /* Hung up. */
|
|
|
|
#define POLLNVAL 0x020 /* Invalid polling request. */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int poll(struct pollfd *pfds, nfds_t nfds, int timeout);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* HAVE_POLL */
|
|
|
|
|
|
|
|
#endif /* LIBCRYPTOCOMPAT_POLL_H */
|