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 by mq_open(3). And the only flag that can show up in this field is O_NONBLOCK. one (1) flag. that's it. ๐Ÿ’€
  • mq_maxmsg and mq_msgsize โ€” these get set when the message queue is created by mq_open(3). mq_maxmsg is the upper limit on how many messages can be placed on the queue using mq_send(3). mq_msgsize is 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 /proc files that put ceilings on the values for these fields โ€” the details live in mq_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 in mqdes is invalid. not it.
  • EINVAL โ€” newattr->mq_flags contained set bits other than O_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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms