Why do we need EPOLLRDHUP when EPOLLHUP seems enough? [duplicate]
Asked Answered
K

1

13

According to the linux man page,

EPOLLHUP

When reading from a channel such as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel.

EPOLLRDHUP

Stream socket peer closed connection, or shut down writing half of connection.

I can hardly tell any difference between EPOLLHUP and EPOLLRDHUP.

To me, whenever EPOLLRDHUP is used EPOLLHUP can be used instead with the same semantics.

Am I right? If not, any explanations?

Kei answered 14/8, 2018 at 3:14 Comment(4)
EPOLLHUP means you can't write, EPOLLRDHUP (and read()==0) means you can't read. medium.com/where-the-flamingcow-roams/…Utah
No. EPOLLHUP means you half-close the socket and the peer half-closes the socket too. (So you can't either read or write) See #52976652.Gudrin
Here is nice explanation for these two flags: https://mcmap.net/q/546908/-how-do-i-use-epollhupMoskow
They mean slightly different things and require different handling. Read the man page for shutdown (2)Hyehyena
E
2
  • EPOLLHUP means that the peer closed their end of the connection. Writing to the connection is closed, and after any (possible) readable data is consumed, reading from the connection is closed, too.
  • EPOLLRDHUP only means that the peer closed their connection, or only half of their connection. If it's only halfway closed, the stream socket turns into a one-way, write-only connection. Writing to the connection may still be open, but after any (possible) readable data is consumed, reading from the connection is closed.

This may be done if the peer calls shutdown() on their socket descriptor, disallowing itself from writing data:

#include <sys/socket.h>

int sockfd = /* ... */;
shutdown(sockfd, SHUT_WR);
Eerie answered 11/3, 2021 at 2:53 Comment(2)
Not possible. The localhost cannot detect any difference between the peer closing the socket and shutting it down for output.Wallenstein
@Wallenstein I don't understand. Where did I say that localhost can tell the difference? Shut down for output in the perspective of the peer, or localhost?Eerie

© 2022 - 2024 — McMap. All rights reserved.