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 | */ |
---|
44 | sem_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 | POSIX_Semaphore_Control *the_semaphore; |
---|
64 | size_t name_len; |
---|
65 | Objects_Get_by_name_error error; |
---|
66 | |
---|
67 | if ( oflag & O_CREAT ) { |
---|
68 | va_start(arg, oflag); |
---|
69 | mode = va_arg( arg, mode_t ); |
---|
70 | value = va_arg( arg, unsigned int ); |
---|
71 | va_end(arg); |
---|
72 | } |
---|
73 | |
---|
74 | _Objects_Allocator_lock(); |
---|
75 | the_semaphore = _POSIX_Semaphore_Get_by_name( name, &name_len, &error ); |
---|
76 | |
---|
77 | /* |
---|
78 | * If the name to id translation worked, then the semaphore exists |
---|
79 | * and we can just return a pointer to the id. Otherwise we may |
---|
80 | * need to check to see if this is a "semaphore does not exist" |
---|
81 | * or some other miscellaneous error on the name. |
---|
82 | */ |
---|
83 | |
---|
84 | if ( the_semaphore == NULL ) { |
---|
85 | |
---|
86 | /* |
---|
87 | * Unless provided a valid name that did not already exist |
---|
88 | * and we are willing to create then it is an error. |
---|
89 | */ |
---|
90 | |
---|
91 | if ( !( error == OBJECTS_GET_BY_NAME_NO_OBJECT && (oflag & O_CREAT) ) ) { |
---|
92 | _Objects_Allocator_unlock(); |
---|
93 | rtems_set_errno_and_return_value( |
---|
94 | _POSIX_Get_by_name_error( error ), |
---|
95 | SEM_FAILED |
---|
96 | ); |
---|
97 | } |
---|
98 | } else { |
---|
99 | |
---|
100 | /* |
---|
101 | * Check for existence with creation. |
---|
102 | */ |
---|
103 | |
---|
104 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
105 | _Objects_Allocator_unlock(); |
---|
106 | rtems_set_errno_and_return_value( EEXIST, SEM_FAILED ); |
---|
107 | } |
---|
108 | |
---|
109 | the_semaphore->open_count += 1; |
---|
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 != 0 ) |
---|
134 | return SEM_FAILED; |
---|
135 | |
---|
136 | return_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 | } |
---|