xref: /freebsd/sys/kern/uipc_sem.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
3  * Copyright (c) 2003-2005 SPARTA, Inc.
4  * Copyright (c) 2005 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project in part by Network
8  * Associates Laboratories, the Security Research Division of Network
9  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
10  * as part of the DARPA CHATS research program.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_posix.h"
38 
39 #include <sys/param.h>
40 #include <sys/condvar.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/kernel.h>
46 #include <sys/ksem.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/posix4.h>
54 #include <sys/semaphore.h>
55 #include <sys/_semaphore.h>
56 #include <sys/stat.h>
57 #include <sys/syscall.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
63 #include <sys/sx.h>
64 #include <sys/vnode.h>
65 
66 #include <security/mac/mac_framework.h>
67 
68 /*
69  * TODO
70  *
71  * - Resource limits?
72  * - Update fstat(1)
73  * - Replace global sem_lock with mtx_pool locks?
74  * - Add a MAC check_create() hook for creating new named semaphores.
75  */
76 
77 #ifndef SEM_MAX
78 #define	SEM_MAX	30
79 #endif
80 
81 #ifdef SEM_DEBUG
82 #define	DP(x)	printf x
83 #else
84 #define	DP(x)
85 #endif
86 
87 struct ksem_mapping {
88 	char		*km_path;
89 	Fnv32_t		km_fnv;
90 	struct ksem	*km_ksem;
91 	LIST_ENTRY(ksem_mapping) km_link;
92 };
93 
94 static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
95 static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
96 static struct sx ksem_dict_lock;
97 static struct mtx ksem_count_lock;
98 static struct mtx sem_lock;
99 static u_long ksem_hash;
100 static int ksem_dead;
101 
102 #define	KSEM_HASH(fnv)	(&ksem_dictionary[(fnv) & ksem_hash])
103 
104 static int nsems = 0;
105 SYSCTL_DECL(_p1003_1b);
106 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
107     "Number of active kernel POSIX semaphores");
108 
109 static int	kern_sem_wait(struct thread *td, semid_t id, int tryflag,
110 		    struct timespec *abstime);
111 static int	ksem_access(struct ksem *ks, struct ucred *ucred);
112 static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
113 		    unsigned int value);
114 static int	ksem_create(struct thread *td, const char *path,
115 		    semid_t *semidp, mode_t mode, unsigned int value,
116 		    int flags);
117 static void	ksem_drop(struct ksem *ks);
118 static int	ksem_get(struct thread *td, semid_t id, struct file **fpp);
119 static struct ksem *ksem_hold(struct ksem *ks);
120 static void	ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
121 static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
122 static void	ksem_module_destroy(void);
123 static int	ksem_module_init(void);
124 static int	ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
125 static int	sem_modload(struct module *module, int cmd, void *arg);
126 
127 static fo_rdwr_t	ksem_read;
128 static fo_rdwr_t	ksem_write;
129 static fo_truncate_t	ksem_truncate;
130 static fo_ioctl_t	ksem_ioctl;
131 static fo_poll_t	ksem_poll;
132 static fo_kqfilter_t	ksem_kqfilter;
133 static fo_stat_t	ksem_stat;
134 static fo_close_t	ksem_closef;
135 
136 /* File descriptor operations. */
137 static struct fileops ksem_ops = {
138 	.fo_read = ksem_read,
139 	.fo_write = ksem_write,
140 	.fo_truncate = ksem_truncate,
141 	.fo_ioctl = ksem_ioctl,
142 	.fo_poll = ksem_poll,
143 	.fo_kqfilter = ksem_kqfilter,
144 	.fo_stat = ksem_stat,
145 	.fo_close = ksem_closef,
146 	.fo_flags = DFLAG_PASSABLE
147 };
148 
149 FEATURE(posix_sem, "POSIX semaphores");
150 
151 static int
152 ksem_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
153     int flags, struct thread *td)
154 {
155 
156 	return (EOPNOTSUPP);
157 }
158 
159 static int
160 ksem_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
161     int flags, struct thread *td)
162 {
163 
164 	return (EOPNOTSUPP);
165 }
166 
167 static int
168 ksem_truncate(struct file *fp, off_t length, struct ucred *active_cred,
169     struct thread *td)
170 {
171 
172 	return (EINVAL);
173 }
174 
175 static int
176 ksem_ioctl(struct file *fp, u_long com, void *data,
177     struct ucred *active_cred, struct thread *td)
178 {
179 
180 	return (EOPNOTSUPP);
181 }
182 
183 static int
184 ksem_poll(struct file *fp, int events, struct ucred *active_cred,
185     struct thread *td)
186 {
187 
188 	return (EOPNOTSUPP);
189 }
190 
191 static int
192 ksem_kqfilter(struct file *fp, struct knote *kn)
193 {
194 
195 	return (EOPNOTSUPP);
196 }
197 
198 static int
199 ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
200     struct thread *td)
201 {
202 	struct ksem *ks;
203 #ifdef MAC
204 	int error;
205 #endif
206 
207 	ks = fp->f_data;
208 
209 #ifdef MAC
210 	error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
211 	if (error)
212 		return (error);
213 #endif
214 
215 	/*
216 	 * Attempt to return sanish values for fstat() on a semaphore
217 	 * file descriptor.
218 	 */
219 	bzero(sb, sizeof(*sb));
220 	sb->st_mode = S_IFREG | ks->ks_mode;		/* XXX */
221 
222 	sb->st_atimespec = ks->ks_atime;
223 	sb->st_ctimespec = ks->ks_ctime;
224 	sb->st_mtimespec = ks->ks_mtime;
225 	sb->st_birthtimespec = ks->ks_birthtime;
226 	sb->st_uid = ks->ks_uid;
227 	sb->st_gid = ks->ks_gid;
228 
229 	return (0);
230 }
231 
232 static int
233 ksem_closef(struct file *fp, struct thread *td)
234 {
235 	struct ksem *ks;
236 
237 	ks = fp->f_data;
238 	fp->f_data = NULL;
239 	ksem_drop(ks);
240 
241 	return (0);
242 }
243 
244 /*
245  * ksem object management including creation and reference counting
246  * routines.
247  */
248 static struct ksem *
249 ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
250 {
251 	struct ksem *ks;
252 
253 	mtx_lock(&ksem_count_lock);
254 	if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
255 		mtx_unlock(&ksem_count_lock);
256 		return (NULL);
257 	}
258 	nsems++;
259 	mtx_unlock(&ksem_count_lock);
260 	ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
261 	ks->ks_uid = ucred->cr_uid;
262 	ks->ks_gid = ucred->cr_gid;
263 	ks->ks_mode = mode;
264 	ks->ks_value = value;
265 	cv_init(&ks->ks_cv, "ksem");
266 	vfs_timestamp(&ks->ks_birthtime);
267 	ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
268 	refcount_init(&ks->ks_ref, 1);
269 #ifdef MAC
270 	mac_posixsem_init(ks);
271 	mac_posixsem_create(ucred, ks);
272 #endif
273 
274 	return (ks);
275 }
276 
277 static struct ksem *
278 ksem_hold(struct ksem *ks)
279 {
280 
281 	refcount_acquire(&ks->ks_ref);
282 	return (ks);
283 }
284 
285 static void
286 ksem_drop(struct ksem *ks)
287 {
288 
289 	if (refcount_release(&ks->ks_ref)) {
290 #ifdef MAC
291 		mac_posixsem_destroy(ks);
292 #endif
293 		cv_destroy(&ks->ks_cv);
294 		free(ks, M_KSEM);
295 		mtx_lock(&ksem_count_lock);
296 		nsems--;
297 		mtx_unlock(&ksem_count_lock);
298 	}
299 }
300 
301 /*
302  * Determine if the credentials have sufficient permissions for read
303  * and write access.
304  */
305 static int
306 ksem_access(struct ksem *ks, struct ucred *ucred)
307 {
308 	int error;
309 
310 	error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
311 	    VREAD | VWRITE, ucred, NULL);
312 	if (error)
313 		error = priv_check_cred(ucred, PRIV_SEM_WRITE, 0);
314 	return (error);
315 }
316 
317 /*
318  * Dictionary management.  We maintain an in-kernel dictionary to map
319  * paths to semaphore objects.  We use the FNV hash on the path to
320  * store the mappings in a hash table.
321  */
322 static struct ksem *
323 ksem_lookup(char *path, Fnv32_t fnv)
324 {
325 	struct ksem_mapping *map;
326 
327 	LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
328 		if (map->km_fnv != fnv)
329 			continue;
330 		if (strcmp(map->km_path, path) == 0)
331 			return (map->km_ksem);
332 	}
333 
334 	return (NULL);
335 }
336 
337 static void
338 ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
339 {
340 	struct ksem_mapping *map;
341 
342 	map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
343 	map->km_path = path;
344 	map->km_fnv = fnv;
345 	map->km_ksem = ksem_hold(ks);
346 	LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
347 }
348 
349 static int
350 ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
351 {
352 	struct ksem_mapping *map;
353 	int error;
354 
355 	LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
356 		if (map->km_fnv != fnv)
357 			continue;
358 		if (strcmp(map->km_path, path) == 0) {
359 #ifdef MAC
360 			error = mac_posixsem_check_unlink(ucred, map->km_ksem);
361 			if (error)
362 				return (error);
363 #endif
364 			error = ksem_access(map->km_ksem, ucred);
365 			if (error)
366 				return (error);
367 			LIST_REMOVE(map, km_link);
368 			ksem_drop(map->km_ksem);
369 			free(map->km_path, M_KSEM);
370 			free(map, M_KSEM);
371 			return (0);
372 		}
373 	}
374 
375 	return (ENOENT);
376 }
377 
378 /* Other helper routines. */
379 static int
380 ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
381     unsigned int value, int flags)
382 {
383 	struct filedesc *fdp;
384 	struct ksem *ks;
385 	struct file *fp;
386 	char *path;
387 	semid_t semid;
388 	Fnv32_t fnv;
389 	int error, fd;
390 
391 	if (value > SEM_VALUE_MAX)
392 		return (EINVAL);
393 
394 	fdp = td->td_proc->p_fd;
395 	mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
396 	error = falloc(td, &fp, &fd);
397 	if (error) {
398 		if (name == NULL)
399 			error = ENOSPC;
400 		return (error);
401 	}
402 
403 	/*
404 	 * Go ahead and copyout the file descriptor now.  This is a bit
405 	 * premature, but it is a lot easier to handle errors as opposed
406 	 * to later when we've possibly created a new semaphore, etc.
407 	 */
408 	semid = fd;
409 	error = copyout(&semid, semidp, sizeof(semid));
410 	if (error) {
411 		fdclose(fdp, fp, fd, td);
412 		fdrop(fp, td);
413 		return (error);
414 	}
415 
416 	if (name == NULL) {
417 		/* Create an anonymous semaphore. */
418 		ks = ksem_alloc(td->td_ucred, mode, value);
419 		if (ks == NULL)
420 			error = ENOSPC;
421 		else
422 			ks->ks_flags |= KS_ANONYMOUS;
423 	} else {
424 		path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
425 		error = copyinstr(name, path, MAXPATHLEN, NULL);
426 
427 		/* Require paths to start with a '/' character. */
428 		if (error == 0 && path[0] != '/')
429 			error = EINVAL;
430 		if (error) {
431 			fdclose(fdp, fp, fd, td);
432 			fdrop(fp, td);
433 			free(path, M_KSEM);
434 			return (error);
435 		}
436 
437 		fnv = fnv_32_str(path, FNV1_32_INIT);
438 		sx_xlock(&ksem_dict_lock);
439 		ks = ksem_lookup(path, fnv);
440 		if (ks == NULL) {
441 			/* Object does not exist, create it if requested. */
442 			if (flags & O_CREAT) {
443 				ks = ksem_alloc(td->td_ucred, mode, value);
444 				if (ks == NULL)
445 					error = ENFILE;
446 				else {
447 					ksem_insert(path, fnv, ks);
448 					path = NULL;
449 				}
450 			} else
451 				error = ENOENT;
452 		} else {
453 			/*
454 			 * Object already exists, obtain a new
455 			 * reference if requested and permitted.
456 			 */
457 			if ((flags & (O_CREAT | O_EXCL)) ==
458 			    (O_CREAT | O_EXCL))
459 				error = EEXIST;
460 			else {
461 #ifdef MAC
462 				error = mac_posixsem_check_open(td->td_ucred,
463 				    ks);
464 				if (error == 0)
465 #endif
466 				error = ksem_access(ks, td->td_ucred);
467 			}
468 			if (error == 0)
469 				ksem_hold(ks);
470 #ifdef INVARIANTS
471 			else
472 				ks = NULL;
473 #endif
474 		}
475 		sx_xunlock(&ksem_dict_lock);
476 		if (path)
477 			free(path, M_KSEM);
478 	}
479 
480 	if (error) {
481 		KASSERT(ks == NULL, ("ksem_create error with a ksem"));
482 		fdclose(fdp, fp, fd, td);
483 		fdrop(fp, td);
484 		return (error);
485 	}
486 	KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
487 
488 	finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
489 
490 	FILEDESC_XLOCK(fdp);
491 	if (fdp->fd_ofiles[fd] == fp)
492 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
493 	FILEDESC_XUNLOCK(fdp);
494 	fdrop(fp, td);
495 
496 	return (0);
497 }
498 
499 static int
500 ksem_get(struct thread *td, semid_t id, struct file **fpp)
501 {
502 	struct ksem *ks;
503 	struct file *fp;
504 	int error;
505 
506 	error = fget(td, id, &fp);
507 	if (error)
508 		return (EINVAL);
509 	if (fp->f_type != DTYPE_SEM) {
510 		fdrop(fp, td);
511 		return (EINVAL);
512 	}
513 	ks = fp->f_data;
514 	if (ks->ks_flags & KS_DEAD) {
515 		fdrop(fp, td);
516 		return (EINVAL);
517 	}
518 	*fpp = fp;
519 	return (0);
520 }
521 
522 /* System calls. */
523 #ifndef _SYS_SYSPROTO_H_
524 struct ksem_init_args {
525 	unsigned int	value;
526 	semid_t		*idp;
527 };
528 #endif
529 int
530 ksem_init(struct thread *td, struct ksem_init_args *uap)
531 {
532 
533 	return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
534 	    0));
535 }
536 
537 #ifndef _SYS_SYSPROTO_H_
538 struct ksem_open_args {
539 	char		*name;
540 	int		oflag;
541 	mode_t		mode;
542 	unsigned int	value;
543 	semid_t		*idp;
544 };
545 #endif
546 int
547 ksem_open(struct thread *td, struct ksem_open_args *uap)
548 {
549 
550 	DP((">>> ksem_open start, pid=%d\n", (int)td->td_proc->p_pid));
551 
552 	if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
553 		return (EINVAL);
554 	return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
555 	    uap->oflag));
556 }
557 
558 #ifndef _SYS_SYSPROTO_H_
559 struct ksem_unlink_args {
560 	char		*name;
561 };
562 #endif
563 int
564 ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
565 {
566 	char *path;
567 	Fnv32_t fnv;
568 	int error;
569 
570 	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
571 	error = copyinstr(uap->name, path, MAXPATHLEN, NULL);
572 	if (error) {
573 		free(path, M_TEMP);
574 		return (error);
575 	}
576 
577 	fnv = fnv_32_str(path, FNV1_32_INIT);
578 	sx_xlock(&ksem_dict_lock);
579 	error = ksem_remove(path, fnv, td->td_ucred);
580 	sx_xunlock(&ksem_dict_lock);
581 	free(path, M_TEMP);
582 
583 	return (error);
584 }
585 
586 #ifndef _SYS_SYSPROTO_H_
587 struct ksem_close_args {
588 	semid_t		id;
589 };
590 #endif
591 int
592 ksem_close(struct thread *td, struct ksem_close_args *uap)
593 {
594 	struct ksem *ks;
595 	struct file *fp;
596 	int error;
597 
598 	error = ksem_get(td, uap->id, &fp);
599 	if (error)
600 		return (error);
601 	ks = fp->f_data;
602 	if (ks->ks_flags & KS_ANONYMOUS) {
603 		fdrop(fp, td);
604 		return (EINVAL);
605 	}
606 	error = kern_close(td, uap->id);
607 	fdrop(fp, td);
608 	return (error);
609 }
610 
611 #ifndef _SYS_SYSPROTO_H_
612 struct ksem_post_args {
613 	semid_t	id;
614 };
615 #endif
616 int
617 ksem_post(struct thread *td, struct ksem_post_args *uap)
618 {
619 	struct file *fp;
620 	struct ksem *ks;
621 	int error;
622 
623 	error = ksem_get(td, uap->id, &fp);
624 	if (error)
625 		return (error);
626 	ks = fp->f_data;
627 
628 	mtx_lock(&sem_lock);
629 #ifdef MAC
630 	error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
631 	if (error)
632 		goto err;
633 #endif
634 	if (ks->ks_value == SEM_VALUE_MAX) {
635 		error = EOVERFLOW;
636 		goto err;
637 	}
638 	++ks->ks_value;
639 	if (ks->ks_waiters > 0)
640 		cv_signal(&ks->ks_cv);
641 	error = 0;
642 	vfs_timestamp(&ks->ks_ctime);
643 err:
644 	mtx_unlock(&sem_lock);
645 	fdrop(fp, td);
646 	return (error);
647 }
648 
649 #ifndef _SYS_SYSPROTO_H_
650 struct ksem_wait_args {
651 	semid_t		id;
652 };
653 #endif
654 int
655 ksem_wait(struct thread *td, struct ksem_wait_args *uap)
656 {
657 
658 	return (kern_sem_wait(td, uap->id, 0, NULL));
659 }
660 
661 #ifndef _SYS_SYSPROTO_H_
662 struct ksem_timedwait_args {
663 	semid_t		id;
664 	const struct timespec *abstime;
665 };
666 #endif
667 int
668 ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
669 {
670 	struct timespec abstime;
671 	struct timespec *ts;
672 	int error;
673 
674 	/*
675 	 * We allow a null timespec (wait forever).
676 	 */
677 	if (uap->abstime == NULL)
678 		ts = NULL;
679 	else {
680 		error = copyin(uap->abstime, &abstime, sizeof(abstime));
681 		if (error != 0)
682 			return (error);
683 		if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
684 			return (EINVAL);
685 		ts = &abstime;
686 	}
687 	return (kern_sem_wait(td, uap->id, 0, ts));
688 }
689 
690 #ifndef _SYS_SYSPROTO_H_
691 struct ksem_trywait_args {
692 	semid_t		id;
693 };
694 #endif
695 int
696 ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
697 {
698 
699 	return (kern_sem_wait(td, uap->id, 1, NULL));
700 }
701 
702 static int
703 kern_sem_wait(struct thread *td, semid_t id, int tryflag,
704     struct timespec *abstime)
705 {
706 	struct timespec ts1, ts2;
707 	struct timeval tv;
708 	struct file *fp;
709 	struct ksem *ks;
710 	int error;
711 
712 	DP((">>> kern_sem_wait entered! pid=%d\n", (int)td->td_proc->p_pid));
713 	error = ksem_get(td, id, &fp);
714 	if (error)
715 		return (error);
716 	ks = fp->f_data;
717 	mtx_lock(&sem_lock);
718 	DP((">>> kern_sem_wait critical section entered! pid=%d\n",
719 	    (int)td->td_proc->p_pid));
720 #ifdef MAC
721 	error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
722 	if (error) {
723 		DP(("kern_sem_wait mac failed\n"));
724 		goto err;
725 	}
726 #endif
727 	DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
728 	vfs_timestamp(&ks->ks_atime);
729 	while (ks->ks_value == 0) {
730 		ks->ks_waiters++;
731 		if (tryflag != 0)
732 			error = EAGAIN;
733 		else if (abstime == NULL)
734 			error = cv_wait_sig(&ks->ks_cv, &sem_lock);
735 		else {
736 			for (;;) {
737 				ts1 = *abstime;
738 				getnanotime(&ts2);
739 				timespecsub(&ts1, &ts2);
740 				TIMESPEC_TO_TIMEVAL(&tv, &ts1);
741 				if (tv.tv_sec < 0) {
742 					error = ETIMEDOUT;
743 					break;
744 				}
745 				error = cv_timedwait_sig(&ks->ks_cv,
746 				    &sem_lock, tvtohz(&tv));
747 				if (error != EWOULDBLOCK)
748 					break;
749 			}
750 		}
751 		ks->ks_waiters--;
752 		if (error)
753 			goto err;
754 	}
755 	ks->ks_value--;
756 	DP(("kern_sem_wait value post-decrement = %d\n", ks->ks_value));
757 	error = 0;
758 err:
759 	mtx_unlock(&sem_lock);
760 	fdrop(fp, td);
761 	DP(("<<< kern_sem_wait leaving, pid=%d, error = %d\n",
762 	    (int)td->td_proc->p_pid, error));
763 	return (error);
764 }
765 
766 #ifndef _SYS_SYSPROTO_H_
767 struct ksem_getvalue_args {
768 	semid_t		id;
769 	int		*val;
770 };
771 #endif
772 int
773 ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
774 {
775 	struct file *fp;
776 	struct ksem *ks;
777 	int error, val;
778 
779 	error = ksem_get(td, uap->id, &fp);
780 	if (error)
781 		return (error);
782 	ks = fp->f_data;
783 
784 	mtx_lock(&sem_lock);
785 #ifdef MAC
786 	error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
787 	if (error) {
788 		mtx_unlock(&sem_lock);
789 		fdrop(fp, td);
790 		return (error);
791 	}
792 #endif
793 	val = ks->ks_value;
794 	vfs_timestamp(&ks->ks_atime);
795 	mtx_unlock(&sem_lock);
796 	fdrop(fp, td);
797 	error = copyout(&val, uap->val, sizeof(val));
798 	return (error);
799 }
800 
801 #ifndef _SYS_SYSPROTO_H_
802 struct ksem_destroy_args {
803 	semid_t		id;
804 };
805 #endif
806 int
807 ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
808 {
809 	struct file *fp;
810 	struct ksem *ks;
811 	int error;
812 
813 	error = ksem_get(td, uap->id, &fp);
814 	if (error)
815 		return (error);
816 	ks = fp->f_data;
817 	if (!(ks->ks_flags & KS_ANONYMOUS)) {
818 		fdrop(fp, td);
819 		return (EINVAL);
820 	}
821 	mtx_lock(&sem_lock);
822 	if (ks->ks_waiters != 0) {
823 		mtx_unlock(&sem_lock);
824 		error = EBUSY;
825 		goto err;
826 	}
827 	ks->ks_flags |= KS_DEAD;
828 	mtx_unlock(&sem_lock);
829 
830 	error = kern_close(td, uap->id);
831 err:
832 	fdrop(fp, td);
833 	return (error);
834 }
835 
836 #define	SYSCALL_DATA(syscallname)				\
837 static int syscallname##_syscall = SYS_##syscallname;		\
838 static int syscallname##_registered;				\
839 static struct sysent syscallname##_old_sysent;			\
840 MAKE_SYSENT(syscallname);
841 
842 #define	SYSCALL_REGISTER(syscallname) do {				\
843 	error = syscall_register(& syscallname##_syscall,		\
844 	    & syscallname##_sysent, & syscallname##_old_sysent);	\
845 	if (error)							\
846 		return (error);						\
847 	syscallname##_registered = 1;					\
848 } while(0)
849 
850 #define	SYSCALL_DEREGISTER(syscallname) do {				\
851 	if (syscallname##_registered) {					\
852 		syscallname##_registered = 0;				\
853 		syscall_deregister(& syscallname##_syscall,		\
854 		    & syscallname##_old_sysent);			\
855 	}								\
856 } while(0)
857 
858 SYSCALL_DATA(ksem_init);
859 SYSCALL_DATA(ksem_open);
860 SYSCALL_DATA(ksem_unlink);
861 SYSCALL_DATA(ksem_close);
862 SYSCALL_DATA(ksem_post);
863 SYSCALL_DATA(ksem_wait);
864 SYSCALL_DATA(ksem_timedwait);
865 SYSCALL_DATA(ksem_trywait);
866 SYSCALL_DATA(ksem_getvalue);
867 SYSCALL_DATA(ksem_destroy);
868 
869 static int
870 ksem_module_init(void)
871 {
872 	int error;
873 
874 	mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
875 	mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
876 	sx_init(&ksem_dict_lock, "ksem dictionary");
877 	ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
878 	p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
879 	p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
880 
881 	SYSCALL_REGISTER(ksem_init);
882 	SYSCALL_REGISTER(ksem_open);
883 	SYSCALL_REGISTER(ksem_unlink);
884 	SYSCALL_REGISTER(ksem_close);
885 	SYSCALL_REGISTER(ksem_post);
886 	SYSCALL_REGISTER(ksem_wait);
887 	SYSCALL_REGISTER(ksem_timedwait);
888 	SYSCALL_REGISTER(ksem_trywait);
889 	SYSCALL_REGISTER(ksem_getvalue);
890 	SYSCALL_REGISTER(ksem_destroy);
891 	return (0);
892 }
893 
894 static void
895 ksem_module_destroy(void)
896 {
897 
898 	SYSCALL_DEREGISTER(ksem_init);
899 	SYSCALL_DEREGISTER(ksem_open);
900 	SYSCALL_DEREGISTER(ksem_unlink);
901 	SYSCALL_DEREGISTER(ksem_close);
902 	SYSCALL_DEREGISTER(ksem_post);
903 	SYSCALL_DEREGISTER(ksem_wait);
904 	SYSCALL_DEREGISTER(ksem_timedwait);
905 	SYSCALL_DEREGISTER(ksem_trywait);
906 	SYSCALL_DEREGISTER(ksem_getvalue);
907 	SYSCALL_DEREGISTER(ksem_destroy);
908 
909 	hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
910 	sx_destroy(&ksem_dict_lock);
911 	mtx_destroy(&ksem_count_lock);
912 	mtx_destroy(&sem_lock);
913 }
914 
915 static int
916 sem_modload(struct module *module, int cmd, void *arg)
917 {
918         int error = 0;
919 
920         switch (cmd) {
921         case MOD_LOAD:
922 		error = ksem_module_init();
923 		if (error)
924 			ksem_module_destroy();
925                 break;
926 
927         case MOD_UNLOAD:
928 		mtx_lock(&ksem_count_lock);
929 		if (nsems != 0) {
930 			error = EOPNOTSUPP;
931 			mtx_unlock(&ksem_count_lock);
932 			break;
933 		}
934 		ksem_dead = 1;
935 		mtx_unlock(&ksem_count_lock);
936 		ksem_module_destroy();
937                 break;
938 
939         case MOD_SHUTDOWN:
940                 break;
941         default:
942                 error = EINVAL;
943                 break;
944         }
945         return (error);
946 }
947 
948 static moduledata_t sem_mod = {
949         "sem",
950         &sem_modload,
951         NULL
952 };
953 
954 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
955 MODULE_VERSION(sem, 1);
956