initial commit
[glibc.git] / fbtl / sem_open.c
1 /* Copyright (C) 2002-2013 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 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <search.h>
23 #include <semaphore.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include "semaphoreP.h"
32 #include <shm-directory.h>
33
34
35 /* Comparison function for search of existing mapping. */
36 int
37 attribute_hidden
38 __sem_search (const void *a, const void *b)
39 {
40 const struct inuse_sem *as = (const struct inuse_sem *) a;
41 const struct inuse_sem *bs = (const struct inuse_sem *) b;
42
43 if (as->ino != bs->ino)
44 /* Cannot return the difference the type is larger than int. */
45 return as->ino < bs->ino ? -1 : (as->ino == bs->ino ? 0 : 1);
46
47 if (as->dev != bs->dev)
48 /* Cannot return the difference the type is larger than int. */
49 return as->dev < bs->dev ? -1 : (as->dev == bs->dev ? 0 : 1);
50
51 return strcmp (as->name, bs->name);
52 }
53
54
55 /* The search tree for existing mappings. */
56 void *__sem_mappings attribute_hidden;
57
58 /* Lock to protect the search tree. */
59 int __sem_mappings_lock attribute_hidden = LLL_LOCK_INITIALIZER;
60
61
62 /* Search for existing mapping and if possible add the one provided. */
63 static sem_t *
64 check_add_mapping (const char *name, size_t namelen, int fd, sem_t *existing)
65 {
66 sem_t *result = SEM_FAILED;
67
68 /* Get the information about the file. */
69 struct stat64 st;
70 if (__fxstat64 (_STAT_VER, fd, &st) == 0)
71 {
72 /* Get the lock. */
73 lll_lock (__sem_mappings_lock, LLL_PRIVATE);
74
75 /* Search for an existing mapping given the information we have. */
76 struct inuse_sem *fake;
77 fake = (struct inuse_sem *) alloca (sizeof (*fake) + namelen);
78 memcpy (fake->name, name, namelen);
79 fake->dev = st.st_dev;
80 fake->ino = st.st_ino;
81
82 struct inuse_sem **foundp = __tfind (fake, &__sem_mappings,
83 __sem_search);
84 if (foundp != NULL)
85 {
86 /* There is already a mapping. Use it. */
87 result = (*foundp)->sem;
88 ++(*foundp)->refcnt;
89 }
90 else
91 {
92 /* We haven't found a mapping. Install ione. */
93 struct inuse_sem *newp;
94
95 newp = (struct inuse_sem *) malloc (sizeof (*newp) + namelen);
96 if (newp != NULL)
97 {
98 /* If the caller hasn't provided any map it now. */
99 if (existing == SEM_FAILED)
100 existing = (sem_t *) mmap (NULL, sizeof (sem_t),
101 PROT_READ | PROT_WRITE, MAP_SHARED,
102 fd, 0);
103
104 newp->dev = st.st_dev;
105 newp->ino = st.st_ino;
106 newp->refcnt = 1;
107 newp->sem = existing;
108 memcpy (newp->name, name, namelen);
109
110 /* Insert the new value. */
111 if (existing != MAP_FAILED
112 && __tsearch (newp, &__sem_mappings, __sem_search) != NULL)
113 /* Successful. */
114 result = existing;
115 else
116 /* Something went wrong while inserting the new
117 value. We fail completely. */
118 free (newp);
119 }
120 }
121
122 /* Release the lock. */
123 lll_unlock (__sem_mappings_lock, LLL_PRIVATE);
124 }
125
126 if (result != existing && existing != SEM_FAILED && existing != MAP_FAILED)
127 {
128 /* Do not disturb errno. */
129 int saved_errno = errno;
130 munmap(existing, sizeof (sem_t));
131 errno = saved_errno;
132 }
133
134 return result;
135 }
136
137
138 sem_t *
139 sem_open (const char *name, int oflag, ...)
140 {
141 int fd;
142 sem_t *result;
143
144 /* Create the name of the final file in local variable SHM_NAME. */
145 SHM_GET_NAME (EINVAL, SEM_FAILED, SEM_SHM_PREFIX);
146
147 /* If the semaphore object has to exist simply open it. */
148 if ((oflag & O_CREAT) == 0 || (oflag & O_EXCL) == 0)
149 {
150 try_again:
151 fd = __libc_open (shm_name,
152 (oflag & ~(O_CREAT|O_ACCMODE)) | O_NOFOLLOW | O_RDWR);
153
154 if (fd == -1)
155 {
156 /* If we are supposed to create the file try this next. */
157 if ((oflag & O_CREAT) != 0 && errno == ENOENT)
158 goto try_create;
159
160 /* Return. errno is already set. */
161 }
162 else
163 /* Check whether we already have this semaphore mapped and
164 create one if necessary. */
165 result = check_add_mapping (name, namelen, fd, SEM_FAILED);
166 }
167 else
168 {
169 /* We have to open a temporary file first since it must have the
170 correct form before we can start using it. */
171 char *tmpfname;
172 mode_t mode;
173 unsigned int value;
174 va_list ap;
175
176 try_create:
177 va_start (ap, oflag);
178
179 #if 0
180 mode = va_arg (ap, mode_t);
181 #else
182 mode = va_arg (ap, int);
183 #endif
184 value = va_arg (ap, unsigned int);
185
186 va_end (ap);
187
188 if (value > SEM_VALUE_MAX)
189 {
190 __set_errno (EINVAL);
191 return SEM_FAILED;
192 }
193
194 /* Create the initial file content. */
195 union
196 {
197 sem_t initsem;
198 struct new_sem newsem;
199 } sem;
200
201 sem.newsem.value = value;
202 sem.newsem.private = 0;
203 sem.newsem.nwaiters = 0;
204
205 /* Initialize the remaining bytes as well. */
206 memset ((char *) &sem.initsem + sizeof (struct new_sem), '\0',
207 sizeof (sem_t) - sizeof (struct new_sem));
208
209 tmpfname = (char *) alloca (shm_dirlen + sizeof SEM_SHM_PREFIX + 6);
210 char *xxxxxx = __mempcpy (tmpfname, shm_dir, shm_dirlen);
211
212 int retries = 0;
213 #define NRETRIES 50
214 while (1)
215 {
216 /* Add the suffix for mktemp. */
217 strcpy (xxxxxx, "XXXXXX");
218
219 /* We really want to use mktemp here. We cannot use mkstemp
220 since the file must be opened with a specific mode. The
221 mode cannot later be set since then we cannot apply the
222 file create mask. */
223 if (__mktemp (tmpfname) == NULL)
224 return SEM_FAILED;
225
226 /* Open the file. Make sure we do not overwrite anything. */
227 fd = __libc_open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
228 if (fd == -1)
229 {
230 if (errno == EEXIST)
231 {
232 if (++retries < NRETRIES)
233 continue;
234
235 __set_errno (EAGAIN);
236 }
237
238 return SEM_FAILED;
239 }
240
241 /* We got a file. */
242 break;
243 }
244
245 if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t)))
246 == sizeof (sem_t)
247 /* Map the sem_t structure from the file. */
248 && (result = (sem_t *) mmap (NULL, sizeof (sem_t),
249 PROT_READ | PROT_WRITE, MAP_SHARED,
250 fd, 0)) != MAP_FAILED)
251 {
252 /* Create the file. Don't overwrite an existing file. */
253 if (link (tmpfname, shm_name) != 0)
254 {
255 /* Undo the mapping. */
256 (void) munmap (result, sizeof (sem_t));
257
258 /* Reinitialize 'result'. */
259 result = SEM_FAILED;
260
261 /* This failed. If O_EXCL is not set and the problem was
262 that the file exists, try again. */
263 if ((oflag & O_EXCL) == 0 && errno == EEXIST)
264 {
265 /* Remove the file. */
266 (void) unlink (tmpfname);
267
268 /* Close the file. */
269 (void) __libc_close (fd);
270
271 goto try_again;
272 }
273 }
274 else
275 /* Insert the mapping into the search tree. This also
276 determines whether another thread sneaked by and already
277 added such a mapping despite the fact that we created it. */
278 result = check_add_mapping (name, namelen, fd, result);
279 }
280
281 /* Now remove the temporary name. This should never fail. If
282 it fails we leak a file name. Better fix the kernel. */
283 (void) unlink (tmpfname);
284 }
285
286 /* Map the mmap error to the error we need. */
287 if (MAP_FAILED != (void *) SEM_FAILED && result == MAP_FAILED)
288 result = SEM_FAILED;
289
290 /* We don't need the file descriptor anymore. */
291 if (fd != -1)
292 {
293 /* Do not disturb errno. */
294 int save = errno;
295 __libc_close (fd);
296 errno = save;
297 }
298
299 return result;
300 }