source: rtems/cpukit/posix/src/mqueueopen.c @ f26145b

4.104.114.84.95
Last change on this file since f26145b was 874297f3, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/16/04 at 10:01:03

Remove stray white spaces.

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