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 | * $Id$ |
---|
22 | */ |
---|
23 | |
---|
24 | #if HAVE_CONFIG_H |
---|
25 | #include "config.h" |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <string.h> |
---|
29 | #include <stdarg.h> |
---|
30 | #include <stdlib.h> |
---|
31 | |
---|
32 | #include <pthread.h> |
---|
33 | #include <limits.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <fcntl.h> |
---|
36 | #include <mqueue.h> |
---|
37 | |
---|
38 | #include <rtems/system.h> |
---|
39 | #include <rtems/score/watchdog.h> |
---|
40 | #include <rtems/score/wkspace.h> |
---|
41 | #include <rtems/seterr.h> |
---|
42 | #include <rtems/posix/mqueue.h> |
---|
43 | #include <rtems/posix/time.h> |
---|
44 | |
---|
45 | /* pure ANSI mode does not have this prototype */ |
---|
46 | size_t strnlen(const char *, size_t); |
---|
47 | |
---|
48 | /*PAGE |
---|
49 | * |
---|
50 | * _POSIX_Message_queue_Create_support |
---|
51 | * |
---|
52 | * This routine does the actual creation and initialization of |
---|
53 | * a poxix message queue. |
---|
54 | */ |
---|
55 | |
---|
56 | int _POSIX_Message_queue_Create_support( |
---|
57 | const char *name_arg, |
---|
58 | int pshared, |
---|
59 | struct mq_attr *attr_ptr, |
---|
60 | POSIX_Message_queue_Control **message_queue |
---|
61 | ) |
---|
62 | { |
---|
63 | POSIX_Message_queue_Control *the_mq; |
---|
64 | CORE_message_queue_Attributes *the_mq_attr; |
---|
65 | struct mq_attr attr; |
---|
66 | char *name; |
---|
67 | size_t n; |
---|
68 | |
---|
69 | n = strnlen( name_arg, NAME_MAX ); |
---|
70 | /* length of name has already been validated */ |
---|
71 | |
---|
72 | _Thread_Disable_dispatch(); |
---|
73 | |
---|
74 | /* |
---|
75 | * There is no real basis for the default values. They will work |
---|
76 | * but were not compared against any existing implementation for |
---|
77 | * compatibility. See README.mqueue for an example program we |
---|
78 | * think will print out the defaults. Report anything you find with it. |
---|
79 | */ |
---|
80 | if ( attr_ptr == NULL ) { |
---|
81 | attr.mq_maxmsg = 10; |
---|
82 | attr.mq_msgsize = 16; |
---|
83 | } else { |
---|
84 | if ( attr_ptr->mq_maxmsg <= 0 ){ |
---|
85 | _Thread_Enable_dispatch(); |
---|
86 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
87 | } |
---|
88 | |
---|
89 | if ( attr_ptr->mq_msgsize <= 0 ){ |
---|
90 | _Thread_Enable_dispatch(); |
---|
91 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
92 | } |
---|
93 | |
---|
94 | attr = *attr_ptr; |
---|
95 | } |
---|
96 | |
---|
97 | the_mq = _POSIX_Message_queue_Allocate(); |
---|
98 | if ( !the_mq ) { |
---|
99 | _Thread_Enable_dispatch(); |
---|
100 | rtems_set_errno_and_return_minus_one( ENFILE ); |
---|
101 | } |
---|
102 | |
---|
103 | the_mq->process_shared = pshared; |
---|
104 | the_mq->named = true; |
---|
105 | the_mq->open_count = 1; |
---|
106 | the_mq->linked = true; |
---|
107 | |
---|
108 | /* |
---|
109 | * Make a copy of the user's string for name just in case it was |
---|
110 | * dynamically constructed. |
---|
111 | */ |
---|
112 | name = _Workspace_Allocate(n+1); |
---|
113 | if (!name) { |
---|
114 | _POSIX_Message_queue_Free( the_mq ); |
---|
115 | _Thread_Enable_dispatch(); |
---|
116 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
117 | } |
---|
118 | strncpy( name, name_arg, n+1 ); |
---|
119 | |
---|
120 | /* |
---|
121 | * NOTE: That thread blocking discipline should be based on the |
---|
122 | * current scheduling policy. |
---|
123 | * |
---|
124 | * Joel: Cite POSIX or OpenGroup on above statement so we can determine |
---|
125 | * if it is a real requirement. |
---|
126 | */ |
---|
127 | the_mq_attr = &the_mq->Message_queue.Attributes; |
---|
128 | the_mq_attr->discipline = CORE_MESSAGE_QUEUE_DISCIPLINES_FIFO; |
---|
129 | |
---|
130 | if ( !_CORE_message_queue_Initialize( |
---|
131 | &the_mq->Message_queue, |
---|
132 | the_mq_attr, |
---|
133 | attr.mq_maxmsg, |
---|
134 | attr.mq_msgsize |
---|
135 | ) ) { |
---|
136 | |
---|
137 | _POSIX_Message_queue_Free( the_mq ); |
---|
138 | _Workspace_Free(name); |
---|
139 | _Thread_Enable_dispatch(); |
---|
140 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
141 | } |
---|
142 | |
---|
143 | _Objects_Open_string( |
---|
144 | &_POSIX_Message_queue_Information, |
---|
145 | &the_mq->Object, |
---|
146 | name |
---|
147 | ); |
---|
148 | |
---|
149 | *message_queue = the_mq; |
---|
150 | |
---|
151 | _Thread_Enable_dispatch(); |
---|
152 | return 0; |
---|
153 | } |
---|