1 1.1 thorpej /* $NetBSD: t_ksem.c,v 1.1 2019/02/03 03:20:24 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Redistribution and use in source and binary forms, with or without 8 1.1 thorpej * modification, are permitted provided that the following conditions 9 1.1 thorpej * are met: 10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 11 1.1 thorpej * notice, this list of conditions and the following disclaimer. 12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 14 1.1 thorpej * documentation and/or other materials provided with the distribution. 15 1.1 thorpej * 16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 27 1.1 thorpej */ 28 1.1 thorpej 29 1.1 thorpej #include <sys/cdefs.h> 30 1.1 thorpej __COPYRIGHT("@(#) Copyright (c) 2019\ 31 1.1 thorpej The NetBSD Foundation, inc. All rights reserved."); 32 1.1 thorpej __RCSID("$NetBSD: t_ksem.c,v 1.1 2019/02/03 03:20:24 thorpej Exp $"); 33 1.1 thorpej 34 1.1 thorpej #include <sys/mman.h> 35 1.1 thorpej #include <sys/wait.h> 36 1.1 thorpej 37 1.1 thorpej #include <errno.h> 38 1.1 thorpej #include <fcntl.h> 39 1.1 thorpej #include <stdio.h> 40 1.1 thorpej #include <unistd.h> 41 1.1 thorpej 42 1.1 thorpej #include <atf-c.h> 43 1.1 thorpej 44 1.1 thorpej #define _LIBC 45 1.1 thorpej #include <sys/ksem.h> 46 1.1 thorpej 47 1.1 thorpej ATF_TC(close_on_unnamed); 48 1.1 thorpej ATF_TC_HEAD(close_on_unnamed, tc) 49 1.1 thorpej { 50 1.1 thorpej atf_tc_set_md_var(tc, "descr", 51 1.1 thorpej "test for correct error on close of unnamed semaphore"); 52 1.1 thorpej } 53 1.1 thorpej ATF_TC_BODY(close_on_unnamed, tc) 54 1.1 thorpej { 55 1.1 thorpej intptr_t ksem; 56 1.1 thorpej 57 1.1 thorpej /* _ksem_close() is invalid on unnamed semaphore. */ 58 1.1 thorpej ksem = 0; 59 1.1 thorpej ATF_REQUIRE_EQ(_ksem_init(0, &ksem), 0); 60 1.1 thorpej ATF_REQUIRE(_ksem_close(ksem) == -1 && errno == EINVAL); 61 1.1 thorpej ATF_REQUIRE_EQ(_ksem_destroy(ksem), 0); 62 1.1 thorpej } 63 1.1 thorpej 64 1.1 thorpej ATF_TC(close_on_unnamed_pshared); 65 1.1 thorpej ATF_TC_HEAD(close_on_unnamed_pshared, tc) 66 1.1 thorpej { 67 1.1 thorpej atf_tc_set_md_var(tc, "descr", 68 1.1 thorpej "test for correct error on close of unnamed pshared semaphore"); 69 1.1 thorpej } 70 1.1 thorpej ATF_TC_BODY(close_on_unnamed_pshared, tc) 71 1.1 thorpej { 72 1.1 thorpej intptr_t ksem; 73 1.1 thorpej 74 1.1 thorpej /* Similar, but lifecycle of pshared is slightly different. */ 75 1.1 thorpej ksem = KSEM_PSHARED; 76 1.1 thorpej ATF_REQUIRE_EQ(_ksem_init(0, &ksem), 0); 77 1.1 thorpej ATF_REQUIRE(_ksem_close(ksem) == -1 && errno == EINVAL); 78 1.1 thorpej ATF_REQUIRE_EQ(_ksem_destroy(ksem), 0); 79 1.1 thorpej } 80 1.1 thorpej 81 1.1 thorpej ATF_TC_WITH_CLEANUP(destroy_on_named); 82 1.1 thorpej ATF_TC_HEAD(destroy_on_named, tc) 83 1.1 thorpej { 84 1.1 thorpej atf_tc_set_md_var(tc, "descr", 85 1.1 thorpej "test for correct error on destroy of named semaphore"); 86 1.1 thorpej } 87 1.1 thorpej ATF_TC_BODY(destroy_on_named, tc) 88 1.1 thorpej { 89 1.1 thorpej intptr_t ksem; 90 1.1 thorpej 91 1.1 thorpej /* Exercise open-unlinked semaphore lifecycle. */ 92 1.1 thorpej ATF_REQUIRE_EQ(_ksem_open("/ksem_x", O_CREAT | O_EXCL, 0644, 0, &ksem), 93 1.1 thorpej 0); 94 1.1 thorpej ATF_REQUIRE(_ksem_destroy(ksem) == -1 && errno == EINVAL); 95 1.1 thorpej ATF_REQUIRE_EQ(_ksem_close(ksem), 0); 96 1.1 thorpej ATF_REQUIRE_EQ(_ksem_unlink("/ksem_x"), 0); 97 1.1 thorpej } 98 1.1 thorpej ATF_TC_CLEANUP(destroy_on_named, tc) 99 1.1 thorpej { 100 1.1 thorpej (void)_ksem_unlink("/ksem_x"); 101 1.1 thorpej } 102 1.1 thorpej 103 1.1 thorpej ATF_TC_WITH_CLEANUP(open_unlinked_lifecycle); 104 1.1 thorpej ATF_TC_HEAD(open_unlinked_lifecycle, tc) 105 1.1 thorpej { 106 1.1 thorpej atf_tc_set_md_var(tc, "descr", 107 1.1 thorpej "Exercises lifecycle of open-unlined semaphores"); 108 1.1 thorpej } 109 1.1 thorpej ATF_TC_BODY(open_unlinked_lifecycle, tc) 110 1.1 thorpej { 111 1.1 thorpej intptr_t ksem, ksem1; 112 1.1 thorpej int val; 113 1.1 thorpej 114 1.1 thorpej /* Exercise open-unlinked semaphore lifecycle. */ 115 1.1 thorpej ATF_REQUIRE_EQ(_ksem_open("/ksem_b", O_CREAT | O_EXCL, 0644, 0, &ksem), 116 1.1 thorpej 0); 117 1.1 thorpej ATF_REQUIRE_EQ(_ksem_unlink("/ksem_b"), 0); 118 1.1 thorpej val = 255; 119 1.1 thorpej ATF_REQUIRE(_ksem_getvalue(ksem, &val) == 0 && val == 0); 120 1.1 thorpej 121 1.1 thorpej ATF_REQUIRE_EQ(_ksem_open("/ksem_b", O_CREAT | O_EXCL, 0644, 1, &ksem1), 122 1.1 thorpej 0); 123 1.1 thorpej ATF_REQUIRE_EQ(_ksem_unlink("/ksem_b"), 0); 124 1.1 thorpej val = 255; 125 1.1 thorpej ATF_REQUIRE(_ksem_getvalue(ksem1, &val) == 0 && val == 1); 126 1.1 thorpej 127 1.1 thorpej ATF_REQUIRE_EQ(_ksem_close(ksem), 0); 128 1.1 thorpej ATF_REQUIRE_EQ(_ksem_close(ksem1), 0); 129 1.1 thorpej } 130 1.1 thorpej ATF_TC_CLEANUP(open_unlinked_lifecycle, tc) 131 1.1 thorpej { 132 1.1 thorpej (void)_ksem_unlink("/ksem_a"); 133 1.1 thorpej (void)_ksem_unlink("/ksem_b"); 134 1.1 thorpej } 135 1.1 thorpej 136 1.1 thorpej ATF_TP_ADD_TCS(tp) 137 1.1 thorpej { 138 1.1 thorpej 139 1.1 thorpej ATF_TP_ADD_TC(tp, close_on_unnamed); 140 1.1 thorpej ATF_TP_ADD_TC(tp, close_on_unnamed_pshared); 141 1.1 thorpej ATF_TP_ADD_TC(tp, destroy_on_named); 142 1.1 thorpej ATF_TP_ADD_TC(tp, open_unlinked_lifecycle); 143 1.1 thorpej 144 1.1 thorpej return atf_no_error(); 145 1.1 thorpej } 146