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-2007. |
---|
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 | * $Id$ |
---|
22 | */ |
---|
23 | |
---|
24 | #if HAVE_CONFIG_H |
---|
25 | #include "config.h" |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <stdarg.h> |
---|
29 | |
---|
30 | #include <pthread.h> |
---|
31 | #include <limits.h> |
---|
32 | #include <errno.h> |
---|
33 | #include <fcntl.h> |
---|
34 | #include <mqueue.h> |
---|
35 | |
---|
36 | #include <rtems/system.h> |
---|
37 | #include <rtems/score/watchdog.h> |
---|
38 | #include <rtems/seterr.h> |
---|
39 | #include <rtems/posix/mqueue.h> |
---|
40 | #include <rtems/posix/time.h> |
---|
41 | |
---|
42 | /*PAGE |
---|
43 | * |
---|
44 | * 15.2.2 Open a Message Queue, P1003.1b-1993, p. 272 |
---|
45 | */ |
---|
46 | |
---|
47 | mqd_t mq_open( |
---|
48 | const char *name, |
---|
49 | int oflag, |
---|
50 | ... |
---|
51 | /* mode_t mode, */ |
---|
52 | /* struct mq_attr attr */ |
---|
53 | ) |
---|
54 | { |
---|
55 | va_list arg; |
---|
56 | mode_t mode; |
---|
57 | struct mq_attr *attr = NULL; |
---|
58 | int status; |
---|
59 | Objects_Id the_mq_id; |
---|
60 | POSIX_Message_queue_Control *the_mq; |
---|
61 | POSIX_Message_queue_Control_fd *the_mq_fd; |
---|
62 | Objects_Locations location; |
---|
63 | |
---|
64 | _Thread_Disable_dispatch(); |
---|
65 | |
---|
66 | if ( oflag & O_CREAT ) { |
---|
67 | va_start(arg, oflag); |
---|
68 | mode = (mode_t) va_arg( arg, unsigned int ); |
---|
69 | attr = (struct mq_attr *) va_arg( arg, struct mq_attr * ); |
---|
70 | va_end(arg); |
---|
71 | } |
---|
72 | |
---|
73 | the_mq_fd = _POSIX_Message_queue_Allocate_fd(); |
---|
74 | if ( !the_mq_fd ) { |
---|
75 | _Thread_Enable_dispatch(); |
---|
76 | rtems_set_errno_and_return_minus_one( ENFILE ); |
---|
77 | } |
---|
78 | the_mq_fd->oflag = oflag; |
---|
79 | |
---|
80 | status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id ); |
---|
81 | |
---|
82 | /* |
---|
83 | * If the name to id translation worked, then the message queue exists |
---|
84 | * and we can just return a pointer to the id. Otherwise we may |
---|
85 | * need to check to see if this is a "message queue does not exist" |
---|
86 | * or some other miscellaneous error on the name. |
---|
87 | */ |
---|
88 | |
---|
89 | if ( status ) { |
---|
90 | |
---|
91 | /* |
---|
92 | * Unless provided a valid name that did not already exist |
---|
93 | * and we are willing to create then it is an error. |
---|
94 | */ |
---|
95 | |
---|
96 | if ( !( status == ENOENT && (oflag & O_CREAT) ) ) { |
---|
97 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
98 | _Thread_Enable_dispatch(); |
---|
99 | rtems_set_errno_and_return_minus_one_cast( status, mqd_t ); |
---|
100 | } |
---|
101 | |
---|
102 | } else { /* name -> ID translation succeeded */ |
---|
103 | |
---|
104 | /* |
---|
105 | * Check for existence with creation. |
---|
106 | */ |
---|
107 | |
---|
108 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
109 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
110 | _Thread_Enable_dispatch(); |
---|
111 | rtems_set_errno_and_return_minus_one_cast( EEXIST, mqd_t ); |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * In this case we need to do an ID->pointer conversion to |
---|
116 | * check the mode. |
---|
117 | */ |
---|
118 | |
---|
119 | the_mq = _POSIX_Message_queue_Get( the_mq_id, &location ); |
---|
120 | the_mq->open_count += 1; |
---|
121 | the_mq_fd->Queue = the_mq; |
---|
122 | _Objects_Open_string( |
---|
123 | &_POSIX_Message_queue_Information_fds, |
---|
124 | &the_mq_fd->Object, |
---|
125 | NULL |
---|
126 | ); |
---|
127 | _Thread_Enable_dispatch(); |
---|
128 | _Thread_Enable_dispatch(); |
---|
129 | return (mqd_t)the_mq_fd->Object.id; |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * At this point, the message queue does not exist and everything has been |
---|
135 | * checked. We should go ahead and create a message queue. |
---|
136 | */ |
---|
137 | |
---|
138 | status = _POSIX_Message_queue_Create_support( |
---|
139 | name, |
---|
140 | TRUE, /* shared across processes */ |
---|
141 | attr, |
---|
142 | &the_mq |
---|
143 | ); |
---|
144 | |
---|
145 | /* |
---|
146 | * errno was set by Create_support, so don't set it again. |
---|
147 | */ |
---|
148 | |
---|
149 | if ( status == -1 ) { |
---|
150 | _Thread_Enable_dispatch(); |
---|
151 | _POSIX_Message_queue_Free_fd( the_mq_fd ); |
---|
152 | return (mqd_t) -1; |
---|
153 | } |
---|
154 | |
---|
155 | the_mq_fd->Queue = the_mq; |
---|
156 | _Objects_Open_string( |
---|
157 | &_POSIX_Message_queue_Information_fds, |
---|
158 | &the_mq_fd->Object, |
---|
159 | NULL |
---|
160 | ); |
---|
161 | |
---|
162 | _Thread_Enable_dispatch(); |
---|
163 | |
---|
164 | return (mqd_t) the_mq_fd->Object.id; |
---|
165 | } |
---|