]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/posix_sem.c
b8438447f4832adbb5aeac21c4617228df92de26
[apple/xnu.git] / bsd / kern / posix_sem.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
25 * All Rights Reserved.
26 */
27 /*
28 * posix_shm.c : Support for POSIX semaphore APIs
29 *
30 * File: posix_sem.c
31 * Author: Ananthakrishna Ramesh
32 *
33 * HISTORY
34 * 2-Sep-1999 A.Ramesh
35 * Created for MacOSX
36 *
37 */
38
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file_internal.h>
44 #include <sys/filedesc.h>
45 #include <sys/stat.h>
46 #include <sys/proc_internal.h>
47 #include <sys/kauth.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/malloc.h>
54 #include <sys/semaphore.h>
55 #include <sys/sysproto.h>
56
57 #include <bsm/audit_kernel.h>
58
59 #include <mach/mach_types.h>
60 #include <mach/vm_prot.h>
61 #include <mach/semaphore.h>
62 #include <mach/sync_policy.h>
63 #include <mach/task.h>
64 #include <kern/kern_types.h>
65 #include <kern/task.h>
66 #include <kern/clock.h>
67 #include <mach/kern_return.h>
68
69 #if KTRACE
70 #include <sys/ktrace.h>
71 #endif
72
73 #define f_flag f_fglob->fg_flag
74 #define f_type f_fglob->fg_type
75 #define f_msgcount f_fglob->fg_msgcount
76 #define f_cred f_fglob->fg_cred
77 #define f_ops f_fglob->fg_ops
78 #define f_offset f_fglob->fg_offset
79 #define f_data f_fglob->fg_data
80 #define PSEMNAMLEN 31 /* maximum name segment length we bother with */
81
82 struct pseminfo {
83 unsigned int psem_flags;
84 unsigned int psem_usecount;
85 mode_t psem_mode;
86 uid_t psem_uid;
87 gid_t psem_gid;
88 char psem_name[PSEMNAMLEN + 1]; /* segment name */
89 semaphore_t psem_semobject;
90 struct proc * sem_proc;
91 };
92 #define PSEMINFO_NULL (struct pseminfo *)0
93
94 #define PSEM_NONE 1
95 #define PSEM_DEFINED 2
96 #define PSEM_ALLOCATED 4
97 #define PSEM_MAPPED 8
98 #define PSEM_INUSE 0x10
99 #define PSEM_REMOVED 0x20
100 #define PSEM_INCREATE 0x40
101 #define PSEM_INDELETE 0x80
102
103 struct psemcache {
104 LIST_ENTRY(psemcache) psem_hash; /* hash chain */
105 struct pseminfo *pseminfo; /* vnode the name refers to */
106 int psem_nlen; /* length of name */
107 char psem_name[PSEMNAMLEN + 1]; /* segment name */
108 };
109 #define PSEMCACHE_NULL (struct psemcache *)0
110
111 struct psemstats {
112 long goodhits; /* hits that we can really use */
113 long neghits; /* negative hits that we can use */
114 long badhits; /* hits we must drop */
115 long falsehits; /* hits with id mismatch */
116 long miss; /* misses */
117 long longnames; /* long names that ignore cache */
118 };
119
120 struct psemname {
121 char *psem_nameptr; /* pointer to looked up name */
122 long psem_namelen; /* length of looked up component */
123 u_long psem_hash; /* hash value of looked up name */
124 };
125
126 struct psemnode {
127 struct pseminfo *pinfo;
128 #if DIAGNOSTIC
129 unsigned int readcnt;
130 unsigned int writecnt;
131 #endif
132 };
133 #define PSEMNODE_NULL (struct psemnode *)0
134
135
136 #define PSEMHASH(pnp) \
137 (&psemhashtbl[(pnp)->psem_hash & psemhash])
138 LIST_HEAD(psemhashhead, psemcache) *psemhashtbl; /* Hash Table */
139 u_long psemhash; /* size of hash table - 1 */
140 long psemnument; /* number of cache entries allocated */
141 long posix_sem_max = 10000; /* tunable for max POSIX semaphores */
142 /* 10000 limits to ~1M of memory */
143 SYSCTL_NODE(_kern, KERN_POSIX, posix, CTLFLAG_RW, 0, "Posix");
144 SYSCTL_NODE(_kern_posix, OID_AUTO, sem, CTLFLAG_RW, 0, "Semaphores");
145 SYSCTL_INT (_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW, &posix_sem_max, 0, "max");
146
147 struct psemstats psemstats; /* cache effectiveness statistics */
148
149 static int psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred);
150 static int psem_cache_search(struct pseminfo **,
151 struct psemname *, struct psemcache **);
152 static int psem_delete(struct pseminfo * pinfo);
153
154 static int psem_read (struct fileproc *fp, struct uio *uio,
155 kauth_cred_t cred, int flags, struct proc *p);
156 static int psem_write (struct fileproc *fp, struct uio *uio,
157 kauth_cred_t cred, int flags, struct proc *p);
158 static int psem_ioctl (struct fileproc *fp, u_long com,
159 caddr_t data, struct proc *p);
160 static int psem_select (struct fileproc *fp, int which, void *wql, struct proc *p);
161 static int psem_closefile (struct fileglob *fp, struct proc *p);
162
163 static int psem_kqfilter (struct fileproc *fp, struct knote *kn, struct proc *p);
164
165 struct fileops psemops =
166 { psem_read, psem_write, psem_ioctl, psem_select, psem_closefile, psem_kqfilter, 0 };
167
168
169 static lck_grp_t *psx_sem_subsys_lck_grp;
170 static lck_grp_attr_t *psx_sem_subsys_lck_grp_attr;
171 static lck_attr_t *psx_sem_subsys_lck_attr;
172 static lck_mtx_t psx_sem_subsys_mutex;
173
174 #define PSEM_SUBSYS_LOCK() lck_mtx_lock(& psx_sem_subsys_mutex)
175 #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_sem_subsys_mutex)
176
177
178 static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp);
179 /* Initialize the mutex governing access to the posix sem subsystem */
180 __private_extern__ void
181 psem_lock_init( void )
182 {
183
184 psx_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
185 lck_grp_attr_setstat(psx_sem_subsys_lck_grp_attr);
186
187 psx_sem_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr);
188
189 psx_sem_subsys_lck_attr = lck_attr_alloc_init();
190 /* lck_attr_setdebug(psx_sem_subsys_lck_attr); */
191 lck_mtx_init(& psx_sem_subsys_mutex, psx_sem_subsys_lck_grp, psx_sem_subsys_lck_attr);
192 }
193
194 /*
195 * Lookup an entry in the cache
196 *
197 *
198 * status of -1 is returned if matches
199 * If the lookup determines that the name does not exist
200 * (negative cacheing), a status of ENOENT is returned. If the lookup
201 * fails, a status of zero is returned.
202 */
203
204 static int
205 psem_cache_search(psemp, pnp, pcache)
206 struct pseminfo **psemp;
207 struct psemname *pnp;
208 struct psemcache **pcache;
209 {
210 struct psemcache *pcp, *nnp;
211 struct psemhashhead *pcpp;
212
213 if (pnp->psem_namelen > PSEMNAMLEN) {
214 psemstats.longnames++;
215 return (0);
216 }
217
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, (u_int)pcp-> psem_nlen))
223 break;
224 }
225
226 if (pcp == 0) {
227 psemstats.miss++;
228 return (0);
229 }
230
231 /* We found a "positive" match, return the vnode */
232 if (pcp->pseminfo) {
233 psemstats.goodhits++;
234 /* TOUCH(ncp); */
235 *psemp = pcp->pseminfo;
236 *pcache = pcp;
237 return (-1);
238 }
239
240 /*
241 * We found a "negative" match, ENOENT notifies client of this match.
242 * The nc_vpid field records whether this is a whiteout.
243 */
244 psemstats.neghits++;
245 return (ENOENT);
246 }
247
248 /*
249 * Add an entry to the cache.
250 */
251 static int
252 psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp)
253 {
254 struct psemhashhead *pcpp;
255 struct pseminfo *dpinfo;
256 struct psemcache *dpcp;
257
258 #if DIAGNOSTIC
259 if (pnp->psem_namelen > NCHNAMLEN)
260 panic("cache_enter: name too long");
261 #endif
262
263
264 /* if the entry has already been added by some one else return */
265 if (psem_cache_search(&dpinfo, pnp, &dpcp) == -1) {
266 return(EEXIST);
267 }
268 if (psemnument >= posix_sem_max)
269 return(ENOSPC);
270 psemnument++;
271 /*
272 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
273 * For negative entries, we have to record whether it is a whiteout.
274 * the whiteout flag is stored in the nc_vpid field which is
275 * otherwise unused.
276 */
277 pcp->pseminfo = psemp;
278 pcp->psem_nlen = pnp->psem_namelen;
279 bcopy(pnp->psem_nameptr, pcp->psem_name, (unsigned)pcp->psem_nlen);
280 pcpp = PSEMHASH(pnp);
281 #if DIAGNOSTIC
282 {
283 struct psemcache *p;
284
285 for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next)
286 if (p == pcp)
287 panic("psem:cache_enter duplicate");
288 }
289 #endif
290 LIST_INSERT_HEAD(pcpp, pcp, psem_hash);
291 return(0);
292 }
293
294 /*
295 * Name cache initialization, from vfs_init() when we are booting
296 */
297 void
298 psem_cache_init(void)
299 {
300 psemhashtbl = hashinit(desiredvnodes, M_SHM, &psemhash);
301 }
302
303 static void
304 psem_cache_delete(struct psemcache *pcp)
305 {
306 #if DIAGNOSTIC
307 if (pcp->psem_hash.le_prev == 0)
308 panic("psem namecache purge le_prev");
309 if (pcp->psem_hash.le_next == pcp)
310 panic("namecache purge le_next");
311 #endif /* DIAGNOSTIC */
312 LIST_REMOVE(pcp, psem_hash);
313 pcp->psem_hash.le_prev = 0;
314 psemnument--;
315 }
316
317 #if NOT_USED
318 /*
319 * Invalidate a all entries to particular vnode.
320 *
321 * We actually just increment the v_id, that will do it. The entries will
322 * be purged by lookup as they get found. If the v_id wraps around, we
323 * need to ditch the entire cache, to avoid confusion. No valid vnode will
324 * ever have (v_id == 0).
325 */
326 static void
327 psem_cache_purge(void)
328 {
329 struct psemcache *pcp;
330 struct psemhashhead *pcpp;
331
332 for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) {
333 while ( (pcp = pcpp->lh_first) )
334 psem_cache_delete(pcp);
335 }
336 }
337 #endif /* NOT_USED */
338
339 int
340 sem_open(struct proc *p, struct sem_open_args *uap, user_addr_t *retval)
341 {
342 struct fileproc *fp;
343 size_t i;
344 struct fileproc *nfp;
345 int indx, error;
346 struct psemname nd;
347 struct pseminfo *pinfo;
348 struct psemcache *pcp;
349 char * pnbuf;
350 char * nameptr;
351 char * cp;
352 size_t pathlen, plen;
353 int fmode ;
354 int cmode = uap->mode;
355 int value = uap->value;
356 int incache = 0;
357 struct psemnode * pnode = PSEMNODE_NULL;
358 struct psemcache * pcache = PSEMCACHE_NULL;
359 kern_return_t kret = KERN_SUCCESS;
360 int pinfo_alloc = 0;
361
362 AUDIT_ARG(fflags, uap->oflag);
363 AUDIT_ARG(mode, uap->mode);
364 AUDIT_ARG(value, uap->value);
365
366 pinfo = PSEMINFO_NULL;
367
368 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
369 if (pnbuf == NULL)
370 return(ENOSPC);
371
372 pathlen = MAXPATHLEN;
373 error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen);
374 if (error) {
375 goto bad;
376 }
377 AUDIT_ARG(text, pnbuf);
378 if ( (pathlen > PSEMNAMLEN) ) {
379 error = ENAMETOOLONG;
380 goto bad;
381 }
382
383 #ifdef PSXSEM_NAME_RESTRICT
384 nameptr = pnbuf;
385 if (*nameptr == '/') {
386 while (*(nameptr++) == '/') {
387 plen--;
388 error = EINVAL;
389 goto bad;
390 }
391 } else {
392 error = EINVAL;
393 goto bad;
394 }
395 #endif /* PSXSEM_NAME_RESTRICT */
396
397 plen = pathlen;
398 nameptr = pnbuf;
399 nd.psem_nameptr = nameptr;
400 nd.psem_namelen = plen;
401 nd. psem_hash =0;
402
403 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
404 nd.psem_hash += (unsigned char)*cp * i;
405 }
406
407 #if KTRACE
408 if (KTRPOINT(p, KTR_NAMEI))
409 ktrnamei(p->p_tracep, nameptr);
410 #endif
411
412 PSEM_SUBSYS_LOCK();
413 error = psem_cache_search(&pinfo, &nd, &pcache);
414
415 if (error == ENOENT) {
416 PSEM_SUBSYS_UNLOCK();
417 error = EINVAL;
418 goto bad;
419
420 }
421 if (!error) {
422 incache = 0;
423 } else
424 incache = 1;
425 fmode = FFLAGS(uap->oflag);
426
427 PSEM_SUBSYS_UNLOCK();
428 error = falloc(p, &nfp, &indx);
429 if (error)
430 goto bad;
431
432 PSEM_SUBSYS_LOCK();
433 fp = nfp;
434 cmode &= ALLPERMS;
435
436 if (((fmode & (O_CREAT | O_EXCL))==(O_CREAT | O_EXCL)) && incache) {
437 /* sem exists and opened O_EXCL */
438 #if notyet
439 if (pinfo->psem_flags & PSEM_INDELETE) {
440 }
441 #endif
442 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid,
443 pinfo->psem_gid, pinfo->psem_mode);
444 PSEM_SUBSYS_UNLOCK();
445 error = EEXIST;
446 goto bad1;
447 }
448 if (((fmode & (O_CREAT | O_EXCL))== O_CREAT) && incache) {
449 /* As per POSIX, O_CREAT has no effect */
450 fmode &= ~O_CREAT;
451 }
452
453 if ( (fmode & O_CREAT) ) {
454 if((value < 0) && (value > SEM_VALUE_MAX)) {
455 PSEM_SUBSYS_UNLOCK();
456 error = EINVAL;
457 goto bad1;
458 }
459 PSEM_SUBSYS_UNLOCK();
460 MALLOC(pinfo, struct pseminfo *, sizeof(struct pseminfo), M_SHM, M_WAITOK|M_ZERO);
461 if (pinfo == NULL) {
462 error = ENOSPC;
463 goto bad1;
464 }
465 PSEM_SUBSYS_LOCK();
466
467 pinfo_alloc = 1;
468 pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE;
469 pinfo->psem_usecount = 1;
470 pinfo->psem_mode = cmode;
471 pinfo->psem_uid = kauth_cred_getuid(kauth_cred_get());
472 pinfo->psem_gid = kauth_cred_get()->cr_gid;
473 PSEM_SUBSYS_UNLOCK();
474 kret = semaphore_create(kernel_task, &pinfo->psem_semobject,
475 SYNC_POLICY_FIFO, value);
476 if(kret != KERN_SUCCESS)
477 goto bad3;
478 PSEM_SUBSYS_LOCK();
479 pinfo->psem_flags &= ~PSEM_DEFINED;
480 pinfo->psem_flags |= PSEM_ALLOCATED;
481 pinfo->sem_proc = p;
482 } else {
483 /* semaphore should exist as it is without O_CREAT */
484 if (!incache) {
485 PSEM_SUBSYS_UNLOCK();
486 error = ENOENT;
487 goto bad1;
488 }
489 if( pinfo->psem_flags & PSEM_INDELETE) {
490 PSEM_SUBSYS_UNLOCK();
491 error = ENOENT;
492 goto bad1;
493 }
494 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid,
495 pinfo->psem_gid, pinfo->psem_mode);
496 if ( (error = psem_access(pinfo, fmode, kauth_cred_get())) ) {
497 PSEM_SUBSYS_UNLOCK();
498 goto bad1;
499 }
500 }
501 PSEM_SUBSYS_UNLOCK();
502 MALLOC(pnode, struct psemnode *, sizeof(struct psemnode), M_SHM, M_WAITOK|M_ZERO);
503 if (pnode == NULL) {
504 error = ENOSPC;
505 goto bad1;
506 }
507 if (!incache) {
508 /*
509 * We allocate a new entry if we are less than the maximum
510 * allowed and the one at the front of the LRU list is in use.
511 * Otherwise we use the one at the front of the LRU list.
512 */
513 MALLOC(pcp, struct psemcache *, sizeof(struct psemcache), M_SHM, M_WAITOK|M_ZERO);
514 if (pcp == NULL) {
515 error = ENOMEM;
516 goto bad2;
517 }
518
519 }
520 PSEM_SUBSYS_LOCK();
521 if (!incache) {
522 if ( (error = psem_cache_add(pinfo, &nd, pcp)) ) {
523 PSEM_SUBSYS_UNLOCK();
524 FREE(pcp, M_SHM);
525 goto bad2;
526 }
527 }
528 pinfo->psem_flags &= ~PSEM_INCREATE;
529 pinfo->psem_usecount++;
530 pnode->pinfo = pinfo;
531 PSEM_SUBSYS_UNLOCK();
532
533 proc_fdlock(p);
534 fp->f_flag = fmode & FMASK;
535 fp->f_type = DTYPE_PSXSEM;
536 fp->f_ops = &psemops;
537 fp->f_data = (caddr_t)pnode;
538 *fdflags(p, indx) &= ~UF_RESERVED;
539 fp_drop(p, indx, fp, 1);
540 proc_fdunlock(p);
541
542 *retval = CAST_USER_ADDR_T(indx);
543 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
544 return (0);
545
546 bad3:
547 switch (kret) {
548 case KERN_RESOURCE_SHORTAGE:
549 error = ENOMEM;
550 case KERN_PROTECTION_FAILURE:
551 error = EACCES;
552 default:
553 error = EINVAL;
554 }
555 goto bad1;
556 bad2:
557 FREE(pnode, M_SHM);
558 bad1:
559 if (pinfo_alloc)
560 FREE(pinfo, M_SHM);
561 fp_free(p, indx, nfp);
562 bad:
563 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
564 return (error);
565 }
566
567 /*
568 * XXX This code is repeated in several places
569 */
570 static int
571 psem_access(struct pseminfo *pinfo, int mode, kauth_cred_t cred)
572 {
573 mode_t mask;
574 int is_member;
575
576 /* Otherwise, user id 0 always gets access. */
577 if (!suser(cred, NULL))
578 return (0);
579
580 mask = 0;
581
582 /* Otherwise, check the owner. */
583 if (kauth_cred_getuid(cred) == pinfo->psem_uid) {
584 if (mode & FREAD)
585 mask |= S_IRUSR;
586 if (mode & FWRITE)
587 mask |= S_IWUSR;
588 return ((pinfo->psem_mode & mask) == mask ? 0 : EACCES);
589 }
590
591 /* Otherwise, check the groups. */
592 if (kauth_cred_ismember_gid(cred, pinfo->psem_gid, &is_member) == 0 && is_member) {
593 if (mode & FREAD)
594 mask |= S_IRGRP;
595 if (mode & FWRITE)
596 mask |= S_IWGRP;
597 return ((pinfo->psem_mode & mask) == mask ? 0 : EACCES);
598 }
599
600 /* Otherwise, check everyone else. */
601 if (mode & FREAD)
602 mask |= S_IROTH;
603 if (mode & FWRITE)
604 mask |= S_IWOTH;
605 return ((pinfo->psem_mode & mask) == mask ? 0 : EACCES);
606 }
607
608 int
609 sem_unlink(__unused struct proc *p, struct sem_unlink_args *uap, __unused register_t *retval)
610 {
611 size_t i;
612 int error=0;
613 struct psemname nd;
614 struct pseminfo *pinfo;
615 char * pnbuf;
616 char * nameptr;
617 char * cp;
618 size_t pathlen, plen;
619 int incache = 0;
620 struct psemcache *pcache = PSEMCACHE_NULL;
621
622 pinfo = PSEMINFO_NULL;
623
624 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
625 if (pnbuf == NULL) {
626 return(ENOSPC); /* XXX non-standard */
627 }
628 pathlen = MAXPATHLEN;
629 error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen);
630 if (error) {
631 goto bad;
632 }
633 AUDIT_ARG(text, pnbuf);
634 if (pathlen > PSEMNAMLEN) {
635 error = ENAMETOOLONG;
636 goto bad;
637 }
638
639
640 #ifdef PSXSEM_NAME_RESTRICT
641 nameptr = pnbuf;
642 if (*nameptr == '/') {
643 while (*(nameptr++) == '/') {
644 plen--;
645 error = EINVAL;
646 goto bad;
647 }
648 } else {
649 error = EINVAL;
650 goto bad;
651 }
652 #endif /* PSXSEM_NAME_RESTRICT */
653
654 plen = pathlen;
655 nameptr = pnbuf;
656 nd.psem_nameptr = nameptr;
657 nd.psem_namelen = plen;
658 nd. psem_hash =0;
659
660 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
661 nd.psem_hash += (unsigned char)*cp * i;
662 }
663
664 PSEM_SUBSYS_LOCK();
665 error = psem_cache_search(&pinfo, &nd, &pcache);
666
667 if (error == ENOENT) {
668 PSEM_SUBSYS_UNLOCK();
669 error = EINVAL;
670 goto bad;
671
672 }
673 if (!error) {
674 PSEM_SUBSYS_UNLOCK();
675 error = EINVAL;
676 goto bad;
677 } else
678 incache = 1;
679 if ( (error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get())) ) {
680 PSEM_SUBSYS_UNLOCK();
681 goto bad;
682 }
683
684 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))==0) {
685 PSEM_SUBSYS_UNLOCK();
686 return (EINVAL);
687 }
688
689 if ( (pinfo->psem_flags & PSEM_INDELETE) ) {
690 PSEM_SUBSYS_UNLOCK();
691 error = 0;
692 goto bad;
693 }
694
695 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid,
696 pinfo->psem_mode);
697
698 pinfo->psem_flags |= PSEM_INDELETE;
699 pinfo->psem_usecount--;
700
701 if (!pinfo->psem_usecount) {
702 psem_delete(pinfo);
703 FREE(pinfo,M_SHM);
704 } else
705 pinfo->psem_flags |= PSEM_REMOVED;
706
707 psem_cache_delete(pcache);
708 PSEM_SUBSYS_UNLOCK();
709 FREE(pcache, M_SHM);
710 error = 0;
711 bad:
712 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
713 return (error);
714 }
715
716 int
717 sem_close(struct proc *p, struct sem_close_args *uap, __unused register_t *retval)
718 {
719 int fd = CAST_DOWN(int,uap->sem);
720 struct fileproc *fp;
721 int error = 0;
722
723 AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */
724
725 proc_fdlock(p);
726 error = fp_lookup(p,fd, &fp, 1);
727 if (error) {
728 proc_fdunlock(p);
729 return(error);
730 }
731 fdrelse(p, fd);
732 error = closef_locked(fp, fp->f_fglob, p);
733 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
734 proc_fdunlock(p);
735 return(error);
736 }
737
738 int
739 sem_wait(struct proc *p, struct sem_wait_args *uap, __unused register_t *retval)
740 {
741 int fd = CAST_DOWN(int,uap->sem);
742 struct fileproc *fp;
743 struct pseminfo * pinfo;
744 struct psemnode * pnode ;
745 kern_return_t kret;
746 int error;
747
748 error = fp_getfpsem(p, fd, &fp, &pnode);
749 if (error)
750 return (error);
751 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
752 error = EINVAL;
753 goto out;
754 }
755 PSEM_SUBSYS_LOCK();
756 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
757 PSEM_SUBSYS_UNLOCK();
758 error = EINVAL;
759 goto out;
760 }
761 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
762 != PSEM_ALLOCATED) {
763 PSEM_SUBSYS_UNLOCK();
764 error = EINVAL;
765 goto out;
766 }
767
768 PSEM_SUBSYS_UNLOCK();
769 kret = semaphore_wait(pinfo->psem_semobject);
770 switch (kret) {
771 case KERN_INVALID_ADDRESS:
772 case KERN_PROTECTION_FAILURE:
773 error = EACCES;
774 break;
775 case KERN_ABORTED:
776 case KERN_OPERATION_TIMED_OUT:
777 error = EINTR;
778 break;
779 case KERN_SUCCESS:
780 error = 0;
781 break;
782 default:
783 error = EINVAL;
784 break;
785 }
786 out:
787 fp_drop(p, fd, fp, 0);
788 return(error);
789
790 }
791
792 int
793 sem_trywait(struct proc *p, struct sem_trywait_args *uap, __unused register_t *retval)
794 {
795 int fd = CAST_DOWN(int,uap->sem);
796 struct fileproc *fp;
797 struct pseminfo * pinfo;
798 struct psemnode * pnode ;
799 kern_return_t kret;
800 mach_timespec_t wait_time;
801 int error;
802
803 error = fp_getfpsem(p, fd, &fp, &pnode);
804 if (error)
805 return (error);
806 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
807 error = EINVAL;
808 goto out;
809 }
810 PSEM_SUBSYS_LOCK();
811 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
812 PSEM_SUBSYS_UNLOCK();
813 error = EINVAL;
814 goto out;
815 }
816 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
817 != PSEM_ALLOCATED) {
818 PSEM_SUBSYS_UNLOCK();
819 error = EINVAL;
820 goto out;
821 }
822
823 PSEM_SUBSYS_UNLOCK();
824 wait_time.tv_sec = 0;
825 wait_time.tv_nsec = 0;
826
827 kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO);
828 switch (kret) {
829 case KERN_INVALID_ADDRESS:
830 case KERN_PROTECTION_FAILURE:
831 error = EINVAL;
832 break;
833 case KERN_ABORTED:
834 error = EINTR;
835 break;
836 case KERN_OPERATION_TIMED_OUT:
837 error = EAGAIN;
838 break;
839 case KERN_SUCCESS:
840 error = 0;
841 break;
842 default:
843 error = EINVAL;
844 break;
845 }
846 out:
847 fp_drop(p, fd, fp, 0);
848 return(error);
849 }
850
851 int
852 sem_post(struct proc *p, struct sem_post_args *uap, __unused register_t *retval)
853 {
854 int fd = CAST_DOWN(int,uap->sem);
855 struct fileproc *fp;
856 struct pseminfo * pinfo;
857 struct psemnode * pnode ;
858 kern_return_t kret;
859 int error;
860
861 error = fp_getfpsem(p, fd, &fp, &pnode);
862 if (error)
863 return (error);
864 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
865 error = EINVAL;
866 goto out;
867 }
868 PSEM_SUBSYS_LOCK();
869 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
870 PSEM_SUBSYS_UNLOCK();
871 error = EINVAL;
872 goto out;
873 }
874 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
875 != PSEM_ALLOCATED) {
876 PSEM_SUBSYS_UNLOCK();
877 error = EINVAL;
878 goto out;
879 }
880
881 PSEM_SUBSYS_UNLOCK();
882 kret = semaphore_signal(pinfo->psem_semobject);
883 switch (kret) {
884 case KERN_INVALID_ADDRESS:
885 case KERN_PROTECTION_FAILURE:
886 error = EINVAL;
887 break;
888 case KERN_ABORTED:
889 case KERN_OPERATION_TIMED_OUT:
890 error = EINTR;
891 break;
892 case KERN_SUCCESS:
893 error = 0;
894 break;
895 default:
896 error = EINVAL;
897 break;
898 }
899 out:
900 fp_drop(p, fd, fp, 0);
901 return(error);
902 }
903
904 int
905 sem_init(__unused struct proc *p, __unused struct sem_init_args *uap, __unused register_t *retval)
906 {
907 return(ENOSYS);
908 }
909
910 int
911 sem_destroy(__unused struct proc *p, __unused struct sem_destroy_args *uap, __unused register_t *retval)
912 {
913 return(ENOSYS);
914 }
915
916 int
917 sem_getvalue(__unused struct proc *p, __unused struct sem_getvalue_args *uap, __unused register_t *retval)
918 {
919 return(ENOSYS);
920 }
921
922 static int
923 psem_close(struct psemnode *pnode, __unused int flags,
924 __unused kauth_cred_t cred, __unused struct proc *p)
925 {
926 int error=0;
927 register struct pseminfo *pinfo;
928
929 PSEM_SUBSYS_LOCK();
930 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){
931 PSEM_SUBSYS_UNLOCK();
932 return(EINVAL);
933 }
934
935 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) {
936 PSEM_SUBSYS_UNLOCK();
937 return(EINVAL);
938 }
939 #if DIAGNOSTIC
940 if(!pinfo->psem_usecount) {
941 kprintf("negative usecount in psem_close\n");
942 }
943 #endif /* DIAGNOSTIC */
944 pinfo->psem_usecount--;
945
946 if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) {
947 PSEM_SUBSYS_UNLOCK();
948 /* lock dropped as only semaphore is destroyed here */
949 error = psem_delete(pinfo);
950 FREE(pinfo,M_SHM);
951 } else {
952 PSEM_SUBSYS_UNLOCK();
953 }
954 /* subsystem lock is dropped when we get here */
955 FREE(pnode, M_SHM);
956 return (error);
957 }
958
959 static int
960 psem_closefile(fg, p)
961 struct fileglob *fg;
962 struct proc *p;
963 {
964 int error;
965
966 /* Not locked as psem_close is called only from here and is locked properly */
967 error = psem_close(((struct psemnode *)fg->fg_data), fg->fg_flag,
968 fg->fg_cred, p);
969
970 return(error);
971 }
972
973 static int
974 psem_delete(struct pseminfo * pinfo)
975 {
976 kern_return_t kret;
977
978 kret = semaphore_destroy(kernel_task, pinfo->psem_semobject);
979
980 switch (kret) {
981 case KERN_INVALID_ADDRESS:
982 case KERN_PROTECTION_FAILURE:
983 return (EINVAL);
984 case KERN_ABORTED:
985 case KERN_OPERATION_TIMED_OUT:
986 return (EINTR);
987 case KERN_SUCCESS:
988 return(0);
989 default:
990 return (EINVAL);
991 }
992 }
993
994 static int
995 psem_read(__unused struct fileproc *fp, __unused struct uio *uio,
996 __unused kauth_cred_t cred, __unused int flags,
997 __unused struct proc *p)
998 {
999 return(ENOTSUP);
1000 }
1001
1002 static int
1003 psem_write(__unused struct fileproc *fp, __unused struct uio *uio,
1004 __unused kauth_cred_t cred, __unused int flags,
1005 __unused struct proc *p)
1006 {
1007 return(ENOTSUP);
1008 }
1009
1010 static int
1011 psem_ioctl(__unused struct fileproc *fp, __unused u_long com,
1012 __unused caddr_t data, __unused struct proc *p)
1013 {
1014 return(ENOTSUP);
1015 }
1016
1017 static int
1018 psem_select(__unused struct fileproc *fp, __unused int which,
1019 __unused void *wql, __unused struct proc *p)
1020 {
1021 return(ENOTSUP);
1022 }
1023
1024 static int
1025 psem_kqfilter(__unused struct fileproc *fp, __unused struct knote *kn,
1026 __unused struct proc *p)
1027 {
1028 return (ENOTSUP);
1029 }
1030