1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MQ_GETATTR 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 mq_getattr, mq_setattr \- get/set message queue attributes
30 .B #include <mqueue.h>
32 .BI "int mq_getattr(mqd_t " mqdes ", struct mq_attr *" attr );
33 .BI "int mq_setattr(mqd_t " mqdes ", const struct mq_attr *restrict " newattr ,
34 .BI " struct mq_attr *restrict " oldattr );
37 Link with \fI\-lrt\fP.
42 respectively retrieve and modify attributes of the message queue
43 referred to by the message queue descriptor
49 structure in the buffer pointed by
51 This structure is defined as:
56 long mq_flags; /* Flags: 0 or O_NONBLOCK */
57 long mq_maxmsg; /* Max. # of messages on queue */
58 long mq_msgsize; /* Max. message size (bytes) */
59 long mq_curmsgs; /* # of messages currently in queue */
66 field contains flags associated with the open message queue description.
67 This field is initialized when the queue is created by
69 The only flag that can appear in this field is
76 fields are set when the message queue is created by
80 field is an upper limit on the number of messages
81 that may be placed on the queue using
85 field is an upper limit on the size of messages
86 that may be placed on the queue.
87 Both of these fields must have a value greater than zero.
90 files that place ceilings on the values for these fields are described in
95 field returns the number of messages currently held in the queue.
98 sets message queue attributes using information supplied in the
100 structure pointed to by
102 The only attribute that can be modified is the setting of the
112 then the buffer that it points to is used to return an
114 structure that contains the same information that is returned by
121 return 0; on error, \-1 is returned, with
123 set to indicate the error.
127 The message queue descriptor specified in
132 .I newattr\->mq_flags
133 contained set bits other than
136 For an explanation of the terms used in this section, see
144 Interface Attribute Value
148 T} Thread safety MT-Safe
154 POSIX.1-2001, POSIX.1-2008.
160 are library functions layered on top of the
161 .BR mq_getsetattr (2)
164 The program below can be used to show the default
168 values that are assigned to a message queue that is created with a call to
173 Here is an example run of the program:
177 $ \fB./a.out /testq\fP
178 Maximum # of messages on queue: 10
179 Maximum message size: 8192
183 Since Linux 3.5, the following
187 can be used to control the defaults:
193 $ \fBcat /proc/sys/fs/mqueue/msg_default\fP
195 $ \fBcat /proc/sys/fs/mqueue/msgsize_default\fP
203 #include <sys/stat.h>
209 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
213 main(int argc, char *argv[])
219 fprintf(stderr, "Usage: %s mq\-name\en", argv[0]);
223 mqd = mq_open(argv[1], O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL);
224 if (mqd == (mqd_t) \-1)
227 if (mq_getattr(mqd, &attr) == \-1)
228 errExit("mq_getattr");
230 printf("Maximum # of messages on queue: %ld\en", attr.mq_maxmsg);
231 printf("Maximum message size: %ld\en", attr.mq_msgsize);
233 if (mq_unlink(argv[1]) == \-1)
234 errExit("mq_unlink");