Home | History | Annotate | Download | only in sem_open
      1 /*
      2 * Copyright (c) 2005, Bull S.A..  All rights reserved.
      3 * Created by: Sebastien Decugis
      4 
      5 * This program is free software; you can redistribute it and/or modify it
      6 * under the terms of version 2 of the GNU General Public License as
      7 * published by the Free Software Foundation.
      8 *
      9 * This program is distributed in the hope that it would be useful, but
     10 * WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12 *
     13 * You should have received a copy of the GNU General Public License along
     14 * with this program; if not, write the Free Software Foundation, Inc.,
     15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16 
     17 * This sample test aims to check the following assertion:
     18 *
     19 *  If a process calls sem_open several times with the same name,
     20 * the same adress must be returned as long as the semaphore
     21 * has not been unlinked or closed as many times as opened.
     22 
     23 * The steps are:
     24 * -> Create a semaphore with sem_open
     25 * -> call sem_open several times with the same name.
     26 * -> Check that the same address is returned for the semaphore.
     27 
     28 * The test fails if a different address is returned.
     29 
     30 */
     31 
     32 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
     33 #define _POSIX_C_SOURCE 200112L
     34 
     35 /******************************************************************************/
     36 /*************************** standard includes ********************************/
     37 /******************************************************************************/
     38 #include <pthread.h>
     39 #include <stdarg.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 
     45 #include <semaphore.h>
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 
     49 /******************************************************************************/
     50 /***************************   Test framework   *******************************/
     51 /******************************************************************************/
     52 #include "../testfrmw/testfrmw.h"
     53 #include "../testfrmw/testfrmw.c"
     54 /* This header is responsible for defining the following macros:
     55  * UNRESOLVED(ret, descr);
     56  *    where descr is a description of the error and ret is an int
     57  *   (error code for example)
     58  * FAILED(descr);
     59  *    where descr is a short text saying why the test has failed.
     60  * PASSED();
     61  *    No parameter.
     62  *
     63  * Both three macros shall terminate the calling process.
     64  * The testcase shall not terminate in any other maneer.
     65  *
     66  * The other file defines the functions
     67  * void output_init()
     68  * void output(char * string, ...)
     69  *
     70  * Those may be used to output information.
     71  */
     72 
     73 /******************************************************************************/
     74 /**************************** Configuration ***********************************/
     75 /******************************************************************************/
     76 #ifndef VERBOSE
     77 #define VERBOSE 1
     78 #endif
     79 
     80 /******************************************************************************/
     81 /***************************    Test case   ***********************************/
     82 /******************************************************************************/
     83 
     84 /* The main test function. */
     85 int main(void)
     86 {
     87 	int ret, i;
     88 	char *name = "/sem_open_15_1";
     89 
     90 	sem_t *sems[4];
     91 
     92 	/* Initialize output */
     93 	output_init();
     94 
     95 	/* Initialize all semaphores */
     96 
     97 	for (i = 0; i < 4; i++) {
     98 		sems[i] = sem_open(name, O_CREAT, 0777, 1);
     99 
    100 		if (sems[i] == SEM_FAILED) {
    101 			UNRESOLVED(errno, "Failed to sem_open");
    102 		}
    103 
    104 	}
    105 
    106 	/* Check all calls returned the same @ */
    107 	for (i = 0; i < 3; i++) {
    108 		if (sems[i] != sems[i + 1]) {
    109 			FAILED("sem_open returned a different address");
    110 		}
    111 
    112 		/* Close some semaphores */
    113 		ret = sem_close(sems[i]);
    114 
    115 		if (ret != 0) {
    116 			UNRESOLVED(errno, "Failed to sem_close");
    117 		}
    118 	}
    119 
    120 	/* Now, reopen, we should still get the same address */
    121 	for (i = 0; i < 3; i++) {
    122 		sems[i] = sem_open(name, O_CREAT, 0777, 1);
    123 
    124 		if (sems[i] == SEM_FAILED) {
    125 			UNRESOLVED(errno, "Failed to sem_open");
    126 		}
    127 
    128 	}
    129 
    130 	/* Check all calls returned the same @ */
    131 	for (i = 0; i < 3; i++) {
    132 		if (sems[i] != sems[i + 1]) {
    133 			FAILED("sem_open returned a different address");
    134 		}
    135 	}
    136 
    137 	/* Close all semaphores */
    138 	for (i = 0; i < 4; i++) {
    139 		ret = sem_close(sems[i]);
    140 
    141 		if (ret != 0) {
    142 			UNRESOLVED(errno, "Failed to sem_close");
    143 		}
    144 	}
    145 
    146 	sem_unlink(name);
    147 
    148 	/* Test passed */
    149 #if VERBOSE > 0
    150 
    151 	output("Test passed\n");
    152 
    153 #endif
    154 
    155 	PASSED;
    156 }
    157