2 * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
3 * Copyright (c) 2003-2005 SPARTA, Inc.
4 * Copyright (c) 2005 Robert N. M. Watson
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_posix.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/module.h>
49 #include <sys/condvar.h>
52 #include <sys/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/sysctl.h>
58 #include <sys/malloc.h>
59 #include <sys/fcntl.h>
61 #include <posix4/ksem.h>
62 #include <posix4/posix4.h>
63 #include <posix4/semaphore.h>
64 #include <posix4/_semaphore.h>
66 static int sem_count_proc(struct proc *p);
67 static struct ksem *sem_lookup_byname(const char *name);
68 static int sem_create(struct thread *td, const char *name,
69 struct ksem **ksret, mode_t mode, unsigned int value);
70 static void sem_free(struct ksem *ksnew);
71 static int sem_perm(struct thread *td, struct ksem *ks);
72 static void sem_enter(struct proc *p, struct ksem *ks);
73 static int sem_leave(struct proc *p, struct ksem *ks);
74 static void sem_exithook(void *arg, struct proc *p);
75 static void sem_forkhook(void *arg, struct proc *p1, struct proc *p2,
77 static int sem_hasopen(struct thread *td, struct ksem *ks);
79 static int kern_sem_close(struct thread *td, semid_t id);
80 static int kern_sem_post(struct thread *td, semid_t id);
81 static int kern_sem_wait(struct thread *td, semid_t id, int tryflag,
82 struct timespec *abstime);
83 static int kern_sem_init(struct thread *td, int dir, unsigned int value,
85 static int kern_sem_open(struct thread *td, int dir, const char *name,
86 int oflag, mode_t mode, unsigned int value, semid_t *idp);
87 static int kern_sem_unlink(struct thread *td, const char *name);
93 #define SEM_MAX_NAMELEN 14
95 #define SEM_TO_ID(x) ((intptr_t)(x))
96 #define ID_TO_SEM(x) id_to_sem(x)
99 * available semaphores go here, this includes sem_init and any semaphores
100 * created via sem_open that have not yet been unlinked.
102 LIST_HEAD(, ksem) ksem_head = LIST_HEAD_INITIALIZER(&ksem_head);
104 * semaphores still in use but have been sem_unlink()'d go here.
106 LIST_HEAD(, ksem) ksem_deadhead = LIST_HEAD_INITIALIZER(&ksem_deadhead);
108 static struct mtx sem_lock;
109 static MALLOC_DEFINE(M_SEM, "sems", "semaphore data");
111 static int nsems = 0;
112 SYSCTL_DECL(_p1003_1b);
113 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0, "");
115 static eventhandler_tag sem_exit_tag, sem_exec_tag, sem_fork_tag;
118 #define DP(x) printf x
125 sem_ref(struct ksem *ks)
128 mtx_assert(&sem_lock, MA_OWNED);
130 DP(("sem_ref: ks = %p, ref = %d\n", ks, ks->ks_ref));
135 sem_rel(struct ksem *ks)
138 mtx_assert(&sem_lock, MA_OWNED);
139 DP(("sem_rel: ks = %p, ref = %d\n", ks, ks->ks_ref - 1));
140 if (--ks->ks_ref == 0)
144 static __inline struct ksem *id_to_sem(semid_t id);
153 mtx_assert(&sem_lock, MA_OWNED);
154 DP(("id_to_sem: id = %0x,%p\n", id, (struct ksem *)id));
155 LIST_FOREACH(ks, &ksem_head, ks_entry) {
156 DP(("id_to_sem: ks = %p\n", ks));
157 if (ks == (struct ksem *)id)
164 sem_lookup_byname(name)
169 mtx_assert(&sem_lock, MA_OWNED);
170 LIST_FOREACH(ks, &ksem_head, ks_entry)
171 if (ks->ks_name != NULL && strcmp(ks->ks_name, name) == 0)
177 sem_create(td, name, ksret, mode, value)
190 DP(("sem_create\n"));
193 if (value > SEM_VALUE_MAX)
195 ret = malloc(sizeof(*ret), M_SEM, M_WAITOK | M_ZERO);
198 if (len > SEM_MAX_NAMELEN) {
200 return (ENAMETOOLONG);
202 /* name must start with a '/' but not contain one. */
203 if (*name != '/' || len < 2 || index(name + 1, '/') != NULL) {
207 ret->ks_name = malloc(len + 1, M_SEM, M_WAITOK);
208 strcpy(ret->ks_name, name);
213 ret->ks_value = value;
216 ret->ks_uid = uc->cr_uid;
217 ret->ks_gid = uc->cr_gid;
219 cv_init(&ret->ks_cv, "sem");
220 LIST_INIT(&ret->ks_users);
222 mac_init_posix_sem(ret);
223 mac_create_posix_sem(uc, ret);
226 sem_enter(td->td_proc, ret);
229 if (nsems >= p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX)) {
230 sem_leave(td->td_proc, ret);
237 mtx_unlock(&sem_lock);
241 #ifndef _SYS_SYSPROTO_H_
242 struct ksem_init_args {
246 int ksem_init(struct thread *td, struct ksem_init_args *uap);
251 struct ksem_init_args *uap;
255 error = kern_sem_init(td, UIO_USERSPACE, uap->value, uap->idp);
260 kern_sem_init(td, dir, value, idp)
270 error = sem_create(td, NULL, &ks, S_IRWXU | S_IRWXG, value);
274 if (dir == UIO_USERSPACE) {
275 error = copyout(&id, idp, sizeof(id));
279 mtx_unlock(&sem_lock);
286 LIST_INSERT_HEAD(&ksem_head, ks, ks_entry);
288 mtx_unlock(&sem_lock);
292 #ifndef _SYS_SYSPROTO_H_
293 struct ksem_open_args {
300 int ksem_open(struct thread *td, struct ksem_open_args *uap);
305 struct ksem_open_args *uap;
307 char name[SEM_MAX_NAMELEN + 1];
311 error = copyinstr(uap->name, name, SEM_MAX_NAMELEN + 1, &done);
314 DP((">>> sem_open start\n"));
315 error = kern_sem_open(td, UIO_USERSPACE,
316 name, uap->oflag, uap->mode, uap->value, uap->idp);
317 DP(("<<< sem_open end\n"));
322 kern_sem_open(td, dir, name, oflag, mode, value, idp)
331 struct ksem *ksnew, *ks;
337 ks = sem_lookup_byname(name);
339 * If we found it but O_EXCL is set, error.
341 if (ks != NULL && (oflag & O_EXCL) != 0) {
342 mtx_unlock(&sem_lock);
346 * If we didn't find it...
350 * didn't ask for creation? error.
352 if ((oflag & O_CREAT) == 0) {
353 mtx_unlock(&sem_lock);
357 * We may block during creation, so drop the lock.
359 mtx_unlock(&sem_lock);
360 error = sem_create(td, name, &ksnew, mode, value);
363 id = SEM_TO_ID(ksnew);
364 if (dir == UIO_USERSPACE) {
365 DP(("about to copyout! %d to %p\n", id, idp));
366 error = copyout(&id, idp, sizeof(id));
369 sem_leave(td->td_proc, ksnew);
371 mtx_unlock(&sem_lock);
375 DP(("about to set! %d to %p\n", id, idp));
379 * We need to make sure we haven't lost a race while
380 * allocating during creation.
383 ks = sem_lookup_byname(name);
386 sem_leave(td->td_proc, ksnew);
388 /* we lost and we can't loose... */
389 if ((oflag & O_EXCL) != 0) {
390 mtx_unlock(&sem_lock);
394 DP(("sem_create: about to add to list...\n"));
395 LIST_INSERT_HEAD(&ksem_head, ksnew, ks_entry);
396 DP(("sem_create: setting list bit...\n"));
397 ksnew->ks_onlist = 1;
398 DP(("sem_create: done, about to unlock...\n"));
402 error = mac_check_posix_sem_open(td->td_ucred, ks);
407 * if we aren't the creator, then enforce permissions.
409 error = sem_perm(td, ks);
413 mtx_unlock(&sem_lock);
415 if (dir == UIO_USERSPACE) {
416 error = copyout(&id, idp, sizeof(id));
420 mtx_unlock(&sem_lock);
426 sem_enter(td->td_proc, ks);
431 mtx_unlock(&sem_lock);
443 DP(("sem_perm: uc(%d,%d) ks(%d,%d,%o)\n",
444 uc->cr_uid, uc->cr_gid,
445 ks->ks_uid, ks->ks_gid, ks->ks_mode));
446 if ((uc->cr_uid == ks->ks_uid && (ks->ks_mode & S_IWUSR) != 0) ||
447 (uc->cr_gid == ks->ks_gid && (ks->ks_mode & S_IWGRP) != 0) ||
448 (ks->ks_mode & S_IWOTH) != 0 || suser(td) == 0)
454 sem_free(struct ksem *ks)
459 LIST_REMOVE(ks, ks_entry);
460 if (ks->ks_name != NULL)
461 free(ks->ks_name, M_SEM);
462 cv_destroy(&ks->ks_cv);
466 static __inline struct kuser *sem_getuser(struct proc *p, struct ksem *ks);
468 static __inline struct kuser *
475 LIST_FOREACH(k, &ks->ks_users, ku_next)
476 if (k->ku_pid == p->p_pid)
487 return ((ks->ks_name == NULL && sem_perm(td, ks) == 0)
488 || sem_getuser(td->td_proc, ks) != NULL);
498 DP(("sem_leave: ks = %p\n", ks));
499 k = sem_getuser(p, ks);
500 DP(("sem_leave: ks = %p, k = %p\n", ks, k));
502 LIST_REMOVE(k, ku_next);
504 DP(("sem_leave: about to free k\n"));
506 DP(("sem_leave: returning\n"));
517 struct kuser *ku, *k;
519 ku = malloc(sizeof(*ku), M_SEM, M_WAITOK);
520 ku->ku_pid = p->p_pid;
522 k = sem_getuser(p, ks);
524 mtx_unlock(&sem_lock);
528 LIST_INSERT_HEAD(&ks->ks_users, ku, ku_next);
530 mtx_unlock(&sem_lock);
533 #ifndef _SYS_SYSPROTO_H_
534 struct ksem_unlink_args {
537 int ksem_unlink(struct thread *td, struct ksem_unlink_args *uap);
543 struct ksem_unlink_args *uap;
545 char name[SEM_MAX_NAMELEN + 1];
549 error = copyinstr(uap->name, name, SEM_MAX_NAMELEN + 1, &done);
550 return (error ? error :
551 kern_sem_unlink(td, name));
555 kern_sem_unlink(td, name)
563 ks = sem_lookup_byname(name);
566 error = mac_check_posix_sem_unlink(td->td_ucred, ks);
568 mtx_unlock(&sem_lock);
572 error = sem_perm(td, ks);
575 DP(("sem_unlink: '%s' ks = %p, error = %d\n", name, ks, error));
577 LIST_REMOVE(ks, ks_entry);
578 LIST_INSERT_HEAD(&ksem_deadhead, ks, ks_entry);
581 mtx_unlock(&sem_lock);
585 #ifndef _SYS_SYSPROTO_H_
586 struct ksem_close_args {
589 int ksem_close(struct thread *td, struct ksem_close_args *uap);
593 ksem_close(struct thread *td, struct ksem_close_args *uap)
596 return (kern_sem_close(td, uap->id));
600 kern_sem_close(td, id)
610 /* this is not a valid operation for unnamed sems */
611 if (ks != NULL && ks->ks_name != NULL)
612 error = sem_leave(td->td_proc, ks);
613 mtx_unlock(&sem_lock);
617 #ifndef _SYS_SYSPROTO_H_
618 struct ksem_post_args {
621 int ksem_post(struct thread *td, struct ksem_post_args *uap);
626 struct ksem_post_args *uap;
629 return (kern_sem_post(td, uap->id));
633 kern_sem_post(td, id)
642 if (ks == NULL || !sem_hasopen(td, ks)) {
647 error = mac_check_posix_sem_post(td->td_ucred, ks);
651 if (ks->ks_value == SEM_VALUE_MAX) {
656 if (ks->ks_waiters > 0)
657 cv_signal(&ks->ks_cv);
660 mtx_unlock(&sem_lock);
664 #ifndef _SYS_SYSPROTO_H_
665 struct ksem_wait_args {
668 int ksem_wait(struct thread *td, struct ksem_wait_args *uap);
674 struct ksem_wait_args *uap;
677 return (kern_sem_wait(td, uap->id, 0, NULL));
680 #ifndef _SYS_SYSPROTO_H_
681 struct ksem_timedwait_args {
683 const struct timespec *abstime;
685 int ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap);
688 ksem_timedwait(td, uap)
690 struct ksem_timedwait_args *uap;
692 struct timespec abstime;
696 /* We allow a null timespec (wait forever). */
697 if (uap->abstime == NULL)
700 error = copyin(uap->abstime, &abstime, sizeof(abstime));
703 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
707 return (kern_sem_wait(td, uap->id, 0, ts));
710 #ifndef _SYS_SYSPROTO_H_
711 struct ksem_trywait_args {
714 int ksem_trywait(struct thread *td, struct ksem_trywait_args *uap);
717 ksem_trywait(td, uap)
719 struct ksem_trywait_args *uap;
722 return (kern_sem_wait(td, uap->id, 1, NULL));
726 kern_sem_wait(td, id, tryflag, abstime)
730 struct timespec *abstime;
732 struct timespec ts1, ts2;
737 DP((">>> kern_sem_wait entered!\n"));
741 DP(("kern_sem_wait ks == NULL\n"));
746 if (!sem_hasopen(td, ks)) {
747 DP(("kern_sem_wait hasopen failed\n"));
752 error = mac_check_posix_sem_wait(td->td_ucred, ks);
754 DP(("kern_sem_wait mac failed\n"));
758 DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
759 if (ks->ks_value == 0) {
763 else if (abstime == NULL)
764 error = cv_wait_sig(&ks->ks_cv, &sem_lock);
769 timespecsub(&ts1, &ts2);
770 TIMESPEC_TO_TIMEVAL(&tv, &ts1);
775 error = cv_timedwait_sig(&ks->ks_cv,
776 &sem_lock, tvtohz(&tv));
777 if (error != EWOULDBLOCK)
790 mtx_unlock(&sem_lock);
791 DP(("<<< kern_sem_wait leaving, error = %d\n", error));
795 #ifndef _SYS_SYSPROTO_H_
796 struct ksem_getvalue_args {
800 int ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap);
803 ksem_getvalue(td, uap)
805 struct ksem_getvalue_args *uap;
811 ks = ID_TO_SEM(uap->id);
812 if (ks == NULL || !sem_hasopen(td, ks)) {
813 mtx_unlock(&sem_lock);
817 error = mac_check_posix_sem_getvalue(td->td_ucred, ks);
819 mtx_unlock(&sem_lock);
824 mtx_unlock(&sem_lock);
825 error = copyout(&val, uap->val, sizeof(val));
829 #ifndef _SYS_SYSPROTO_H_
830 struct ksem_destroy_args {
833 int ksem_destroy(struct thread *td, struct ksem_destroy_args *uap);
836 ksem_destroy(td, uap)
838 struct ksem_destroy_args *uap;
844 ks = ID_TO_SEM(uap->id);
845 if (ks == NULL || !sem_hasopen(td, ks) ||
846 ks->ks_name != NULL) {
851 error = mac_check_posix_sem_destroy(td->td_ucred, ks);
855 if (ks->ks_waiters != 0) {
862 mtx_unlock(&sem_lock);
867 * Count the number of kusers associated with a proc, so as to guess at how
868 * many to allocate when forking.
878 mtx_assert(&sem_lock, MA_OWNED);
881 LIST_FOREACH(ks, &ksem_head, ks_entry) {
882 LIST_FOREACH(ku, &ks->ks_users, ku_next) {
883 if (ku->ku_pid == p->p_pid)
887 LIST_FOREACH(ks, &ksem_deadhead, ks_entry) {
888 LIST_FOREACH(ku, &ks->ks_users, ku_next) {
889 if (ku->ku_pid == p->p_pid)
897 * When a process forks, the child process must gain a reference to each open
898 * semaphore in the parent process, whether it is unlinked or not. This
899 * requires allocating a kuser structure for each semaphore reference in the
900 * new process. Because the set of semaphores in the parent can change while
901 * the fork is in progress, we have to handle races -- first we attempt to
902 * allocate enough storage to acquire references to each of the semaphores,
903 * then we enter the semaphores and release the temporary references.
906 sem_forkhook(arg, p1, p2, flags)
912 struct ksem *ks, **sem_array;
913 int count, i, new_count;
917 count = sem_count_proc(p1);
919 mtx_unlock(&sem_lock);
923 mtx_assert(&sem_lock, MA_OWNED);
924 mtx_unlock(&sem_lock);
925 sem_array = malloc(sizeof(struct ksem *) * count, M_TEMP, M_WAITOK);
927 new_count = sem_count_proc(p1);
928 if (count < new_count) {
929 /* Lost race, repeat and allocate more storage. */
930 free(sem_array, M_TEMP);
935 * Given an array capable of storing an adequate number of semaphore
936 * references, now walk the list of semaphores and acquire a new
937 * reference for any semaphore opened by p1.
941 LIST_FOREACH(ks, &ksem_head, ks_entry) {
942 LIST_FOREACH(ku, &ks->ks_users, ku_next) {
943 if (ku->ku_pid == p1->p_pid) {
951 LIST_FOREACH(ks, &ksem_deadhead, ks_entry) {
952 LIST_FOREACH(ku, &ks->ks_users, ku_next) {
953 if (ku->ku_pid == p1->p_pid) {
961 mtx_unlock(&sem_lock);
962 KASSERT(i == count, ("sem_forkhook: i != count (%d, %d)", i, count));
964 * Now cause p2 to enter each of the referenced semaphores, then
965 * release our temporary reference. This is pretty inefficient.
966 * Finally, free our temporary array.
968 for (i = 0; i < count; i++) {
969 sem_enter(p2, sem_array[i]);
971 sem_rel(sem_array[i]);
972 mtx_unlock(&sem_lock);
974 free(sem_array, M_TEMP);
982 struct ksem *ks, *ksnext;
985 ks = LIST_FIRST(&ksem_head);
987 ksnext = LIST_NEXT(ks, ks_entry);
991 ks = LIST_FIRST(&ksem_deadhead);
993 ksnext = LIST_NEXT(ks, ks_entry);
997 mtx_unlock(&sem_lock);
1001 sem_modload(struct module *module, int cmd, void *arg)
1007 mtx_init(&sem_lock, "sem", "semaphore", MTX_DEF);
1008 p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
1009 p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
1010 sem_exit_tag = EVENTHANDLER_REGISTER(process_exit, sem_exithook,
1011 NULL, EVENTHANDLER_PRI_ANY);
1012 sem_exec_tag = EVENTHANDLER_REGISTER(process_exec, sem_exithook,
1013 NULL, EVENTHANDLER_PRI_ANY);
1014 sem_fork_tag = EVENTHANDLER_REGISTER(process_fork, sem_forkhook, NULL, EVENTHANDLER_PRI_ANY);
1021 EVENTHANDLER_DEREGISTER(process_exit, sem_exit_tag);
1022 EVENTHANDLER_DEREGISTER(process_exec, sem_exec_tag);
1023 EVENTHANDLER_DEREGISTER(process_fork, sem_fork_tag);
1024 mtx_destroy(&sem_lock);
1035 static moduledata_t sem_mod = {
1041 SYSCALL_MODULE_HELPER(ksem_init);
1042 SYSCALL_MODULE_HELPER(ksem_open);
1043 SYSCALL_MODULE_HELPER(ksem_unlink);
1044 SYSCALL_MODULE_HELPER(ksem_close);
1045 SYSCALL_MODULE_HELPER(ksem_post);
1046 SYSCALL_MODULE_HELPER(ksem_wait);
1047 SYSCALL_MODULE_HELPER(ksem_timedwait);
1048 SYSCALL_MODULE_HELPER(ksem_trywait);
1049 SYSCALL_MODULE_HELPER(ksem_getvalue);
1050 SYSCALL_MODULE_HELPER(ksem_destroy);
1052 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
1053 MODULE_VERSION(sem, 1);