2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
33 * posix_sem.c : Support for POSIX semaphore APIs
36 * Author: Ananthakrishna Ramesh
44 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
45 * support for mandatory and extensible security protections. This notice
46 * is included in support of clause 2.2 (b) of the Apple Public License,
50 #include <sys/cdefs.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/file_internal.h>
55 #include <sys/filedesc.h>
57 #include <sys/proc_internal.h>
58 #include <sys/kauth.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/vnode.h>
62 #include <sys/ioctl.h>
64 #include <sys/malloc.h>
65 #include <sys/semaphore.h>
66 #include <sys/sysproto.h>
67 #include <sys/proc_info.h>
70 #include <sys/vnode_internal.h>
71 #include <security/mac_framework.h>
74 #include <security/audit/audit.h>
76 #include <mach/mach_types.h>
77 #include <mach/vm_prot.h>
78 #include <mach/semaphore.h>
79 #include <mach/sync_policy.h>
80 #include <mach/task.h>
81 #include <kern/kern_types.h>
82 #include <kern/task.h>
83 #include <kern/clock.h>
84 #include <mach/kern_return.h>
86 #define f_flag fp_glob->fg_flag
87 #define f_ops fp_glob->fg_ops
88 #define f_data fp_glob->fg_data
90 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
93 unsigned int psem_flags
;
94 unsigned int psem_usecount
;
98 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
99 semaphore_t psem_semobject
;
100 struct label
* psem_label
;
101 pid_t psem_creator_pid
;
102 uint64_t psem_creator_uniqueid
;
104 #define PSEMINFO_NULL (struct pseminfo *)0
107 #define PSEM_DEFINED 2
108 #define PSEM_ALLOCATED 4
109 #define PSEM_MAPPED 8
110 #define PSEM_INUSE 0x10
111 #define PSEM_REMOVED 0x20
112 #define PSEM_INCREATE 0x40
113 #define PSEM_INDELETE 0x80
116 LIST_ENTRY(psemcache
) psem_hash
; /* hash chain */
117 struct pseminfo
*pseminfo
; /* vnode the name refers to */
118 size_t psem_nlen
; /* length of name */
119 char psem_name
[PSEMNAMLEN
+ 1]; /* segment name */
121 #define PSEMCACHE_NULL (struct psemcache *)0
123 #define PSEMCACHE_NOTFOUND (0)
124 #define PSEMCACHE_FOUND (-1)
125 #define PSEMCACHE_NEGATIVE (ENOENT)
128 long goodhits
; /* hits that we can really use */
129 long neghits
; /* negative hits that we can use */
130 long badhits
; /* hits we must drop */
131 long falsehits
; /* hits with id mismatch */
132 long miss
; /* misses */
133 long longnames
; /* long names that ignore cache */
137 char *psem_nameptr
; /* pointer to looked up name */
138 size_t psem_namelen
; /* length of looked up component */
139 u_int32_t psem_hash
; /* hash value of looked up name */
143 struct pseminfo
*pinfo
;
145 unsigned int readcnt
;
146 unsigned int writecnt
;
149 #define PSEMNODE_NULL (struct psemnode *)0
152 #define PSEMHASH(pnp) \
153 (&psemhashtbl[(pnp)->psem_hash & psemhash])
154 LIST_HEAD(psemhashhead
, psemcache
) * psemhashtbl
; /* Hash Table */
155 u_long psemhash
; /* size of hash table - 1 */
156 long psemnument
; /* number of cache entries allocated */
157 long posix_sem_max
= 10000; /* tunable for max POSIX semaphores */
158 /* 10000 limits to ~1M of memory */
159 SYSCTL_NODE(_kern
, KERN_POSIX
, posix
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Posix");
160 SYSCTL_NODE(_kern_posix
, OID_AUTO
, sem
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Semaphores");
161 SYSCTL_LONG(_kern_posix_sem
, OID_AUTO
, max
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &posix_sem_max
, "max");
163 struct psemstats psemstats
; /* cache effectiveness statistics */
165 static int psem_access(struct pseminfo
*pinfo
, mode_t mode
, kauth_cred_t cred
);
166 static int psem_cache_search(struct pseminfo
**,
167 struct psemname
*, struct psemcache
**);
168 static int psem_delete(struct pseminfo
* pinfo
);
170 static int psem_closefile(struct fileglob
*fp
, vfs_context_t ctx
);
171 static int psem_unlink_internal(struct pseminfo
*pinfo
, struct psemcache
*pcache
);
173 static const struct fileops psemops
= {
174 .fo_type
= DTYPE_PSXSEM
,
175 .fo_read
= fo_no_read
,
176 .fo_write
= fo_no_write
,
177 .fo_ioctl
= fo_no_ioctl
,
178 .fo_select
= fo_no_select
,
179 .fo_close
= psem_closefile
,
180 .fo_drain
= fo_no_drain
,
181 .fo_kqfilter
= fo_no_kqfilter
,
184 static LCK_GRP_DECLARE(psx_sem_subsys_lck_grp
, "posix semaphores");
185 static LCK_MTX_DECLARE(psx_sem_subsys_mutex
, &psx_sem_subsys_lck_grp
);
187 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(&psx_sem_subsys_mutex)
188 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(&psx_sem_subsys_mutex)
189 #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED)
192 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
193 static void psem_cache_delete(struct psemcache
*pcp
);
194 int psem_cache_purge_all(proc_t
);
197 * Lookup an entry in the cache
200 * status of -1 is returned if matches
201 * If the lookup determines that the name does not exist
202 * (negative cacheing), a status of ENOENT is returned. If the lookup
203 * fails, a status of zero is returned.
207 psem_cache_search(struct pseminfo
**psemp
, struct psemname
*pnp
,
208 struct psemcache
**pcache
)
210 struct psemcache
*pcp
, *nnp
;
211 struct psemhashhead
*pcpp
;
213 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
214 psemstats
.longnames
++;
215 return PSEMCACHE_NOTFOUND
;
218 pcpp
= PSEMHASH(pnp
);
219 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
220 nnp
= pcp
->psem_hash
.le_next
;
221 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
222 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, pcp
->psem_nlen
)) {
229 return PSEMCACHE_NOTFOUND
;
232 /* We found a "positive" match, return the vnode */
234 psemstats
.goodhits
++;
236 *psemp
= pcp
->pseminfo
;
238 return PSEMCACHE_FOUND
;
242 * We found a "negative" match, ENOENT notifies client of this match.
243 * The nc_vpid field records whether this is a whiteout.
246 return PSEMCACHE_NEGATIVE
;
250 * Add an entry to the cache.
253 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
255 struct psemhashhead
*pcpp
;
256 struct pseminfo
*dpinfo
;
257 struct psemcache
*dpcp
;
260 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
261 panic("cache_enter: name too long");
266 /* if the entry has already been added by some one else return */
267 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == PSEMCACHE_FOUND
) {
270 if (psemnument
>= posix_sem_max
) {
275 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
276 * For negative entries, we have to record whether it is a whiteout.
277 * the whiteout flag is stored in the nc_vpid field which is
280 pcp
->pseminfo
= psemp
;
281 pcp
->psem_nlen
= pnp
->psem_namelen
;
282 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, pcp
->psem_nlen
);
283 pcpp
= PSEMHASH(pnp
);
288 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
) {
290 panic("psem:cache_enter duplicate");
295 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
300 * Name cache initialization, from vfs_init() when we are booting
303 psem_cache_init(void)
305 psemhashtbl
= hashinit((int)(posix_sem_max
/ 2), M_SHM
, &psemhash
);
309 psem_cache_delete(struct psemcache
*pcp
)
312 if (pcp
->psem_hash
.le_prev
== 0) {
313 panic("psem namecache purge le_prev");
315 if (pcp
->psem_hash
.le_next
== pcp
) {
316 panic("namecache purge le_next");
318 #endif /* DIAGNOSTIC */
319 LIST_REMOVE(pcp
, psem_hash
);
320 pcp
->psem_hash
.le_prev
= NULL
;
325 * Remove all cached psem entries. Open semaphores (with a positive refcount)
326 * will continue to exist, but their cache entries tying them to a particular
327 * name/path will be removed making all future lookups on the name fail.
330 psem_cache_purge_all(__unused proc_t p
)
332 struct psemcache
*pcp
, *tmppcp
;
333 struct psemhashhead
*pcpp
;
336 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
341 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
342 LIST_FOREACH_SAFE(pcp
, pcpp
, psem_hash
, tmppcp
) {
343 assert(pcp
->psem_nlen
);
345 * unconditionally unlink the cache entry
347 error
= psem_unlink_internal(pcp
->pseminfo
, pcp
);
353 assert(psemnument
== 0);
356 PSEM_SUBSYS_UNLOCK();
359 printf("%s: Error %d removing all semaphores: %ld remain!\n",
360 __func__
, error
, psemnument
);
366 * In order to support unnamed POSIX semaphores, the named
367 * POSIX semaphores will have to move out of the per-process
368 * open filetable, and into a global table that is shared with
369 * unnamed POSIX semaphores, since unnamed POSIX semaphores
370 * are typically used by declaring instances in shared memory,
371 * and there's no other way to do this without changing the
372 * underlying type, which would introduce binary compatibility
376 sem_open(proc_t p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
381 struct pseminfo
*pinfo
;
382 struct fileproc
*fp
= NULL
;
384 struct pseminfo
*new_pinfo
= PSEMINFO_NULL
;
385 struct psemnode
*new_pnode
= PSEMNODE_NULL
;
386 struct psemcache
*pcache
= PSEMCACHE_NULL
;
389 size_t pathlen
, plen
;
391 mode_t cmode
= (mode_t
)uap
->mode
;
392 int value
= uap
->value
;
394 struct psemcache
*pcp
= PSEMCACHE_NULL
;
395 kern_return_t kret
= KERN_INVALID_ADDRESS
; /* default fail */
397 AUDIT_ARG(fflags
, uap
->oflag
);
398 AUDIT_ARG(mode
, (mode_t
)uap
->mode
);
399 AUDIT_ARG(value32
, uap
->value
);
401 pinfo
= PSEMINFO_NULL
;
404 * Preallocate everything we might need up front to avoid taking
405 * and dropping the lock, opening us up to race conditions.
407 pnbuf
= zalloc_flags(ZV_NAMEI
, Z_WAITOK
| Z_ZERO
);
409 pathlen
= MAXPATHLEN
;
410 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
414 AUDIT_ARG(text
, pnbuf
);
415 if ((pathlen
> PSEMNAMLEN
)) {
416 error
= ENAMETOOLONG
;
420 #ifdef PSXSEM_NAME_RESTRICT
422 if (*nameptr
== '/') {
423 while (*(nameptr
++) == '/') {
432 #endif /* PSXSEM_NAME_RESTRICT */
436 nd
.psem_nameptr
= nameptr
;
437 nd
.psem_namelen
= plen
;
440 for (cp
= nameptr
, i
= 1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
441 nd
.psem_hash
+= (unsigned char)*cp
* i
;
445 * attempt to allocate a new fp; if unsuccessful, the fp will be
446 * left unmodified (NULL).
448 error
= falloc(p
, &fp
, &indx
, vfs_context_current());
454 * We allocate a new entry if we are less than the maximum
455 * allowed and the one at the front of the LRU list is in use.
456 * Otherwise we use the one at the front of the LRU list.
458 pcp
= kheap_alloc(KM_SHM
, sizeof(struct psemcache
), Z_WAITOK
| Z_ZERO
);
459 if (pcp
== PSEMCACHE_NULL
) {
464 new_pinfo
= kheap_alloc(KM_SHM
, sizeof(struct pseminfo
), Z_WAITOK
| Z_ZERO
);
465 if (new_pinfo
== NULL
) {
470 mac_posixsem_label_init(new_pinfo
);
474 * Provisionally create the semaphore in the new_pinfo; we have to do
475 * this here to prevent locking later. We use the value of kret to
476 * signal success or failure, which is why we set its default value
477 * to KERN_INVALID_ADDRESS, above.
480 fmode
= (mode_t
)FFLAGS(uap
->oflag
);
482 if ((fmode
& O_CREAT
)) {
483 if ((value
< 0) || (value
> SEM_VALUE_MAX
)) {
488 kret
= semaphore_create(kernel_task
, &new_pinfo
->psem_semobject
, SYNC_POLICY_FIFO
, value
);
490 if (kret
!= KERN_SUCCESS
) {
492 case KERN_RESOURCE_SHORTAGE
:
495 case KERN_PROTECTION_FAILURE
:
505 new_pnode
= kheap_alloc(KM_SHM
, sizeof(struct psemnode
), Z_WAITOK
| Z_ZERO
);
506 if (new_pnode
== NULL
) {
512 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
514 if (error
== PSEMCACHE_NEGATIVE
) {
519 if (error
== PSEMCACHE_FOUND
) {
527 if (((fmode
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)) && incache
) {
528 /* sem exists and opened O_EXCL */
530 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
533 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
534 pinfo
->psem_gid
, pinfo
->psem_mode
);
538 if (((fmode
& (O_CREAT
| O_EXCL
)) == O_CREAT
) && incache
) {
539 /* As per POSIX, O_CREAT has no effect */
543 if ((fmode
& O_CREAT
)) {
544 /* create a new one (commit the allocation) */
546 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
547 pinfo
->psem_usecount
= 1;
548 pinfo
->psem_mode
= cmode
;
549 pinfo
->psem_uid
= kauth_getuid();
550 pinfo
->psem_gid
= kauth_getgid();
551 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
552 pinfo
->psem_name
[PSEMNAMLEN
] = 0;
553 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
554 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
555 pinfo
->psem_creator_pid
= p
->p_pid
;
556 pinfo
->psem_creator_uniqueid
= p
->p_uniqueid
;
559 error
= mac_posixsem_check_create(kauth_cred_get(), nameptr
);
563 mac_posixsem_label_associate(kauth_cred_get(), pinfo
, nameptr
);
566 /* semaphore should exist as it is without O_CREAT */
571 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
575 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
576 pinfo
->psem_gid
, pinfo
->psem_mode
);
578 error
= mac_posixsem_check_open(kauth_cred_get(), pinfo
);
583 if ((error
= psem_access(pinfo
, fmode
, kauth_cred_get()))) {
589 /* if successful, this will consume the pcp */
590 if ((error
= psem_cache_add(pinfo
, &nd
, pcp
))) {
594 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
595 pinfo
->psem_usecount
++;
596 new_pnode
->pinfo
= pinfo
;
597 PSEM_SUBSYS_UNLOCK();
600 * if incache, we did not use the new pcp or the new pcp or the
601 * new . and we must free them.
604 kheap_free(KM_SHM
, pcp
, sizeof(struct psemcache
));
605 pcp
= PSEMCACHE_NULL
;
606 if (new_pinfo
!= PSEMINFO_NULL
) {
607 /* return value ignored - we can't _not_ do this */
608 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
610 mac_posixsem_label_destroy(new_pinfo
);
612 kheap_free(KM_SHM
, new_pinfo
, sizeof(struct pseminfo
));
613 new_pinfo
= PSEMINFO_NULL
;
618 fp
->f_flag
= fmode
& FMASK
;
619 fp
->f_ops
= &psemops
;
620 fp
->f_data
= (caddr_t
)new_pnode
;
621 procfdtbl_releasefd(p
, indx
, NULL
);
622 fp_drop(p
, indx
, fp
, 1);
625 *retval
= CAST_USER_ADDR_T(indx
);
626 zfree(ZV_NAMEI
, pnbuf
);
630 PSEM_SUBSYS_UNLOCK();
632 kheap_free(KM_SHM
, pcp
, sizeof(struct psemcache
));
634 kheap_free(KM_SHM
, new_pnode
, sizeof(struct psemnode
));
637 fp_free(p
, indx
, fp
);
640 if (new_pinfo
!= PSEMINFO_NULL
) {
642 * kret signals whether or not we successfully created a
643 * Mach semaphore for this semaphore; if so, we need to
646 if (kret
== KERN_SUCCESS
) {
647 /* return value ignored - we can't _not_ do this */
648 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
651 mac_posixsem_label_destroy(new_pinfo
);
653 kheap_free(KM_SHM
, new_pinfo
, sizeof(struct pseminfo
));
657 zfree(ZV_NAMEI
, pnbuf
);
663 * XXX This code is repeated in several places
666 psem_access(struct pseminfo
*pinfo
, mode_t mode
, kauth_cred_t cred
)
668 mode_t mode_req
= ((mode
& FREAD
) ? S_IRUSR
: 0) |
669 ((mode
& FWRITE
) ? S_IWUSR
: 0);
671 /* Otherwise, user id 0 always gets access. */
672 if (!suser(cred
, NULL
)) {
676 return posix_cred_access(cred
, pinfo
->psem_uid
, pinfo
->psem_gid
, pinfo
->psem_mode
, mode_req
);
680 psem_unlink_internal(struct pseminfo
*pinfo
, struct psemcache
*pcache
)
682 PSEM_SUBSYS_ASSERT_HELD();
684 if (!pinfo
|| !pcache
) {
688 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
)) == 0) {
692 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
696 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
699 pinfo
->psem_flags
|= PSEM_INDELETE
;
700 pinfo
->psem_usecount
--;
702 if (!pinfo
->psem_usecount
) {
704 kheap_free(KM_SHM
, pinfo
, sizeof(struct pseminfo
));
706 pinfo
->psem_flags
|= PSEM_REMOVED
;
709 psem_cache_delete(pcache
);
710 kheap_free(KM_SHM
, pcache
, sizeof(struct psemcache
));
716 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused
int32_t *retval
)
721 struct pseminfo
*pinfo
;
726 struct psemcache
*pcache
= PSEMCACHE_NULL
;
728 pinfo
= PSEMINFO_NULL
;
730 pnbuf
= zalloc(ZV_NAMEI
);
732 pathlen
= MAXPATHLEN
;
733 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
737 AUDIT_ARG(text
, pnbuf
);
738 if (pathlen
> PSEMNAMLEN
) {
739 error
= ENAMETOOLONG
;
745 #ifdef PSXSEM_NAME_RESTRICT
746 if (*nameptr
== '/') {
747 while (*(nameptr
++) == '/') {
756 #endif /* PSXSEM_NAME_RESTRICT */
758 nd
.psem_nameptr
= nameptr
;
759 nd
.psem_namelen
= pathlen
;
762 for (cp
= nameptr
, i
= 1; *cp
!= 0 && i
<= pathlen
; i
++, cp
++) {
763 nd
.psem_hash
+= (unsigned char)*cp
* i
;
767 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
769 if (error
!= PSEMCACHE_FOUND
) {
770 PSEM_SUBSYS_UNLOCK();
776 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
778 PSEM_SUBSYS_UNLOCK();
782 if ((error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get()))) {
783 PSEM_SUBSYS_UNLOCK();
787 error
= psem_unlink_internal(pinfo
, pcache
);
788 PSEM_SUBSYS_UNLOCK();
791 zfree(ZV_NAMEI
, pnbuf
);
796 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused
int32_t *retval
)
798 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
801 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
804 if ((fp
= fp_get_noref_locked(p
, fd
)) == NULL
) {
808 if (FILEGLOB_DTYPE(fp
->fp_glob
) != DTYPE_PSXSEM
) {
812 return fp_close_and_unlock(p
, fd
, fp
, 0);
816 sem_wait(proc_t p
, struct sem_wait_args
*uap
, int32_t *retval
)
818 __pthread_testcancel(1);
819 return sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
);
823 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused
int32_t *retval
)
825 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
827 struct pseminfo
* pinfo
;
828 struct psemnode
* pnode
;
832 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
836 pnode
= (struct psemnode
*)fp
->f_data
;
839 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
840 PSEM_SUBSYS_UNLOCK();
844 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
846 PSEM_SUBSYS_UNLOCK();
851 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
853 PSEM_SUBSYS_UNLOCK();
857 PSEM_SUBSYS_UNLOCK();
858 kret
= semaphore_wait(pinfo
->psem_semobject
);
860 case KERN_INVALID_ADDRESS
:
861 case KERN_PROTECTION_FAILURE
:
865 case KERN_OPERATION_TIMED_OUT
:
876 fp_drop(p
, fd
, fp
, 0);
881 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused
int32_t *retval
)
883 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
885 struct pseminfo
* pinfo
;
886 struct psemnode
* pnode
;
888 mach_timespec_t wait_time
;
891 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
895 pnode
= (struct psemnode
*)fp
->f_data
;
898 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
899 PSEM_SUBSYS_UNLOCK();
903 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
905 PSEM_SUBSYS_UNLOCK();
910 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
912 PSEM_SUBSYS_UNLOCK();
916 PSEM_SUBSYS_UNLOCK();
917 wait_time
.tv_sec
= 0;
918 wait_time
.tv_nsec
= 0;
920 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
922 case KERN_INVALID_ADDRESS
:
923 case KERN_PROTECTION_FAILURE
:
929 case KERN_OPERATION_TIMED_OUT
:
940 fp_drop(p
, fd
, fp
, 0);
945 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused
int32_t *retval
)
947 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
949 struct pseminfo
* pinfo
;
950 struct psemnode
* pnode
;
954 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
958 pnode
= (struct psemnode
*)fp
->f_data
;
961 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
962 PSEM_SUBSYS_UNLOCK();
966 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
968 PSEM_SUBSYS_UNLOCK();
973 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
975 PSEM_SUBSYS_UNLOCK();
979 PSEM_SUBSYS_UNLOCK();
980 kret
= semaphore_signal(pinfo
->psem_semobject
);
982 case KERN_INVALID_ADDRESS
:
983 case KERN_PROTECTION_FAILURE
:
987 case KERN_OPERATION_TIMED_OUT
:
998 fp_drop(p
, fd
, fp
, 0);
1003 psem_close(struct psemnode
*pnode
)
1006 struct pseminfo
*pinfo
;
1009 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
1010 PSEM_SUBSYS_UNLOCK();
1014 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1015 PSEM_SUBSYS_UNLOCK();
1019 if (!pinfo
->psem_usecount
) {
1020 kprintf("negative usecount in psem_close\n");
1022 #endif /* DIAGNOSTIC */
1023 pinfo
->psem_usecount
--;
1025 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1026 PSEM_SUBSYS_UNLOCK();
1027 /* lock dropped as only semaphore is destroyed here */
1028 error
= psem_delete(pinfo
);
1029 kheap_free(KM_SHM
, pinfo
, sizeof(struct pseminfo
));
1031 PSEM_SUBSYS_UNLOCK();
1033 /* subsystem lock is dropped when we get here */
1034 kheap_free(KM_SHM
, pnode
, sizeof(struct psemnode
));
1039 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1042 * Not locked as psem_close is called only from here and is locked
1045 return psem_close((struct psemnode
*)fg
->fg_data
);
1049 psem_delete(struct pseminfo
* pinfo
)
1053 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1055 mac_posixsem_label_destroy(pinfo
);
1059 case KERN_INVALID_ADDRESS
:
1060 case KERN_PROTECTION_FAILURE
:
1063 case KERN_OPERATION_TIMED_OUT
:
1073 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1075 struct pseminfo
*pinfo
;
1076 struct vinfo_stat
*sb
;
1079 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
1080 PSEM_SUBSYS_UNLOCK();
1085 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1086 PSEM_SUBSYS_UNLOCK();
1091 sb
= &info
->psem_stat
;
1092 bzero(sb
, sizeof(struct vinfo_stat
));
1094 sb
->vst_mode
= pinfo
->psem_mode
;
1095 sb
->vst_uid
= pinfo
->psem_uid
;
1096 sb
->vst_gid
= pinfo
->psem_gid
;
1097 sb
->vst_size
= pinfo
->psem_usecount
;
1098 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+ 1);
1100 PSEM_SUBSYS_UNLOCK();
1106 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1108 struct psemnode
*pnode
;
1109 struct pseminfo
*psem
;
1112 pnode
= (struct psemnode
*)fp
->fp_glob
->fg_data
;
1113 if (pnode
!= NULL
) {
1114 psem
= pnode
->pinfo
;
1116 mac_posixsem_vnode_label_associate(
1117 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1121 PSEM_SUBSYS_UNLOCK();