mq_open(3)
๐ mq_open(3) โ the manual page, but make it bestie
okay so. bestie. we're doing mq_open(3) today, from the Library Functions Manual, and honestly? it's giving "let me introduce you to my message queue." ๐ฉท
It lives in the Real-time library โ that's librt, linked with -lrt. write that down. ๐
โจ NAME โจ
mq_open โ open a message queue. that's it. that's the whole job. open the queue, girl. ๐
๐ LIBRARY ๐
Real-time library (librt, -lrt). don't forget the -lrt, I'm literally begging you. ๐
๐ธ SYNOPSIS ๐ธ
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <mqueue.h>
mqd_t mq_open(const char *name, int oflag);
mqd_t mq_open(const char *name, int oflag, mode_t mode,
struct mq_attr *attr);
two (2) whole forms. one chill, one with the extra args. pick your fighter. โญ
๐ฆ DESCRIPTION ๐ฆ
mq_open() creates a brand new POSIX message queue or opens one that already exists. no cap. it's both at once, it's a dual threat. ๐
The queue is identified by name. and listen โ for the details of how name is constructed, you gotta go read mq_overview(7). that's where the lore lives. ๐
The oflag argument is the flags that control how the call behaves. (you get the flag values defined by including <fcntl.h>, btw.) and here's the thing: exactly one of these three must be in oflag. exactly one. not zero, not two. one. ๐จ
O_RDONLYโ open the queue to receive messages only. inbox mode. ๐ฅO_WRONLYโ open the queue to send messages only. outbox mode. ๐คO_RDWRโ open the queue to both send and receive messages. both. main character energy. ๐
then, zero or more of these can additionally be ORed into oflag:
O_CLOEXEC(since Linux 2.6.26) โ sets the close-on-exec flag on the message queue descriptor. why is that useful? go readopen(2), it explains the whole thing. iykyk. ๐ซถO_CREATโ creates the message queue if it doesn't already exist. and the ownership? the owner (user ID) of the queue gets set to the effective user ID of the calling process, and the group ownership (group ID) gets set to the effective group ID of the calling process. it's YOURS now bestie. ๐O_EXCLโ ifO_CREATwas inoflagand a queue with that name already exists? fail. with the errorEEXIST. no duplicates allowed, we're exclusive here. ๐O_NONBLOCKโ opens the queue in nonblocking mode. so in the situations wheremq_receive(3)andmq_send(3)would normally just... block... they instead fail with the errorEAGAIN. antisocial behavior, honestly. ๐ญ
๐ผ the extra arguments (only if you asked for O_CREAT)
If O_CREAT is specified in oflag, then you must supply two additional arguments. surprise! ๐
The mode argument specifies the permissions placed on the new queue, exactly as for open(2). (and you get the symbolic definitions for those permission bits by including <sys/stat.h>.) oh and โ the permission settings get masked against the process umask. don't forget the umask, it will humble you. ๐
The fields of the struct mq_attr pointed to by attr specify the maximum number of messages and the maximum size of messages the queue will allow. here's the struct, verbatim, no edits:
struct mq_attr {
long mq_flags; /* Flags (ignored for mq_open()) */
long mq_maxmsg; /* Max. # of messages on queue */
long mq_msgsize; /* Max. message size (bytes) */
long mq_curmsgs; /* # of messages currently in queue
(ignored for mq_open()) */
};
and here's the tea: when you call mq_open(), only mq_maxmsg and mq_msgsize are employed. the values in the remaining fields? ignored. literally ignored. sit down. ๐ช
If attr is NULL, then the queue gets created with implementation-defined default attributes. and since Linux 3.5, there are two /proc files you can use to control those defaults โ mq_overview(7) has the details. โจ
๐ RETURN VALUE ๐
On success, mq_open() returns a message queue descriptor for use by the other message queue functions. slay. ๐
On error, mq_open() returns (mqd_t) -1, with errno set to indicate the error. so always check errno, bestie, always. ๐ฉ
๐ ERRORS ๐
EACCESโ the queue exists, but the caller doesn't have permission to open it in the specified mode. rude, but fair.EACCESโ name contained more than one slash. yeah. one slash. that's the limit.EEXISTโ bothO_CREATandO_EXCLwere specified inoflag, but a queue with this name already exists.EINVALโ name doesn't follow the format inmq_overview(7).EINVALโO_CREATwas specified inoflag, and attr was notNULL, butattr->mq_maxmsgorattr->mq_msqsizewas invalid. both of these fields must be greater than zero. and in a process that's unprivileged (i.e. does not have theCAP_SYS_RESOURCEcapability),attr->mq_maxmsgmust be less than or equal to themsg_maxlimit, andattr->mq_msgsizemust be less than or equal to themsgsize_maxlimit. also โ even in a privileged process โattr->mq_maxmsgcannot exceed theHARD_MAXlimit. (details of these limits are inmq_overview(7).) limits on limits on limits, it's a whole thing. ๐ฎโ๐จEMFILEโ the per-process limit on the number of open file and message queue descriptors has been reached (see the description ofRLIMIT_NOFILEingetrlimit(2)).ENAMETOOLONGโ name was too long. ๐ฅฒENFILEโ the system-wide limit on the total number of open files and message queues has been reached. the whole system said "no more."ENOENTโ theO_CREATflag was not specified inoflag, and no queue with this name exists.ENOENTโ name was just"/"followed by no other characters. literally just a slash. nothing. void. ๐ณ๏ธENOMEMโ insufficient memory.ENOSPCโ insufficient space for the creation of a new message queue. this probably happened because thequeues_maxlimit was encountered; seemq_overview(7).
๐ฉบ ATTRIBUTES ๐ฉบ
For an explanation of the terms used in this section, see attributes(7).
- Interface:
mq_open() - Attribute: Thread safety
- Value:
MT-Safe
translation: it's safe across threads. go off. ๐งตโจ
๐ฐ๏ธ VERSIONS ๐ฐ๏ธ
๐ง C library/kernel differences
okay this part is genuinely cool. the mq_open() library function is implemented on top of a system call of the same name. the library function does the check that name starts with a slash (/), and gives the EINVAL error if it doesn't. but the kernel system call expects name to contain no preceding slash โ so the C library function passes name without the preceding slash (i.e., name+1) to the system call. sneaky. love that for them. ๐
๐ STANDARDS ๐
POSIX.1-2008. periodt. ๐
๐๏ธ HISTORY ๐๏ธ
POSIX.1-2001. it's been here. it's been iconic. ๐
๐ BUGS ๐
Before Linux 2.6.14, the process umask was not applied to the permissions specified in mode. so likeโฆ girl. patch your kernel, respectfully. ๐
๐ซถ SEE ALSO ๐ซถ
mq_close(3), mq_getattr(3), mq_notify(3), mq_receive(3), mq_send(3), mq_unlink(3), mq_overview(7)
and that's the whole page, bestie! this one's from the Linux man-pages 6.20, dated 2026-02-08. go open a queue, you're gonna eat. ๐โจ๐๐ฆ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms