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