[ab87219] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Creates a new POSIX Message Queue or Opens an Existing Queue |
---|
[5cb175bb] | 5 | * @ingroup POSIXAPI |
---|
[ab87219] | 6 | */ |
---|
| 7 | |
---|
[4dc89814] | 8 | /* |
---|
| 9 | * NOTE: The structure of the routines is identical to that of POSIX |
---|
| 10 | * Message_queues to leave the option of having unnamed message |
---|
| 11 | * queues at a future date. They are currently not part of the |
---|
[874297f3] | 12 | * POSIX standard but unnamed message_queues are. This is also |
---|
| 13 | * the reason for the apparently unnecessary tracking of |
---|
[4dc89814] | 14 | * the process_shared attribute. [In addition to the fact that |
---|
| 15 | * it would be trivial to add pshared to the mq_attr structure |
---|
| 16 | * and have process private message queues.] |
---|
| 17 | * |
---|
| 18 | * This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open |
---|
| 19 | * time. |
---|
| 20 | * |
---|
[53afba12] | 21 | * COPYRIGHT (c) 1989-2009. |
---|
[feaa007] | 22 | * On-Line Applications Research Corporation (OAR). |
---|
| 23 | * |
---|
| 24 | * The license and distribution terms for this file may be |
---|
| 25 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 26 | * http://www.rtems.org/license/LICENSE. |
---|
[4dc89814] | 27 | */ |
---|
| 28 | |
---|
[f42b726] | 29 | #if HAVE_CONFIG_H |
---|
| 30 | #include "config.h" |
---|
| 31 | #endif |
---|
| 32 | |
---|
[c8982e5] | 33 | #include <rtems/posix/mqueueimpl.h> |
---|
| 34 | #include <rtems/score/wkspace.h> |
---|
[4dc89814] | 35 | |
---|
[c8982e5] | 36 | #include <stdarg.h> |
---|
[4dc89814] | 37 | #include <fcntl.h> |
---|
| 38 | |
---|
[f0ff18e] | 39 | #define MQ_OPEN_FAILED ((mqd_t) -1) |
---|
| 40 | |
---|
[c8982e5] | 41 | /* |
---|
| 42 | * There is no real basis for the default values. They will work |
---|
| 43 | * but were not compared against any existing implementation for |
---|
| 44 | * compatibility. See README.mqueue for an example program we |
---|
| 45 | * think will print out the defaults. Report anything you find with it. |
---|
| 46 | */ |
---|
| 47 | static const struct mq_attr _POSIX_Message_queue_Default_attributes = { |
---|
| 48 | .mq_maxmsg = 10, |
---|
| 49 | .mq_msgsize = 16 |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | static mqd_t _POSIX_Message_queue_Create( |
---|
| 53 | const char *name_arg, |
---|
| 54 | size_t name_len, |
---|
| 55 | int oflag, |
---|
| 56 | const struct mq_attr *attr |
---|
[3e36a167] | 57 | ) |
---|
| 58 | { |
---|
[c8982e5] | 59 | POSIX_Message_queue_Control *the_mq; |
---|
| 60 | char *name; |
---|
[3e36a167] | 61 | |
---|
| 62 | /* length of name has already been validated */ |
---|
| 63 | |
---|
[c8982e5] | 64 | if ( attr->mq_maxmsg <= 0 ){ |
---|
| 65 | rtems_set_errno_and_return_value( EINVAL, MQ_OPEN_FAILED ); |
---|
| 66 | } |
---|
[3e36a167] | 67 | |
---|
[c8982e5] | 68 | if ( attr->mq_msgsize <= 0 ){ |
---|
| 69 | rtems_set_errno_and_return_value( EINVAL, MQ_OPEN_FAILED ); |
---|
[3e36a167] | 70 | } |
---|
| 71 | |
---|
[c8982e5] | 72 | the_mq = _POSIX_Message_queue_Allocate_unprotected(); |
---|
[3e36a167] | 73 | if ( !the_mq ) { |
---|
[c8982e5] | 74 | rtems_set_errno_and_return_value( ENFILE, MQ_OPEN_FAILED ); |
---|
[3e36a167] | 75 | } |
---|
| 76 | |
---|
| 77 | /* |
---|
| 78 | * Make a copy of the user's string for name just in case it was |
---|
| 79 | * dynamically constructed. |
---|
| 80 | */ |
---|
| 81 | name = _Workspace_String_duplicate( name_arg, name_len ); |
---|
| 82 | if ( !name ) { |
---|
| 83 | _POSIX_Message_queue_Free( the_mq ); |
---|
[c8982e5] | 84 | rtems_set_errno_and_return_value( ENOMEM, MQ_OPEN_FAILED ); |
---|
[3e36a167] | 85 | } |
---|
| 86 | |
---|
| 87 | the_mq->open_count = 1; |
---|
| 88 | the_mq->linked = true; |
---|
[c8982e5] | 89 | the_mq->oflag = oflag; |
---|
[3e36a167] | 90 | |
---|
| 91 | /* |
---|
| 92 | * NOTE: That thread blocking discipline should be based on the |
---|
| 93 | * current scheduling policy. |
---|
| 94 | * |
---|
| 95 | * Joel: Cite POSIX or OpenGroup on above statement so we can determine |
---|
| 96 | * if it is a real requirement. |
---|
| 97 | */ |
---|
[c8982e5] | 98 | if ( |
---|
| 99 | !_CORE_message_queue_Initialize( |
---|
| 100 | &the_mq->Message_queue, |
---|
| 101 | CORE_MESSAGE_QUEUE_DISCIPLINES_FIFO, |
---|
| 102 | attr->mq_maxmsg, |
---|
| 103 | attr->mq_msgsize |
---|
| 104 | ) |
---|
| 105 | ) { |
---|
[3e36a167] | 106 | _POSIX_Message_queue_Free( the_mq ); |
---|
[c8982e5] | 107 | _Workspace_Free( name ); |
---|
| 108 | rtems_set_errno_and_return_value( ENOSPC, MQ_OPEN_FAILED ); |
---|
[3e36a167] | 109 | } |
---|
| 110 | |
---|
| 111 | _Objects_Open_string( |
---|
| 112 | &_POSIX_Message_queue_Information, |
---|
| 113 | &the_mq->Object, |
---|
| 114 | name |
---|
| 115 | ); |
---|
[c8982e5] | 116 | return the_mq->Object.id; |
---|
[3e36a167] | 117 | } |
---|
| 118 | |
---|
[53afba12] | 119 | /* |
---|
[4dc89814] | 120 | * 15.2.2 Open a Message Queue, P1003.1b-1993, p. 272 |
---|
| 121 | */ |
---|
| 122 | mqd_t mq_open( |
---|
| 123 | const char *name, |
---|
| 124 | int oflag, |
---|
| 125 | ... |
---|
| 126 | /* mode_t mode, */ |
---|
| 127 | /* struct mq_attr attr */ |
---|
| 128 | ) |
---|
| 129 | { |
---|
[c8982e5] | 130 | POSIX_Message_queue_Control *the_mq; |
---|
| 131 | size_t name_len; |
---|
| 132 | Objects_Get_by_name_error error; |
---|
| 133 | mqd_t status; |
---|
[53092d1] | 134 | |
---|
[c8982e5] | 135 | _Objects_Allocator_lock(); |
---|
[c904df5] | 136 | the_mq = _POSIX_Message_queue_Get_by_name( name, &name_len, &error ); |
---|
[53092d1] | 137 | |
---|
[4dc89814] | 138 | /* |
---|
| 139 | * If the name to id translation worked, then the message queue exists |
---|
| 140 | * and we can just return a pointer to the id. Otherwise we may |
---|
| 141 | * need to check to see if this is a "message queue does not exist" |
---|
| 142 | * or some other miscellaneous error on the name. |
---|
| 143 | */ |
---|
[c904df5] | 144 | if ( the_mq == NULL ) { |
---|
[c8982e5] | 145 | va_list ap; |
---|
| 146 | const struct mq_attr *attr; |
---|
| 147 | |
---|
[7fbef786] | 148 | /* |
---|
| 149 | * Unless provided a valid name that did not already exist |
---|
| 150 | * and we are willing to create then it is an error. |
---|
| 151 | */ |
---|
[c8982e5] | 152 | |
---|
| 153 | if ( error != OBJECTS_GET_BY_NAME_NO_OBJECT || ( oflag & O_CREAT ) == 0 ) { |
---|
[23fec9f0] | 154 | _Objects_Allocator_unlock(); |
---|
[c904df5] | 155 | rtems_set_errno_and_return_value( |
---|
| 156 | _POSIX_Get_by_name_error( error ), |
---|
| 157 | MQ_OPEN_FAILED |
---|
| 158 | ); |
---|
[4dc89814] | 159 | } |
---|
[7fbef786] | 160 | |
---|
[c8982e5] | 161 | va_start( ap, oflag ); |
---|
| 162 | va_arg( ap, mode_t ); |
---|
| 163 | attr = va_arg( ap, const struct mq_attr * ); |
---|
| 164 | va_end( ap ); |
---|
| 165 | |
---|
| 166 | if ( attr == NULL ) { |
---|
| 167 | attr = &_POSIX_Message_queue_Default_attributes; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /* |
---|
| 171 | * At this point, the message queue does not exist and everything has been |
---|
| 172 | * checked. We should go ahead and create a message queue. |
---|
| 173 | */ |
---|
| 174 | |
---|
| 175 | status = _POSIX_Message_queue_Create( name, name_len, oflag, attr ); |
---|
| 176 | } else { |
---|
| 177 | |
---|
[7fbef786] | 178 | /* |
---|
| 179 | * Check for existence with creation. |
---|
| 180 | */ |
---|
[c8982e5] | 181 | |
---|
| 182 | if ( ( oflag & ( O_CREAT | O_EXCL ) ) == ( O_CREAT | O_EXCL ) ) { |
---|
[23fec9f0] | 183 | _Objects_Allocator_unlock(); |
---|
[f0ff18e] | 184 | rtems_set_errno_and_return_value( EEXIST, MQ_OPEN_FAILED ); |
---|
[4dc89814] | 185 | } |
---|
[7fbef786] | 186 | |
---|
[4dc89814] | 187 | the_mq->open_count += 1; |
---|
[c8982e5] | 188 | status = the_mq->Object.id; |
---|
[4dc89814] | 189 | } |
---|
[874297f3] | 190 | |
---|
[23fec9f0] | 191 | _Objects_Allocator_unlock(); |
---|
[c8982e5] | 192 | return status; |
---|
[4dc89814] | 193 | } |
---|