| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: uipc_sem.c,v 1.31 2011/04/12 20:37:25 rmind Exp $ */ | | 1 | /* $NetBSD: uipc_sem.c,v 1.32 2011/04/14 00:32:23 rmind Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Mindaugas Rasiukevicius. | | 8 | * by Mindaugas Rasiukevicius. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -50,27 +50,27 @@ | | | @@ -50,27 +50,27 @@ |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
55 | * SUCH DAMAGE. | | 55 | * SUCH DAMAGE. |
56 | */ | | 56 | */ |
57 | | | 57 | |
58 | /* | | 58 | /* |
59 | * Implementation of POSIX semaphore. | | 59 | * Implementation of POSIX semaphore. |
60 | */ | | 60 | */ |
61 | | | 61 | |
62 | #include <sys/cdefs.h> | | 62 | #include <sys/cdefs.h> |
63 | __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.31 2011/04/12 20:37:25 rmind Exp $"); | | 63 | __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.32 2011/04/14 00:32:23 rmind Exp $"); |
64 | | | 64 | |
65 | #include <sys/param.h> | | 65 | #include <sys/param.h> |
66 | #include <sys/systm.h> | | 66 | #include <sys/systm.h> |
67 | #include <sys/kernel.h> | | 67 | #include <sys/kernel.h> |
68 | #include <sys/proc.h> | | 68 | #include <sys/proc.h> |
69 | #include <sys/ksem.h> | | 69 | #include <sys/ksem.h> |
70 | #include <sys/syscall.h> | | 70 | #include <sys/syscall.h> |
71 | #include <sys/stat.h> | | 71 | #include <sys/stat.h> |
72 | #include <sys/kmem.h> | | 72 | #include <sys/kmem.h> |
73 | #include <sys/fcntl.h> | | 73 | #include <sys/fcntl.h> |
74 | #include <sys/file.h> | | 74 | #include <sys/file.h> |
75 | #include <sys/filedesc.h> | | 75 | #include <sys/filedesc.h> |
76 | #include <sys/kauth.h> | | 76 | #include <sys/kauth.h> |
| @@ -450,27 +450,27 @@ do_ksem_open(struct lwp *l, const char * | | | @@ -450,27 +450,27 @@ do_ksem_open(struct lwp *l, const char * |
450 | KASSERT(ksnew == NULL); | | 450 | KASSERT(ksnew == NULL); |
451 | error = ENOENT; | | 451 | error = ENOENT; |
452 | goto err; | | 452 | goto err; |
453 | } | | 453 | } |
454 | | | 454 | |
455 | /* Check for the limit locked. */ | | 455 | /* Check for the limit locked. */ |
456 | if (nsems >= ksem_max) { | | 456 | if (nsems >= ksem_max) { |
457 | mutex_exit(&ksem_lock); | | 457 | mutex_exit(&ksem_lock); |
458 | error = ENFILE; | | 458 | error = ENFILE; |
459 | goto err; | | 459 | goto err; |
460 | } | | 460 | } |
461 | | | 461 | |
462 | /* | | 462 | /* |
463 | * Finally, insert semaphore into the hash. | | 463 | * Finally, insert semaphore into the list. |
464 | * Note: it already has the initial reference. | | 464 | * Note: it already has the initial reference. |
465 | */ | | 465 | */ |
466 | ks = ksnew; | | 466 | ks = ksnew; |
467 | LIST_INSERT_HEAD(&ksem_head, ks, ks_entry); | | 467 | LIST_INSERT_HEAD(&ksem_head, ks, ks_entry); |
468 | nsems++; | | 468 | nsems++; |
469 | mutex_exit(&ksem_lock); | | 469 | mutex_exit(&ksem_lock); |
470 | | | 470 | |
471 | ksnew = NULL; | | 471 | ksnew = NULL; |
472 | } | | 472 | } |
473 | KASSERT(ks != NULL); | | 473 | KASSERT(ks != NULL); |
474 | fp->f_data = ks; | | 474 | fp->f_data = ks; |
475 | fd_affix(p, fp, fd); | | 475 | fd_affix(p, fp, fd); |
476 | err: | | 476 | err: |
| @@ -666,41 +666,39 @@ sys__ksem_getvalue(struct lwp *l, const | | | @@ -666,41 +666,39 @@ sys__ksem_getvalue(struct lwp *l, const |
666 | fd_putfile(fd); | | 666 | fd_putfile(fd); |
667 | | | 667 | |
668 | return copyout(&val, SCARG(uap, value), sizeof(val)); | | 668 | return copyout(&val, SCARG(uap, value), sizeof(val)); |
669 | } | | 669 | } |
670 | | | 670 | |
671 | int | | 671 | int |
672 | sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap, | | 672 | sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap, |
673 | register_t *retval) | | 673 | register_t *retval) |
674 | { | | 674 | { |
675 | /* { | | 675 | /* { |
676 | intptr_t id; | | 676 | intptr_t id; |
677 | } */ | | 677 | } */ |
678 | int fd = (int)SCARG(uap, id), error; | | 678 | int fd = (int)SCARG(uap, id), error; |
679 | struct sys_close_args cuap; | | | |
680 | ksem_t *ks; | | 679 | ksem_t *ks; |
681 | | | 680 | |
682 | error = ksem_get(fd, &ks); | | 681 | error = ksem_get(fd, &ks); |
683 | if (error) { | | 682 | if (error) { |
684 | return error; | | 683 | return error; |
685 | } | | 684 | } |
686 | KASSERT(mutex_owned(&ks->ks_lock)); | | 685 | KASSERT(mutex_owned(&ks->ks_lock)); |
687 | | | 686 | |
688 | /* Operation is only for unnamed semaphores. */ | | 687 | /* Operation is only for unnamed semaphores. */ |
689 | if (ks->ks_name != NULL) { | | 688 | if (ks->ks_name != NULL) { |
690 | error = EINVAL; | | 689 | error = EINVAL; |
691 | goto out; | | 690 | goto out; |
692 | } | | 691 | } |
693 | /* Cannot destroy if there are waiters. */ | | 692 | /* Cannot destroy if there are waiters. */ |
694 | if (ks->ks_waiters) { | | 693 | if (ks->ks_waiters) { |
695 | error = EBUSY; | | 694 | error = EBUSY; |
696 | goto out; | | 695 | goto out; |
697 | } | | 696 | } |
698 | out: | | 697 | out: |
699 | mutex_exit(&ks->ks_lock); | | 698 | mutex_exit(&ks->ks_lock); |
700 | fd_putfile(fd); | | | |
701 | if (error) { | | 699 | if (error) { |
| | | 700 | fd_putfile(fd); |
702 | return error; | | 701 | return error; |
703 | } | | 702 | } |
704 | SCARG(&cuap, fd) = fd; | | 703 | return fd_close(fd); |
705 | return sys_close(l, (const void *)&cuap, retval); | | | |
706 | } | | 704 | } |