1 | /* Copyright (C) 2002-2021 Free Software Foundation, Inc. |
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 |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <fcntl.h> |
20 | #include <semaphore.h> |
21 | #include <stdarg.h> |
22 | #include <unistd.h> |
23 | #include <sys/mman.h> |
24 | #include "semaphoreP.h" |
25 | #include <shm-directory.h> |
26 | #include <sem_routines.h> |
27 | #include <futex-internal.h> |
28 | #include <libc-lock.h> |
29 | |
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 | |
36 | sem_t * |
37 | __sem_open (const char *name, int oflag, ...) |
38 | { |
39 | int fd; |
40 | sem_t *result; |
41 | |
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 | |
50 | struct shmdir_name dirname; |
51 | if (__shm_get_name (&dirname, name, true) != 0) |
52 | { |
53 | __set_errno (EINVAL); |
54 | return SEM_FAILED; |
55 | } |
56 | |
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 | |
64 | /* If the semaphore object has to exist simply open it. */ |
65 | if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0) |
66 | { |
67 | try_again: |
68 | fd = __open (dirname.name, |
69 | (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR); |
70 | |
71 | if (fd == -1) |
72 | { |
73 | /* If we are supposed to create the file try this next. */ |
74 | if ((oflag & O_CREAT) != 0 && errno == ENOENT) |
75 | goto try_create; |
76 | |
77 | /* Return. errno is already set. */ |
78 | } |
79 | else |
80 | /* Check whether we already have this semaphore mapped and |
81 | create one if necessary. */ |
82 | result = __sem_check_add_mapping (name, fd, SEM_FAILED); |
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. */ |
88 | mode_t mode; |
89 | unsigned int value; |
90 | va_list ap; |
91 | |
92 | try_create: |
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); |
103 | result = SEM_FAILED; |
104 | goto out; |
105 | } |
106 | |
107 | /* Create the initial file content. */ |
108 | union |
109 | { |
110 | sem_t initsem; |
111 | struct new_sem newsem; |
112 | } sem; |
113 | |
114 | __new_sem_open_init (&sem.newsem, value); |
115 | |
116 | /* Initialize the remaining bytes as well. */ |
117 | memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0', |
118 | sizeof (sem_t) - sizeof (struct new_sem)); |
119 | |
120 | char tmpfname[] = SHMDIR "sem.XXXXXX" ; |
121 | int retries = 0; |
122 | #define NRETRIES 50 |
123 | while (1) |
124 | { |
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. */ |
129 | if (__mktemp (tmpfname) == NULL) |
130 | { |
131 | result = SEM_FAILED; |
132 | goto out; |
133 | } |
134 | |
135 | /* Open the file. Make sure we do not overwrite anything. */ |
136 | fd = __open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode); |
137 | if (fd == -1) |
138 | { |
139 | if (errno == EEXIST) |
140 | { |
141 | if (++retries < NRETRIES) |
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 | } |
148 | |
149 | __set_errno (EAGAIN); |
150 | } |
151 | |
152 | result = SEM_FAILED; |
153 | goto out; |
154 | } |
155 | |
156 | /* We got a file. */ |
157 | break; |
158 | } |
159 | |
160 | if (TEMP_FAILURE_RETRY (write (fd, &sem.initsem, sizeof (sem_t))) |
161 | == sizeof (sem_t) |
162 | /* Map the sem_t structure from the file. */ |
163 | && (result = (sem_t *) __mmap (NULL, sizeof (sem_t), |
164 | PROT_READ | PROT_WRITE, MAP_SHARED, |
165 | fd, 0)) != MAP_FAILED) |
166 | { |
167 | /* Create the file. Don't overwrite an existing file. */ |
168 | if (__link (tmpfname, dirname.name) != 0) |
169 | { |
170 | /* Undo the mapping. */ |
171 | __munmap (result, sizeof (sem_t)); |
172 | |
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) |
179 | { |
180 | /* Remove the file. */ |
181 | __unlink (tmpfname); |
182 | |
183 | /* Close the file. */ |
184 | __close (fd); |
185 | |
186 | goto try_again; |
187 | } |
188 | } |
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. */ |
193 | result = __sem_check_add_mapping (name, fd, result); |
194 | } |
195 | |
196 | /* Now remove the temporary name. This should never fail. If |
197 | it fails we leak a file name. Better fix the kernel. */ |
198 | __unlink (tmpfname); |
199 | } |
200 | |
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 | |
205 | /* We don't need the file descriptor anymore. */ |
206 | if (fd != -1) |
207 | { |
208 | /* Do not disturb errno. */ |
209 | int save = errno; |
210 | __close (fd); |
211 | errno = save; |
212 | } |
213 | |
214 | out: |
215 | #ifdef __libc_ptf_call |
216 | __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); |
217 | #endif |
218 | |
219 | return result; |
220 | } |
221 | #if PTHREAD_IN_LIBC |
222 | versioned_symbol (libc, __sem_open, sem_open, GLIBC_2_34); |
223 | # if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1_1, GLIBC_2_34) |
224 | compat_symbol (libpthread, __sem_open, sem_open, GLIBC_2_1_1); |
225 | # endif |
226 | #else /* !PTHREAD_IN_LIBC */ |
227 | strong_alias (__sem_open, sem_open) |
228 | #endif |
229 | |