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