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

4.115
Last change on this file since f0ff18e was f0ff18e, checked in by Sebastian Huber <sebastian.huber@…>, on 03/09/15 at 12:18:32

score: Add rtems_set_errno_and_return_value()

Remove rtems_set_errno_and_return_minus_one_cast().

  • 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/posix/time.h>
32#include <rtems/seterr.h>
33
34/*
35 *  sem_open
36 *
37 *  Opens a named semaphore.  Used in conjunction with the sem_close
38 *  and sem_unlink commands.
39 *
40 *  11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
41 *
42 *  NOTE: When oflag is O_CREAT, then optional third and fourth
43 *        parameters must be present.
44 */
45sem_t *sem_open(
46  const char *name,
47  int         oflag,
48  ...
49  /* mode_t mode, */
50  /* unsigned int value */
51)
52{
53  /*
54   * mode is set but never used. GCC gives a warning for this
55   * and we need to tell GCC not to complain. But we have to
56   * have it because we have to work through the variable
57   * arguments to get to attr.
58   */
59  mode_t                     mode RTEMS_COMPILER_UNUSED_ATTRIBUTE;
60
61  va_list                    arg;
62  unsigned int               value = 0;
63  int                        status;
64  Objects_Id                 the_semaphore_id;
65  POSIX_Semaphore_Control   *the_semaphore;
66  Objects_Locations          location;
67  size_t                     name_len;
68
69  if ( oflag & O_CREAT ) {
70    va_start(arg, oflag);
71    mode = va_arg( arg, mode_t );
72    value = va_arg( arg, unsigned int );
73    va_end(arg);
74  }
75
76  _Objects_Allocator_lock();
77  status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id, &name_len );
78
79  /*
80   *  If the name to id translation worked, then the semaphore exists
81   *  and we can just return a pointer to the id.  Otherwise we may
82   *  need to check to see if this is a "semaphore does not exist"
83   *  or some other miscellaneous error on the name.
84   */
85
86  if ( status ) {
87
88    /*
89     * Unless provided a valid name that did not already exist
90     * and we are willing to create then it is an error.
91     */
92
93    if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
94      _Objects_Allocator_unlock();
95      rtems_set_errno_and_return_value( status, SEM_FAILED );
96    }
97  } else {
98
99    /*
100     * Check for existence with creation.
101     */
102
103    if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
104      _Objects_Allocator_unlock();
105      rtems_set_errno_and_return_value( EEXIST, SEM_FAILED );
106    }
107
108    the_semaphore = _POSIX_Semaphore_Get( (sem_t *) &the_semaphore_id, &location );
109    the_semaphore->open_count += 1;
110    _Thread_Enable_dispatch();
111    _Objects_Allocator_unlock();
112    goto return_id;
113  }
114
115  /*
116   *  At this point, the semaphore does not exist and everything has been
117   *  checked. We should go ahead and create a semaphore.
118   */
119
120  status =_POSIX_Semaphore_Create_support(
121    name,
122    name_len,
123    false,         /* not shared across processes */
124    value,
125    &the_semaphore
126  );
127
128  /*
129   * errno was set by Create_support, so don't set it again.
130   */
131
132  _Objects_Allocator_unlock();
133
134  if ( status == -1 )
135    return SEM_FAILED;
136
137return_id:
138  #if defined(RTEMS_USE_16_BIT_OBJECT)
139    the_semaphore->Semaphore_id = the_semaphore->Object.id;
140    return &the_semaphore->Semaphore_id;
141  #else
142    return (sem_t *)&the_semaphore->Object.id;
143  #endif
144}
Note: See TracBrowser for help on using the repository browser.