xref: /PHP-7.3/win32/select.c (revision 8d3f8ca1)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Wez Furlong <wez@thebrainroom.com>                           |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "php_network.h"
21 
22 /* Win32 select() will only work with sockets, so we roll our own implementation here.
23  * - If you supply only sockets, this simply passes through to winsock select().
24  * - If you supply file handles, there is no way to distinguish between
25  *   ready for read/write or OOB, so any set in which the handle is found will
26  *   be marked as ready.
27  * - If you supply a mixture of handles and sockets, the system will interleave
28  *   calls between select() and WaitForMultipleObjects(). The time slicing may
29  *   cause this function call to take up to 100 ms longer than you specified.
30  * - Calling this with NULL sets as a portable way to sleep with sub-second
31  *   accuracy is not supported.
32  * */
php_select(php_socket_t max_fd,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * tv)33 PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
34 {
35 	ULONGLONG ms_total, limit;
36 	HANDLE handles[MAXIMUM_WAIT_OBJECTS];
37 	int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
38 	int n_handles = 0, i;
39 	fd_set sock_read, sock_write, sock_except;
40 	fd_set aread, awrite, aexcept;
41 	int sock_max_fd = -1;
42 	struct timeval tvslice;
43 	int retcode;
44 
45 	/* As max_fd is unsigned, non socket might overflow. */
46 	if (max_fd > (php_socket_t)INT_MAX) {
47 		return -1;
48 	}
49 
50 #define SAFE_FD_ISSET(fd, set)	(set != NULL && FD_ISSET(fd, set))
51 
52 	/* calculate how long we need to wait in milliseconds */
53 	if (tv == NULL) {
54 		ms_total = INFINITE;
55 	} else {
56 		ms_total = tv->tv_sec * 1000;
57 		ms_total += tv->tv_usec / 1000;
58 	}
59 
60 	FD_ZERO(&sock_read);
61 	FD_ZERO(&sock_write);
62 	FD_ZERO(&sock_except);
63 
64 	/* build an array of handles for non-sockets */
65 	for (i = 0; (uint32_t)i < max_fd; i++) {
66 		if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
67 			handles[n_handles] = (HANDLE)(zend_uintptr_t)_get_osfhandle(i);
68 			if (handles[n_handles] == INVALID_HANDLE_VALUE) {
69 				/* socket */
70 				if (SAFE_FD_ISSET(i, rfds)) {
71 					FD_SET((uint32_t)i, &sock_read);
72 				}
73 				if (SAFE_FD_ISSET(i, wfds)) {
74 					FD_SET((uint32_t)i, &sock_write);
75 				}
76 				if (SAFE_FD_ISSET(i, efds)) {
77 					FD_SET((uint32_t)i, &sock_except);
78 				}
79 				if (i > sock_max_fd) {
80 					sock_max_fd = i;
81 				}
82 			} else {
83 				handle_slot_to_fd[n_handles] = i;
84 				n_handles++;
85 			}
86 		}
87 	}
88 
89 	if (n_handles == 0) {
90 		/* plain sockets only - let winsock handle the whole thing */
91 		return select(-1, rfds, wfds, efds, tv);
92 	}
93 
94 	/* mixture of handles and sockets; lets multiplex between
95 	 * winsock and waiting on the handles */
96 
97 	FD_ZERO(&aread);
98 	FD_ZERO(&awrite);
99 	FD_ZERO(&aexcept);
100 
101 	limit = GetTickCount64() + ms_total;
102 	do {
103 		retcode = 0;
104 
105 		if (sock_max_fd >= 0) {
106 			/* overwrite the zero'd sets here; the select call
107 			 * will clear those that are not active */
108 			aread = sock_read;
109 			awrite = sock_write;
110 			aexcept = sock_except;
111 
112 			tvslice.tv_sec = 0;
113 			tvslice.tv_usec = 100000;
114 
115 			retcode = select(-1, &aread, &awrite, &aexcept, &tvslice);
116 		}
117 		if (n_handles > 0) {
118 			/* check handles */
119 			DWORD wret;
120 
121 			wret = WaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100);
122 
123 			if (wret == WAIT_TIMEOUT) {
124 				/* set retcode to 0; this is the default.
125 				 * select() may have set it to something else,
126 				 * in which case we leave it alone, so this branch
127 				 * does nothing */
128 				;
129 			} else if (wret == WAIT_FAILED) {
130 				if (retcode == 0) {
131 					retcode = -1;
132 				}
133 			} else {
134 				if (retcode < 0) {
135 					retcode = 0;
136 				}
137 				for (i = 0; i < n_handles; i++) {
138 					if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
139 						if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
140 							FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
141 						}
142 						if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
143 							FD_SET((uint32_t)handle_slot_to_fd[i], &awrite);
144 						}
145 						if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
146 							FD_SET((uint32_t)handle_slot_to_fd[i], &aexcept);
147 						}
148 						retcode++;
149 					}
150 				}
151 			}
152 		}
153 	} while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit));
154 
155 	if (rfds) {
156 		*rfds = aread;
157 	}
158 	if (wfds) {
159 		*wfds = awrite;
160 	}
161 	if (efds) {
162 		*efds = aexcept;
163 	}
164 
165 	return retcode;
166 }
167 
168 /*
169  * Local variables:
170  * tab-width: 4
171  * c-basic-offset: 4
172  * End:
173  * vim600: noet sw=4 ts=4 fdm=marker
174  * vim<600: noet sw=4 ts=4
175  */
176