6-1.c revision 515b5a7a702d903c83b14d647d9fadff35062d3f
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*  A call to sem_open with the same name refers to a new semaphore, once
21* sem_unlink has been called.
22
23* The steps are:
24* -> open a semaphore
25* -> unlink
26* -> try to reconnect (should fail)
27* -> open with O_CREATE
28* -> check the semaphore are different
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.h"
53#include "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#define SEM_NAME  "/sem_unlink_6_1"
81
82/******************************************************************************/
83/***************************    Test case   ***********************************/
84/******************************************************************************/
85
86/* The main test function. */
87int main( int argc, char * argv[] )
88{
89	int ret, value;
90
91	sem_t * sem1, * sem2;
92
93	/* Initialize output */
94	output_init();
95
96	/* Create the semaphore */
97	sem1 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 );
98
99	if (( sem1 == SEM_FAILED ) && ( errno == EEXIST ) )
100	{
101		sem_unlink( SEM_NAME );
102		sem1 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 );
103	}
104
105	if (sem1 == SEM_FAILED )
106	{
107		UNRESOLVED( errno, "Failed to create the semaphore" );
108	}
109
110	/* Unlink */
111	ret = sem_unlink( SEM_NAME );
112
113	if (ret != 0 )
114	{
115		UNRESOLVED( errno, "Failed to unlink the semaphore" );
116	}
117
118	/* Try reconnect */
119	sem2 = sem_open( SEM_NAME, 0 );
120
121	if (sem2 != SEM_FAILED )
122	{
123		FAILED( "Reconnecting the unlinked semaphore did not failed" );
124	}
125
126	if (errno != ENOENT )
127	{
128		output( "Error %d: %s\n", errno, strerror( errno ) );
129		FAILED( "Reconnecting the unlinked semaphore failed with a wrong error" );
130	}
131
132	/* Reopen the semaphore */
133	sem2 = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 3 );
134
135	if (sem2 == SEM_FAILED )
136	{
137		output( "Gor error %d: %s\n", errno, strerror( errno ) );
138		FAILED( "Failed to recreate the semaphore" );
139	}
140
141	/* Check the semaphore have different values */
142	ret = sem_getvalue( sem1, &value );
143
144	if (ret != 0 )
145	{
146		UNRESOLVED( errno, "Failed to read sem1 value" );
147	}
148
149	if (value != 1 )
150	{
151		output( "Read: %d\n", value );
152		FAILED( "Semaphore value is not as expected" );
153	}
154
155	ret = sem_getvalue( sem2, &value );
156
157	if (ret != 0 )
158	{
159		UNRESOLVED( errno, "Failed to read sem1 value" );
160	}
161
162	if (value != 3 )
163	{
164		output( "Read: %d\n", value );
165		FAILED( "Semaphore value is not as expected" );
166	}
167
168	/* Unlink */
169	ret = sem_unlink( SEM_NAME );
170
171	if (ret != 0 )
172	{
173		UNRESOLVED( errno, "Failed to unlink the semaphore" );
174	}
175
176	/* close both */
177	ret = sem_close( sem1 );
178
179	if (ret != 0 )
180	{
181		UNRESOLVED( errno, "Failed to close the semaphore" );
182	}
183
184	ret = sem_close( sem2 );
185
186	if (ret != 0 )
187	{
188		UNRESOLVED( errno, "Failed to close the semaphore" );
189	}
190
191
192	/* Test passed */
193#if VERBOSE > 0
194	output( "Test passed\n" );
195
196#endif
197	PASSED;
198}
199
200
201