Home | History | Annotate | Line # | Download | only in librt
t_sem.c revision 1.1
      1 /* $NetBSD: t_sem.c,v 1.1 2010/07/16 13:56:32 jmmv 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.1 2010/07/16 13:56:32 jmmv 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 	ATF_REQUIRE(sysconf(_SC_SEMAPHORES) != -1);
     87 
     88 	sem_b = sem_open("/sem_b", O_CREAT | O_EXCL, 0644, 0);
     89 	ATF_REQUIRE(sem_b != SEM_FAILED);
     90 
     91 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
     92 	ATF_REQUIRE_EQ(val, 0);
     93 
     94 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
     95 	ATF_REQUIRE_EQ(sem_getvalue(sem_b, &val), 0);
     96 	ATF_REQUIRE_EQ(val, 1);
     97 
     98 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
     99 	ATF_REQUIRE_EQ(sem_trywait(sem_b), -1);
    100 	ATF_REQUIRE_EQ(errno, EAGAIN);
    101 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    102 	ATF_REQUIRE_EQ(sem_trywait(sem_b), 0);
    103 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    104 	ATF_REQUIRE_EQ(sem_wait(sem_b), 0);
    105 	ATF_REQUIRE_EQ(sem_post(sem_b), 0);
    106 
    107 	ATF_REQUIRE_EQ(sem_close(sem_b), 0);
    108 	ATF_REQUIRE_EQ(sem_unlink("/sem_b"), 0);
    109 }
    110 
    111 ATF_TC(child);
    112 ATF_TC_HEAD(child, tc)
    113 {
    114 	atf_tc_set_md_var(tc, "descr", "Checks using semaphores to synchronize "
    115 	    "parent with multiple child processes");
    116 }
    117 ATF_TC_BODY(child, tc)
    118 {
    119 	pid_t children[NCHILDREN];
    120 	unsigned i, j;
    121 	sem_t *sem_a;
    122 	int status;
    123 
    124 	pid_t pid;
    125 
    126 	sem_a = sem_open("/sem_a", O_CREAT | O_EXCL, 0644, 0);
    127 	ATF_REQUIRE(sem_a != SEM_FAILED);
    128 
    129 	for (j = 1; j <= 2; j++) {
    130 		for (i = 0; i < NCHILDREN; i++) {
    131 			switch ((pid = fork())) {
    132 			case -1:
    133 				atf_tc_fail("fork() returned -1");
    134 			case 0:
    135 				printf("PID %d waiting for semaphore...\n",
    136 				    getpid());
    137 				ATF_REQUIRE_MSG(sem_wait(sem_a) == 0,
    138 				    "sem_wait failed; iteration %d", j);
    139 				printf("PID %d got semaphore\n", getpid());
    140 				_exit(0);
    141 			default:
    142 				children[i] = pid;
    143 				break;
    144 			}
    145 		}
    146 
    147 		for (i = 0; i < NCHILDREN; i++) {
    148 			sleep(1);
    149 			printf("main loop %d: posting...\n", j);
    150 			ATF_REQUIRE_EQ(sem_post(sem_a), 0);
    151 		}
    152 
    153 		for (i = 0; i < NCHILDREN; i++) {
    154 			ATF_REQUIRE_EQ(waitpid(children[i], &status, 0), children[i]);
    155 			ATF_REQUIRE(WIFEXITED(status));
    156 			ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
    157 		}
    158 	}
    159 
    160 	ATF_REQUIRE_EQ(sem_close(sem_a), 0);
    161 	ATF_REQUIRE_EQ(sem_unlink("/sem_a"), 0);
    162 }
    163 
    164 ATF_TP_ADD_TCS(tp)
    165 {
    166 
    167 	ATF_TP_ADD_TC(tp, basic);
    168 	ATF_TP_ADD_TC(tp, child);
    169 
    170 	return atf_no_error();
    171 }
    172