1/* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
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>
28
29#if !PTHREAD_IN_LIBC
30/* The private names are not exported from libc. */
31# define __link link
32# define __unlink unlink
33#endif
34
35#define SEM_OPEN_FLAGS (O_RDWR | O_NOFOLLOW | O_CLOEXEC)
36
37sem_t *
38__sem_open (const char *name, int oflag, ...)
39{
40 int fd;
41 sem_t *result;
42
43 /* Check that shared futexes are supported. */
44 int err = futex_supports_pshared (PTHREAD_PROCESS_SHARED);
45 if (err != 0)
46 {
47 __set_errno (err);
48 return SEM_FAILED;
49 }
50
51 struct shmdir_name dirname;
52 int ret = __shm_get_name (&dirname, name, true);
53 if (ret != 0)
54 {
55 __set_errno (ret);
56 return SEM_FAILED;
57 }
58
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
65
66 /* If the semaphore object has to exist simply open it. */
67 if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
68 {
69 try_again:
70 fd = __open (dirname.name, (oflag & O_EXCL) | SEM_OPEN_FLAGS);
71
72 if (fd == -1)
73 {
74 /* If we are supposed to create the file try this next. */
75 if ((oflag & O_CREAT) != 0 && errno == ENOENT)
76 goto try_create;
77
78 /* Return. errno is already set. */
79 }
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);
84 }
85 else
86 {
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;
92
93 try_create:
94 va_start (ap, oflag);
95
96 mode = va_arg (ap, mode_t);
97 value = va_arg (ap, unsigned int);
98
99 va_end (ap);
100
101 if (value > SEM_VALUE_MAX)
102 {
103 __set_errno (EINVAL);
104 result = SEM_FAILED;
105 goto out;
106 }
107
108 /* Create the initial file content. */
109 union
110 {
111 sem_t initsem;
112 struct new_sem newsem;
113 } sem;
114
115 __new_sem_open_init (sem: &sem.newsem, value);
116
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));
120
121 char tmpfname[] = SHMDIR "sem.XXXXXX";
122 int retries = 0;
123#define NRETRIES 50
124 while (1)
125 {
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)
131 {
132 result = SEM_FAILED;
133 goto out;
134 }
135
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)
139 {
140 if (errno == EEXIST)
141 {
142 if (++retries < NRETRIES)
143 {
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;
148 }
149
150 __set_errno (EAGAIN);
151 }
152
153 result = SEM_FAILED;
154 goto out;
155 }
156
157 /* We got a file. */
158 break;
159 }
160
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)
167 {
168 /* Create the file. Don't overwrite an existing file. */
169 if (__link (from: tmpfname, to: dirname.name) != 0)
170 {
171 /* Undo the mapping. */
172 __munmap (result, sizeof (sem_t));
173
174 /* Reinitialize 'result'. */
175 result = SEM_FAILED;
176
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)
180 {
181 /* Remove the file. */
182 __unlink (name: tmpfname);
183
184 /* Close the file. */
185 __close (fd);
186
187 goto try_again;
188 }
189 }
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, existing: result);
195 }
196
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 (name: tmpfname);
200 }
201
202 /* Map the mmap error to the error we need. */
203 if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
204 result = SEM_FAILED;
205
206 /* We don't need the file descriptor anymore. */
207 if (fd != -1)
208 {
209 /* Do not disturb errno. */
210 int save = errno;
211 __close (fd);
212 errno = save;
213 }
214
215out:
216#ifdef __libc_ptf_call
217 __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
218#endif
219
220 return result;
221}
222#if PTHREAD_IN_LIBC
223versioned_symbol (libc, __sem_open, sem_open, GLIBC_2_34);
224# if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1_1, GLIBC_2_34)
225compat_symbol (libpthread, __sem_open, sem_open, GLIBC_2_1_1);
226# endif
227#else /* !PTHREAD_IN_LIBC */
228strong_alias (__sem_open, sem_open)
229#endif
230

source code of glibc/sysdeps/pthread/sem_open.c