Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the maximal path length allowed for unix-sockets on linux 108?

When creating a unix socket, the path name (man 7 unix) is allowed to be maximally 108 chars long. For a friend this caused a bug in his program because his path was longer. Now we wonder how exactly that number was determined.

I have the suspicion that the number was determined so that sizeof of that struct sockaddr_un is unambiguous compared to the sizeof of other sockaddresses like sockaddr_in. But if they wanted to avoid clashes with other sizeof values, why not use a prime number for example? Can someone please provide an authorative source for that?

like image 985
Johannes Schaub - litb Avatar asked Jan 16 '16 16:01

Johannes Schaub - litb


2 Answers

It was to match the space available in a handy kernel data structure.

EDIT:

Quoting "The Design and Implementation of the 4.4BSD Operating System" by McKusick et. al. (page 369):

The memory management facilities revolve around a data structure called an mbuf. Mbufs, or memory buffers, are 128 bytes long, with 100 or 108 bytes of this space reserved for data storage.

like image 65
John Hascall Avatar answered Oct 20 '22 06:10

John Hascall


If you cannot find it sometimes it just means that there is nothing to find. But It can also mean that you couldn't find it. However, I would like to share what I found so far and

I make the hard guess that the number is arbritary.

My guess is supported by these two statements from the GNU C Library:

char sun_path[108]

This is the file name to use. Incomplete: Why is 108 a magic number? RMS suggests making this a zero-length array and tweaking the example following to use alloca to allocate an appropriate amount of storage based on the length of the filename.

(Where RMS should be Richard M. Stallman (another guess))

Date Type: struct sockaddr
...

char sa_data[14]

This is the actual socket address data, which is format-dependent. Its length also depends on the format, and may well be more than 14. The length 14 of sa_data is essentially arbitrary.

PS: Don't know why but this kind of questions makes me really curious.

like image 31
terence hill Avatar answered Oct 20 '22 08:10

terence hill