1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <stdarg.h> |
---|
17 | |
---|
18 | #include <errno.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <pthread.h> |
---|
21 | #include <semaphore.h> |
---|
22 | #include <limits.h> |
---|
23 | |
---|
24 | #include <rtems/system.h> |
---|
25 | #include <rtems/score/object.h> |
---|
26 | #include <rtems/posix/semaphore.h> |
---|
27 | #include <rtems/posix/time.h> |
---|
28 | #include <rtems/seterr.h> |
---|
29 | |
---|
30 | /*PAGE |
---|
31 | * |
---|
32 | * sem_open |
---|
33 | * |
---|
34 | * Opens a named semaphore. Used in conjunction with the sem_close |
---|
35 | * and sem_unlink commands. |
---|
36 | * |
---|
37 | * 11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221 |
---|
38 | * |
---|
39 | * NOTE: When oflag is O_CREAT, then optional third and fourth |
---|
40 | * parameters must be present. |
---|
41 | */ |
---|
42 | |
---|
43 | sem_t *sem_open( |
---|
44 | const char *name, |
---|
45 | int oflag, |
---|
46 | ... |
---|
47 | /* mode_t mode, */ |
---|
48 | /* unsigned int value */ |
---|
49 | ) |
---|
50 | { |
---|
51 | va_list arg; |
---|
52 | mode_t mode; |
---|
53 | unsigned int value = 0; |
---|
54 | int status; |
---|
55 | sem_t the_semaphore_id; |
---|
56 | sem_t *id; |
---|
57 | POSIX_Semaphore_Control *the_semaphore; |
---|
58 | Objects_Locations location; |
---|
59 | |
---|
60 | _Thread_Disable_dispatch(); |
---|
61 | |
---|
62 | if ( oflag & O_CREAT ) { |
---|
63 | va_start(arg, oflag); |
---|
64 | mode = (mode_t) va_arg( arg, unsigned int ); |
---|
65 | value = va_arg( arg, unsigned int ); |
---|
66 | va_end(arg); |
---|
67 | } |
---|
68 | |
---|
69 | status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id ); |
---|
70 | |
---|
71 | /* |
---|
72 | * If the name to id translation worked, then the semaphore exists |
---|
73 | * and we can just return a pointer to the id. Otherwise we may |
---|
74 | * need to check to see if this is a "semaphore does not exist" |
---|
75 | * or some other miscellaneous error on the name. |
---|
76 | */ |
---|
77 | |
---|
78 | if ( status ) { |
---|
79 | |
---|
80 | /* |
---|
81 | * Unless provided a valid name that did not already exist |
---|
82 | * and we are willing to create then it is an error. |
---|
83 | */ |
---|
84 | |
---|
85 | if ( !( status == ENOENT && (oflag & O_CREAT) ) ) { |
---|
86 | _Thread_Enable_dispatch(); |
---|
87 | rtems_set_errno_and_return_minus_one_cast( status, sem_t * ); |
---|
88 | } |
---|
89 | } else { |
---|
90 | |
---|
91 | /* |
---|
92 | * Check for existence with creation. |
---|
93 | */ |
---|
94 | |
---|
95 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
96 | _Thread_Enable_dispatch(); |
---|
97 | rtems_set_errno_and_return_minus_one_cast( EEXIST, sem_t * ); |
---|
98 | } |
---|
99 | |
---|
100 | the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location ); |
---|
101 | the_semaphore->open_count += 1; |
---|
102 | _Thread_Enable_dispatch(); |
---|
103 | _Thread_Enable_dispatch(); |
---|
104 | goto return_id; |
---|
105 | } |
---|
106 | |
---|
107 | /* |
---|
108 | * At this point, the semaphore does not exist and everything has been |
---|
109 | * checked. We should go ahead and create a semaphore. |
---|
110 | */ |
---|
111 | |
---|
112 | status =_POSIX_Semaphore_Create_support( |
---|
113 | name, |
---|
114 | false, /* not shared across processes */ |
---|
115 | value, |
---|
116 | &the_semaphore |
---|
117 | ); |
---|
118 | |
---|
119 | /* |
---|
120 | * errno was set by Create_support, so don't set it again. |
---|
121 | */ |
---|
122 | |
---|
123 | _Thread_Enable_dispatch(); |
---|
124 | |
---|
125 | if ( status == -1 ) |
---|
126 | return SEM_FAILED; |
---|
127 | |
---|
128 | return_id: |
---|
129 | #if defined(RTEMS_USE_16_BIT_OBJECT) |
---|
130 | the_semaphore->Semaphore_id = the_semaphore->Object.id; |
---|
131 | id = &the_semaphore->Semaphore_id; |
---|
132 | #else |
---|
133 | id = (sem_t *)&the_semaphore->Object.id; |
---|
134 | #endif |
---|
135 | return id; |
---|
136 | } |
---|