xref: /freebsd/lib/libsys/mq_open.2 (revision ddbfb544c6c9726ae97bcaf105d11dec8b292877)
18269e767SBrooks Davis.\" Copyright (c) 2005 David Xu <davidxu@FreeBSD.org>
28269e767SBrooks Davis.\" All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice(s), this list of conditions and the following disclaimer as
98269e767SBrooks Davis.\"    the first lines of this file unmodified other than the possible
108269e767SBrooks Davis.\"    addition of one or more copyright notices.
118269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
128269e767SBrooks Davis.\"    notice(s), this list of conditions and the following disclaimer in
138269e767SBrooks Davis.\"    the documentation and/or other materials provided with the
148269e767SBrooks Davis.\"    distribution.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
178269e767SBrooks Davis.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
198269e767SBrooks Davis.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
208269e767SBrooks Davis.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
218269e767SBrooks Davis.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
228269e767SBrooks Davis.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
238269e767SBrooks Davis.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
248269e767SBrooks Davis.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
258269e767SBrooks Davis.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
268269e767SBrooks Davis.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278269e767SBrooks Davis.\"
288269e767SBrooks Davis.\" Portions of this text are reprinted and reproduced in electronic form
298269e767SBrooks Davis.\" from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology --
308269e767SBrooks Davis.\" Portable Operating System Interface (POSIX), The Open Group Base
318269e767SBrooks Davis.\" Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of
328269e767SBrooks Davis.\" Electrical and Electronics Engineers, Inc and The Open Group.  In the
338269e767SBrooks Davis.\" event of any discrepancy between this version and the original IEEE and
348269e767SBrooks Davis.\" The Open Group Standard, the original IEEE and The Open Group Standard is
358269e767SBrooks Davis.\" the referee document.  The original Standard can be obtained online at
368269e767SBrooks Davis.\"	http://www.opengroup.org/unix/online.html.
378269e767SBrooks Davis.\"
38*ddbfb544SRicardo Branco.Dd May 15, 2024
398269e767SBrooks Davis.Dt MQ_OPEN 2
408269e767SBrooks Davis.Os
418269e767SBrooks Davis.Sh NAME
428269e767SBrooks Davis.Nm mq_open
438269e767SBrooks Davis.Nd "open a message queue (REALTIME)"
448269e767SBrooks Davis.Sh LIBRARY
458269e767SBrooks Davis.Lb librt
468269e767SBrooks Davis.Sh SYNOPSIS
478269e767SBrooks Davis.In mqueue.h
488269e767SBrooks Davis.Ft mqd_t
498269e767SBrooks Davis.Fn mq_open "const char *name" "int oflag" "..."
508269e767SBrooks Davis.Sh DESCRIPTION
518269e767SBrooks DavisThe
528269e767SBrooks Davis.Fn mq_open
538269e767SBrooks Davissystem call establishes the connection between a process and a message queue
548269e767SBrooks Daviswith a message queue descriptor.
558269e767SBrooks DavisIt creates an open message queue
568269e767SBrooks Davisdescription that refers to the message queue, and a message queue descriptor
578269e767SBrooks Davisthat refers to that open message queue description.
588269e767SBrooks DavisThe message queue
598269e767SBrooks Davisdescriptor is used by other functions to refer to that message queue.
608269e767SBrooks DavisThe
618269e767SBrooks Davis.Fa name
628269e767SBrooks Davisargument points to a string naming a message queue.
638269e767SBrooks DavisThe
648269e767SBrooks Davis.Fa name
658269e767SBrooks Davisargument should conform to the construction rules for a pathname.
668269e767SBrooks DavisThe
678269e767SBrooks Davis.Fa name
688269e767SBrooks Davisshould begin with a slash character.
698269e767SBrooks DavisProcesses calling
708269e767SBrooks Davis.Fn mq_open
718269e767SBrooks Daviswith the same value of
728269e767SBrooks Davis.Fa name
738269e767SBrooks Davisrefers to the same message queue object, as long as that name has not been
748269e767SBrooks Davisremoved.
758269e767SBrooks DavisIf the
768269e767SBrooks Davis.Fa name
778269e767SBrooks Davisargument is not the name of an existing message queue and creation is not
788269e767SBrooks Davisrequested,
798269e767SBrooks Davis.Fn mq_open
808269e767SBrooks Daviswill fail and return an error.
818269e767SBrooks Davis.Pp
828269e767SBrooks DavisThe
838269e767SBrooks Davis.Fa oflag
848269e767SBrooks Davisargument requests the desired receive and/or send access to the message
858269e767SBrooks Davisqueue.
868269e767SBrooks DavisThe requested access permission to receive messages or send messages
878269e767SBrooks Daviswould be granted if the calling process would be granted read or write access,
888269e767SBrooks Davisrespectively, to an equivalently protected file.
898269e767SBrooks Davis.Pp
908269e767SBrooks DavisThe value of
918269e767SBrooks Davis.Fa oflag
928269e767SBrooks Davisis the bitwise-inclusive OR of values from the following list.
938269e767SBrooks DavisApplications should specify exactly one of the first three values (access
948269e767SBrooks Davismodes) below in the value of
958269e767SBrooks Davis.Fa oflag :
968269e767SBrooks Davis.Bl -tag -width ".Dv O_NONBLOCK"
978269e767SBrooks Davis.It Dv O_RDONLY
988269e767SBrooks DavisOpen the message queue for receiving messages.
998269e767SBrooks DavisThe process can use the
1008269e767SBrooks Davisreturned message queue descriptor with
1018269e767SBrooks Davis.Fn mq_receive ,
1028269e767SBrooks Davisbut not
1038269e767SBrooks Davis.Fn mq_send .
1048269e767SBrooks DavisA message queue may be open multiple times in the same or different processes
1058269e767SBrooks Davisfor receiving messages.
1068269e767SBrooks Davis.It Dv O_WRONLY
1078269e767SBrooks DavisOpen the queue for sending messages.
1088269e767SBrooks DavisThe process can use the returned
1098269e767SBrooks Davismessage queue descriptor with
1108269e767SBrooks Davis.Fn mq_send
1118269e767SBrooks Davisbut not
1128269e767SBrooks Davis.Fn mq_receive .
1138269e767SBrooks DavisA message queue may be open multiple times in the same or different processes
1148269e767SBrooks Davisfor sending messages.
1158269e767SBrooks Davis.It Dv O_RDWR
1168269e767SBrooks DavisOpen the queue for both receiving and sending messages.
1178269e767SBrooks DavisThe process can use
1188269e767SBrooks Davisany of the functions allowed for
1198269e767SBrooks Davis.Dv O_RDONLY
1208269e767SBrooks Davisand
1218269e767SBrooks Davis.Dv O_WRONLY .
1228269e767SBrooks DavisA message queue may be open multiple times in the same or different processes
1238269e767SBrooks Davisfor sending messages.
1248269e767SBrooks Davis.El
1258269e767SBrooks Davis.Pp
1268269e767SBrooks DavisAny combination of the remaining flags may be specified in the value of
1278269e767SBrooks Davis.Fa oflag :
1288269e767SBrooks Davis.Bl -tag -width ".Dv O_NONBLOCK"
1298269e767SBrooks Davis.It Dv O_CREAT
1308269e767SBrooks DavisCreate a message queue.
1318269e767SBrooks DavisIt requires two additional arguments:
1328269e767SBrooks Davis.Fa mode ,
1338269e767SBrooks Daviswhich is of type
1348269e767SBrooks Davis.Vt mode_t ,
1358269e767SBrooks Davisand
1368269e767SBrooks Davis.Fa attr ,
1378269e767SBrooks Daviswhich is a pointer to an
1388269e767SBrooks Davis.Vt mq_attr
1398269e767SBrooks Davisstructure.
1408269e767SBrooks DavisIf the pathname
1418269e767SBrooks Davis.Fa name
1428269e767SBrooks Davishas already been used to create a message queue that still exists, then
1438269e767SBrooks Davisthis flag has no effect, except as noted under
1448269e767SBrooks Davis.Dv O_EXCL .
1458269e767SBrooks DavisOtherwise, a message queue will be created without any messages
1468269e767SBrooks Davisin it.
1478269e767SBrooks DavisThe user ID of the message queue will be set to the effective user ID
1488269e767SBrooks Davisof the process, and the group ID of the message queue will be set to the
1498269e767SBrooks Daviseffective group ID of the process.
1508269e767SBrooks DavisThe permission bits of the message queue
1518269e767SBrooks Daviswill be set to the value of the
1528269e767SBrooks Davis.Fa mode
1538269e767SBrooks Davisargument, except those set in the file mode creation mask of the process.
1548269e767SBrooks DavisWhen bits in
1558269e767SBrooks Davis.Fa mode
1568269e767SBrooks Davisother than the file permission bits are specified, the effect is
1578269e767SBrooks Davisunspecified.
1588269e767SBrooks DavisIf
1598269e767SBrooks Davis.Fa attr
1608269e767SBrooks Davisis
1618269e767SBrooks Davis.Dv NULL ,
1628269e767SBrooks Davisthe message queue is created with implementation-defined default message
1638269e767SBrooks Davisqueue attributes.
1648269e767SBrooks DavisIf attr is
1658269e767SBrooks Davis.Pf non- Dv NULL
1668269e767SBrooks Davisand the calling process has the
1678269e767SBrooks Davisappropriate privilege on name, the message queue
1688269e767SBrooks Davis.Va mq_maxmsg
1698269e767SBrooks Davisand
1708269e767SBrooks Davis.Va mq_msgsize
1718269e767SBrooks Davisattributes will be set to the values of the corresponding members in the
1728269e767SBrooks Davis.Vt mq_attr
1738269e767SBrooks Davisstructure referred to by
1748269e767SBrooks Davis.Fa attr .
1758269e767SBrooks DavisIf
1768269e767SBrooks Davis.Fa attr
1778269e767SBrooks Davisis
1788269e767SBrooks Davis.Pf non- Dv NULL ,
1798269e767SBrooks Davisbut the calling process does not have the appropriate privilege
1808269e767SBrooks Davison name, the
1818269e767SBrooks Davis.Fn mq_open
1828269e767SBrooks Davisfunction will fail and return an error without creating the message queue.
1838269e767SBrooks Davis.It Dv O_EXCL
1848269e767SBrooks DavisIf
1858269e767SBrooks Davis.Dv O_EXCL
1868269e767SBrooks Davisand
1878269e767SBrooks Davis.Dv O_CREAT
1888269e767SBrooks Davisare set,
1898269e767SBrooks Davis.Fn mq_open
1908269e767SBrooks Daviswill fail if the message queue name exists.
1918269e767SBrooks Davis.It Dv O_NONBLOCK
1928269e767SBrooks DavisDetermines whether an
1938269e767SBrooks Davis.Fn mq_send
1948269e767SBrooks Davisor
1958269e767SBrooks Davis.Fn mq_receive
1968269e767SBrooks Daviswaits for resources or messages that are not currently available, or fails
1978269e767SBrooks Daviswith
1988269e767SBrooks Davis.Va errno
1998269e767SBrooks Davisset to
2008269e767SBrooks Davis.Er EAGAIN ;
2018269e767SBrooks Davissee
2028269e767SBrooks Davis.Xr mq_send 2
2038269e767SBrooks Davisand
2048269e767SBrooks Davis.Xr mq_receive 2
2058269e767SBrooks Davisfor details.
2068269e767SBrooks Davis.El
2078269e767SBrooks Davis.Pp
2088269e767SBrooks DavisThe
2098269e767SBrooks Davis.Fn mq_open
2108269e767SBrooks Davissystem call does not add or remove messages from the queue.
2118269e767SBrooks Davis.Sh NOTES
2128269e767SBrooks Davis.Fx
2138269e767SBrooks Davisimplements message queue based on file descriptor.
2148269e767SBrooks DavisThe descriptor
2158269e767SBrooks Davisis inherited by child after
2168269e767SBrooks Davis.Xr fork 2 .
2178269e767SBrooks DavisThe descriptor is closed in a new image after
2188269e767SBrooks Davis.Xr exec 3 .
2198269e767SBrooks DavisThe
2208269e767SBrooks Davis.Xr select 2
2218269e767SBrooks Davisand
2228269e767SBrooks Davis.Xr kevent 2
2238269e767SBrooks Davissystem calls are supported for message queue descriptor.
2248269e767SBrooks Davis.Pp
2258269e767SBrooks DavisPlease see the
2261a720cbeSAlexander Ziaee.Xr mqueuefs 4
2278269e767SBrooks Davisman page for instructions on loading the module or compiling the service into
2288269e767SBrooks Davisthe kernel.
2298269e767SBrooks Davis.Pp
2308269e767SBrooks DavisThe number of queues available, the maximum number of messages per queue
2318269e767SBrooks Davisand the maximum message size are tunable
2328269e767SBrooks Davis.Xr sysctl 8
2338269e767SBrooks Davisparameters.
2348269e767SBrooks DavisTheir defaults are as follows.
2358269e767SBrooks Davis.Bl -column kern.mqueue.maxmsgsize integerxxx
2368269e767SBrooks Davis.It Sy "Name	Type	Default"
2378269e767SBrooks Davis.It "kern.mqueue.maxmq	integer	100"
2388269e767SBrooks Davis.It "kern.mqueue.maxmsgsize	integer	16384"
2398269e767SBrooks Davis.It "kern.mqueue.maxmsg	integer	100"
2408269e767SBrooks Davis.El
2418269e767SBrooks Davis.Sh RETURN VALUES
2428269e767SBrooks DavisUpon successful completion, the function returns a message queue
2438269e767SBrooks Davisdescriptor; otherwise, the function returns
2448269e767SBrooks Davis.Po Vt mqd_t Pc Ns \-1
2458269e767SBrooks Davisand sets the global variable
2468269e767SBrooks Davis.Va errno
2478269e767SBrooks Davisto indicate the error.
2488269e767SBrooks Davis.Sh ERRORS
2498269e767SBrooks DavisThe
2508269e767SBrooks Davis.Fn mq_open
2518269e767SBrooks Davissystem call
2528269e767SBrooks Daviswill fail if:
2538269e767SBrooks Davis.Bl -tag -width Er
2548269e767SBrooks Davis.It Bq Er EACCES
2558269e767SBrooks DavisThe message queue exists and the permissions specified by
2568269e767SBrooks Davis.Fa oflag
2578269e767SBrooks Davisare denied, or the message queue does not exist and permission to create the
2588269e767SBrooks Davismessage queue is denied.
2598269e767SBrooks Davis.It Bq Er EEXIST
2608269e767SBrooks Davis.Dv O_CREAT
2618269e767SBrooks Davisand
2628269e767SBrooks Davis.Dv O_EXCL
2638269e767SBrooks Davisare set and the named message queue already exists.
2648269e767SBrooks Davis.It Bq Er EINTR
2658269e767SBrooks DavisThe
2668269e767SBrooks Davis.Fn mq_open
2678269e767SBrooks Davisfunction was interrupted by a signal.
2688269e767SBrooks Davis.It Bq Er EINVAL
2698269e767SBrooks DavisThe
2708269e767SBrooks Davis.Fn mq_open
2718269e767SBrooks Davisfunction is not supported for the given name.
2728269e767SBrooks Davis.It Bq Er EINVAL
2738269e767SBrooks Davis.Dv O_CREAT
2748269e767SBrooks Daviswas specified in
2758269e767SBrooks Davis.Fa oflag ,
2768269e767SBrooks Davisthe value of
2778269e767SBrooks Davis.Fa attr
2788269e767SBrooks Davisis not
2798269e767SBrooks Davis.Dv NULL ,
2808269e767SBrooks Davisand either
2818269e767SBrooks Davis.Va mq_maxmsg
2828269e767SBrooks Davisor
2838269e767SBrooks Davis.Va mq_msgsize
2848269e767SBrooks Daviswas less than or equal to zero.
2858269e767SBrooks Davis.It Bq Er EMFILE
2868269e767SBrooks DavisToo many message queue descriptors or file descriptors are currently in use
2878269e767SBrooks Davisby this process.
2888269e767SBrooks Davis.It Bq Er ENAMETOOLONG
2898269e767SBrooks DavisThe length of the
2908269e767SBrooks Davis.Fa name
2918269e767SBrooks Davisargument exceeds
2928269e767SBrooks Davis.Brq Dv PATH_MAX
2938269e767SBrooks Davisor a pathname component
2948269e767SBrooks Davisis longer than
2958269e767SBrooks Davis.Brq Dv NAME_MAX .
2968269e767SBrooks Davis.It Bq Er ENFILE
2978269e767SBrooks DavisToo many message queues are currently open in the system.
2988269e767SBrooks Davis.It Bq Er ENOENT
2998269e767SBrooks Davis.Dv O_CREAT
3008269e767SBrooks Davisis not set and the named message queue does not exist.
3018269e767SBrooks Davis.It Bq Er ENOSPC
3028269e767SBrooks DavisThere is insufficient space for the creation of the new message queue.
3038269e767SBrooks Davis.El
3048269e767SBrooks Davis.Sh SEE ALSO
3058269e767SBrooks Davis.Xr mq_close 2 ,
3068269e767SBrooks Davis.Xr mq_getattr 2 ,
3078269e767SBrooks Davis.Xr mq_receive 2 ,
3088269e767SBrooks Davis.Xr mq_send 2 ,
3098269e767SBrooks Davis.Xr mq_setattr 2 ,
3108269e767SBrooks Davis.Xr mq_unlink 2 ,
3118269e767SBrooks Davis.Xr mq_timedreceive 3 ,
3128269e767SBrooks Davis.Xr mq_timedsend 3 ,
3131a720cbeSAlexander Ziaee.Xr mqueuefs 4
3148269e767SBrooks Davis.Sh STANDARDS
3158269e767SBrooks DavisThe
3168269e767SBrooks Davis.Fn mq_open
3178269e767SBrooks Davissystem call conforms to
3188269e767SBrooks Davis.St -p1003.1-2004 .
3198269e767SBrooks Davis.Sh HISTORY
3208269e767SBrooks DavisSupport for POSIX message queues first appeared in
3218269e767SBrooks Davis.Fx 7.0 .
3228269e767SBrooks Davis.Sh BUGS
3238269e767SBrooks DavisThis implementation places strict requirements on the value of
3248269e767SBrooks Davis.Fa name :
325*ddbfb544SRicardo Brancoit may begin with a slash
3268269e767SBrooks Davis.Pq Ql /
3278269e767SBrooks Davisand contain no other slash characters.
3288269e767SBrooks Davis.Pp
3298269e767SBrooks DavisThe
3308269e767SBrooks Davis.Fa mode
3318269e767SBrooks Davisand
3328269e767SBrooks Davis.Fa attr
3338269e767SBrooks Davisarguments are variadic and may result in different calling conventions
3348269e767SBrooks Davisthan might otherwise be expected.
3358269e767SBrooks Davis.Sh COPYRIGHT
3368269e767SBrooks DavisPortions of this text are reprinted and reproduced in electronic form
3378269e767SBrooks Davisfrom IEEE Std 1003.1, 2004 Edition, Standard for Information Technology --
3388269e767SBrooks DavisPortable Operating System Interface (POSIX), The Open Group Base
3398269e767SBrooks DavisSpecifications Issue 6, Copyright (C) 2001-2004 by the Institute of
3408269e767SBrooks DavisElectrical and Electronics Engineers, Inc and The Open Group.  In the
3418269e767SBrooks Davisevent of any discrepancy between this version and the original IEEE and
3428269e767SBrooks DavisThe Open Group Standard, the original IEEE and The Open Group Standard is
3438269e767SBrooks Davisthe referee document.  The original Standard can be obtained online at
3448269e767SBrooks Davishttp://www.opengroup.org/unix/online.html.
345