---
name: "mq_getattr"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/mq_getattr/3"
---

# mq\_getattr(3)

get/set message queue attributes

## NAME

mq\_getattr, mq\_setattr - get/set message queue attributes

## LIBRARY

Real-time library (*librt*, *-lrt*)

## 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

**mq\_getattr**() and **mq\_setattr**() respectively retrieve and modify attributes of the message queue referred to by the message queue descriptor *mqdes*.

**mq\_getattr**() returns an *mq\_attr* structure in the buffer pointed by *attr*. 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 */
};
```

The *mq\_flags* field contains flags associated with the open message queue description. This field is initialized when the queue is created by [**mq\_open**(3)](/mq_open/3.md). The only flag that can appear in this field is **O\_NONBLOCK**.

The *mq\_maxmsg* and *mq\_msgsize* fields are set when the message queue is created by [**mq\_open**(3)](/mq_open/3.md). The *mq\_maxmsg* field is an upper limit on the number of messages that may be placed on the queue using [**mq\_send**(3)](/mq_send/3.md). The *mq\_msgsize* field is an upper limit on the size of messages that may be placed on the queue. Both of these fields must have a value greater than zero. Two */proc* files that place ceilings on the values for these fields are described in [**mq\_overview**(7)](/mq_overview/7.md).

The *mq\_curmsgs* field returns the number of messages currently held in the queue.

**mq\_setattr**() sets message queue attributes using information supplied in the *mq\_attr* structure pointed to by *newattr*. The only attribute that can be modified is the setting of the **O\_NONBLOCK** flag in *mq\_flags*. The other fields in *newattr* are ignored. If the *oldattr* field is not NULL, then the buffer that it points to is used to return an *mq\_attr* structure that contains the same information that is returned by **mq\_getattr**().

## RETURN VALUE

On success **mq\_getattr**() and **mq\_setattr**() return 0; on error, -1 is returned, with *errno* set to indicate the error.

## ERRORS

- **EBADF**

  The message queue descriptor specified in *mqdes* is invalid.

- **EINVAL**

  *newattr-\>mq\_flags* contained set bits other than **O\_NONBLOCK**.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **mq\_getattr**(), **mq\_setattr**() | Thread safety | MT-Safe |

## VERSIONS

On Linux, **mq\_getattr**() and **mq\_setattr**() are library functions layered on top of the [**mq\_getsetattr**(2)](/mq_getsetattr/2.md) system call.

## STANDARDS

POSIX.1-2008.

## HISTORY

POSIX.1-2001.

## EXAMPLES

The program below can be used to show the default *mq\_maxmsg* and *mq\_msgsize* values that are assigned to a message queue that is created with a call to [**mq\_open**(3)](/mq_open/3.md) in which the *attr* argument is NULL. Here is 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)](/mq_overview/7.md)) 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_close/3.md), [**mq\_notify**(3)](/mq_notify/3.md), [**mq\_open**(3)](/mq_open/3.md), [**mq\_receive**(3)](/mq_receive/3.md), [**mq\_send**(3)](/mq_send/3.md), [**mq\_unlink**(3)](/mq_unlink/3.md), [**mq\_overview**(7)](/mq_overview/7.md)
