Question

Is there a particular reason that POSIX message queues require the name be preceded with a '/'?

For example, if were to attempt to open a message queue with the name "thequeue". I would have to use:

mq_open("/thequeue", O_RDONLY);
Was it helpful?

Solution

It's not just message queues but all POSIX IPC including semaphores and shared memory. If you have to attribute it to anything it would be usual Unix historical implementation differences compounded by POSIX's own desire to unite things which more or less failed in this instance.

Richard Stevens devotes a small section to this in Unix Network Programming Vol II. I have probably read those pages a half dozen times over the years and I still don't think I can adequately summarize what he is trying to say. What he is unambiguous about is that POSIX.1 said:

  • the name must conform to existing rules for pathname

  • if name begins with a slash then different calls all reference the same IPC object. If name does not begin with a slash then the result is implementation defined.

  • the implementation of additional slashes in the name is implementation defined.

The current POSIX docs forsem_open, mq_open, and shm_open all list the following restrictions:

  1. It is unspecified whether the name appears in the file system and is visible to other functions that take pathnames as arguments.

  2. The name argument conforms to the construction rules for a pathname, except that

    a) the interpretation of slash characters other than the leading slash character in name is implementation-defined

    b) the length limits for the name argument are implementation-defined and need not be the same as the pathname limits PATH_MAX and NAME_MAX

    c) if name begins with the slash character, then processes calling these functions with the same value of name shall refer to the same IPC object as long as that name has not been removed. If name does not begin with the slash character, the effect is implementation-defined.

In other words, things remain more or less the same as Stevens described.

All of which seems to be a very long-winded way of saying, however unsatisfying, that you need to (1) start the name with slash and (2) not use other slashes in the name or you are entering implementation-defined territory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top