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_t
*psx_sem_subsys_lck_grp
;
185 static lck_grp_attr_t
*psx_sem_subsys_lck_grp_attr
;
186 static lck_attr_t
*psx_sem_subsys_lck_attr
;
187 static lck_mtx_t psx_sem_subsys_mutex
;
189 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
190 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
191 #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED)
194 static int psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
);
195 static void psem_cache_delete(struct psemcache
*pcp
);
196 int psem_cache_purge_all(proc_t
);
199 /* Initialize the mutex governing access to the posix sem subsystem */
200 __private_extern__
void
201 psem_lock_init( void )
203 psx_sem_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
205 psx_sem_subsys_lck_grp
= lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr
);
207 psx_sem_subsys_lck_attr
= lck_attr_alloc_init();
208 lck_mtx_init(&psx_sem_subsys_mutex
, psx_sem_subsys_lck_grp
, psx_sem_subsys_lck_attr
);
212 * Lookup an entry in the cache
215 * status of -1 is returned if matches
216 * If the lookup determines that the name does not exist
217 * (negative cacheing), a status of ENOENT is returned. If the lookup
218 * fails, a status of zero is returned.
222 psem_cache_search(struct pseminfo
**psemp
, struct psemname
*pnp
,
223 struct psemcache
**pcache
)
225 struct psemcache
*pcp
, *nnp
;
226 struct psemhashhead
*pcpp
;
228 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
229 psemstats
.longnames
++;
230 return PSEMCACHE_NOTFOUND
;
233 pcpp
= PSEMHASH(pnp
);
234 for (pcp
= pcpp
->lh_first
; pcp
!= 0; pcp
= nnp
) {
235 nnp
= pcp
->psem_hash
.le_next
;
236 if (pcp
->psem_nlen
== pnp
->psem_namelen
&&
237 !bcmp(pcp
->psem_name
, pnp
->psem_nameptr
, pcp
->psem_nlen
)) {
244 return PSEMCACHE_NOTFOUND
;
247 /* We found a "positive" match, return the vnode */
249 psemstats
.goodhits
++;
251 *psemp
= pcp
->pseminfo
;
253 return PSEMCACHE_FOUND
;
257 * We found a "negative" match, ENOENT notifies client of this match.
258 * The nc_vpid field records whether this is a whiteout.
261 return PSEMCACHE_NEGATIVE
;
265 * Add an entry to the cache.
268 psem_cache_add(struct pseminfo
*psemp
, struct psemname
*pnp
, struct psemcache
*pcp
)
270 struct psemhashhead
*pcpp
;
271 struct pseminfo
*dpinfo
;
272 struct psemcache
*dpcp
;
275 if (pnp
->psem_namelen
> PSEMNAMLEN
) {
276 panic("cache_enter: name too long");
281 /* if the entry has already been added by some one else return */
282 if (psem_cache_search(&dpinfo
, pnp
, &dpcp
) == PSEMCACHE_FOUND
) {
285 if (psemnument
>= posix_sem_max
) {
290 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
291 * For negative entries, we have to record whether it is a whiteout.
292 * the whiteout flag is stored in the nc_vpid field which is
295 pcp
->pseminfo
= psemp
;
296 pcp
->psem_nlen
= pnp
->psem_namelen
;
297 bcopy(pnp
->psem_nameptr
, pcp
->psem_name
, pcp
->psem_nlen
);
298 pcpp
= PSEMHASH(pnp
);
303 for (p
= pcpp
->lh_first
; p
!= 0; p
= p
->psem_hash
.le_next
) {
305 panic("psem:cache_enter duplicate");
310 LIST_INSERT_HEAD(pcpp
, pcp
, psem_hash
);
315 * Name cache initialization, from vfs_init() when we are booting
318 psem_cache_init(void)
320 psemhashtbl
= hashinit((int)(posix_sem_max
/ 2), M_SHM
, &psemhash
);
324 psem_cache_delete(struct psemcache
*pcp
)
327 if (pcp
->psem_hash
.le_prev
== 0) {
328 panic("psem namecache purge le_prev");
330 if (pcp
->psem_hash
.le_next
== pcp
) {
331 panic("namecache purge le_next");
333 #endif /* DIAGNOSTIC */
334 LIST_REMOVE(pcp
, psem_hash
);
335 pcp
->psem_hash
.le_prev
= NULL
;
340 * Remove all cached psem entries. Open semaphores (with a positive refcount)
341 * will continue to exist, but their cache entries tying them to a particular
342 * name/path will be removed making all future lookups on the name fail.
345 psem_cache_purge_all(__unused proc_t p
)
347 struct psemcache
*pcp
, *tmppcp
;
348 struct psemhashhead
*pcpp
;
351 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
356 for (pcpp
= &psemhashtbl
[psemhash
]; pcpp
>= psemhashtbl
; pcpp
--) {
357 LIST_FOREACH_SAFE(pcp
, pcpp
, psem_hash
, tmppcp
) {
358 assert(pcp
->psem_nlen
);
360 * unconditionally unlink the cache entry
362 error
= psem_unlink_internal(pcp
->pseminfo
, pcp
);
368 assert(psemnument
== 0);
371 PSEM_SUBSYS_UNLOCK();
374 printf("%s: Error %d removing all semaphores: %ld remain!\n",
375 __func__
, error
, psemnument
);
381 * In order to support unnamed POSIX semaphores, the named
382 * POSIX semaphores will have to move out of the per-process
383 * open filetable, and into a global table that is shared with
384 * unnamed POSIX semaphores, since unnamed POSIX semaphores
385 * are typically used by declaring instances in shared memory,
386 * and there's no other way to do this without changing the
387 * underlying type, which would introduce binary compatibility
391 sem_open(proc_t p
, struct sem_open_args
*uap
, user_addr_t
*retval
)
396 struct pseminfo
*pinfo
;
397 struct fileproc
*fp
= NULL
;
399 struct pseminfo
*new_pinfo
= PSEMINFO_NULL
;
400 struct psemnode
*new_pnode
= PSEMNODE_NULL
;
401 struct psemcache
*pcache
= PSEMCACHE_NULL
;
404 size_t pathlen
, plen
;
406 mode_t cmode
= (mode_t
)uap
->mode
;
407 int value
= uap
->value
;
409 struct psemcache
*pcp
= PSEMCACHE_NULL
;
410 kern_return_t kret
= KERN_INVALID_ADDRESS
; /* default fail */
412 AUDIT_ARG(fflags
, uap
->oflag
);
413 AUDIT_ARG(mode
, (mode_t
)uap
->mode
);
414 AUDIT_ARG(value32
, uap
->value
);
416 pinfo
= PSEMINFO_NULL
;
419 * Preallocate everything we might need up front to avoid taking
420 * and dropping the lock, opening us up to race conditions.
422 pnbuf
= zalloc_flags(ZV_NAMEI
, Z_WAITOK
| Z_ZERO
);
424 pathlen
= MAXPATHLEN
;
425 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
429 AUDIT_ARG(text
, pnbuf
);
430 if ((pathlen
> PSEMNAMLEN
)) {
431 error
= ENAMETOOLONG
;
435 #ifdef PSXSEM_NAME_RESTRICT
437 if (*nameptr
== '/') {
438 while (*(nameptr
++) == '/') {
447 #endif /* PSXSEM_NAME_RESTRICT */
451 nd
.psem_nameptr
= nameptr
;
452 nd
.psem_namelen
= plen
;
455 for (cp
= nameptr
, i
= 1; *cp
!= 0 && i
<= plen
; i
++, cp
++) {
456 nd
.psem_hash
+= (unsigned char)*cp
* i
;
460 * attempt to allocate a new fp; if unsuccessful, the fp will be
461 * left unmodified (NULL).
463 error
= falloc(p
, &fp
, &indx
, vfs_context_current());
469 * We allocate a new entry if we are less than the maximum
470 * allowed and the one at the front of the LRU list is in use.
471 * Otherwise we use the one at the front of the LRU list.
473 MALLOC(pcp
, struct psemcache
*, sizeof(struct psemcache
), M_SHM
, M_WAITOK
| M_ZERO
);
474 if (pcp
== PSEMCACHE_NULL
) {
479 MALLOC(new_pinfo
, struct pseminfo
*, sizeof(struct pseminfo
), M_SHM
, M_WAITOK
| M_ZERO
);
480 if (new_pinfo
== NULL
) {
485 mac_posixsem_label_init(new_pinfo
);
489 * Provisionally create the semaphore in the new_pinfo; we have to do
490 * this here to prevent locking later. We use the value of kret to
491 * signal success or failure, which is why we set its default value
492 * to KERN_INVALID_ADDRESS, above.
495 fmode
= (mode_t
)FFLAGS(uap
->oflag
);
497 if ((fmode
& O_CREAT
)) {
498 if ((value
< 0) || (value
> SEM_VALUE_MAX
)) {
503 kret
= semaphore_create(kernel_task
, &new_pinfo
->psem_semobject
, SYNC_POLICY_FIFO
, value
);
505 if (kret
!= KERN_SUCCESS
) {
507 case KERN_RESOURCE_SHORTAGE
:
510 case KERN_PROTECTION_FAILURE
:
520 MALLOC(new_pnode
, struct psemnode
*, sizeof(struct psemnode
), M_SHM
, M_WAITOK
| M_ZERO
);
521 if (new_pnode
== NULL
) {
527 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
529 if (error
== PSEMCACHE_NEGATIVE
) {
534 if (error
== PSEMCACHE_FOUND
) {
542 if (((fmode
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)) && incache
) {
543 /* sem exists and opened O_EXCL */
545 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
548 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
549 pinfo
->psem_gid
, pinfo
->psem_mode
);
553 if (((fmode
& (O_CREAT
| O_EXCL
)) == O_CREAT
) && incache
) {
554 /* As per POSIX, O_CREAT has no effect */
558 if ((fmode
& O_CREAT
)) {
559 /* create a new one (commit the allocation) */
561 pinfo
->psem_flags
= PSEM_DEFINED
| PSEM_INCREATE
;
562 pinfo
->psem_usecount
= 1;
563 pinfo
->psem_mode
= cmode
;
564 pinfo
->psem_uid
= kauth_getuid();
565 pinfo
->psem_gid
= kauth_getgid();
566 bcopy(pnbuf
, &pinfo
->psem_name
[0], PSEMNAMLEN
);
567 pinfo
->psem_name
[PSEMNAMLEN
] = 0;
568 pinfo
->psem_flags
&= ~PSEM_DEFINED
;
569 pinfo
->psem_flags
|= PSEM_ALLOCATED
;
570 pinfo
->psem_creator_pid
= p
->p_pid
;
571 pinfo
->psem_creator_uniqueid
= p
->p_uniqueid
;
574 error
= mac_posixsem_check_create(kauth_cred_get(), nameptr
);
578 mac_posixsem_label_associate(kauth_cred_get(), pinfo
, nameptr
);
581 /* semaphore should exist as it is without O_CREAT */
586 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
590 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
,
591 pinfo
->psem_gid
, pinfo
->psem_mode
);
593 error
= mac_posixsem_check_open(kauth_cred_get(), pinfo
);
598 if ((error
= psem_access(pinfo
, fmode
, kauth_cred_get()))) {
604 /* if successful, this will consume the pcp */
605 if ((error
= psem_cache_add(pinfo
, &nd
, pcp
))) {
609 pinfo
->psem_flags
&= ~PSEM_INCREATE
;
610 pinfo
->psem_usecount
++;
611 new_pnode
->pinfo
= pinfo
;
612 PSEM_SUBSYS_UNLOCK();
615 * if incache, we did not use the new pcp or the new pcp or the
616 * new . and we must free them.
620 pcp
= PSEMCACHE_NULL
;
621 if (new_pinfo
!= PSEMINFO_NULL
) {
622 /* return value ignored - we can't _not_ do this */
623 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
625 mac_posixsem_label_destroy(new_pinfo
);
627 FREE(new_pinfo
, M_SHM
);
628 new_pinfo
= PSEMINFO_NULL
;
633 fp
->f_flag
= fmode
& FMASK
;
634 fp
->f_ops
= &psemops
;
635 fp
->f_data
= (caddr_t
)new_pnode
;
636 procfdtbl_releasefd(p
, indx
, NULL
);
637 fp_drop(p
, indx
, fp
, 1);
640 *retval
= CAST_USER_ADDR_T(indx
);
641 zfree(ZV_NAMEI
, pnbuf
);
645 PSEM_SUBSYS_UNLOCK();
647 if (pcp
!= PSEMCACHE_NULL
) {
651 if (new_pnode
!= PSEMNODE_NULL
) {
652 FREE(new_pnode
, M_SHM
);
656 fp_free(p
, indx
, fp
);
659 if (new_pinfo
!= PSEMINFO_NULL
) {
661 * kret signals whether or not we successfully created a
662 * Mach semaphore for this semaphore; if so, we need to
665 if (kret
== KERN_SUCCESS
) {
666 /* return value ignored - we can't _not_ do this */
667 (void)semaphore_destroy(kernel_task
, new_pinfo
->psem_semobject
);
670 mac_posixsem_label_destroy(new_pinfo
);
672 FREE(new_pinfo
, M_SHM
);
676 zfree(ZV_NAMEI
, pnbuf
);
682 * XXX This code is repeated in several places
685 psem_access(struct pseminfo
*pinfo
, mode_t mode
, kauth_cred_t cred
)
687 mode_t mode_req
= ((mode
& FREAD
) ? S_IRUSR
: 0) |
688 ((mode
& FWRITE
) ? S_IWUSR
: 0);
690 /* Otherwise, user id 0 always gets access. */
691 if (!suser(cred
, NULL
)) {
695 return posix_cred_access(cred
, pinfo
->psem_uid
, pinfo
->psem_gid
, pinfo
->psem_mode
, mode_req
);
699 psem_unlink_internal(struct pseminfo
*pinfo
, struct psemcache
*pcache
)
701 PSEM_SUBSYS_ASSERT_HELD();
703 if (!pinfo
|| !pcache
) {
707 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
)) == 0) {
711 if (pinfo
->psem_flags
& PSEM_INDELETE
) {
715 AUDIT_ARG(posix_ipc_perm
, pinfo
->psem_uid
, pinfo
->psem_gid
,
718 pinfo
->psem_flags
|= PSEM_INDELETE
;
719 pinfo
->psem_usecount
--;
721 if (!pinfo
->psem_usecount
) {
725 pinfo
->psem_flags
|= PSEM_REMOVED
;
728 psem_cache_delete(pcache
);
735 sem_unlink(__unused proc_t p
, struct sem_unlink_args
*uap
, __unused
int32_t *retval
)
740 struct pseminfo
*pinfo
;
745 struct psemcache
*pcache
= PSEMCACHE_NULL
;
747 pinfo
= PSEMINFO_NULL
;
749 pnbuf
= zalloc(ZV_NAMEI
);
751 pathlen
= MAXPATHLEN
;
752 error
= copyinstr(uap
->name
, pnbuf
, MAXPATHLEN
, &pathlen
);
756 AUDIT_ARG(text
, pnbuf
);
757 if (pathlen
> PSEMNAMLEN
) {
758 error
= ENAMETOOLONG
;
764 #ifdef PSXSEM_NAME_RESTRICT
765 if (*nameptr
== '/') {
766 while (*(nameptr
++) == '/') {
775 #endif /* PSXSEM_NAME_RESTRICT */
777 nd
.psem_nameptr
= nameptr
;
778 nd
.psem_namelen
= pathlen
;
781 for (cp
= nameptr
, i
= 1; *cp
!= 0 && i
<= pathlen
; i
++, cp
++) {
782 nd
.psem_hash
+= (unsigned char)*cp
* i
;
786 error
= psem_cache_search(&pinfo
, &nd
, &pcache
);
788 if (error
!= PSEMCACHE_FOUND
) {
789 PSEM_SUBSYS_UNLOCK();
795 error
= mac_posixsem_check_unlink(kauth_cred_get(), pinfo
, nameptr
);
797 PSEM_SUBSYS_UNLOCK();
801 if ((error
= psem_access(pinfo
, pinfo
->psem_mode
, kauth_cred_get()))) {
802 PSEM_SUBSYS_UNLOCK();
806 error
= psem_unlink_internal(pinfo
, pcache
);
807 PSEM_SUBSYS_UNLOCK();
810 zfree(ZV_NAMEI
, pnbuf
);
815 sem_close(proc_t p
, struct sem_close_args
*uap
, __unused
int32_t *retval
)
817 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
820 AUDIT_ARG(fd
, fd
); /* XXX This seems wrong; uap->sem is a pointer */
823 if ((fp
= fp_get_noref_locked(p
, fd
)) == NULL
) {
827 if (FILEGLOB_DTYPE(fp
->fp_glob
) != DTYPE_PSXSEM
) {
831 return fp_close_and_unlock(p
, fd
, fp
, 0);
835 sem_wait(proc_t p
, struct sem_wait_args
*uap
, int32_t *retval
)
837 __pthread_testcancel(1);
838 return sem_wait_nocancel(p
, (struct sem_wait_nocancel_args
*)uap
, retval
);
842 sem_wait_nocancel(proc_t p
, struct sem_wait_nocancel_args
*uap
, __unused
int32_t *retval
)
844 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
846 struct pseminfo
* pinfo
;
847 struct psemnode
* pnode
;
851 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
855 pnode
= (struct psemnode
*)fp
->f_data
;
858 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
859 PSEM_SUBSYS_UNLOCK();
863 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
865 PSEM_SUBSYS_UNLOCK();
870 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
872 PSEM_SUBSYS_UNLOCK();
876 PSEM_SUBSYS_UNLOCK();
877 kret
= semaphore_wait(pinfo
->psem_semobject
);
879 case KERN_INVALID_ADDRESS
:
880 case KERN_PROTECTION_FAILURE
:
884 case KERN_OPERATION_TIMED_OUT
:
895 fp_drop(p
, fd
, fp
, 0);
900 sem_trywait(proc_t p
, struct sem_trywait_args
*uap
, __unused
int32_t *retval
)
902 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
904 struct pseminfo
* pinfo
;
905 struct psemnode
* pnode
;
907 mach_timespec_t wait_time
;
910 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
914 pnode
= (struct psemnode
*)fp
->f_data
;
917 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
918 PSEM_SUBSYS_UNLOCK();
922 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
924 PSEM_SUBSYS_UNLOCK();
929 error
= mac_posixsem_check_wait(kauth_cred_get(), pinfo
);
931 PSEM_SUBSYS_UNLOCK();
935 PSEM_SUBSYS_UNLOCK();
936 wait_time
.tv_sec
= 0;
937 wait_time
.tv_nsec
= 0;
939 kret
= semaphore_timedwait(pinfo
->psem_semobject
, MACH_TIMESPEC_ZERO
);
941 case KERN_INVALID_ADDRESS
:
942 case KERN_PROTECTION_FAILURE
:
948 case KERN_OPERATION_TIMED_OUT
:
959 fp_drop(p
, fd
, fp
, 0);
964 sem_post(proc_t p
, struct sem_post_args
*uap
, __unused
int32_t *retval
)
966 int fd
= CAST_DOWN_EXPLICIT(int, uap
->sem
);
968 struct pseminfo
* pinfo
;
969 struct psemnode
* pnode
;
973 error
= fp_get_ftype(p
, fd
, DTYPE_PSXSEM
, EBADF
, &fp
);
977 pnode
= (struct psemnode
*)fp
->f_data
;
980 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
981 PSEM_SUBSYS_UNLOCK();
985 if ((pinfo
->psem_flags
& (PSEM_DEFINED
| PSEM_ALLOCATED
))
987 PSEM_SUBSYS_UNLOCK();
992 error
= mac_posixsem_check_post(kauth_cred_get(), pinfo
);
994 PSEM_SUBSYS_UNLOCK();
998 PSEM_SUBSYS_UNLOCK();
999 kret
= semaphore_signal(pinfo
->psem_semobject
);
1001 case KERN_INVALID_ADDRESS
:
1002 case KERN_PROTECTION_FAILURE
:
1006 case KERN_OPERATION_TIMED_OUT
:
1017 fp_drop(p
, fd
, fp
, 0);
1022 psem_close(struct psemnode
*pnode
)
1025 struct pseminfo
*pinfo
;
1028 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
1029 PSEM_SUBSYS_UNLOCK();
1033 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1034 PSEM_SUBSYS_UNLOCK();
1038 if (!pinfo
->psem_usecount
) {
1039 kprintf("negative usecount in psem_close\n");
1041 #endif /* DIAGNOSTIC */
1042 pinfo
->psem_usecount
--;
1044 if ((pinfo
->psem_flags
& PSEM_REMOVED
) && !pinfo
->psem_usecount
) {
1045 PSEM_SUBSYS_UNLOCK();
1046 /* lock dropped as only semaphore is destroyed here */
1047 error
= psem_delete(pinfo
);
1050 PSEM_SUBSYS_UNLOCK();
1052 /* subsystem lock is dropped when we get here */
1058 psem_closefile(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1061 * Not locked as psem_close is called only from here and is locked
1064 return psem_close((struct psemnode
*)fg
->fg_data
);
1068 psem_delete(struct pseminfo
* pinfo
)
1072 kret
= semaphore_destroy(kernel_task
, pinfo
->psem_semobject
);
1074 mac_posixsem_label_destroy(pinfo
);
1078 case KERN_INVALID_ADDRESS
:
1079 case KERN_PROTECTION_FAILURE
:
1082 case KERN_OPERATION_TIMED_OUT
:
1092 fill_pseminfo(struct psemnode
*pnode
, struct psem_info
* info
)
1094 struct pseminfo
*pinfo
;
1095 struct vinfo_stat
*sb
;
1098 if ((pinfo
= pnode
->pinfo
) == PSEMINFO_NULL
) {
1099 PSEM_SUBSYS_UNLOCK();
1104 if ((pinfo
->psem_flags
& PSEM_ALLOCATED
) != PSEM_ALLOCATED
) {
1105 PSEM_SUBSYS_UNLOCK();
1110 sb
= &info
->psem_stat
;
1111 bzero(sb
, sizeof(struct vinfo_stat
));
1113 sb
->vst_mode
= pinfo
->psem_mode
;
1114 sb
->vst_uid
= pinfo
->psem_uid
;
1115 sb
->vst_gid
= pinfo
->psem_gid
;
1116 sb
->vst_size
= pinfo
->psem_usecount
;
1117 bcopy(&pinfo
->psem_name
[0], &info
->psem_name
[0], PSEMNAMLEN
+ 1);
1119 PSEM_SUBSYS_UNLOCK();
1125 psem_label_associate(struct fileproc
*fp
, struct vnode
*vp
, vfs_context_t ctx
)
1127 struct psemnode
*pnode
;
1128 struct pseminfo
*psem
;
1131 pnode
= (struct psemnode
*)fp
->fp_glob
->fg_data
;
1132 if (pnode
!= NULL
) {
1133 psem
= pnode
->pinfo
;
1135 mac_posixsem_vnode_label_associate(
1136 vfs_context_ucred(ctx
), psem
, psem
->psem_label
,
1140 PSEM_SUBSYS_UNLOCK();