mq_getattr(3)
๐ NAME ๐
okay so bestie, welcome to mq_getattr(3) โ this is the Library Functions Manual page for mq_getattr, mq_setattr, and they're here to get/set message queue attributes. short n sweet, but so much tea inside ๐
๐๏ธ LIBRARY ๐๏ธ
Real-time library, aka librt, and you link it with -lrt. slay.
โจ SYNOPSIS โจ
#include <mqueue.h>
int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
int mq_setattr(mqd_t mqdes, const struct mq_attr *restrict newattr,
struct mq_attr *restrict oldattr);
๐ฆ DESCRIPTION ๐ฆ
So like, mq_getattr() retrieves attributes, and mq_setattr() modifies them. Which attributes? The ones of the message queue that the message queue descriptor mqdes points at. that's the whole vibe ๐ซถ
mq_getattr() returns an mq_attr structure in the buffer that attr points to. And this structure is defined as:
struct mq_attr {
long mq_flags; /* Flags: 0 or O_NONBLOCK */
long mq_maxmsg; /* Max. # of messages on queue */
long mq_msgsize; /* Max. message size (bytes) */
long mq_curmsgs; /* # of messages currently in queue */
};
okay let's break down each field because they're all main characters fr fr:
mq_flagsโ holds flags tied to the open message queue description. It gets initialized when the queue is created bymq_open(3). And the only flag that can show up in this field isO_NONBLOCK. one (1) flag. that's it. ๐mq_maxmsgandmq_msgsizeโ these get set when the message queue is created bymq_open(3).mq_maxmsgis the upper limit on how many messages can be placed on the queue usingmq_send(3).mq_msgsizeis the upper limit on the size of messages that can be placed on the queue. Both of these fields must have a value greater than zero, no debating. and there are two/procfiles that put ceilings on the values for these fields โ the details live inmq_overview(7).mq_curmsgsโ returns the number of messages currently held in the queue. the receipt, basically. ๐งพ
Now for mq_setattr(): it sets message queue attributes using the info supplied in the mq_attr structure pointed to by newattr. Here's the catch bestie โ the only attribute you can modify is the setting of the O_NONBLOCK flag in mq_flags. The other fields in newattr are ignored. literally just looked at and then vibed past. ๐
If the oldattr field is not NULL, then the buffer it points to gets used to return an mq_attr structure containing the same information that mq_getattr() returns. cute lil two-for-one.
๐ฅ RETURN VALUE ๐ฅ
On success, mq_getattr() and mq_setattr() return 0. On error, -1 is returned, and errno is set to indicate the error. classic.
๐ ERRORS ๐
EBADFโ the message queue descriptor specified inmqdesis invalid. not it.EINVALโnewattr->mq_flagscontained set bits other thanO_NONBLOCK. you tried to sneak extra flags in there and it Did Not Work.
๐ธ ATTRIBUTES ๐ธ
For an explanation of the terms used in this section, see attributes(7).
- Interface:
mq_getattr(),mq_setattr() - Attribute: Thread safety
- Value:
MT-Safe
she's thread safe, she's unbothered, she's moisturized โจ
๐ VERSIONS ๐
On Linux, mq_getattr() and mq_setattr() are library functions layered on top of the mq_getsetattr(2) system call. so like, they're the pretty face and the syscall is doing the actual work underneath. iconic team effort.
๐ STANDARDS ๐
POSIX.1-2008.
๐ HISTORY ๐
POSIX.1-2001. she's been around, she's got lore.
๐ EXAMPLES ๐
The program below can be used to show the default mq_maxmsg and mq_msgsize values that get assigned to a message queue created with a call to mq_open(3) where the attr argument is NULL. (yes, NULL. we're letting the system decide.) and here's an example run of the program:
$ ./a.out /testq;
Maximum # of messages on queue: 10
Maximum message size: 8192
Since Linux 3.5, the following /proc files (described in mq_overview(7)) can be used to control the defaults:
$ uname -sr;
Linux 3.8.0
$ cat /proc/sys/fs/mqueue/msg_default;
10
$ cat /proc/sys/fs/mqueue/msgsize_default;
8192
๐ง Program source ๐ง
#include <err.h>
#include <fcntl.h>
#include <mqueue.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
mqd_t mqd;
struct mq_attr attr;
if (argc != 2) {
fprintf(stderr, "Usage: %s mq-name\n", argv[0]);
exit(EXIT_FAILURE);
}
mqd = mq_open(argv[1], O_CREAT | O_EXCL, 0600, NULL);
if (mqd == (mqd_t) -1)
err(EXIT_FAILURE, "mq_open");
if (mq_getattr(mqd, &attr) == -1)
err(EXIT_FAILURE, "mq_getattr");
printf("Maximum # of messages on queue: %ld\n", attr.mq_maxmsg);
printf("Maximum message size: %ld\n", attr.mq_msgsize);
if (mq_unlink(argv[1]) == -1)
err(EXIT_FAILURE, "mq_unlink");
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
mq_close(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3), mq_unlink(3), mq_overview(7)
and that's the whole page bestie!! this one's from Linux man-pages 6.20, dated 2026-02-08 โ go forth and get/set those attributes, you absolute legend ๐ โจ๐ฆ
๐ฆ 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