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., 59
15* Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18* This sample test aims to check the following assertion:
19*
20*  If a process calls sem_open several times with the same name,
21* the same adress must be returned as long as the semaphore
22* has not been unlinked or closed as many times as opened.
23
24
25* The steps are:
26* -> Create a semaphore with sem_open
27* -> call sem_open several times with the same name.
28* -> Check that the same address is returned for the semaphore.
29
30* The test fails if a different address is returned.
31
32*/
33
34
35/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
36#define _POSIX_C_SOURCE 200112L
37
38/******************************************************************************/
39/*************************** standard includes ********************************/
40/******************************************************************************/
41#include <pthread.h>
42#include <stdarg.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#include <semaphore.h>
49#include <errno.h>
50#include <fcntl.h>
51
52/******************************************************************************/
53/***************************   Test framework   *******************************/
54/******************************************************************************/
55#include "testfrmw.h"
56#include "testfrmw.c"
57/* This header is responsible for defining the following macros:
58 * UNRESOLVED(ret, descr);
59 *    where descr is a description of the error and ret is an int
60 *   (error code for example)
61 * FAILED(descr);
62 *    where descr is a short text saying why the test has failed.
63 * PASSED();
64 *    No parameter.
65 *
66 * Both three macros shall terminate the calling process.
67 * The testcase shall not terminate in any other maneer.
68 *
69 * The other file defines the functions
70 * void output_init()
71 * void output(char * string, ...)
72 *
73 * Those may be used to output information.
74 */
75
76/******************************************************************************/
77/**************************** Configuration ***********************************/
78/******************************************************************************/
79#ifndef VERBOSE
80#define VERBOSE 1
81#endif
82
83/******************************************************************************/
84/***************************    Test case   ***********************************/
85/******************************************************************************/
86
87/* The main test function. */
88int main( int argc, char * argv[] )
89{
90	int ret, i;
91	char * name = "/sem_open_15_1";
92
93	sem_t * sems[ 4 ];
94
95	/* Initialize output */
96	output_init();
97
98	/* Initialize all semaphores */
99
100	for ( i = 0; i < 4; i++ )
101	{
102		sems[ i ] = sem_open( name, O_CREAT, 0777, 1 );
103
104		if ( sems[ i ] == SEM_FAILED )
105		{
106			UNRESOLVED( errno, "Failed to sem_open" );
107		}
108
109	}
110
111	/* Check all calls returned the same @ */
112	for ( i = 0; i < 3; i++ )
113	{
114		if ( sems[ i ] != sems[ i + 1 ] )
115		{
116			FAILED( "sem_open returned a different address" );
117		}
118
119		/* Close some semaphores */
120		ret = sem_close( sems[ i ] );
121
122		if ( ret != 0 )
123		{
124			UNRESOLVED( errno, "Failed to sem_close" );
125		}
126	}
127
128	/* Now, reopen, we should still get the same address */
129	for ( i = 0; i < 3; i++ )
130	{
131		sems[ i ] = sem_open( name, O_CREAT, 0777, 1 );
132
133		if ( sems[ i ] == SEM_FAILED )
134		{
135			UNRESOLVED( errno, "Failed to sem_open" );
136		}
137
138	}
139
140	/* Check all calls returned the same @ */
141	for ( i = 0; i < 3; i++ )
142	{
143		if ( sems[ i ] != sems[ i + 1 ] )
144		{
145			FAILED( "sem_open returned a different address" );
146		}
147	}
148
149
150	/* Close all semaphores */
151	for ( i = 0; i < 4; i++ )
152	{
153		ret = sem_close( sems[ i ] );
154
155		if ( ret != 0 )
156		{
157			UNRESOLVED( errno, "Failed to sem_close" );
158		}
159	}
160
161	sem_unlink( name );
162
163	/* Test passed */
164#if VERBOSE > 0
165
166	output( "Test passed\n" );
167
168#endif
169
170	PASSED;
171}
172
173
174