]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/sem_open.c
1 /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include <semaphore.h>
23 #include "semaphoreP.h"
24 #include <shm-directory.h>
25 #include <sem_routines.h>
26 #include <futex-internal.h>
27 #include <libc-lock.h>
30 /* The private names are not exported from libc. */
32 # define __unlink unlink
36 __sem_open (const char *name
, int oflag
, ...)
41 /* Check that shared futexes are supported. */
42 int err
= futex_supports_pshared (PTHREAD_PROCESS_SHARED
);
49 struct shmdir_name dirname
;
50 if (__shm_get_name (&dirname
, name
, true) != 0)
56 /* Disable asynchronous cancellation. */
57 #ifdef __libc_ptf_call
59 __libc_ptf_call (__pthread_setcancelstate
,
60 (PTHREAD_CANCEL_DISABLE
, &state
), 0);
63 /* If the semaphore object has to exist simply open it. */
64 if ((oflag
& O_CREAT
) == 0 || (oflag
& O_EXCL
) == 0)
67 fd
= __open (dirname
.name
,
68 (oflag
& ~(O_CREAT
|O_ACCMODE
)) | O_NOFOLLOW
| O_RDWR
);
72 /* If we are supposed to create the file try this next. */
73 if ((oflag
& O_CREAT
) != 0 && errno
== ENOENT
)
76 /* Return. errno is already set. */
79 /* Check whether we already have this semaphore mapped and
80 create one if necessary. */
81 result
= __sem_check_add_mapping (name
, fd
, SEM_FAILED
);
85 /* We have to open a temporary file first since it must have the
86 correct form before we can start using it. */
94 mode
= va_arg (ap
, mode_t
);
95 value
= va_arg (ap
, unsigned int);
99 if (value
> SEM_VALUE_MAX
)
101 __set_errno (EINVAL
);
106 /* Create the initial file content. */
110 struct new_sem newsem
;
113 __new_sem_open_init (&sem
.newsem
, value
);
115 /* Initialize the remaining bytes as well. */
116 memset ((char *) &sem
.initsem
+ sizeof (struct new_sem
), '\0',
117 sizeof (sem_t
) - sizeof (struct new_sem
));
119 char tmpfname
[] = SHMDIR
"sem.XXXXXX";
124 /* We really want to use mktemp here. We cannot use mkstemp
125 since the file must be opened with a specific mode. The
126 mode cannot later be set since then we cannot apply the
128 if (__mktemp (tmpfname
) == NULL
)
134 /* Open the file. Make sure we do not overwrite anything. */
135 fd
= __open (tmpfname
, O_RDWR
| O_CREAT
| O_EXCL
, mode
);
140 if (++retries
< NRETRIES
)
142 /* Restore the six placeholder bytes before the
143 null terminator before the next attempt. */
144 memcpy (tmpfname
+ sizeof (tmpfname
) - 7, "XXXXXX", 6);
148 __set_errno (EAGAIN
);
159 if (TEMP_FAILURE_RETRY (write (fd
, &sem
.initsem
, sizeof (sem_t
)))
161 /* Map the sem_t structure from the file. */
162 && (result
= (sem_t
*) __mmap (NULL
, sizeof (sem_t
),
163 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
164 fd
, 0)) != MAP_FAILED
)
166 /* Create the file. Don't overwrite an existing file. */
167 if (__link (tmpfname
, dirname
.name
) != 0)
169 /* Undo the mapping. */
170 __munmap (result
, sizeof (sem_t
));
172 /* Reinitialize 'result'. */
175 /* This failed. If O_EXCL is not set and the problem was
176 that the file exists, try again. */
177 if ((oflag
& O_EXCL
) == 0 && errno
== EEXIST
)
179 /* Remove the file. */
182 /* Close the file. */
189 /* Insert the mapping into the search tree. This also
190 determines whether another thread sneaked by and already
191 added such a mapping despite the fact that we created it. */
192 result
= __sem_check_add_mapping (name
, fd
, result
);
195 /* Now remove the temporary name. This should never fail. If
196 it fails we leak a file name. Better fix the kernel. */
200 /* Map the mmap error to the error we need. */
201 if (MAP_FAILED
!= (void *) SEM_FAILED
&& result
== MAP_FAILED
)
204 /* We don't need the file descriptor anymore. */
207 /* Do not disturb errno. */
214 #ifdef __libc_ptf_call
215 __libc_ptf_call (__pthread_setcancelstate
, (state
, NULL
), 0);
221 versioned_symbol (libc
, __sem_open
, sem_open
, GLIBC_2_34
);
222 # if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1_1, GLIBC_2_34)
223 compat_symbol (libpthread
, __sem_open
, sem_open
, GLIBC_2_1_1
);
225 #else /* !PTHREAD_IN_LIBC */
226 strong_alias (__sem_open
, sem_open
)