3

For a bound and connected (to an unbound peer) socket created using socket(AF_UNIX, SOCK_SEQPACKET, 0), is there a way to detect that the remote end has hung up, without using poll (the POLLHUP flag will be set), select (for some reason, appearing in the write fd set), or the like?

Unlike SOCK_STREAM, 0-length payloads are valid and indistinguishable from the end of the stream (as far as I can tell; I keep getting 0-length chunks when calling recvmsg on a socket for which the remote end has called shutdown or close). Also there aren't any known (to me) flags visible in struct msghdr that signal the end of the connection. The only thing I see in the Linux kernel source (net/unix/af_unix.c:unix_seqpacket_recvmsg) is the kernel returning -ENOTCONN presumably if the socket is not connected, and then performing the same work as for SOCK_DGRAM.

Note that it is important to use recvmsg rather than recv, recvfrom, etc, because I need to check the flags for MSG_TRUNC.

pilona
  • 320
  • 2
  • 8
  • 2
    Why are `0`-length payloads valid? The man page says `0` return means no more data will be sent from the connected peer. – jxh Aug 13 '13 at 19:24
  • That's for `SOCK_STREAM`, despite what the manual says. Documentation about `SOCK_SEQPACKET` is scarce. – pilona Aug 13 '13 at 21:27
  • I've got some sample code that does demonstrate a valid 0-length message being sent, or at least the syscall doing that (don't know how to trace what's going on in the kernel). No matter, `recvmsg` hangs until the client does `sendmsg` with the 0-length chunk. – pilona Aug 13 '13 at 21:30
  • Why is the client allowed to send a `0`-length chunk? Why doesn't your code just close the connection when that happens? – jxh Aug 13 '13 at 21:39
  • I could do that, in *my* case, but was really wondering whether there could be a more general answer, or if the sockets API really can't handle this. I'd have to try to see whether one can pass ancillary data when using `SOCK_SEQPACKET` and with 0-length chunks. If one can, then this would be a specific kludge. – pilona Aug 14 '13 at 02:41
  • To me, the issue is really to figure out why and how the client is sending `0`-length chunks. The man page says this means the client has indicated it will send no more data. So, is that what the client did, or not? If not, what did it do instead? If you are trying to distinguish the difference between the client calling `shutdown()` and `close()`, there isn't a way without sending data. See [this answer](http://stackoverflow.com/a/11552492/315052). – jxh Aug 14 '13 at 02:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35397/discussion-between-pilona-and-jxh) – pilona Aug 14 '13 at 03:05

0 Answers0