1 | /* |
---|
2 | * NOTE: The structure of the routines is identical to that of POSIX |
---|
3 | * Message_queues to leave the option of having unnamed message |
---|
4 | * queues at a future date. They are currently not part of the |
---|
5 | * POSIX standard but unnamed message_queues are. This is also |
---|
6 | * the reason for the apparently unnecessary tracking of |
---|
7 | * the process_shared attribute. [In addition to the fact that |
---|
8 | * it would be trivial to add pshared to the mq_attr structure |
---|
9 | * and have process private message queues.] |
---|
10 | * |
---|
11 | * This code ignores the O_RDONLY/O_WRONLY/O_RDWR flag at open |
---|
12 | * time. |
---|
13 | * |
---|
14 | * COPYRIGHT (c) 1989-2009. |
---|
15 | * On-Line Applications Research Corporation (OAR). |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #if HAVE_CONFIG_H |
---|
23 | #include "config.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdarg.h> |
---|
27 | |
---|
28 | #include <pthread.h> |
---|
29 | #include <limits.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <fcntl.h> |
---|
32 | #include <mqueue.h> |
---|
33 | |
---|
34 | #include <rtems/system.h> |
---|
35 | #include <rtems/score/watchdog.h> |
---|
36 | #include <rtems/seterr.h> |
---|
37 | #include <rtems/posix/mqueue.h> |
---|
38 | #include <rtems/posix/time.h> |
---|
39 | |
---|
40 | /* |
---|
41 | * 15.2.2 Open a Message Queue, P1003.1b-1993, p. 272 |
---|
42 | */ |
---|
43 | mqd_t mq_open( |
---|
44 | const char *name, |
---|
45 | int oflag, |
---|
46 | ... |
---|
47 | /* mode_t mode, */ |
---|
48 | /* struct mq_attr attr */ |
---|
49 | ) |
---|
50 | { |
---|
51 | va_list arg; |
---|
52 | mode_t mode; |
---|
53 | struct mq_attr *attr = NULL; |
---|
54 | int status; |
---|
55 | Objects_Id the_mq_id; |
---|
56 | POSIX_Message_queue_Control *the_mq; |
---|
57 | POSIX_Message_queue_Control_fd *the_mq_fd; |
---|
58 | Objects_Locations location; |
---|
59 | size_t name_len; |
---|
60 | |
---|
61 | _Thread_Disable_dispatch(); |
---|
62 | |
---|
63 | if ( oflag & O_CREAT ) { |
---|
64 | va_start(arg, oflag); |
---|
65 | mode = va_arg( arg, mode_t ); |
---|
66 | attr = va_arg( arg, struct mq_attr * ); |
---|
67 | va_end(arg); |
---|
68 | } |
---|
69 | |
---|
70 | the_mq_fd = _POSIX_Message_queue_Allocate_fd(); |
---|
71 | if ( !the_mq_fd ) { |
---|
72 | _Thread_Enable_dispatch(); |
---|
73 | rtems_set_errno_and_return_minus_one( ENFILE ); |
---|
74 | } |
---|
75 | the_mq_fd->oflag = oflag; |
---|
76 | |
---|
77 | status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id, &name_len ); |
---|
78 | |
---|
79 | /* |
---|
80 | * If the name to id translation worked, then the message queue exists |
---|
81 | * and we can just return a pointer to the id. Otherwise we may |
---|
82 | * need to check to see if this is a "message queue does not exist" |
---|
83 | * or some other miscellaneous error on the name. |
---|
84 | */ |
---|
85 | if ( status ) { |
---|
86 | /* |
---|
87 | * Unless provided a valid name that did not already exist |
---|
88 | * and we are willing to create then it is an error. |
---|
89 | */ |
---|
90 | if ( !( status == ENOENT && (oflag & O_CREAT) ) ) { |
---|
91 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
92 | _Thread_Enable_dispatch(); |
---|
93 | rtems_set_errno_and_return_minus_one_cast( status, mqd_t ); |
---|
94 | } |
---|
95 | |
---|
96 | } else { /* name -> ID translation succeeded */ |
---|
97 | /* |
---|
98 | * Check for existence with creation. |
---|
99 | */ |
---|
100 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
101 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
102 | _Thread_Enable_dispatch(); |
---|
103 | rtems_set_errno_and_return_minus_one_cast( EEXIST, mqd_t ); |
---|
104 | } |
---|
105 | |
---|
106 | /* |
---|
107 | * In this case we need to do an ID->pointer conversion to |
---|
108 | * check the mode. |
---|
109 | */ |
---|
110 | the_mq = _POSIX_Message_queue_Get( the_mq_id, &location ); |
---|
111 | the_mq->open_count += 1; |
---|
112 | the_mq_fd->Queue = the_mq; |
---|
113 | _Objects_Open_string( |
---|
114 | &_POSIX_Message_queue_Information_fds, |
---|
115 | &the_mq_fd->Object, |
---|
116 | NULL |
---|
117 | ); |
---|
118 | _Thread_Enable_dispatch(); |
---|
119 | _Thread_Enable_dispatch(); |
---|
120 | return (mqd_t)the_mq_fd->Object.id; |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | /* |
---|
125 | * At this point, the message queue does not exist and everything has been |
---|
126 | * checked. We should go ahead and create a message queue. |
---|
127 | */ |
---|
128 | status = _POSIX_Message_queue_Create_support( |
---|
129 | name, |
---|
130 | name_len, |
---|
131 | true, /* shared across processes */ |
---|
132 | attr, |
---|
133 | &the_mq |
---|
134 | ); |
---|
135 | |
---|
136 | /* |
---|
137 | * errno was set by Create_support, so don't set it again. |
---|
138 | */ |
---|
139 | if ( status == -1 ) { |
---|
140 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
141 | _Thread_Enable_dispatch(); |
---|
142 | return (mqd_t) -1; |
---|
143 | } |
---|
144 | |
---|
145 | the_mq_fd->Queue = the_mq; |
---|
146 | _Objects_Open_string( |
---|
147 | &_POSIX_Message_queue_Information_fds, |
---|
148 | &the_mq_fd->Object, |
---|
149 | NULL |
---|
150 | ); |
---|
151 | |
---|
152 | _Thread_Enable_dispatch(); |
---|
153 | |
---|
154 | return (mqd_t) the_mq_fd->Object.id; |
---|
155 | } |
---|