1mq_getattr(3)              Library Functions Manual              mq_getattr(3)
2
3
4

NAME

6       mq_getattr, mq_setattr - get/set message queue attributes
7

LIBRARY

9       Real-time library (librt, -lrt)
10

SYNOPSIS

12       #include <mqueue.h>
13
14       int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
15       int mq_setattr(mqd_t mqdes, const struct mq_attr *restrict newattr,
16                      struct mq_attr *restrict oldattr);
17

DESCRIPTION

19       mq_getattr()  and  mq_setattr()  respectively  retrieve  and modify at‐
20       tributes of the message queue referred to by the message queue descrip‐
21       tor mqdes.
22
23       mq_getattr()  returns  an  mq_attr  structure  in the buffer pointed by
24       attr.  This structure is defined as:
25
26           struct mq_attr {
27               long mq_flags;       /* Flags: 0 or O_NONBLOCK */
28               long mq_maxmsg;      /* Max. # of messages on queue */
29               long mq_msgsize;     /* Max. message size (bytes) */
30               long mq_curmsgs;     /* # of messages currently in queue */
31           };
32
33       The mq_flags field contains flags  associated  with  the  open  message
34       queue description.  This field is initialized when the queue is created
35       by mq_open(3).  The only flag that can appear in this field  is  O_NON‐
36       BLOCK.
37
38       The  mq_maxmsg  and mq_msgsize fields are set when the message queue is
39       created by mq_open(3).  The mq_maxmsg field is an upper  limit  on  the
40       number  of  messages  that may be placed on the queue using mq_send(3).
41       The mq_msgsize field is an upper limit on the size of messages that may
42       be placed on the queue.  Both of these fields must have a value greater
43       than zero.  Two /proc files that place ceilings on the values for these
44       fields are described in mq_overview(7).
45
46       The  mq_curmsgs  field returns the number of messages currently held in
47       the queue.
48
49       mq_setattr() sets message queue attributes using  information  supplied
50       in  the  mq_attr  structure  pointed to by newattr.  The only attribute
51       that can be modified is the setting of the O_NONBLOCK flag in mq_flags.
52       The  other  fields in newattr are ignored.  If the oldattr field is not
53       NULL, then the buffer that it points to is used to  return  an  mq_attr
54       structure  that  contains  the  same  information  that  is returned by
55       mq_getattr().
56

RETURN VALUE

58       On success mq_getattr() and mq_setattr() return 0; on error, -1 is  re‐
59       turned, with errno set to indicate the error.
60

ERRORS

62       EBADF  The message queue descriptor specified in mqdes is invalid.
63
64       EINVAL newattr->mq_flags contained set bits other than O_NONBLOCK.
65

ATTRIBUTES

67       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
68       tributes(7).
69
70       ┌────────────────────────────────────────────┬───────────────┬─────────┐
71Interface                                   Attribute     Value   
72       ├────────────────────────────────────────────┼───────────────┼─────────┤
73mq_getattr(), mq_setattr()                  │ Thread safety │ MT-Safe │
74       └────────────────────────────────────────────┴───────────────┴─────────┘
75

VERSIONS

77       On Linux, mq_getattr() and mq_setattr() are library  functions  layered
78       on top of the mq_getsetattr(2) system call.
79

STANDARDS

81       POSIX.1-2008.
82

HISTORY

84       POSIX.1-2001.
85

EXAMPLES

87       The  program  below  can  be  used  to  show  the default mq_maxmsg and
88       mq_msgsize values that are assigned to a message queue that is  created
89       with  a call to mq_open(3) in which the attr argument is NULL.  Here is
90       an example run of the program:
91
92           $ ./a.out /testq
93           Maximum # of messages on queue:   10
94           Maximum message size:             8192
95
96       Since Linux 3.5, the  following  /proc  files  (described  in  mq_over‐
97       view(7)) can be used to control the defaults:
98
99           $ uname -sr
100           Linux 3.8.0
101           $ cat /proc/sys/fs/mqueue/msg_default
102           10
103           $ cat /proc/sys/fs/mqueue/msgsize_default
104           8192
105
106   Program source
107
108       #include <fcntl.h>
109       #include <mqueue.h>
110       #include <stdio.h>
111       #include <stdlib.h>
112       #include <sys/stat.h>
113       #include <unistd.h>
114
115       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
116                               } while (0)
117
118       int
119       main(int argc, char *argv[])
120       {
121           mqd_t mqd;
122           struct mq_attr attr;
123
124           if (argc != 2) {
125               fprintf(stderr, "Usage: %s mq-name\n", argv[0]);
126               exit(EXIT_FAILURE);
127           }
128
129           mqd = mq_open(argv[1], O_CREAT | O_EXCL, 0600, NULL);
130           if (mqd == (mqd_t) -1)
131               errExit("mq_open");
132
133           if (mq_getattr(mqd, &attr) == -1)
134               errExit("mq_getattr");
135
136           printf("Maximum # of messages on queue:   %ld\n", attr.mq_maxmsg);
137           printf("Maximum message size:             %ld\n", attr.mq_msgsize);
138
139           if (mq_unlink(argv[1]) == -1)
140               errExit("mq_unlink");
141
142           exit(EXIT_SUCCESS);
143       }
144

SEE ALSO

146       mq_close(3),   mq_notify(3),   mq_open(3),  mq_receive(3),  mq_send(3),
147       mq_unlink(3), mq_overview(7)
148
149
150
151Linux man-pages 6.05              2023-07-20                     mq_getattr(3)
Impressum