In the man unix(7) I found the following:
pathname: a UNIX domain socket can be bound to a null-terminated filesystem pathname using bind(2). When the address of a pathname socket is returned (by one of the system calls noted above), its length is
offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
The struct sockaddr_un
is defined as
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[108]; /* pathname */
};
So I would guess that we can simply do strlen(sun_path) + sizeof(sun_family)
I don't understand the +1
they added. Can you please give an explanation? I understand the offsetof
uses for portabiliy as described
On Linux, the above
offsetof()
expression equates to the same value assizeof(sa_family_t)
, but some other implementations include other fields before sun_path, so theoffsetof()
expression more portably describes the size of the address structure.
But this +1
is not clear to me.