Home | History | Annotate | Line # | Download | only in librt
t_sem.c revision 1.2
      1 /* $NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright (C) 2000 Jason Evans <jasone (at) freebsd.org>.
     31  * All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice(s), this list of conditions and the following disclaimer as
     38  *    the first lines of this file unmodified other than the possible
     39  *    addition of one or more copyright notices.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice(s), this list of conditions and the following disclaimer in
     42  *    the documentation and/or other materials provided with the
     43  *    distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
     46  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
     49  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     52  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     54  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     55  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
     60  The NetBSD Foundation, inc. All rights reserved.");
     61 __RCSID("$NetBSD: t_sem.c,v 1.2 2010/11/08 13:05:49 njoly Exp $");
     62 
     63 #include <sys/wait.h>
     64 
     65 #include <errno.h>
     66 #include <fcntl.h>
     67 #include <semaphore.h>
     68 #include <stdio.h>
     69 #include <unistd.h>
     70 
     71 #include <atf-c.h>
     72 
     73 #define NCHILDREN 10
     74 
     75 ATF_TC(basic);
     76 ATF_TC_HEAD(basic, tc)
     77 {
     78 	atf_tc_set_md_var(tc, "descr", "Checks basic functionality of POSIX "
     79 	    "semaphores");
     80 }
     81 ATF_TC_BODY(basic, tc)
     82 {
     83 	int val;
     84 	sem_t *sem_b;
     85 
     86 	if (sysconf(_SC_SEMAPHORES) == -1)
     87 		atf_tc_skip("POSIX semaphores not supported");
     88 
     89 	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
     90 	ATF_REQUIRE(sem_b != SEM_FAILED);
     91 
     92 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
     93 	ATF_REQUIRE_EQ(val, 0);
     94 
     95 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
     96 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
     97 	ATF_REQUIRE_EQ(val, 1);
     98 
     99 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
    100 	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
    101 	ATF_REQUIRE_EQ(errno, EAGAIN);
    102 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    103 	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
    104 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    105 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
    106 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    107 
    108 	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
    109 	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
    110 }
    111 
    112 ATF_TC(child);
    113 ATF_TC_HEAD(child, tc)
    114 {
    115 	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
    116 	    "parent with multiple child processes");
    117 }
    118 ATF_TC_BODY(child, tc)
    119 {
    120 	pid_t children[NCHILDREN];
    121 	unsigned i, j;
    122 	sem_t *sem_a;
    123 	int status;
    124 
    125 	pid_t pid;
    126 
    127 	if (sysconf(_SC_SEMAPHORES) == -1)
    128 		atf_tc_skip("POSIX semaphores not supported");
    129 
    130 	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
    131 	ATF_REQUIRE(sem_a != SEM_FAILED);
    132 
    133 	for (j = 1; j <= 2; j++) {
    134 		for (i = 0; i < NCHILDREN; i++) {
    135 			switch ((pid = fork())) {
    136 			case -1:
    137 				atf_tc_fail("fork() returned -1");
    138 			case 0:
    139 				printf("PID %d waiting for semaphore...\n",
    140 				    getpid());
    141 				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
    142 				    "sem_wait failed; iteration %d", j);
    143 				printf("PID %d got semaphore\n", getpid());
    144 				_exit(0);
    145 			default:
    146 				children[i] = pid;
    147 				break;
    148 			}
    149 		}
    150 
    151 		for (i = 0; i < NCHILDREN; i++) {
    152 			sleep(1);
    153 			printf("main loop %d: posting...\n", j);
    154 			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
    155 		}
    156 
    157 		for (i = 0; i < NCHILDREN; i++) {
    158 			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
    159 			ATF_REQUIRE(WIFEXITED(status));
    160 			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
    161 		}
    162 	}
    163 
    164 	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
    165 	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
    166 }
    167 
    168 ATF_TP_ADD_TCS(tp)
    169 {
    170 
    171 	ATF_TP_ADD_TC(tp, basic);
    172 	ATF_TP_ADD_TC(tp, child);
    173 
    174 	return atf_no_error();
    175 }
    176