source: rtems/cpukit/posix/src/mqueueopen.c @ 3bacb250

4.104.115
Last change on this file since 3bacb250 was 53afba12, checked in by Joel Sherrill <joel.sherrill@…>, on 08/06/09 at 19:26:56

2009-08-06 Joel Sherrill <joel.sherrill@…>

  • posix/src/mqueuecreatesupp.c, posix/src/mqueuenametoid.c, posix/src/mqueueopen.c, posix/src/semaphorecreatesupp.c: Tinker with error handling for name too long. Use strnlen to ensure we do not run off the end of the maximum length string.
  • Property mode set to 100644
File size: 4.2 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 *  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 <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/*
43 *  15.2.2 Open a Message Queue, P1003.1b-1993, p. 272
44 */
45mqd_t mq_open(
46  const char *name,
47  int         oflag,
48  ...
49  /* mode_t mode, */
50  /* struct mq_attr  attr */
51)
52{
53  va_list                         arg;
54  mode_t                          mode;
55  struct mq_attr                 *attr = NULL;
56  int                             status;
57  Objects_Id                      the_mq_id;
58  POSIX_Message_queue_Control    *the_mq;
59  POSIX_Message_queue_Control_fd *the_mq_fd;
60  Objects_Locations               location;
61
62  _Thread_Disable_dispatch();
63
64  if ( oflag & O_CREAT ) {
65    va_start(arg, oflag);
66    mode = (mode_t) va_arg( arg, unsigned int );
67    attr = (struct mq_attr *) va_arg( arg, struct mq_attr * );
68    va_end(arg);
69  }
70
71  the_mq_fd = _POSIX_Message_queue_Allocate_fd();
72  if ( !the_mq_fd ) {
73    _Thread_Enable_dispatch();
74    rtems_set_errno_and_return_minus_one( ENFILE );
75  }
76  the_mq_fd->oflag = oflag;
77
78  status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id );
79
80  /*
81   *  If the name to id translation worked, then the message queue exists
82   *  and we can just return a pointer to the id.  Otherwise we may
83   *  need to check to see if this is a "message queue does not exist"
84   *  or some other miscellaneous error on the name.
85   */
86  if ( status ) {
87    /*
88     * Unless provided a valid name that did not already exist
89     * and we are willing to create then it is an error.
90     */
91    if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
92      _POSIX_Message_queue_Free_fd( the_mq_fd );
93      _Thread_Enable_dispatch();
94      rtems_set_errno_and_return_minus_one_cast( status, mqd_t );
95    }
96
97  } else {                /* name -> ID translation succeeded */
98    /*
99     * Check for existence with creation.
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    the_mq = _POSIX_Message_queue_Get( the_mq_id, &location );
112    the_mq->open_count += 1;
113    the_mq_fd->Queue = the_mq;
114    _Objects_Open_string(
115      &_POSIX_Message_queue_Information_fds,
116      &the_mq_fd->Object,
117      NULL
118    );
119    _Thread_Enable_dispatch();
120    _Thread_Enable_dispatch();
121    return (mqd_t)the_mq_fd->Object.id;
122
123  }
124
125  /*
126   *  At this point, the message queue does not exist and everything has been
127   *  checked. We should go ahead and create a message queue.
128   */
129  status = _POSIX_Message_queue_Create_support(
130    name,
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}
Note: See TracBrowser for help on using the repository browser.