2018-08-31 06:31:35 +03:00
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/fswait.h>
|
|
|
|
|
|
|
|
extern char * _argv_0;
|
|
|
|
|
2023-04-18 02:58:10 +03:00
|
|
|
static char * poll_print_flags(int flags) {
|
|
|
|
static char buf[100];
|
|
|
|
char * b = buf;
|
|
|
|
int saw_something = 0;
|
|
|
|
#define OPT(n) do { if (flags & n) { b += sprintf(b, "%s%s", saw_something ? "|" : "", #n); saw_something = 1; } } while (0)
|
|
|
|
OPT(POLLIN);
|
|
|
|
OPT(POLLOUT);
|
|
|
|
OPT(POLLRDHUP);
|
|
|
|
OPT(POLLERR);
|
|
|
|
OPT(POLLHUP);
|
|
|
|
OPT(POLLNVAL);
|
|
|
|
OPT(POLLPRI);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2023-04-18 13:40:25 +03:00
|
|
|
extern int __libc_debug;
|
|
|
|
|
2018-08-31 06:31:35 +03:00
|
|
|
int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
|
|
|
|
int count_pollin = 0;
|
|
|
|
|
|
|
|
for (nfds_t i = 0; i < nfds; ++i) {
|
|
|
|
if (fds[i].events & POLLIN) {
|
|
|
|
count_pollin++;
|
|
|
|
}
|
|
|
|
fds[i].revents = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nfds_t i = 0; i < nfds; ++i) {
|
|
|
|
if (fds[i].events & (~POLLIN)) {
|
2023-04-18 02:58:10 +03:00
|
|
|
if (fds[i].events == (POLLIN | POLLPRI)) continue;
|
2023-04-18 13:40:25 +03:00
|
|
|
if (__libc_debug) {
|
|
|
|
fprintf(stderr, "%s: poll: unsupported bit set in fds: %s\n", _argv_0, poll_print_flags(fds[i].events));
|
|
|
|
}
|
2023-04-18 02:58:10 +03:00
|
|
|
if ((fds[i].events & POLLOUT) && nfds == 1) {
|
|
|
|
fds[i].revents |= POLLOUT;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
//if (fds[i].events & POLLIN) continue;
|
|
|
|
//fprintf(stderr, "%s: poll: the above error is fatal\n", _argv_0);
|
|
|
|
//return -EINVAL;
|
2018-08-31 06:31:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fswait_fds[count_pollin];
|
|
|
|
int fswait_backref[count_pollin];
|
2019-12-05 11:49:36 +03:00
|
|
|
int fswait_results[count_pollin];
|
2018-08-31 06:31:35 +03:00
|
|
|
int j = 0;
|
|
|
|
for (nfds_t i = 0; i < nfds; ++i) {
|
|
|
|
if (fds[i].events & POLLIN) {
|
|
|
|
fswait_fds[j] = fds[i].fd;
|
|
|
|
fswait_backref[j] = i;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:49:36 +03:00
|
|
|
int ret = fswait3(count_pollin, fswait_fds, timeout, fswait_results);
|
2018-08-31 06:31:35 +03:00
|
|
|
|
|
|
|
if (ret >= 0 && ret < count_pollin) {
|
2019-12-05 11:49:36 +03:00
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < count_pollin; ++i) {
|
|
|
|
if (fswait_results[i]) {
|
|
|
|
fds[fswait_backref[i]].revents = POLLIN;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
2018-08-31 06:31:35 +03:00
|
|
|
} else if (ret == count_pollin) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return ret; /* Error */
|
|
|
|
}
|
|
|
|
}
|