1 /* Copyright (C) 2002-2024 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
35 #define SEM_OPEN_FLAGS (O_RDWR | O_NOFOLLOW | O_CLOEXEC)
38 __sem_open (const char *name
, int oflag
, ...)
43 /* Check that shared futexes are supported. */
44 int err
= futex_supports_pshared (PTHREAD_PROCESS_SHARED
);
51 struct shmdir_name dirname
;
52 int ret
= __shm_get_name (&dirname
, name
, true);
59 /* Disable asynchronous cancellation. */
60 #ifdef __libc_ptf_call
62 __libc_ptf_call (__pthread_setcancelstate
,
63 (PTHREAD_CANCEL_DISABLE
, &state
), 0);
66 /* If the semaphore object has to exist simply open it. */
67 if ((oflag
& O_CREAT
) == 0 || (oflag
& O_EXCL
) == 0)
70 fd
= __open (dirname
.name
, (oflag
& O_EXCL
) | SEM_OPEN_FLAGS
);
74 /* If we are supposed to create the file try this next. */
75 if ((oflag
& O_CREAT
) != 0 && errno
== ENOENT
)
78 /* Return. errno is already set. */
81 /* Check whether we already have this semaphore mapped and
82 create one if necessary. */
83 result
= __sem_check_add_mapping (name
, fd
, SEM_FAILED
);
87 /* We have to open a temporary file first since it must have the
88 correct form before we can start using it. */
96 mode
= va_arg (ap
, mode_t
);
97 value
= va_arg (ap
, unsigned int);
101 if (value
> SEM_VALUE_MAX
)
103 __set_errno (EINVAL
);
108 /* Create the initial file content. */
112 struct new_sem newsem
;
115 __new_sem_open_init (&sem
.newsem
, value
);
117 /* Initialize the remaining bytes as well. */
118 memset ((char *) &sem
.initsem
+ sizeof (struct new_sem
), '\0',
119 sizeof (sem_t
) - sizeof (struct new_sem
));
121 char tmpfname
[] = SHMDIR
"sem.XXXXXX";
126 /* We really want to use mktemp here. We cannot use mkstemp
127 since the file must be opened with a specific mode. The
128 mode cannot later be set since then we cannot apply the
130 if (__mktemp (tmpfname
) == NULL
)
136 /* Open the file. Make sure we do not overwrite anything. */
137 fd
= __open (tmpfname
, O_CREAT
| O_EXCL
| SEM_OPEN_FLAGS
, mode
);
142 if (++retries
< NRETRIES
)
144 /* Restore the six placeholder bytes before the
145 null terminator before the next attempt. */
146 memcpy (tmpfname
+ sizeof (tmpfname
) - 7, "XXXXXX", 6);
150 __set_errno (EAGAIN
);
161 if (TEMP_FAILURE_RETRY (write (fd
, &sem
.initsem
, sizeof (sem_t
)))
163 /* Map the sem_t structure from the file. */
164 && (result
= (sem_t
*) __mmap (NULL
, sizeof (sem_t
),
165 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
166 fd
, 0)) != MAP_FAILED
)
168 /* Create the file. Don't overwrite an existing file. */
169 if (__link (tmpfname
, dirname
.name
) != 0)
171 /* Undo the mapping. */
172 __munmap (result
, sizeof (sem_t
));
174 /* Reinitialize 'result'. */
177 /* This failed. If O_EXCL is not set and the problem was
178 that the file exists, try again. */
179 if ((oflag
& O_EXCL
) == 0 && errno
== EEXIST
)
181 /* Remove the file. */
184 /* Close the file. */
191 /* Insert the mapping into the search tree. This also
192 determines whether another thread sneaked by and already
193 added such a mapping despite the fact that we created it. */
194 result
= __sem_check_add_mapping (name
, fd
, result
);
197 /* Now remove the temporary name. This should never fail. If
198 it fails we leak a file name. Better fix the kernel. */
202 /* Map the mmap error to the error we need. */
203 if (MAP_FAILED
!= (void *) SEM_FAILED
&& result
== MAP_FAILED
)
206 /* We don't need the file descriptor anymore. */
209 /* Do not disturb errno. */
216 #ifdef __libc_ptf_call
217 __libc_ptf_call (__pthread_setcancelstate
, (state
, NULL
), 0);
223 versioned_symbol (libc
, __sem_open
, sem_open
, GLIBC_2_34
);
224 # if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1_1, GLIBC_2_34)
225 compat_symbol (libpthread
, __sem_open
, sem_open
, GLIBC_2_1_1
);
227 #else /* !PTHREAD_IN_LIBC */
228 strong_alias (__sem_open
, sem_open
)