]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 2002-2020 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <fcntl.h> | |
76a50749 | 21 | #include <pthread.h> |
fddfebbd | 22 | #include <search.h> |
76a50749 UD |
23 | #include <semaphore.h> |
24 | #include <stdarg.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/mman.h> | |
30 | #include <sys/stat.h> | |
76a50749 | 31 | #include "semaphoreP.h" |
e4f639e4 | 32 | #include <shm-directory.h> |
a2f0363f | 33 | #include <futex-internal.h> |
91dd866f | 34 | #include <libc-lock.h> |
76a50749 | 35 | |
fddfebbd UD |
36 | /* Comparison function for search of existing mapping. */ |
37 | int | |
38 | attribute_hidden | |
39 | __sem_search (const void *a, const void *b) | |
40 | { | |
41 | const struct inuse_sem *as = (const struct inuse_sem *) a; | |
42 | const struct inuse_sem *bs = (const struct inuse_sem *) b; | |
43 | ||
44 | if (as->ino != bs->ino) | |
45 | /* Cannot return the difference the type is larger than int. */ | |
46 | return as->ino < bs->ino ? -1 : (as->ino == bs->ino ? 0 : 1); | |
47 | ||
48 | if (as->dev != bs->dev) | |
49 | /* Cannot return the difference the type is larger than int. */ | |
50 | return as->dev < bs->dev ? -1 : (as->dev == bs->dev ? 0 : 1); | |
51 | ||
52 | return strcmp (as->name, bs->name); | |
53 | } | |
54 | ||
55 | ||
56 | /* The search tree for existing mappings. */ | |
57 | void *__sem_mappings attribute_hidden; | |
58 | ||
59 | /* Lock to protect the search tree. */ | |
e51deae7 | 60 | int __sem_mappings_lock attribute_hidden = LLL_LOCK_INITIALIZER; |
fddfebbd UD |
61 | |
62 | ||
63 | /* Search for existing mapping and if possible add the one provided. */ | |
64 | static sem_t * | |
65 | check_add_mapping (const char *name, size_t namelen, int fd, sem_t *existing) | |
66 | { | |
67 | sem_t *result = SEM_FAILED; | |
68 | ||
69 | /* Get the information about the file. */ | |
70 | struct stat64 st; | |
71 | if (__fxstat64 (_STAT_VER, fd, &st) == 0) | |
72 | { | |
73 | /* Get the lock. */ | |
e51deae7 | 74 | lll_lock (__sem_mappings_lock, LLL_PRIVATE); |
fddfebbd UD |
75 | |
76 | /* Search for an existing mapping given the information we have. */ | |
77 | struct inuse_sem *fake; | |
78 | fake = (struct inuse_sem *) alloca (sizeof (*fake) + namelen); | |
79 | memcpy (fake->name, name, namelen); | |
80 | fake->dev = st.st_dev; | |
81 | fake->ino = st.st_ino; | |
82 | ||
d051b143 JM |
83 | struct inuse_sem **foundp = __tfind (fake, &__sem_mappings, |
84 | __sem_search); | |
fddfebbd UD |
85 | if (foundp != NULL) |
86 | { | |
87 | /* There is already a mapping. Use it. */ | |
88 | result = (*foundp)->sem; | |
89 | ++(*foundp)->refcnt; | |
90 | } | |
dcfc8224 | 91 | else |
fddfebbd | 92 | { |
dcfc8224 | 93 | /* We haven't found a mapping. Install ione. */ |
fddfebbd UD |
94 | struct inuse_sem *newp; |
95 | ||
96 | newp = (struct inuse_sem *) malloc (sizeof (*newp) + namelen); | |
97 | if (newp != NULL) | |
98 | { | |
dcfc8224 UD |
99 | /* If the caller hasn't provided any map it now. */ |
100 | if (existing == SEM_FAILED) | |
101 | existing = (sem_t *) mmap (NULL, sizeof (sem_t), | |
102 | PROT_READ | PROT_WRITE, MAP_SHARED, | |
103 | fd, 0); | |
104 | ||
fddfebbd UD |
105 | newp->dev = st.st_dev; |
106 | newp->ino = st.st_ino; | |
107 | newp->refcnt = 1; | |
108 | newp->sem = existing; | |
109 | memcpy (newp->name, name, namelen); | |
110 | ||
111 | /* Insert the new value. */ | |
dcfc8224 | 112 | if (existing != MAP_FAILED |
d051b143 | 113 | && __tsearch (newp, &__sem_mappings, __sem_search) != NULL) |
fddfebbd UD |
114 | /* Successful. */ |
115 | result = existing; | |
116 | else | |
117 | /* Something went wrong while inserting the new | |
118 | value. We fail completely. */ | |
119 | free (newp); | |
120 | } | |
121 | } | |
122 | ||
123 | /* Release the lock. */ | |
e51deae7 | 124 | lll_unlock (__sem_mappings_lock, LLL_PRIVATE); |
fddfebbd UD |
125 | } |
126 | ||
dcfc8224 | 127 | if (result != existing && existing != SEM_FAILED && existing != MAP_FAILED) |
fddfebbd UD |
128 | { |
129 | /* Do not disturb errno. */ | |
e4f639e4 RM |
130 | int save = errno; |
131 | munmap (existing, sizeof (sem_t)); | |
132 | errno = save; | |
fddfebbd UD |
133 | } |
134 | ||
135 | return result; | |
136 | } | |
137 | ||
138 | ||
76a50749 UD |
139 | sem_t * |
140 | sem_open (const char *name, int oflag, ...) | |
141 | { | |
76a50749 | 142 | int fd; |
e4f639e4 | 143 | sem_t *result; |
76a50749 | 144 | |
a2f0363f TR |
145 | /* Check that shared futexes are supported. */ |
146 | int err = futex_supports_pshared (PTHREAD_PROCESS_SHARED); | |
147 | if (err != 0) | |
148 | { | |
149 | __set_errno (err); | |
150 | return SEM_FAILED; | |
151 | } | |
152 | ||
e4f639e4 RM |
153 | /* Create the name of the final file in local variable SHM_NAME. */ |
154 | SHM_GET_NAME (EINVAL, SEM_FAILED, SEM_SHM_PREFIX); | |
76a50749 | 155 | |
91dd866f AZ |
156 | /* Disable asynchronous cancellation. */ |
157 | #ifdef __libc_ptf_call | |
158 | int state; | |
159 | __libc_ptf_call (__pthread_setcancelstate, | |
160 | (PTHREAD_CANCEL_DISABLE, &state), 0); | |
161 | #endif | |
162 | ||
76a50749 | 163 | /* If the semaphore object has to exist simply open it. */ |
3857ca78 | 164 | if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0) |
76a50749 | 165 | { |
3857ca78 | 166 | try_again: |
e4f639e4 | 167 | fd = __libc_open (shm_name, |
3857ca78 | 168 | (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR); |
76a50749 UD |
169 | |
170 | if (fd == -1) | |
3857ca78 UD |
171 | { |
172 | /* If we are supposed to create the file try this next. */ | |
fddfebbd | 173 | if ((oflag & O_CREAT) != 0 && errno == ENOENT) |
3857ca78 | 174 | goto try_create; |
76a50749 | 175 | |
3857ca78 UD |
176 | /* Return. errno is already set. */ |
177 | } | |
178 | else | |
dcfc8224 UD |
179 | /* Check whether we already have this semaphore mapped and |
180 | create one if necessary. */ | |
181 | result = check_add_mapping (name, namelen, fd, SEM_FAILED); | |
76a50749 UD |
182 | } |
183 | else | |
184 | { | |
185 | /* We have to open a temporary file first since it must have the | |
186 | correct form before we can start using it. */ | |
187 | char *tmpfname; | |
188 | mode_t mode; | |
189 | unsigned int value; | |
190 | va_list ap; | |
191 | ||
3857ca78 | 192 | try_create: |
76a50749 UD |
193 | va_start (ap, oflag); |
194 | ||
195 | mode = va_arg (ap, mode_t); | |
196 | value = va_arg (ap, unsigned int); | |
197 | ||
198 | va_end (ap); | |
199 | ||
200 | if (value > SEM_VALUE_MAX) | |
201 | { | |
202 | __set_errno (EINVAL); | |
91dd866f AZ |
203 | result = SEM_FAILED; |
204 | goto out; | |
76a50749 UD |
205 | } |
206 | ||
76a50749 | 207 | /* Create the initial file content. */ |
5efe8650 UD |
208 | union |
209 | { | |
210 | sem_t initsem; | |
211 | struct new_sem newsem; | |
212 | } sem; | |
76a50749 | 213 | |
042e1521 CD |
214 | #if __HAVE_64B_ATOMICS |
215 | sem.newsem.data = value; | |
216 | #else | |
217 | sem.newsem.value = value << SEM_VALUE_SHIFT; | |
5efe8650 | 218 | sem.newsem.nwaiters = 0; |
042e1521 | 219 | #endif |
05598a09 PF |
220 | /* pad is used as a mutex on pre-v9 sparc and ignored otherwise. */ |
221 | sem.newsem.pad = 0; | |
222 | ||
042e1521 | 223 | /* This always is a shared semaphore. */ |
a2f0363f | 224 | sem.newsem.private = FUTEX_SHARED; |
76a50749 UD |
225 | |
226 | /* Initialize the remaining bytes as well. */ | |
5efe8650 | 227 | memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0', |
3d2dd6ca | 228 | sizeof (sem_t) - sizeof (struct new_sem)); |
76a50749 | 229 | |
e4f639e4 RM |
230 | tmpfname = __alloca (shm_dirlen + sizeof SEM_SHM_PREFIX + 6); |
231 | char *xxxxxx = __mempcpy (tmpfname, shm_dir, shm_dirlen); | |
fddfebbd UD |
232 | |
233 | int retries = 0; | |
234 | #define NRETRIES 50 | |
235 | while (1) | |
236 | { | |
237 | /* Add the suffix for mktemp. */ | |
238 | strcpy (xxxxxx, "XXXXXX"); | |
239 | ||
240 | /* We really want to use mktemp here. We cannot use mkstemp | |
241 | since the file must be opened with a specific mode. The | |
242 | mode cannot later be set since then we cannot apply the | |
243 | file create mask. */ | |
cfa8054f | 244 | if (__mktemp (tmpfname) == NULL) |
91dd866f AZ |
245 | { |
246 | result = SEM_FAILED; | |
247 | goto out; | |
248 | } | |
fddfebbd UD |
249 | |
250 | /* Open the file. Make sure we do not overwrite anything. */ | |
251 | fd = __libc_open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode); | |
252 | if (fd == -1) | |
253 | { | |
254 | if (errno == EEXIST) | |
255 | { | |
256 | if (++retries < NRETRIES) | |
257 | continue; | |
258 | ||
259 | __set_errno (EAGAIN); | |
260 | } | |
261 | ||
91dd866f AZ |
262 | result = SEM_FAILED; |
263 | goto out; | |
fddfebbd UD |
264 | } |
265 | ||
266 | /* We got a file. */ | |
267 | break; | |
268 | } | |
269 | ||
5efe8650 | 270 | if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t))) |
3857ca78 | 271 | == sizeof (sem_t) |
3857ca78 UD |
272 | /* Map the sem_t structure from the file. */ |
273 | && (result = (sem_t *) mmap (NULL, sizeof (sem_t), | |
274 | PROT_READ | PROT_WRITE, MAP_SHARED, | |
275 | fd, 0)) != MAP_FAILED) | |
76a50749 | 276 | { |
3857ca78 | 277 | /* Create the file. Don't overwrite an existing file. */ |
e4f639e4 | 278 | if (link (tmpfname, shm_name) != 0) |
3857ca78 | 279 | { |
3857ca78 UD |
280 | /* Undo the mapping. */ |
281 | (void) munmap (result, sizeof (sem_t)); | |
76a50749 | 282 | |
3857ca78 UD |
283 | /* Reinitialize 'result'. */ |
284 | result = SEM_FAILED; | |
285 | ||
286 | /* This failed. If O_EXCL is not set and the problem was | |
287 | that the file exists, try again. */ | |
288 | if ((oflag & O_EXCL) == 0 && errno == EEXIST) | |
73983ece UD |
289 | { |
290 | /* Remove the file. */ | |
291 | (void) unlink (tmpfname); | |
292 | ||
293 | /* Close the file. */ | |
294 | (void) __libc_close (fd); | |
295 | ||
296 | goto try_again; | |
297 | } | |
76a50749 | 298 | } |
fddfebbd UD |
299 | else |
300 | /* Insert the mapping into the search tree. This also | |
301 | determines whether another thread sneaked by and already | |
302 | added such a mapping despite the fact that we created it. */ | |
303 | result = check_add_mapping (name, namelen, fd, result); | |
76a50749 | 304 | } |
76a50749 | 305 | |
3857ca78 UD |
306 | /* Now remove the temporary name. This should never fail. If |
307 | it fails we leak a file name. Better fix the kernel. */ | |
308 | (void) unlink (tmpfname); | |
76a50749 UD |
309 | } |
310 | ||
3857ca78 UD |
311 | /* Map the mmap error to the error we need. */ |
312 | if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED) | |
313 | result = SEM_FAILED; | |
314 | ||
76a50749 | 315 | /* We don't need the file descriptor anymore. */ |
73983ece | 316 | if (fd != -1) |
fddfebbd UD |
317 | { |
318 | /* Do not disturb errno. */ | |
e4f639e4 RM |
319 | int save = errno; |
320 | __libc_close (fd); | |
321 | errno = save; | |
fddfebbd | 322 | } |
76a50749 | 323 | |
91dd866f AZ |
324 | out: |
325 | #ifdef __libc_ptf_call | |
326 | __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); | |
327 | #endif | |
328 | ||
76a50749 UD |
329 | return result; |
330 | } |