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