source: rtems/cpukit/posix/src/semopen.c @ f97536d

5
Last change on this file since f97536d was f97536d, checked in by Sebastian Huber <sebastian.huber@…>, on 10/16/15 at 06:21:48

basdefs.h: Add and use RTEMS_UNUSED

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief Function Creates New POSIX semaphore or Opens an existing Semaphore
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2007.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <stdarg.h>
22
23#include <errno.h>
24#include <fcntl.h>
25#include <pthread.h>
26#include <semaphore.h>
27#include <limits.h>
28
29#include <rtems/system.h>
30#include <rtems/posix/semaphoreimpl.h>
31#include <rtems/seterr.h>
32
33/*
34 *  sem_open
35 *
36 *  Opens a named semaphore.  Used in conjunction with the sem_close
37 *  and sem_unlink commands.
38 *
39 *  11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
40 *
41 *  NOTE: When oflag is O_CREAT, then optional third and fourth
42 *        parameters must be present.
43 */
44sem_t *sem_open(
45  const char *name,
46  int         oflag,
47  ...
48  /* mode_t mode, */
49  /* unsigned int value */
50)
51{
52  /*
53   * mode is set but never used. GCC gives a warning for this
54   * and we need to tell GCC not to complain. But we have to
55   * have it because we have to work through the variable
56   * arguments to get to attr.
57   */
58  mode_t                     mode RTEMS_UNUSED;
59
60  va_list                    arg;
61  unsigned int               value = 0;
62  int                        status;
63  Objects_Id                 the_semaphore_id;
64  POSIX_Semaphore_Control   *the_semaphore;
65  Objects_Locations          location;
66  size_t                     name_len;
67
68  if ( oflag & O_CREAT ) {
69    va_start(arg, oflag);
70    mode = va_arg( arg, mode_t );
71    value = va_arg( arg, unsigned int );
72    va_end(arg);
73  }
74
75  _Objects_Allocator_lock();
76  status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id, &name_len );
77
78  /*
79   *  If the name to id translation worked, then the semaphore exists
80   *  and we can just return a pointer to the id.  Otherwise we may
81   *  need to check to see if this is a "semaphore does not exist"
82   *  or some other miscellaneous error on the name.
83   */
84
85  if ( status ) {
86
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
92    if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
93      _Objects_Allocator_unlock();
94      rtems_set_errno_and_return_value( status, SEM_FAILED );
95    }
96  } else {
97
98    /*
99     * Check for existence with creation.
100     */
101
102    if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
103      _Objects_Allocator_unlock();
104      rtems_set_errno_and_return_value( EEXIST, SEM_FAILED );
105    }
106
107    the_semaphore = _POSIX_Semaphore_Get( (sem_t *) &the_semaphore_id, &location );
108    the_semaphore->open_count += 1;
109    _Thread_Enable_dispatch();
110    _Objects_Allocator_unlock();
111    goto return_id;
112  }
113
114  /*
115   *  At this point, the semaphore does not exist and everything has been
116   *  checked. We should go ahead and create a semaphore.
117   */
118
119  status =_POSIX_Semaphore_Create_support(
120    name,
121    name_len,
122    false,         /* not shared across processes */
123    value,
124    &the_semaphore
125  );
126
127  /*
128   * errno was set by Create_support, so don't set it again.
129   */
130
131  _Objects_Allocator_unlock();
132
133  if ( status == -1 )
134    return SEM_FAILED;
135
136return_id:
137  #if defined(RTEMS_USE_16_BIT_OBJECT)
138    the_semaphore->Semaphore_id = the_semaphore->Object.id;
139    return &the_semaphore->Semaphore_id;
140  #else
141    return &the_semaphore->Object.id;
142  #endif
143}
Note: See TracBrowser for help on using the repository browser.