MQ_OPEN(2)
π MQ_OPEN(2) β bestie, we're opening a message queue π
okay so this is the System Calls Manual page for mq_open, section 2, and honestly? it's giving β¨REALTIMEβ¨ communication. pull up a chair, grab a boba, we're doing this. π§
πΈ NAME πΈ
mq_open β open a message queue (REALTIME)
π LIBRARY π
POSIX Real-time Library (librt, -lrt)
lowkey important: you gotta link -lrt, bestie. don't forget or you'll be crying at the linker. π
β¨ SYNOPSIS β¨
#include <mqueue.h>
mqd_t
mq_open(const char *name, int oflag, ...);
π DESCRIPTION π
So mq_open() is literally the matchmaker. It establishes the connection between a process and a message queue, using a message queue descriptor. It creates two things, and yes they're different, stay with me:
- an open message queue description that refers to the message queue
- a message queue descriptor that refers to that open message queue description
Other functions use that descriptor to refer to the message queue. That's its whole job. π
The name argument points to a string naming a message queue. It should conform to the construction rules for a pathname, and it should begin with a slash character. When different processes call mq_open() with the same value of name, they're referring to the same message queue object β as long as that name hasn't been removed. And if name is not the name of an existing message queue and creation wasn't requested? mq_open() will fail and return an error. no queue for you, bestie. π
The oflag argument requests the desired receive and/or send access to the message queue. The requested access permission to receive messages or send messages would be granted if the calling process would be granted read or write access, respectively, to an equivalently protected file.
The value of oflag is the bitwise-inclusive OR of values from the following list. Applications should specify exactly one of the first three values (access modes) below in the value of oflag:
O_RDONLYβ Open the message queue for receiving messages. The process can use the returned message queue descriptor withmq_receive(), but notmq_send(). A message queue may be open multiple times in the same or different processes for receiving messages. π₯O_WRONLYβ Open the queue for sending messages. The process can use the returned message queue descriptor withmq_send()but notmq_receive(). A message queue may be open multiple times in the same or different processes for sending messages. π€O_RDWRβ Open the queue for both receiving and sending messages. The process can use any of the functions allowed forO_RDONLYandO_WRONLY. A message queue may be open multiple times in the same or different processes for sending messages. π
Any combination of the remaining flags may be specified in the value of oflag:
O_CREATβ Create a message queue. It requires two additional arguments:mode, which is of typemode_t, andattr, which is a pointer to anmq_attrstructure. If the pathnamenamehas already been used to create a message queue that still exists, then this flag has no effect, except as noted underO_EXCL. Otherwise, a message queue will be created without any messages in it. The user ID of the message queue will be set to the effective user ID of the process, and the group ID of the message queue will be set to the effective group ID of the process. The permission bits of the message queue will be set to the value of themodeargument, except those set in the file mode creation mask of the process. When bits inmodeother than the file permission bits are specified, the effect is unspecified. IfattrisNULL, the message queue is created with implementation-defined default message queue attributes. Ifattris non-NULLand the calling process has the appropriate privilege onname, the message queuemq_maxmsgandmq_msgsizeattributes will be set to the values of the corresponding members in themq_attrstructure referred to byattr. Ifattris non-NULL, but the calling process does not have the appropriate privilege onname, themq_open()function will fail and return an error without creating the message queue. π«O_EXCLβ IfO_EXCLandO_CREATare set,mq_open()will fail if the message queue name exists. (they said no duplicates, and they meant it π )O_NONBLOCKβ Determines whether anmq_send()ormq_receive()waits for resources or messages that are not currently available, or fails witherrnoset toEAGAIN; seemq_send(2)andmq_receive(2)for details. β©
And a lil plot twist: the mq_open() system call does not add or remove messages from the queue. it just opens the door, it doesn't touch your stuff. πͺβ¨
π NOTES π
FreeBSD implements message queue based on file descriptor. The descriptor is inherited by child after fork(2). The descriptor is closed in a new image after exec(3). The select(2) and kevent(2) system calls are supported for message queue descriptor. π
Please see the mqueuefs(5) man page for instructions on loading the module or compiling the service into the kernel. iykyk. π§
π«Ά RETURN VALUES π«Ά
Upon successful completion, the function returns a message queue descriptor; otherwise, the function returns (mqd_t)-1 and sets the global variable errno to indicate the error. slay when it works, -1 when it doesn't. π
π¨ ERRORS π¨
The mq_open() system call will fail if:
[EACCES]β The message queue exists and the permissions specified byoflagare denied, or the message queue does not exist and permission to create the message queue is denied.[EEXIST]βO_CREATandO_EXCLare set and the named message queue already exists.[EINTR]β Themq_open()function was interrupted by a signal.[EINVAL]β Themq_open()function is not supported for the givenname.[EINVAL]βO_CREATwas specified inoflag, the value ofattris notNULL, and eithermq_maxmsgormq_msgsizewas less than or equal to zero.[EMFILE]β Too many message queue descriptors or file descriptors are currently in use by this process.[ENAMETOOLONG]β The length of thenameargument exceeds{PATH_MAX}or a pathname component is longer than{NAME_MAX}.[ENFILE]β Too many message queues are currently open in the system.[ENOENT]βO_CREATis not set and the named message queue does not exist.[ENOSPC]β There is insufficient space for the creation of the new message queue.
yes, EINVAL shows up twice. that's not me being messy, that's the manpage being thorough. two whole separate reasons. π
π SEE ALSO π
mq_close(2), mq_getattr(2), mq_receive(2), mq_send(2), mq_setattr(2), mq_timedreceive(3), mq_timedsend(3), mq_unlink(3), mqueuefs(5)
the whole friend group fr fr π«
π STANDARDS π
The mq_open() system call conforms to IEEE Std 1003.1-2004 (βPOSIX.1β). certified iconic. π
π¦ HISTORY π¦
Support for POSIX message queues first appeared in FreeBSD 7.0. a classic, honestly. β¨
π BUGS π
This implementation places strict requirements on the value of name: it must begin with a slash (β/β) and contain no other slash characters.
bestie. i'm so serious."/foo/bar"? nope. just"/foo". one slash, at the front, and that's it. π
Β©οΈ COPYRIGHT Β©οΈ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
and that's the whole page, no cap. π this Debian version dropped on September 15, 2014, so shoutout to the Debian girlies keeping the MQ_OPEN(2) lore alive all this time β go open a queue, bestie, you got this. πβ¨πΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2005 David Xu <davidxu@FreeBSD.org>
- Copyright (C) 2001-2004 by the Institute of
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms