]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/sem_open.c
nptl: Move sem_close, sem_open into libc
[thirdparty/glibc.git] / sysdeps / pthread / sem_open.c
CommitLineData
2b778ceb 1/* Copyright (C) 2002-2021 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 18
76a50749 19#include <fcntl.h>
76a50749
UD
20#include <semaphore.h>
21#include <stdarg.h>
76a50749
UD
22#include <unistd.h>
23#include <sys/mman.h>
76a50749 24#include "semaphoreP.h"
e4f639e4 25#include <shm-directory.h>
da4aea0b 26#include <sem_routines.h>
a2f0363f 27#include <futex-internal.h>
91dd866f 28#include <libc-lock.h>
76a50749 29
0b7d48d1
FW
30#if !PTHREAD_IN_LIBC
31/* The private names are not exported from libc. */
32# define __link link
33# define __unlink unlink
34#endif
35
76a50749 36sem_t *
0b7d48d1 37__sem_open (const char *name, int oflag, ...)
76a50749 38{
76a50749 39 int fd;
e4f639e4 40 sem_t *result;
76a50749 41
a2f0363f
TR
42 /* Check that shared futexes are supported. */
43 int err = futex_supports_pshared (PTHREAD_PROCESS_SHARED);
44 if (err != 0)
45 {
46 __set_errno (err);
47 return SEM_FAILED;
48 }
49
e9fed243
FW
50 struct shmdir_name dirname;
51 if (__shm_get_name (&dirname, name, true) != 0)
52 {
53 __set_errno (EINVAL);
54 return SEM_FAILED;
55 }
76a50749 56
91dd866f
AZ
57 /* Disable asynchronous cancellation. */
58#ifdef __libc_ptf_call
59 int state;
60 __libc_ptf_call (__pthread_setcancelstate,
61 (PTHREAD_CANCEL_DISABLE, &state), 0);
62#endif
63
76a50749 64 /* If the semaphore object has to exist simply open it. */
3857ca78 65 if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
76a50749 66 {
3857ca78 67 try_again:
0b7d48d1
FW
68 fd = __open (dirname.name,
69 (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR);
76a50749
UD
70
71 if (fd == -1)
3857ca78
UD
72 {
73 /* If we are supposed to create the file try this next. */
fddfebbd 74 if ((oflag & O_CREAT) != 0 && errno == ENOENT)
3857ca78 75 goto try_create;
76a50749 76
3857ca78
UD
77 /* Return. errno is already set. */
78 }
79 else
dcfc8224
UD
80 /* Check whether we already have this semaphore mapped and
81 create one if necessary. */
da4aea0b 82 result = __sem_check_add_mapping (name, fd, SEM_FAILED);
76a50749
UD
83 }
84 else
85 {
86 /* We have to open a temporary file first since it must have the
87 correct form before we can start using it. */
76a50749
UD
88 mode_t mode;
89 unsigned int value;
90 va_list ap;
91
3857ca78 92 try_create:
76a50749
UD
93 va_start (ap, oflag);
94
95 mode = va_arg (ap, mode_t);
96 value = va_arg (ap, unsigned int);
97
98 va_end (ap);
99
100 if (value > SEM_VALUE_MAX)
101 {
102 __set_errno (EINVAL);
91dd866f
AZ
103 result = SEM_FAILED;
104 goto out;
76a50749
UD
105 }
106
76a50749 107 /* Create the initial file content. */
5efe8650
UD
108 union
109 {
110 sem_t initsem;
111 struct new_sem newsem;
112 } sem;
76a50749 113
a28296e7 114 __new_sem_open_init (&sem.newsem, value);
76a50749
UD
115
116 /* Initialize the remaining bytes as well. */
5efe8650 117 memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0',
3d2dd6ca 118 sizeof (sem_t) - sizeof (struct new_sem));
76a50749 119
e9fed243 120 char tmpfname[] = SHMDIR "sem.XXXXXX";
fddfebbd
UD
121 int retries = 0;
122#define NRETRIES 50
123 while (1)
124 {
fddfebbd
UD
125 /* We really want to use mktemp here. We cannot use mkstemp
126 since the file must be opened with a specific mode. The
127 mode cannot later be set since then we cannot apply the
128 file create mask. */
cfa8054f 129 if (__mktemp (tmpfname) == NULL)
91dd866f
AZ
130 {
131 result = SEM_FAILED;
132 goto out;
133 }
fddfebbd
UD
134
135 /* Open the file. Make sure we do not overwrite anything. */
0b7d48d1 136 fd = __open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
fddfebbd
UD
137 if (fd == -1)
138 {
139 if (errno == EEXIST)
140 {
141 if (++retries < NRETRIES)
e9fed243
FW
142 {
143 /* Restore the six placeholder bytes before the
144 null terminator before the next attempt. */
145 memcpy (tmpfname + sizeof (tmpfname) - 7, "XXXXXX", 6);
146 continue;
147 }
fddfebbd
UD
148
149 __set_errno (EAGAIN);
150 }
151
91dd866f
AZ
152 result = SEM_FAILED;
153 goto out;
fddfebbd
UD
154 }
155
156 /* We got a file. */
157 break;
158 }
159
173e0ab0 160 if (TEMP_FAILURE_RETRY (write (fd, &sem.initsem, sizeof (sem_t)))
3857ca78 161 == sizeof (sem_t)
3857ca78 162 /* Map the sem_t structure from the file. */
0b7d48d1
FW
163 && (result = (sem_t *) __mmap (NULL, sizeof (sem_t),
164 PROT_READ | PROT_WRITE, MAP_SHARED,
165 fd, 0)) != MAP_FAILED)
76a50749 166 {
3857ca78 167 /* Create the file. Don't overwrite an existing file. */
0b7d48d1 168 if (__link (tmpfname, dirname.name) != 0)
3857ca78 169 {
3857ca78 170 /* Undo the mapping. */
0b7d48d1 171 __munmap (result, sizeof (sem_t));
76a50749 172
3857ca78
UD
173 /* Reinitialize 'result'. */
174 result = SEM_FAILED;
175
176 /* This failed. If O_EXCL is not set and the problem was
177 that the file exists, try again. */
178 if ((oflag & O_EXCL) == 0 && errno == EEXIST)
73983ece
UD
179 {
180 /* Remove the file. */
0b7d48d1 181 __unlink (tmpfname);
73983ece
UD
182
183 /* Close the file. */
0b7d48d1 184 __close (fd);
73983ece
UD
185
186 goto try_again;
187 }
76a50749 188 }
fddfebbd
UD
189 else
190 /* Insert the mapping into the search tree. This also
191 determines whether another thread sneaked by and already
192 added such a mapping despite the fact that we created it. */
da4aea0b 193 result = __sem_check_add_mapping (name, fd, result);
76a50749 194 }
76a50749 195
3857ca78
UD
196 /* Now remove the temporary name. This should never fail. If
197 it fails we leak a file name. Better fix the kernel. */
0b7d48d1 198 __unlink (tmpfname);
76a50749
UD
199 }
200
3857ca78
UD
201 /* Map the mmap error to the error we need. */
202 if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
203 result = SEM_FAILED;
204
76a50749 205 /* We don't need the file descriptor anymore. */
73983ece 206 if (fd != -1)
fddfebbd
UD
207 {
208 /* Do not disturb errno. */
e4f639e4 209 int save = errno;
0b7d48d1 210 __close (fd);
e4f639e4 211 errno = save;
fddfebbd 212 }
76a50749 213
91dd866f
AZ
214out:
215#ifdef __libc_ptf_call
216 __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
217#endif
218
76a50749
UD
219 return result;
220}
0b7d48d1
FW
221#if PTHREAD_IN_LIBC
222versioned_symbol (libc, __sem_open, sem_open, GLIBC_2_34);
223# if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1_1, GLIBC_2_34)
224compat_symbol (libpthread, __sem_open, sem_open, GLIBC_2_1_1);
225# endif
226#else /* !PTHREAD_IN_LIBC */
227strong_alias (__sem_open, sem_open)
228#endif