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*  sem_unlink does not block.
21
22* The steps are:
23* -> open a semaphore
24* -> create a thread which blocks on the semaphore
25* -> call sem_unlink
26* -> clean up
27
28* The test fails when it hangs on sem_unlink.
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_7_1"
81
82/******************************************************************************/
83/***************************    Test case   ***********************************/
84/******************************************************************************/
85
86void * threaded ( void * arg )
87{
88	int ret = 0;
89
90	do
91	{
92		ret = sem_wait( arg );
93	}
94	while ( ( ret != 0 ) && ( errno == EINTR ) );
95
96	if ( ret != 0 )
97	{
98		UNRESOLVED( errno, "Failed to wait for the semaphore" );
99	}
100
101	return NULL;
102}
103
104
105/* The main test function. */
106int main( int argc, char * argv[] )
107{
108	int ret;
109	pthread_t thread;
110	sem_t * sem;
111
112	/* Initialize output */
113	output_init();
114
115	/* Create the semaphore */
116	sem = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 );
117
118	if ( ( sem == SEM_FAILED ) && ( errno == EEXIST ) )
119	{
120		sem_unlink( SEM_NAME );
121		sem = sem_open( SEM_NAME, O_CREAT | O_EXCL, 0777, 1 );
122	}
123
124	if ( sem == SEM_FAILED )
125	{
126		UNRESOLVED( errno, "Failed to create the semaphore" );
127	}
128
129	/* Create the child thread */
130	ret = pthread_create( &thread, NULL, threaded, sem );
131
132	if ( ret != 0 )
133	{
134		UNRESOLVED( ret, "Failed to create the thread" );
135	}
136
137	/* Let some time for the thread to block */
138	sleep( 1 );
139
140	/* Unlink */
141	ret = sem_unlink( SEM_NAME );
142
143	if ( ret != 0 )
144	{
145		UNRESOLVED( errno, "Failed to unlink the semaphore" );
146	}
147
148	/* Now, we're success */
149	ret = sem_post( sem );
150
151	if ( ret != 0 )
152	{
153		UNRESOLVED( errno, "Failed to post the semaphore" );
154	}
155
156	/* Join the thread */
157	ret = pthread_join( thread, NULL );
158
159	if ( ret != 0 )
160	{
161		UNRESOLVED( ret, "Failed to join the thread" );
162	}
163
164	/* close  */
165	ret = sem_close( sem );
166
167	if ( ret != 0 )
168	{
169		UNRESOLVED( errno, "Failed to close the semaphore" );
170	}
171
172	/* Test passed */
173#if VERBOSE > 0
174	output( "Test passed\n" );
175
176#endif
177	PASSED;
178}
179
180
181