1MQ_OPEN(3)                 Linux Programmer's Manual                MQ_OPEN(3)
2
3
4

NAME

6       mq_open - open a message queue
7

SYNOPSIS

9       #include <fcntl.h>           /* For O_* constants */
10       #include <sys/stat.h>        /* For mode constants */
11       #include <mqueue.h>
12
13       mqd_t mq_open(const char *name, int oflag);
14       mqd_t mq_open(const char *name, int oflag, mode_t mode,
15                     struct mq_attr *attr);
16
17       Link with -lrt.
18

DESCRIPTION

20       mq_open() creates a new POSIX message queue or opens an existing queue.
21       The queue is identified by name.  For details of  the  construction  of
22       name, see mq_overview(7).
23
24       The  oflag  argument  specifies flags that control the operation of the
25       call.  (Definitions of the flags values can be  obtained  by  including
26       <fcntl.h>.)  Exactly one of the following must be specified in oflag:
27
28       O_RDONLY
29              Open the queue to receive messages only.
30
31       O_WRONLY
32              Open the queue to send messages only.
33
34       O_RDWR Open the queue to both send and receive messages.
35
36       Zero or more of the following flags can additionally be ORed in oflag:
37
38       O_CLOEXEC (since Linux 2.6.26)
39              Set  the  close-on-exec  flag  for the message queue descriptor.
40              See open(2) for a discussion of why this flag is useful.
41
42       O_CREAT
43              Create the message queue if it does not exist.  The owner  (user
44              ID)  of the message queue is set to the effective user ID of the
45              calling process.  The group ownership (group ID) is set  to  the
46              effective group ID of the calling process.
47
48       O_EXCL If  O_CREAT  was  specified in oflag, and a queue with the given
49              name already exists, then fail with the error EEXIST.
50
51       O_NONBLOCK
52              Open the queue in  nonblocking  mode.   In  circumstances  where
53              mq_receive(3)  and  mq_send(3) would normally block, these func‐
54              tions instead fail with the error EAGAIN.
55
56       If O_CREAT is specified in oflag, then two additional arguments must be
57       supplied.   The mode argument specifies the permissions to be placed on
58       the new queue, as for open(2).  (Symbolic definitions for  the  permis‐
59       sions bits can be obtained by including <sys/stat.h>.)  The permissions
60       settings are masked against the process umask.
61
62       The fields of the struct mq_attr pointed to attr  specify  the  maximum
63       number of messages and the maximum size of messages that the queue will
64       allow.  This structure is defined as follows:
65
66           struct mq_attr {
67               long mq_flags;       /* Flags (ignored for mq_open()) */
68               long mq_maxmsg;      /* Max. # of messages on queue */
69               long mq_msgsize;     /* Max. message size (bytes) */
70               long mq_curmsgs;     /* # of messages currently in queue
71                                       (ignored for mq_open()) */
72           };
73
74       Only the mq_maxmsg and mq_msgsize  fields  are  employed  when  calling
75       mq_open(); the values in the remaining fields are ignored.
76
77       If  attr is NULL, then the queue is created with implementation-defined
78       default attributes.  Since Linux 3.5, two /proc files can  be  used  to
79       control these defaults; see mq_overview(7) for details.
80

RETURN VALUE

82       On  success,  mq_open()  returns  a message queue descriptor for use by
83       other message queue functions.  On error, mq_open() returns (mqd_t) -1,
84       with errno set to indicate the error.
85

ERRORS

87       EACCES The  queue  exists,  but  the caller does not have permission to
88              open it in the specified mode.
89
90       EACCES name contained more than one slash.
91
92       EEXIST Both O_CREAT and O_EXCL were specified in  oflag,  but  a  queue
93              with this name already exists.
94
95       EINVAL name doesn't follow the format in mq_overview(7).
96
97       EINVAL O_CREAT  was  specified  in  oflag,  and  attr was not NULL, but
98              attr->mq_maxmsg or attr->mq_msqsize was invalid.  Both of  these
99              fields must be greater than zero.  In a process that is unprivi‐
100              leged  (does  not   have   the   CAP_SYS_RESOURCE   capability),
101              attr->mq_maxmsg must be less than or equal to the msg_max limit,
102              and attr->mq_msgsize must be less than  or  equal  to  the  msg‐
103              size_max  limit.   In  addition,  even  in a privileged process,
104              attr->mq_maxmsg cannot exceed the HARD_MAX limit.  (See mq_over‐
105              view(7) for details of these limits.)
106
107       EMFILE The  per-process  limit  on  the number of open file and message
108              queue descriptors has  been  reached  (see  the  description  of
109              RLIMIT_NOFILE in getrlimit(2)).
110
111       ENAMETOOLONG
112              name was too long.
113
114       ENFILE The system-wide limit on the total number of open files and mes‐
115              sage queues has been reached.
116
117       ENOENT The O_CREAT flag was not specified in oflag, and no  queue  with
118              this name exists.
119
120       ENOENT name was just "/" followed by no other characters.
121
122       ENOMEM Insufficient memory.
123
124       ENOSPC Insufficient  space  for  the  creation  of a new message queue.
125              This probably occurred because the queues_max limit was  encoun‐
126              tered; see mq_overview(7).
127

ATTRIBUTES

129       For   an   explanation   of   the  terms  used  in  this  section,  see
130       attributes(7).
131
132       ┌──────────┬───────────────┬─────────┐
133Interface Attribute     Value   
134       ├──────────┼───────────────┼─────────┤
135mq_open() │ Thread safety │ MT-Safe │
136       └──────────┴───────────────┴─────────┘

CONFORMING TO

138       POSIX.1-2001, POSIX.1-2008.
139

NOTES

141   C library/kernel differences
142       The mq_open() library function is implemented on top of a  system  call
143       of  the  same  name.   The library function performs the check that the
144       name starts with a slash (/), giving the EINVAL error if it  does  not.
145       The  kernel  system call expects name to contain no preceding slash, so
146       the C library function passes name without the preceding  slash  (i.e.,
147       name+1) to the system call.
148

BUGS

150       In kernels before 2.6.14, the process umask was not applied to the per‐
151       missions specified in mode.
152

SEE ALSO

154       mq_close(3), mq_getattr(3),  mq_notify(3),  mq_receive(3),  mq_send(3),
155       mq_unlink(3), mq_overview(7)
156

COLOPHON

158       This  page  is  part of release 4.16 of the Linux man-pages project.  A
159       description of the project, information about reporting bugs,  and  the
160       latest     version     of     this    page,    can    be    found    at
161       https://www.kernel.org/doc/man-pages/.
162
163
164
165Linux                             2017-09-15                        MQ_OPEN(3)
Impressum