Hi Ingo,

I'm modifying some Unix sockets code to add support for abstract sockets (in Linux only, of course), and while I (mostly) know how bind(2) works, I've found surprising that none of the bind(2) pages I've read documented at all how the socklen_t parameter works. I checked the Linux, OpenBSD, and POSIX manual pages.

All of the pages just say that the parameter tells the kernel the size of the structure, as if one was obligated to pass it as 'sizeof(struct sockaddr_un)'. And while that's partly correct, in that the kernel will not read past that, it's not a great thing to word it, or at least some more info could be added:

A user can use that field to make the kernel read less bytes than what the structure can hold. You can for example pass 4 as the argument, and then sun_path will effectively be 2 bytes. No matter what the real size of the structure was.

This is only lightly mentioned in a paragraph of Linux's unix(7), AFAICS:

       *  abstract: an abstract socket address is  distinguished
          (from  a pathname socket) by the fact that sun_path[0]
          is a null byte ('\0').  The socket’s address  in  this
          namespace is given by the additional bytes in sun_path
          that  are  covered  by the specified length of the ad‐
          dress structure.  (Null bytes in the name have no spe‐
          cial significance.)  The name has no  connection  with
          filesystem pathnames.  When the address of an abstract
          socket  is  returned,  the returned addrlen is greater
          than sizeof(sa_family_t) (i.e., greater than  2),  and
          the  name of the socket is contained in the first (ad‐
          drlen - sizeof(sa_family_t)) bytes of sun_path.


This is especially important, because sun_path is one of the few places in C where we deal with fixed-width char arrays, instead of NUL-terminated strings, so programmers should know in detail how the kernel will handle corner cases, and AFAIK different kernels behave differently with sun_path, so it is even more important to document it in detail.

I more or less know how bind(2) works regarding socklen_t, but am not comfortable enough to write documentation about it. Would you mind documenting it in OpenBSD, so that it may help me document it in Linux?

Cheers,

Alex

--
Alejandro Colomar
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to