Possible bug with poll syscall
I am using poll with the POLLIN flag to wait for connection requests on a set of listening sockets in a server process. Although clients attempt to connect to those sockets, poll does returns zero after the expiration of the timeout. I believe this might be a bug. As far as I understand poll should be woken up by connection requests and should signal them with a POLLIN. But, then again, I might have misunderstood the specification.
Would anyone please shed some light on this issue?
Thank you very much.
Alex Baretta
Quoted 5 lines · Alex Baretta
I am using poll with the POLLIN flag to wait for connection requests on a set of listening sockets in a server process. Although clients attempt to connect to those sockets, poll does returns zero after the expiration of the timeout.
The very same thing happens if I use select. It seems highly unlikely that this should be the specified behavior of poll and select alike. Is one now forced to used threads to manage multiple ports?
Greetings,
Alex Baretta
Quoted 7 lines · Alex Baretta
Quoted 5 lines · Alex Baretta
I am using poll with the POLLIN flag to wait for connection requests on a set of listening sockets in a server process. Although clients attempt to connect to those sockets, poll does returns zero after the expiration of the timeout.
...
There was a bug in my code. I am unable to find it, but I wrote a minimal to case to prove my point, and actually I proved myself wrong. Test case follows. If I ever find the time I'll try to experiment and discover why in "the real thing" poll did not work for me.
#include <sys/poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main(int argc, char **argv) {
struct pollfd fds;
int res1, res2, nevents;
struct sockaddr_in sockaddr;
fds.fd = socket(PF_INET, SOCK_STREAM, 0); fds.events = POLLIN; sockaddr.sin_family = AF_INET; sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); sockaddr.sin_port = htons(50000);
res1 = bind(fds.fd, (struct sockaddr *)&sockaddr,
sizeof(sockaddr));
res2 = listen(fds.fd, 20);
if (fds.fd == -1 || res1 == -1 || res2 == -1) {
fprintf(stderr, "The program failed miserably.\n");
exit(1);
} fprintf(stderr, "I'm about to suspend myself on a poll
syscall!\n");
nevents = poll(&fds, 1, -1);
fprintf(stderr, "Waking up: nevents = %d\n", nevents);
return 0;
};