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 | * $Id$ |
---|
15 | */ |
---|
16 | |
---|
17 | #include <stdarg.h> |
---|
18 | |
---|
19 | #include <pthread.h> |
---|
20 | #include <limits.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <fcntl.h> |
---|
23 | #include <mqueue.h> |
---|
24 | |
---|
25 | #include <rtems/system.h> |
---|
26 | #include <rtems/score/watchdog.h> |
---|
27 | #include <rtems/posix/seterr.h> |
---|
28 | #include <rtems/posix/mqueue.h> |
---|
29 | #include <rtems/posix/time.h> |
---|
30 | |
---|
31 | /*PAGE |
---|
32 | * |
---|
33 | * 15.2.2 Open a Message Queue, P1003.1b-1993, p. 272 |
---|
34 | */ |
---|
35 | |
---|
36 | mqd_t mq_open( |
---|
37 | const char *name, |
---|
38 | int oflag, |
---|
39 | ... |
---|
40 | /* mode_t mode, */ |
---|
41 | /* struct mq_attr attr */ |
---|
42 | ) |
---|
43 | { |
---|
44 | va_list arg; |
---|
45 | mode_t mode; |
---|
46 | struct mq_attr *attr; |
---|
47 | int status; |
---|
48 | Objects_Id the_mq_id; |
---|
49 | POSIX_Message_queue_Control *the_mq; |
---|
50 | |
---|
51 | if ( oflag & O_CREAT ) { |
---|
52 | va_start(arg, oflag); |
---|
53 | mode = (mode_t) va_arg( arg, mode_t ); |
---|
54 | attr = (struct mq_attr *) va_arg( arg, struct mq_attr * ); |
---|
55 | va_end(arg); |
---|
56 | } |
---|
57 | |
---|
58 | status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id ); |
---|
59 | |
---|
60 | /* |
---|
61 | * If the name to id translation worked, then the message queue exists |
---|
62 | * and we can just return a pointer to the id. Otherwise we may |
---|
63 | * need to check to see if this is a "message queue does not exist" |
---|
64 | * or some other miscellaneous error on the name. |
---|
65 | */ |
---|
66 | |
---|
67 | if ( status ) { |
---|
68 | |
---|
69 | if ( status == EINVAL ) { /* name -> ID translation failed */ |
---|
70 | if ( !(oflag & O_CREAT) ) { /* willing to create it? */ |
---|
71 | set_errno_and_return_minus_one( ENOENT ); |
---|
72 | return (mqd_t) -1; |
---|
73 | } |
---|
74 | /* we are willing to create it */ |
---|
75 | } |
---|
76 | set_errno_and_return_minus_one( status ); /* some type of error */ |
---|
77 | return (mqd_t) -1; |
---|
78 | |
---|
79 | } else { /* name -> ID translation succeeded */ |
---|
80 | |
---|
81 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
82 | set_errno_and_return_minus_one( EEXIST ); |
---|
83 | return (mqd_t) -1; |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * XXX In this case we need to do an ID->pointer conversion to |
---|
88 | * check the mode. This is probably a good place for a subroutine. |
---|
89 | */ |
---|
90 | |
---|
91 | the_mq->open_count += 1; |
---|
92 | |
---|
93 | return (mqd_t)&the_mq->Object.id; |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | /* XXX verify this comment... |
---|
98 | * |
---|
99 | * At this point, the message queue does not exist and everything has been |
---|
100 | * checked. We should go ahead and create a message queue. |
---|
101 | */ |
---|
102 | |
---|
103 | status = _POSIX_Message_queue_Create_support( |
---|
104 | name, |
---|
105 | TRUE, /* shared across processes */ |
---|
106 | oflag, |
---|
107 | attr, |
---|
108 | &the_mq |
---|
109 | ); |
---|
110 | |
---|
111 | if ( status == -1 ) |
---|
112 | return (mqd_t) -1; |
---|
113 | |
---|
114 | return (mqd_t) &the_mq->Object.id; |
---|
115 | } |
---|
116 | |
---|