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 read open(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 โ€” if O_CREAT was in oflag and a queue with that name already exists? fail. with the error EEXIST. no duplicates allowed, we're exclusive here. ๐Ÿ’…
  • O_NONBLOCK โ€” opens the queue in nonblocking mode. so in the situations where mq_receive(3) and mq_send(3) would normally just... block... they instead fail with the error EAGAIN. 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 โ€” both O_CREAT and O_EXCL were specified in oflag, but a queue with this name already exists.
  • EINVAL โ€” name doesn't follow the format in mq_overview(7).
  • EINVAL โ€” O_CREAT was specified in oflag, and attr was not NULL, but attr->mq_maxmsg or attr->mq_msqsize was invalid. both of these fields must be greater than zero. and in a process that's unprivileged (i.e. does not have the CAP_SYS_RESOURCE capability), attr->mq_maxmsg must be less than or equal to the msg_max limit, and attr->mq_msgsize must be less than or equal to the msgsize_max limit. also โ€” even in a privileged process โ€” attr->mq_maxmsg cannot exceed the HARD_MAX limit. (details of these limits are in mq_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 of RLIMIT_NOFILE in getrlimit(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 โ€” the O_CREAT flag was not specified in oflag, 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 the queues_max limit was encountered; see mq_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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms