1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup POSIXAPI |
---|
7 | * |
---|
8 | * @brief POSIX Message Queue and Send Support |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * NOTE: The structure of the routines is identical to that of POSIX |
---|
13 | * Message_queues to leave the option of having unnamed message |
---|
14 | * queues at a future date. They are currently not part of the |
---|
15 | * POSIX standard but unnamed message_queues are. This is also |
---|
16 | * the reason for the apparently unnecessary tracking of |
---|
17 | * the process_shared attribute. [In addition to the fact that |
---|
18 | * it would be trivial to add pshared to the mq_attr structure |
---|
19 | * and have process private message queues.] |
---|
20 | * |
---|
21 | * This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open |
---|
22 | * time. |
---|
23 | * |
---|
24 | * COPYRIGHT (c) 1989-2008. |
---|
25 | * On-Line Applications Research Corporation (OAR). |
---|
26 | * |
---|
27 | * Redistribution and use in source and binary forms, with or without |
---|
28 | * modification, are permitted provided that the following conditions |
---|
29 | * are met: |
---|
30 | * 1. Redistributions of source code must retain the above copyright |
---|
31 | * notice, this list of conditions and the following disclaimer. |
---|
32 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
33 | * notice, this list of conditions and the following disclaimer in the |
---|
34 | * documentation and/or other materials provided with the distribution. |
---|
35 | * |
---|
36 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
37 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
39 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
40 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
41 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
42 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
43 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
44 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
46 | * POSSIBILITY OF SUCH DAMAGE. |
---|
47 | */ |
---|
48 | |
---|
49 | #ifdef HAVE_CONFIG_H |
---|
50 | #include "config.h" |
---|
51 | #endif |
---|
52 | |
---|
53 | #include <rtems/posix/mqueueimpl.h> |
---|
54 | |
---|
55 | #include <fcntl.h> |
---|
56 | |
---|
57 | int _POSIX_Message_queue_Send_support( |
---|
58 | mqd_t mqdes, |
---|
59 | const char *msg_ptr, |
---|
60 | size_t msg_len, |
---|
61 | unsigned int msg_prio, |
---|
62 | const struct timespec *abstime, |
---|
63 | Thread_queue_Enqueue_callout enqueue_callout |
---|
64 | ) |
---|
65 | { |
---|
66 | POSIX_Message_queue_Control *the_mq; |
---|
67 | Thread_queue_Context queue_context; |
---|
68 | Status_Control status; |
---|
69 | Thread_Control *executing; |
---|
70 | |
---|
71 | /* |
---|
72 | * Validate the priority. |
---|
73 | * XXX - Do not validate msg_prio is not less than 0. |
---|
74 | */ |
---|
75 | |
---|
76 | if ( msg_prio > MQ_PRIO_MAX ) { |
---|
77 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
78 | } |
---|
79 | |
---|
80 | the_mq = _POSIX_Message_queue_Get( mqdes, &queue_context ); |
---|
81 | |
---|
82 | if ( the_mq == NULL ) { |
---|
83 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
84 | } |
---|
85 | |
---|
86 | if ( ( the_mq->oflag & O_ACCMODE ) == O_RDONLY ) { |
---|
87 | _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); |
---|
88 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
89 | } |
---|
90 | |
---|
91 | _Thread_queue_Context_set_enqueue_callout( &queue_context, enqueue_callout ); |
---|
92 | _Thread_queue_Context_set_timeout_argument( &queue_context, abstime, true ); |
---|
93 | |
---|
94 | _CORE_message_queue_Acquire_critical( |
---|
95 | &the_mq->Message_queue, |
---|
96 | &queue_context |
---|
97 | ); |
---|
98 | |
---|
99 | if ( the_mq->open_count == 0 ) { |
---|
100 | _CORE_message_queue_Release( &the_mq->Message_queue, &queue_context ); |
---|
101 | rtems_set_errno_and_return_minus_one( EBADF ); |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * Now perform the actual message receive |
---|
106 | */ |
---|
107 | executing = _Thread_Executing; |
---|
108 | status = _CORE_message_queue_Submit( |
---|
109 | &the_mq->Message_queue, |
---|
110 | executing, |
---|
111 | msg_ptr, |
---|
112 | msg_len, |
---|
113 | _POSIX_Message_queue_Priority_to_core( msg_prio ), |
---|
114 | ( the_mq->oflag & O_NONBLOCK ) == 0, |
---|
115 | &queue_context |
---|
116 | ); |
---|
117 | return _POSIX_Zero_or_minus_one_plus_errno( status ); |
---|
118 | } |
---|