Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:21

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup POSIXAPI
0007  *
0008  * @brief POSIX Message Queue and Send Support
0009  */
0010 
0011 /*
0012  *  NOTE:  The structure of the routines is identical to that of POSIX
0013  *         Message_queues to leave the option of having unnamed message
0014  *         queues at a future date.  They are currently not part of the
0015  *         POSIX standard but unnamed message_queues are.  This is also
0016  *         the reason for the apparently unnecessary tracking of
0017  *         the process_shared attribute.  [In addition to the fact that
0018  *         it would be trivial to add pshared to the mq_attr structure
0019  *         and have process private message queues.]
0020  *
0021  *         This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open
0022  *         time.
0023  *
0024  *  COPYRIGHT (c) 1989-2008.
0025  *  On-Line Applications Research Corporation (OAR).
0026  *
0027  * Redistribution and use in source and binary forms, with or without
0028  * modification, are permitted provided that the following conditions
0029  * are met:
0030  * 1. Redistributions of source code must retain the above copyright
0031  *    notice, this list of conditions and the following disclaimer.
0032  * 2. Redistributions in binary form must reproduce the above copyright
0033  *    notice, this list of conditions and the following disclaimer in the
0034  *    documentation and/or other materials provided with the distribution.
0035  *
0036  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0037  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0038  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0039  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0040  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0041  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0042  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0043  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0044  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0045  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0046  * POSSIBILITY OF SUCH DAMAGE.
0047  */
0048 
0049 #ifdef HAVE_CONFIG_H
0050 #include "config.h"
0051 #endif
0052 
0053 #include <rtems/posix/mqueueimpl.h>
0054 
0055 #include <fcntl.h>
0056 
0057 int _POSIX_Message_queue_Send_support(
0058   mqd_t                         mqdes,
0059   const char                   *msg_ptr,
0060   size_t                        msg_len,
0061   unsigned int                  msg_prio,
0062   const struct timespec        *abstime,
0063   Thread_queue_Enqueue_callout  enqueue_callout
0064 )
0065 {
0066   POSIX_Message_queue_Control *the_mq;
0067   Thread_queue_Context         queue_context;
0068   Status_Control               status;
0069   Thread_Control              *executing;
0070 
0071   /*
0072    * Validate the priority.
0073    * XXX - Do not validate msg_prio is not less than 0.
0074    */
0075 
0076   if ( msg_prio > MQ_PRIO_MAX ) {
0077     rtems_set_errno_and_return_minus_one( EINVAL );
0078   }
0079 
0080   the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context );
0081 
0082   if ( the_mq == NULL ) {
0083     rtems_set_errno_and_return_minus_one( EBADF );
0084   }
0085 
0086   if ( ( the_mq->oflag & O_ACCMODE ) == O_RDONLY ) {
0087     _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
0088     rtems_set_errno_and_return_minus_one( EBADF );
0089   }
0090 
0091   _Thread_queue_Context_set_enqueue_callout( &queue_context, enqueue_callout );
0092   _Thread_queue_Context_set_timeout_argument( &queue_context, abstime, true );
0093 
0094   _CORE_message_queue_Acquire_critical(
0095     &the_mq->Message_queue,
0096     &queue_context
0097   );
0098 
0099   if ( the_mq->open_count == 0 ) {
0100     _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context );
0101     rtems_set_errno_and_return_minus_one( EBADF );
0102   }
0103 
0104   /*
0105    *  Now perform the actual message receive
0106    */
0107   executing = _Thread_Executing;
0108   status = _CORE_message_queue_Submit(
0109     &the_mq->Message_queue,
0110     executing,
0111     msg_ptr,
0112     msg_len,
0113     _POSIX_Message_queue_Priority_to_core( msg_prio ),
0114     ( the_mq->oflag & O_NONBLOCK ) == 0,
0115     &queue_context
0116   );
0117   return _POSIX_Zero_or_minus_one_plus_errno( status );
0118 }