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_init returns -1 and sets errno to ENOSPC if the system lacks a resource
21* or SEM_NSEMS_MAX has been reached.
22
23
24* The steps are:
25* -> Try and sem_init SEM_NSEMS_MAX semaphores.
26* -> Try and sem_init an additional semaphore.
27
28* The test fails if the last creation does not return an error.
29
30*/
31
32
33/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
34#define _POSIX_C_SOURCE 200112L
35
36/******************************************************************************/
37/*************************** standard includes ********************************/
38/******************************************************************************/
39#include <pthread.h>
40#include <stdarg.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include <semaphore.h>
47#include <errno.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/******************************************************************************/
81/***************************    Test case   ***********************************/
82/******************************************************************************/
83
84
85/* The main test function. */
86int main( int argc, char * argv[] )
87{
88	int ret, i;
89	sem_t *sems;
90	sem_t sem_last;
91
92	long max;
93
94	/* Initialize output */
95	output_init();
96
97	max = sysconf( _SC_SEM_NSEMS_MAX );
98
99	if ( max <= 0 )
100	{
101		output( "sysconf( _SC_SEM_NSEMS_MAX ) = %ld\n", max );
102		UNTESTED( "There is no constraint on SEM_NSEMS_MAX" );
103	}
104
105	sems = ( sem_t * ) calloc( max, sizeof( sem_t ) );
106
107	if ( sems == NULL )
108	{
109		UNRESOLVED( errno, "Failed to alloc space" );
110	}
111
112
113	for ( i = 0; i < max; i++ )
114	{
115		ret = sem_init( &sems[ i ], 0, 0 );
116
117		if ( ret != 0 )
118		{
119			output( "sem_init failed to initialize the %d nth semaphore.\n", i );
120			output( "Tryed to initialize %ld.\n", max );
121			output( "Error is %d: %s\n", errno, strerror( errno ) );
122
123			for ( ; i > 0; i-- )
124				sem_destroy( &sems[ i - 1 ] );
125
126			free( sems );
127
128			PASSED;
129		}
130	}
131
132	ret = sem_init( &sem_last, 0, 1 );
133
134	if ( ret == 0 )
135	{
136		FAILED( "We were able to sem_init more than SEM_NSEMS_MAX semaphores" );
137	}
138
139	if ( errno != ENOSPC )
140	{
141		output( "Error is %d: %s\n", errno, strerror( errno ) );
142	}
143
144	for ( i = 0; i < max; i++ )
145		sem_destroy( &sems[ i ] );
146
147	free( sems );
148
149
150	/* Test passed */
151#if VERBOSE > 0
152
153	output( "Test passed\n" );
154
155#endif
156
157	PASSED;
158}
159
160
161