]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/gen/sem_new.c
Merge MAP_GUARD.
[FreeBSD/stable/10.git] / lib / libc / gen / sem_new.c
1 /*
2  * Copyright (C) 2010 David Xu <davidxu@freebsd.org>.
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice(s), this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified other than the possible
11  *    addition of one or more copyright notices.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice(s), this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  * 
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include "namespace.h"
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <machine/atomic.h>
39 #include <sys/umtx.h>
40 #include <limits.h>
41 #include <fcntl.h>
42 #include <pthread.h>
43 #include <stdarg.h>
44 #include <stdbool.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48 #include <semaphore.h>
49 #include <unistd.h>
50 #include "un-namespace.h"
51 #include "libc_private.h"
52
53 __weak_reference(_sem_close, sem_close);
54 __weak_reference(_sem_destroy, sem_destroy);
55 __weak_reference(_sem_getvalue, sem_getvalue);
56 __weak_reference(_sem_init, sem_init);
57 __weak_reference(_sem_open, sem_open);
58 __weak_reference(_sem_post, sem_post);
59 __weak_reference(_sem_timedwait, sem_timedwait);
60 __weak_reference(_sem_trywait, sem_trywait);
61 __weak_reference(_sem_unlink, sem_unlink);
62 __weak_reference(_sem_wait, sem_wait);
63
64 #define SEM_PREFIX      "/tmp/SEMD"
65 #define SEM_MAGIC       ((u_int32_t)0x73656d31)
66
67 struct sem_nameinfo {
68         int open_count;
69         char *name;
70         dev_t dev;
71         ino_t ino;
72         sem_t *sem;
73         LIST_ENTRY(sem_nameinfo) next;
74 };
75
76 static pthread_once_t once = PTHREAD_ONCE_INIT;
77 static pthread_mutex_t sem_llock;
78 static LIST_HEAD(, sem_nameinfo) sem_list = LIST_HEAD_INITIALIZER(sem_list);
79
80 static void
81 sem_prefork(void)
82 {
83         
84         _pthread_mutex_lock(&sem_llock);
85 }
86
87 static void
88 sem_postfork(void)
89 {
90
91         _pthread_mutex_unlock(&sem_llock);
92 }
93
94 static void
95 sem_child_postfork(void)
96 {
97
98         _pthread_mutex_unlock(&sem_llock);
99 }
100
101 static void
102 sem_module_init(void)
103 {
104
105         _pthread_mutex_init(&sem_llock, NULL);
106         _pthread_atfork(sem_prefork, sem_postfork, sem_child_postfork);
107 }
108
109 static inline int
110 sem_check_validity(sem_t *sem)
111 {
112
113         if (sem->_magic == SEM_MAGIC)
114                 return (0);
115         errno = EINVAL;
116         return (-1);
117 }
118
119 int
120 _sem_init(sem_t *sem, int pshared, unsigned int value)
121 {
122
123         if (value > SEM_VALUE_MAX) {
124                 errno = EINVAL;
125                 return (-1);
126         }
127  
128         bzero(sem, sizeof(sem_t));
129         sem->_magic = SEM_MAGIC;
130         sem->_kern._count = (u_int32_t)value;
131         sem->_kern._has_waiters = 0;
132         sem->_kern._flags = pshared ? USYNC_PROCESS_SHARED : 0;
133         return (0);
134 }
135
136 sem_t *
137 _sem_open(const char *name, int flags, ...)
138 {
139         char path[PATH_MAX];
140         struct stat sb;
141         va_list ap;
142         struct sem_nameinfo *ni;
143         sem_t *sem, tmp;
144         int errsave, fd, len, mode, value;
145
146         ni = NULL;
147         sem = NULL;
148         fd = -1;
149         value = 0;
150
151         if (name[0] != '/') {
152                 errno = EINVAL;
153                 return (SEM_FAILED);
154         }
155         name++;
156         strcpy(path, SEM_PREFIX);
157         if (strlcat(path, name, sizeof(path)) >= sizeof(path)) {
158                 errno = ENAMETOOLONG;
159                 return (SEM_FAILED);
160         }
161         if (flags & ~(O_CREAT|O_EXCL)) {
162                 errno = EINVAL;
163                 return (SEM_FAILED);
164         }
165         if ((flags & O_CREAT) != 0) {
166                 va_start(ap, flags);
167                 mode = va_arg(ap, int);
168                 value = va_arg(ap, int);
169                 va_end(ap);
170         }
171         fd = -1;
172         _pthread_once(&once, sem_module_init);
173
174         _pthread_mutex_lock(&sem_llock);
175         LIST_FOREACH(ni, &sem_list, next) {
176                 if (ni->name != NULL && strcmp(name, ni->name) == 0) {
177                         fd = _open(path, flags | O_RDWR | O_CLOEXEC |
178                             O_EXLOCK, mode);
179                         if (fd == -1 || _fstat(fd, &sb) == -1) {
180                                 ni = NULL;
181                                 goto error;
182                         }
183                         if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT |
184                             O_EXCL) || ni->dev != sb.st_dev ||
185                             ni->ino != sb.st_ino) {
186                                 ni->name = NULL;
187                                 ni = NULL;
188                                 break;
189                         }
190                         ni->open_count++;
191                         sem = ni->sem;
192                         _pthread_mutex_unlock(&sem_llock);
193                         _close(fd);
194                         return (sem);
195                 }
196         }
197
198         len = sizeof(*ni) + strlen(name) + 1;
199         ni = (struct sem_nameinfo *)malloc(len);
200         if (ni == NULL) {
201                 errno = ENOSPC;
202                 goto error;
203         }
204
205         ni->name = (char *)(ni+1);
206         strcpy(ni->name, name);
207
208         if (fd == -1) {
209                 fd = _open(path, flags | O_RDWR | O_CLOEXEC | O_EXLOCK, mode);
210                 if (fd == -1 || _fstat(fd, &sb) == -1)
211                         goto error;
212         }
213         if (sb.st_size < sizeof(sem_t)) {
214                 tmp._magic = SEM_MAGIC;
215                 tmp._kern._has_waiters = 0;
216                 tmp._kern._count = value;
217                 tmp._kern._flags = USYNC_PROCESS_SHARED | SEM_NAMED;
218                 if (_write(fd, &tmp, sizeof(tmp)) != sizeof(tmp))
219                         goto error;
220         }
221         flock(fd, LOCK_UN);
222         sem = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
223             MAP_SHARED | MAP_NOSYNC, fd, 0);
224         if (sem == MAP_FAILED) {
225                 sem = NULL;
226                 if (errno == ENOMEM)
227                         errno = ENOSPC;
228                 goto error;
229         }
230         if (sem->_magic != SEM_MAGIC) {
231                 errno = EINVAL;
232                 goto error;
233         }
234         ni->open_count = 1;
235         ni->sem = sem;
236         ni->dev = sb.st_dev;
237         ni->ino = sb.st_ino;
238         LIST_INSERT_HEAD(&sem_list, ni, next);
239         _close(fd);
240         _pthread_mutex_unlock(&sem_llock);
241         return (sem);
242
243 error:
244         errsave = errno;
245         if (fd != -1)
246                 _close(fd);
247         if (sem != NULL)
248                 munmap(sem, sizeof(sem_t));
249         free(ni);
250         _pthread_mutex_unlock(&sem_llock);
251         errno = errsave;
252         return (SEM_FAILED);
253 }
254
255 int
256 _sem_close(sem_t *sem)
257 {
258         struct sem_nameinfo *ni;
259         bool last;
260
261         if (sem_check_validity(sem) != 0)
262                 return (-1);
263
264         if (!(sem->_kern._flags & SEM_NAMED)) {
265                 errno = EINVAL;
266                 return (-1);
267         }
268
269         _pthread_once(&once, sem_module_init);
270
271         _pthread_mutex_lock(&sem_llock);
272         LIST_FOREACH(ni, &sem_list, next) {
273                 if (sem == ni->sem) {
274                         last = --ni->open_count == 0;
275                         if (last)
276                                 LIST_REMOVE(ni, next);
277                         _pthread_mutex_unlock(&sem_llock);
278                         if (last) {
279                                 munmap(sem, sizeof(*sem));
280                                 free(ni);
281                         }
282                         return (0);
283                 }
284         }
285         _pthread_mutex_unlock(&sem_llock);
286         errno = EINVAL;
287         return (-1);
288 }
289
290 int
291 _sem_unlink(const char *name)
292 {
293         char path[PATH_MAX];
294
295         if (name[0] != '/') {
296                 errno = ENOENT;
297                 return -1;
298         }
299         name++;
300         strcpy(path, SEM_PREFIX);
301         if (strlcat(path, name, sizeof(path)) >= sizeof(path)) {
302                 errno = ENAMETOOLONG;
303                 return (-1);
304         }
305
306         return (unlink(path));
307 }
308
309 int
310 _sem_destroy(sem_t *sem)
311 {
312
313         if (sem_check_validity(sem) != 0)
314                 return (-1);
315
316         if (sem->_kern._flags & SEM_NAMED) {
317                 errno = EINVAL;
318                 return (-1);
319         }
320         sem->_magic = 0;
321         return (0);
322 }
323
324 int
325 _sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
326 {
327
328         if (sem_check_validity(sem) != 0)
329                 return (-1);
330
331         *sval = (int)sem->_kern._count;
332         return (0);
333 }
334
335 static __inline int
336 usem_wake(struct _usem *sem)
337 {
338
339         return (_umtx_op(sem, UMTX_OP_SEM_WAKE, 0, NULL, NULL));
340 }
341
342 static __inline int
343 usem_wait(struct _usem *sem, const struct timespec *abstime)
344 {
345         struct _umtx_time *tm_p, timeout;
346         size_t tm_size;
347
348         if (abstime == NULL) {
349                 tm_p = NULL;
350                 tm_size = 0;
351         } else {
352                 timeout._clockid = CLOCK_REALTIME;
353                 timeout._flags = UMTX_ABSTIME;
354                 timeout._timeout = *abstime;
355                 tm_p = &timeout;
356                 tm_size = sizeof(timeout);
357         }
358         return _umtx_op(sem, UMTX_OP_SEM_WAIT, 0, 
359                     (void *)tm_size, __DECONST(void*, tm_p));
360 }
361
362 int
363 _sem_trywait(sem_t *sem)
364 {
365         int val;
366
367         if (sem_check_validity(sem) != 0)
368                 return (-1);
369
370         while ((val = sem->_kern._count) > 0) {
371                 if (atomic_cmpset_acq_int(&sem->_kern._count, val, val - 1))
372                         return (0);
373         }
374         errno = EAGAIN;
375         return (-1);
376 }
377
378 int
379 _sem_timedwait(sem_t * __restrict sem,
380         const struct timespec * __restrict abstime)
381 {
382         int val, retval;
383
384         if (sem_check_validity(sem) != 0)
385                 return (-1);
386
387         retval = 0;
388         _pthread_testcancel();
389         for (;;) {
390                 while ((val = sem->_kern._count) > 0) {
391                         if (atomic_cmpset_acq_int(&sem->_kern._count, val, val - 1))
392                                 return (0);
393                 }
394
395                 if (retval) {
396                         _pthread_testcancel();
397                         break;
398                 }
399
400                 /*
401                  * The timeout argument is only supposed to
402                  * be checked if the thread would have blocked.
403                  */
404                 if (abstime != NULL) {
405                         if (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0) {
406                                 errno = EINVAL;
407                                 return (-1);
408                         }
409                 }
410                 _pthread_cancel_enter(1);
411                 retval = usem_wait(&sem->_kern, abstime);
412                 _pthread_cancel_leave(0);
413         }
414         return (retval);
415 }
416
417 int
418 _sem_wait(sem_t *sem)
419 {
420
421         return (_sem_timedwait(sem, NULL));
422 }
423
424 /*
425  * POSIX:
426  * The sem_post() interface is reentrant with respect to signals and may be
427  * invoked from a signal-catching function. 
428  * The implementation does not use lock, so it should be safe.
429  */
430 int
431 _sem_post(sem_t *sem)
432 {
433         unsigned int count;
434
435         if (sem_check_validity(sem) != 0)
436                 return (-1);
437
438         do {
439                 count = sem->_kern._count;
440                 if (count + 1 > SEM_VALUE_MAX) {
441                         errno = EOVERFLOW;
442                         return (-1);
443                 }
444         } while(!atomic_cmpset_rel_int(&sem->_kern._count, count, count+1));
445         (void)usem_wake(&sem->_kern);
446         return (0);
447 }