Skip to content

Commit de5a582

Browse files
authored
Use php_pollfd_for_ms more consistently for single fd polling (#22872)
* ftp: For single fd poll calls, use php_pollfd_for_ms * pgsql: Use php_pollfd_for_ms in compat shim This is only used on pre-libpq 17. * fastcgi: Use php_pollfd_for_ms for single fd This also handles the case where poll isn't available (does that even matter nowadays?) * io: Use php_pollfd_for_ms for single fd poll * cli: Convert to php_pollfd_for from select This is not a poll call, but it is functionally the same as one; convert it to the single fd polling function which is clearer to read.
1 parent 16da1f5 commit de5a582

6 files changed

Lines changed: 17 additions & 100 deletions

File tree

ext/ftp/ftp.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,9 @@ bool ftp_login(ftpbuf_t *ftp, const char *user, const size_t user_len, const cha
308308

309309
case SSL_ERROR_WANT_READ:
310310
case SSL_ERROR_WANT_WRITE: {
311-
php_pollfd p;
312-
int i;
311+
int i, events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT;
313312

314-
p.fd = ftp->fd;
315-
p.events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT;
316-
p.revents = 0;
317-
318-
i = php_poll2(&p, 1, 300);
313+
i = php_pollfd_for_ms(ftp->fd, events, 300);
319314

320315
retry = i > 0;
321316
}
@@ -1388,14 +1383,9 @@ static int single_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t size) {
13881383

13891384
case SSL_ERROR_WANT_READ:
13901385
case SSL_ERROR_WANT_CONNECT: {
1391-
php_pollfd p;
1392-
int i;
1393-
1394-
p.fd = fd;
1395-
p.events = POLLOUT;
1396-
p.revents = 0;
1386+
int i, events = POLLOUT;
13971387

1398-
i = php_poll2(&p, 1, 300);
1388+
i = php_pollfd_for_ms(fd, events, 300);
13991389

14001390
retry = i > 0;
14011391
}
@@ -1521,14 +1511,9 @@ static int my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len)
15211511

15221512
case SSL_ERROR_WANT_READ:
15231513
case SSL_ERROR_WANT_CONNECT: {
1524-
php_pollfd p;
1525-
int i;
1514+
int i, events = POLLIN|POLLPRI;
15261515

1527-
p.fd = fd;
1528-
p.events = POLLIN|POLLPRI;
1529-
p.revents = 0;
1530-
1531-
i = php_poll2(&p, 1, 300);
1516+
i = php_pollfd_for_ms(fd, events, 300);
15321517

15331518
retry = i > 0;
15341519
}
@@ -1825,14 +1810,9 @@ static databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp)
18251810

18261811
case SSL_ERROR_WANT_READ:
18271812
case SSL_ERROR_WANT_WRITE: {
1828-
php_pollfd p;
1829-
int i;
1830-
1831-
p.fd = data->fd;
1832-
p.events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT;
1833-
p.revents = 0;
1813+
int i, events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT;
18341814

1835-
i = php_poll2(&p, 1, 300);
1815+
i = php_pollfd_for_ms(data->fd, events, 300);
18361816

18371817
retry = i > 0;
18381818
}

ext/pgsql/pgsql.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,19 +448,14 @@ static int PQsocketPoll(int socket, int read, int write, time_t timeout)
448448
if (!read && !write)
449449
return 0;
450450

451-
php_pollfd fd;
452-
int ts = -1;
453-
454-
fd.fd = socket;
455-
fd.events = POLLERR;
456-
fd.revents = 0;
451+
int ts = -1, events = 0;
457452

458453
if (read) {
459-
fd.events |= POLLIN;
454+
events |= POLLIN;
460455
}
461456

462457
if (write) {
463-
fd.events |= POLLOUT;
458+
events |= POLLOUT;
464459
}
465460

466461
if (timeout != (time_t)ts) {
@@ -473,7 +468,7 @@ static int PQsocketPoll(int socket, int read, int write, time_t timeout)
473468
}
474469
}
475470

476-
return php_poll2(&fd, 1, ts);
471+
return php_pollfd_for_ms(socket, events, ts);
477472
}
478473
#endif
479474

main/fastcgi.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ static int is_impersonate = 0;
6868
# include <netdb.h>
6969
# include <signal.h>
7070

71-
# if defined(HAVE_POLL_H) && defined(HAVE_POLL)
72-
# include <poll.h>
73-
# elif defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
74-
# include <sys/poll.h>
75-
# endif
76-
# if defined(HAVE_SYS_SELECT_H)
77-
# include <sys/select.h>
78-
# endif
79-
8071
#ifndef INADDR_NONE
8172
#define INADDR_NONE ((unsigned long) -1)
8273
#endif
@@ -1426,42 +1417,16 @@ int fcgi_accept_request(fcgi_request *req)
14261417
break;
14271418
#else
14281419
if (req->fd >= 0) {
1429-
#if defined(HAVE_POLL)
1430-
struct pollfd fds;
14311420
int ret;
14321421

1433-
fds.fd = req->fd;
1434-
fds.events = POLLIN;
1435-
fds.revents = 0;
14361422
do {
14371423
errno = 0;
1438-
ret = poll(&fds, 1, 5000);
1424+
ret = php_pollfd_for_ms(req->fd, POLLIN, 5000);
14391425
} while (ret < 0 && errno == EINTR);
1440-
if (ret > 0 && (fds.revents & POLLIN)) {
1426+
if (ret & POLLIN) {
14411427
break;
14421428
}
14431429
fcgi_close(req, 1, 0);
1444-
#else
1445-
if (req->fd < FD_SETSIZE) {
1446-
struct timeval tv = {5,0};
1447-
fd_set set;
1448-
int ret;
1449-
1450-
FD_ZERO(&set);
1451-
FD_SET(req->fd, &set);
1452-
do {
1453-
errno = 0;
1454-
ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
1455-
} while (ret < 0 && errno == EINTR);
1456-
if (ret > 0 && FD_ISSET(req->fd, &set)) {
1457-
break;
1458-
}
1459-
fcgi_close(req, 1, 0);
1460-
} else {
1461-
fcgi_log(FCGI_ERROR, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
1462-
fcgi_close(req, 1, 0);
1463-
}
1464-
#endif
14651430
}
14661431
#endif
14671432
}

main/io/php_io.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <winsock2.h>
2424
#else
2525
#include <unistd.h>
26-
#include <poll.h>
2726
#endif
2827

2928
static php_io php_io_instance = {
@@ -106,13 +105,9 @@ static int php_io_generic_wait_for_data(php_io_fd *fd)
106105
? -1
107106
: (int) (fd->timeout.tv_sec * 1000 + fd->timeout.tv_usec / 1000);
108107

109-
struct pollfd pfd;
110-
pfd.fd = fd->fd;
111-
pfd.events = POLLIN;
112-
113108
int ret;
114109
do {
115-
ret = poll(&pfd, 1, timeout_ms);
110+
ret = php_pollfd_for_ms(fd->fd, POLLIN, timeout_ms);
116111
} while (ret == -1 && errno == EINTR);
117112

118113
return ret;

main/io/php_io_copy_linux.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ static inline int php_io_linux_wait_for_data(php_io_fd *fd)
5454
timeout_ms = ptimeout->tv_sec * 1000 + ptimeout->tv_usec / 1000;
5555
}
5656

57-
struct pollfd pfd;
58-
pfd.fd = fd->fd;
59-
pfd.events = POLLIN;
60-
6157
int ret;
6258
do {
63-
ret = poll(&pfd, 1, timeout_ms);
59+
ret = php_pollfd_for_ms(fd->fd, POLLIN, timeout_ms);
6460
} while (ret == -1 && errno == EINTR);
6561

6662
return ret;

sapi/cli/php_cli.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@
7878
#include "php_cli_process_title.h"
7979
#include "php_cli_process_title_arginfo.h"
8080

81-
#ifndef PHP_WIN32
82-
# define php_select(m, r, w, e, t) select(m, r, w, e, t)
83-
#else
84-
# include "win32/select.h"
85-
#endif
86-
8781
#if defined(PHP_WIN32) && defined(HAVE_OPENSSL_EXT)
8882
# include "openssl/applink.c"
8983
#endif
@@ -218,20 +212,12 @@ static void print_extensions(void) /* {{{ */
218212
#ifdef PHP_WRITE_STDOUT
219213
static inline bool sapi_cli_select(php_socket_t fd)
220214
{
221-
fd_set wfd;
222215
struct timeval tv;
223-
int ret;
224-
225-
FD_ZERO(&wfd);
226-
227-
PHP_SAFE_FD_SET(fd, &wfd);
228216

229217
tv.tv_sec = (long)FG(default_socket_timeout);
230218
tv.tv_usec = 0;
231219

232-
ret = php_select(fd+1, NULL, &wfd, NULL, &tv);
233-
234-
return ret != -1;
220+
return php_pollfd_for(fd, POLLOUT, &tv) != -1;
235221
}
236222
#endif
237223

0 commit comments

Comments
 (0)