MSGRCV(3)                  POSIX Programmer's Manual                 MSGRCV(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       msgrcv -- XSI message receive operation

SSYYNNOOPPSSIISS
       #include <sys/msg.h>

       ssize_t msgrcv(int _m_s_q_i_d, void *_m_s_g_p, size_t _m_s_g_s_z, long _m_s_g_t_y_p,
           int _m_s_g_f_l_g);

DDEESSCCRRIIPPTTIIOONN
       The _m_s_g_r_c_v() function operates on XSI message queues (see the Base
       Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _3_._2_2_5, _M_e_s_s_a_g_e _Q_u_e_u_e).  It
       is unspecified whether this function interoperates with the realtime
       interprocess communication facilities defined in _S_e_c_t_i_o_n _2_._8, _R_e_a_l_t_i_m_e.

       The _m_s_g_r_c_v() function shall read a message from the queue associated
       with the message queue identifier specified by _m_s_q_i_d and place it in
       the user-defined buffer pointed to by _m_s_g_p.

       The application shall ensure that the argument _m_s_g_p points to a user-
       defined buffer that contains first a field of type lloonngg specifying the
       type of the message, and then a data portion that holds the data bytes
       of the message. The structure below is an example of what this user-
       defined buffer might look like:

           ssttrruucctt mmyymmssgg {{
               lloonngg    mmttyyppee;;     //** MMeessssaaggee ttyyppee.. **//
               cchhaarr    mmtteexxtt[[11]];;  //** MMeessssaaggee tteexxtt.. **//
           }}

       The structure member _m_t_y_p_e is the received message's type as specified
       by the sending process.

       The structure member _m_t_e_x_t is the text of the message.

       The argument _m_s_g_s_z specifies the size in bytes of _m_t_e_x_t.  The received
       message shall be truncated to _m_s_g_s_z bytes if it is larger than _m_s_g_s_z
       and (_m_s_g_f_l_g & MSG_NOERROR) is non-zero.  The truncated part of the
       message shall be lost and no indication of the truncation shall be
       given to the calling process.

       If the value of _m_s_g_s_z is greater than {SSIZE_MAX}, the result is
       implementation-defined.

       The argument _m_s_g_t_y_p specifies the type of message requested as follows:

        *  If _m_s_g_t_y_p is 0, the first message on the queue shall be received.

        *  If _m_s_g_t_y_p is greater than 0, the first message of type _m_s_g_t_y_p shall
           be received.

        *  If _m_s_g_t_y_p is less than 0, the first message of the lowest type that
           is less than or equal to the absolute value of _m_s_g_t_y_p shall be
           received.

       The argument _m_s_g_f_l_g specifies the action to be taken if a message of
       the desired type is not on the queue. These are as follows:

        *  If (_m_s_g_f_l_g & IPC_NOWAIT) is non-zero, the calling thread shall
           return immediately with a return value of -1 and _e_r_r_n_o set to
           [[EENNOOMMSSGG]].

        *  If (_m_s_g_f_l_g & IPC_NOWAIT) is 0, the calling thread shall suspend
           execution until one of the following occurs:

           --  A message of the desired type is placed on the queue.

           --  The message queue identifier _m_s_q_i_d is removed from the system;
               when this occurs, _e_r_r_n_o shall be set to [[EEIIDDRRMM]] and -1 shall be
               returned.

           --  The calling thread receives a signal that is to be caught; in
               this case a message is not received and the calling thread
               resumes execution in the manner prescribed in _s_i_g_a_c_t_i_o_n().

       Upon successful completion, the following actions are taken with
       respect to the data structure associated with _m_s_q_i_d:

        *  mmssgg__qqnnuumm shall be decremented by 1.

        *  mmssgg__llrrppiidd shall be set to the process ID of the calling process.

        *  mmssgg__rrttiimmee shall be set to the current time, as described in _S_e_c_t_i_o_n
           _2_._7_._1, _I_P_C _G_e_n_e_r_a_l _D_e_s_c_r_i_p_t_i_o_n.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _m_s_g_r_c_v() shall return a value equal to the
       number of bytes actually placed into the buffer _m_t_e_x_t.  Otherwise, no
       message shall be received, _m_s_g_r_c_v() shall return -1, and _e_r_r_n_o shall be
       set to indicate the error.

EERRRROORRSS
       The _m_s_g_r_c_v() function shall fail if:

       EE22BBIIGG  The value of _m_t_e_x_t is greater than _m_s_g_s_z and (_m_s_g_f_l_g &
              MSG_NOERROR) is 0.

       EEAACCCCEESS Operation permission is denied to the calling process; see
              _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n.

       EEIIDDRRMM  The message queue identifier _m_s_q_i_d is removed from the system.

       EEIINNTTRR  The _m_s_g_r_c_v() function was interrupted by a signal.

       EEIINNVVAALL _m_s_q_i_d is not a valid message queue identifier.

       EENNOOMMSSGG The queue does not contain a message of the desired type and
              (_m_s_g_f_l_g & IPC_NOWAIT) is non-zero.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   RReecceeiivviinngg aa MMeessssaaggee
       The following example receives the first message on the queue (based on
       the value of the _m_s_g_t_y_p argument, 0). The queue is identified by the
       _m_s_q_i_d argument (assuming that the value has previously been set). This
       call specifies that an error should be reported if no message is
       available, but not if the message is too large. The message size is
       calculated directly using the _s_i_z_e_o_f operator.

           ##iinncclluuddee <<ssyyss//mmssgg..hh>>
           ......
           iinntt rreessuulltt;;
           iinntt mmssqqiidd;;
           ssttrruucctt mmeessssaaggee {{
               lloonngg ttyyppee;;
               cchhaarr tteexxtt[[2200]];;
           }} mmssgg;;
           lloonngg mmssggttyypp == 00;;
           ......
           rreessuulltt == mmssggrrccvv((mmssqqiidd,, ((vvooiidd **)) &&mmssgg,, ssiizzeeooff((mmssgg..tteexxtt)),,
                    mmssggttyypp,, MMSSGG__NNOOEERRRROORR || IIPPCC__NNOOWWAAIITT));;

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The POSIX Realtime Extension defines alternative interfaces for
       interprocess communication (IPC). Application developers who need to
       use IPC should design their applications so that modules using the IPC
       routines described in _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n can
       be easily modified to use the alternative interfaces.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n, _S_e_c_t_i_o_n _2_._8, _R_e_a_l_t_i_m_e,
       _m_q___c_l_o_s_e(), _m_q___g_e_t_a_t_t_r(), _m_q___n_o_t_i_f_y(), _m_q___o_p_e_n(), _m_q___r_e_c_e_i_v_e(),
       _m_q___s_e_n_d(), _m_q___s_e_t_a_t_t_r(), _m_q___u_n_l_i_n_k(), _m_s_g_c_t_l(), _m_s_g_g_e_t(), _m_s_g_s_n_d(),
       _s_i_g_a_c_t_i_o_n()

       The Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _3_._2_2_5, _M_e_s_s_a_g_e
       _Q_u_e_u_e, <<ssyyss__mmssgg..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                            MSGRCV(3)
