x86-64: Don't use SSE resolvers for ISA level 3 or above
[glibc.git] / sysdeps / pthread / sem_open.c
blobe41236157a5d1b0a1c1fc478bdb0f459b61bd29c
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/>. */
18 #include <fcntl.h>
19 #include <semaphore.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #include <sys/mman.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>
29 #if !PTHREAD_IN_LIBC
30 /* The private names are not exported from libc. */
31 # define __link link
32 # define __unlink unlink
33 #endif
35 #define SEM_OPEN_FLAGS (O_RDWR | O_NOFOLLOW | O_CLOEXEC)
37 sem_t *
38 __sem_open (const char *name, int oflag, ...)
40 int fd;
41 sem_t *result;
43 /* Check that shared futexes are supported. */
44 int err = futex_supports_pshared (PTHREAD_PROCESS_SHARED);
45 if (err != 0)
47 __set_errno (err);
48 return SEM_FAILED;
51 struct shmdir_name dirname;
52 int ret = __shm_get_name (&dirname, name, true);
53 if (ret != 0)
55 __set_errno (ret);
56 return SEM_FAILED;
59 /* Disable asynchronous cancellation. */
60 #ifdef __libc_ptf_call
61 int state;
62 __libc_ptf_call (__pthread_setcancelstate,
63 (PTHREAD_CANCEL_DISABLE, &state), 0);
64 #endif
66 /* If the semaphore object has to exist simply open it. */
67 if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
69 try_again:
70 fd = __open (dirname.name, (oflag & O_EXCL) | SEM_OPEN_FLAGS);
72 if (fd == -1)
74 /* If we are supposed to create the file try this next. */
75 if ((oflag & O_CREAT) != 0 && errno == ENOENT)
76 goto try_create;
78 /* Return. errno is already set. */
80 else
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);
85 else
87 /* We have to open a temporary file first since it must have the
88 correct form before we can start using it. */
89 mode_t mode;
90 unsigned int value;
91 va_list ap;
93 try_create:
94 va_start (ap, oflag);
96 mode = va_arg (ap, mode_t);
97 value = va_arg (ap, unsigned int);
99 va_end (ap);
101 if (value > SEM_VALUE_MAX)
103 __set_errno (EINVAL);
104 result = SEM_FAILED;
105 goto out;
108 /* Create the initial file content. */
109 union
111 sem_t initsem;
112 struct new_sem newsem;
113 } sem;
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";
122 int retries = 0;
123 #define NRETRIES 50
124 while (1)
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
129 file create mask. */
130 if (__mktemp (tmpfname) == NULL)
132 result = SEM_FAILED;
133 goto out;
136 /* Open the file. Make sure we do not overwrite anything. */
137 fd = __open (tmpfname, O_CREAT | O_EXCL | SEM_OPEN_FLAGS, mode);
138 if (fd == -1)
140 if (errno == EEXIST)
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);
147 continue;
150 __set_errno (EAGAIN);
153 result = SEM_FAILED;
154 goto out;
157 /* We got a file. */
158 break;
161 if (TEMP_FAILURE_RETRY (write (fd, &sem.initsem, sizeof (sem_t)))
162 == 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'. */
175 result = SEM_FAILED;
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. */
182 __unlink (tmpfname);
184 /* Close the file. */
185 __close (fd);
187 goto try_again;
190 else
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. */
199 __unlink (tmpfname);
202 /* Map the mmap error to the error we need. */
203 if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
204 result = SEM_FAILED;
206 /* We don't need the file descriptor anymore. */
207 if (fd != -1)
209 /* Do not disturb errno. */
210 int save = errno;
211 __close (fd);
212 errno = save;
215 out:
216 #ifdef __libc_ptf_call
217 __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
218 #endif
220 return result;
222 #if PTHREAD_IN_LIBC
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);
226 # endif
227 #else /* !PTHREAD_IN_LIBC */
228 strong_alias (__sem_open, sem_open)
229 #endif