source: rtems/testsuites/psxtests/psxsem01/init.c @ 17e3808

4.104.11
Last change on this file since 17e3808 was 17e3808, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 22, 2009 at 3:37:54 PM

2009-07-22 Joel Sherrill <joel.sherrill@…>

  • psxsem01/Makefile.am, psxsem01/init.c: Much clean up.
  • psxsem01/system.h: Removed.
  • Property mode set to 100644
File size: 10.7 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2009.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#include <sched.h>
13#include <semaphore.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <time.h>
17#include <tmacros.h>
18#include <pmacros.h>
19#include "test_support.h"
20
21#define MAX_SEMS  10
22
23void *POSIX_Init(
24  void *argument
25)
26{
27  int             status;
28  int             value;
29  int             i;
30  sem_t           sems[MAX_SEMS];
31  sem_t           sem2;
32  sem_t           *n_sem1;
33  sem_t           *n_sem2;
34  struct timespec waittime;
35  char            failure_msg[80];
36
37  puts( "\n\n*** POSIX SEMAPHORE MANAGER TEST 1 ***" );
38
39  puts( "Init: sem_init - UNSUCCESSFUL (EINVAL)" );
40  status = sem_init(NULL, 0, 1);
41  fatal_posix_service_status( status, -1, "sem_init error return status");
42  fatal_posix_service_status( errno, EINVAL, "sem_init errorno EINVAL" );
43
44  puts( "Init: sem_init - SUCCESSFUL" );
45  for (i = 0; i < MAX_SEMS; i++) {
46    status = sem_init(&sems[i], 0, i);
47    sprintf(failure_msg, "sem_init %d", i );
48    fatal_posix_service_status( status, 0, failure_msg);
49  }
50  puts( "Init: sem_init - UNSUCCESSFUL (ENOSPC)" );
51  status = sem_init(&sem2, 0, 1);
52  fatal_posix_service_status( status, -1, "sem_init error return status");
53  fatal_posix_service_status( errno, ENOSPC, "sem_init errorno ENOSPC" );
54
55  puts( "Init: sem_init - UNSUCCESSFUL (ENOSYS -- pshared not supported)" );
56  status = sem_init(&sem2, 1, 1);
57  fatal_posix_service_status( status, -1, "sem_init error return status");
58  fatal_posix_service_status( errno, ENOSYS, "sem_init errno set to ENOSYS");
59
60  puts( "Init: sem_getvalue - SUCCESSFUL ");
61  for (i = 0; i < MAX_SEMS; i++) {
62    status = sem_getvalue(&sems[i], &value);
63    sprintf( failure_msg, "sem_getvalue %d", i );
64    fatal_posix_service_status( status, 0, failure_msg );
65    fatal_posix_service_status( value, i, "sem_getvalue correct value" );
66  }
67  puts( "Init: sem_getvalue - UNSUCCESSFUL ");
68  status = sem_getvalue(&sem2, &value);
69  fatal_posix_service_status( status, -1, "sem_getvalue error return status");
70  fatal_posix_service_status( errno, EINVAL, "sem_getvalue errno EINVAL");
71
72  puts( "Init: sem_destroy - SUCCESSFUL" );
73  status = sem_destroy(&sems[0]);
74  fatal_posix_service_status( status, 0, "sem_destroy semaphore 0");
75
76  puts( "Init: sem_destroy - UNSUCCESSFUL (EINVAL)" );
77  status = sem_destroy(&sem2);
78  fatal_posix_service_status( status, -1, "sem_destroy error return status");
79  fatal_posix_service_status( errno, EINVAL, "sem_destroy errno EINVAL");
80
81  puts( "Init: sem_wait - SUCCESSFUL" );
82  status = sem_wait(&sems[1]);
83  fatal_posix_service_status( status, 0, "sem_wait semaphore 1");
84  /* sem[1].count = 0 */
85
86  puts( "Init: sem_wait - UNSUCCESSFUL (EINVAL)" );
87  status = sem_wait(&sem2);
88  fatal_posix_service_status( status, -1, "sem_wait error return status");
89  fatal_posix_service_status( errno, EINVAL, "sem_wait errno EINVAL");
90
91  puts( "Init: sem_post - SUCCESSFUL" );
92  status = sem_post(&sems[1]);
93  fatal_posix_service_status( status, 0, "sem_post semaphore 1");
94  /* sem[1].count = 1 */
95
96  puts( "Init: sem_wait - SUCCESSFUL (after a sem_post)" );
97  status = sem_wait(&sems[1]);
98  fatal_posix_service_status( status, 0, "sem_wait semaphore 1");
99  /* sem[1].count = 0 */
100
101  puts( "Init: sem_trywait - SUCCESSFUL" );
102  status = sem_trywait(&sems[2]);
103  fatal_posix_service_status( status, 0, "sem_trywait semaphore 2");
104  /* sem[2].count = 1 */
105
106  puts( "Init: sem_trywait - UNSUCCESSFUL (EAGAIN)" );
107  status = sem_trywait(&sems[1]);
108  fatal_posix_service_status( status, -1, "sem_trywait error return status");
109  fatal_posix_service_status( errno, EAGAIN, "sem_trywait errno EAGAIN");
110  /* sem[1].count = 0 */
111
112  puts( "Init: sem_trywait - UNSUCCESSFUL (EINVAL)" );
113  status = sem_trywait(&sem2);
114  fatal_posix_service_status( status, -1, "sem_trywait error return status");
115  fatal_posix_service_status( errno, EINVAL, "sem_trywait errno EINVAL");
116
117#if 0
118  status = sem_post(&sems[2]);
119  fatal_posix_service_status( status, 0, "sem_post semaphore 2");
120  /* sem[2].count = 2 */
121#else
122  /* sem[2].count = 1 */
123#endif
124
125  puts( "Init: sem_timedwait - SUCCESSFUL" );
126  waittime.tv_sec = time(NULL) + 1;
127  waittime.tv_nsec = 100;
128  status = sem_timedwait(&sems[2], &waittime);
129  fatal_posix_service_status( status, 0, "sem_timedwait semaphore 2");
130  /* sem[2].count = 0 */
131
132  puts( "Init: sem_timedwait - UNSUCCESSFUL (ETIMEDOUT)" );
133  status = sem_timedwait(&sems[2], &waittime);
134  fatal_posix_service_status( status, -1, "sem_timedwait error return status");
135  fatal_posix_service_status(
136    errno, ETIMEDOUT, "sem_timedwait errno ETIMEDOUT");
137
138  /*
139   * To do this case, we must be blocking when we want the semaphore.
140   * POSIX doesn't want you to check the error if you can get the resource.
141   */
142
143#if 1
144  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL) -- skipping" );
145#else
146  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL)" );
147  waittime.tv_sec = 0;
148  waittime.tv_nsec = 0x7FFFFFFF;
149  status = sem_timedwait(&sems[2], &waittime);
150  fatal_posix_service_status( status, -1, "sem_timedwait error return status");
151  fatal_posix_service_status( errno, EINVAL, "sem_init errno EINVAL");
152#endif
153
154  puts( "Init: sem_post - UNSUCCESSFUL (EINVAL)" );
155  status = sem_post(&sem2);
156  fatal_posix_service_status( status, -1, "sem_post error return status");
157  fatal_posix_service_status( errno, EINVAL, "sem_post errno EINVAL");
158
159  puts( "Init: sem_destroy - SUCCESSFUL" );
160  for (i = 1; i < MAX_SEMS; i++) {
161    status = sem_destroy(&sems[i]);
162    sprintf( failure_msg, "sem_destroy %d", i );
163    fatal_posix_service_status( status, 0, failure_msg );
164  }
165
166  /* Modes are currently unsupported */
167
168  /*
169   * Validate all sem_open return paths.
170   */
171
172  puts( "Init: sem_open - UNSUCCESSFUL (ENAMETOOLONG)" );
173  n_sem1 = sem_open(Get_Too_Long_Name(), O_CREAT, 0777, 1 );
174  fatal_posix_service_status(
175    (int) n_sem1, (int) SEM_FAILED, "sem_open error return status");
176  fatal_posix_service_status(
177    errno, ENAMETOOLONG, "sem_open errorno ENAMETOOLONG" );
178
179  puts( "Init: sem_open - sem1 SUCCESSFUL" );
180  n_sem1 = sem_open( "sem1",O_CREAT, 0777, 1 ); 
181  assert( n_sem1 != SEM_FAILED );
182
183  puts( "Init: sem_destroy - named sem1 - EINVAL" );
184  status = sem_destroy(n_sem1);
185  fatal_posix_service_status( status, -1, "sem_destroy named semaphore");
186  fatal_posix_service_status( errno, EINVAL,  "sem_destroy named semaphore");
187
188  puts( "Init: sem_open - Create an Existing sem (EEXIST)" );
189  n_sem2 = sem_open("sem1", O_CREAT | O_EXCL, 0777, 1);
190  fatal_posix_service_status(
191    (int) n_sem2, (int) SEM_FAILED, "sem_open error return status" );
192  fatal_posix_service_status( errno, EEXIST,  "sem_open errno EEXIST");
193
194  puts( "Init: sem_open - Open new sem without create flag (ENOENT)" );
195  n_sem2 = sem_open("sem3", O_EXCL, 0777, 1);
196  fatal_posix_service_status(
197    (int) n_sem2, (int) SEM_FAILED, "sem_open error return status" );
198  fatal_posix_service_status( errno, ENOENT,  "sem_open errno EEXIST");
199
200  /*
201   * XXX - Could not hit the following errors:
202   *   E_POSIX_Semaphore_Create_support only fails if
203   *     ENOSYS - When semaphore is shared between processes.
204   *     ENOSPC - When out of memory.
205   */
206
207  /*
208   * Validate we can wait on a semaphore opened with sem_open.
209   */
210
211  puts( "Init: sem_wait on sem1" );
212  status = sem_wait(n_sem1);
213  fatal_posix_service_status( status, 0, "sem_wait opened semaphore");
214
215  /*
216   * Validate a second open returns the same semaphore.
217   */
218
219  puts( "Init: sem_open - Open an existing sem ( same id )" );
220  n_sem2 = sem_open("sem1", 0 );
221  fatal_posix_service_status(
222    (int) n_sem2, (int) n_sem1, "sem_open error return status" );
223
224  /*
225   * Unlink the semaphore, then verify an open of the same name produces a
226   * different semaphore.
227   */
228
229  puts( "Init: sem_unlink - sem1 SUCCESSFUL" );
230  status = sem_unlink( "sem1" );
231  fatal_posix_service_status( status, 0, "sem_unlink locked semaphore");
232
233  puts( "Init: sem_open - Reopen sem1 SUCCESSFUL with a different id" );
234  n_sem2 = sem_open( "sem1", O_CREAT | O_EXCL, 0777, 1);
235  assert( n_sem2 != SEM_FAILED );
236  assert( n_sem2 != n_sem1 );
237
238  /*
239   * Validate we can call close on a semaphore opened with sem_open.
240   */
241
242  puts( "Init: sem_close (1) - SUCCESSFUL" );
243  status = sem_close( n_sem1 );
244  fatal_posix_service_status( status, 0, "sem_close semaphore");
245
246
247  /*
248   * Validate it n_sem2 (the last open for sem1 name can be
249   * correctly closed and unlinked.
250   */
251
252  puts( "Init: sem_close (2) - SUCCESSFUL" );
253  status = sem_close( n_sem2 );
254  fatal_posix_service_status( status, 0, "sem_close semaphore");
255
256  puts( "Init: sem_unlink - sem1 (2) SUCCESSFUL" );
257  status = sem_unlink( "sem1" );
258  fatal_posix_service_status( status, 0, "sem_unlink locked semaphore");
259
260  puts( "Init: sem_close - UNSUCCESSFUL (EINVAL)" );
261  status = sem_close(n_sem2);
262  fatal_posix_service_status( status, -1, "sem_close error return status");
263  fatal_posix_service_status( errno, EINVAL, "sem_close errno EINVAL");
264
265  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
266  status = sem_unlink("sem1");
267  fatal_posix_service_status( status, -1, "sem_unlink error return status");
268  fatal_posix_service_status( errno, ENOENT, "sem_close errno EINVAL");
269
270
271  /*
272   * Validate we can unlink (2)
273   */
274
275  puts( "Init: sem_unlink (NULL) - EINVAL" );
276  status = sem_unlink( NULL );
277  fatal_posix_service_status( status, -1, "sem_unlink error return status");
278  fatal_posix_service_status( errno, EINVAL, "sem_unlink errno value");
279
280  puts( "Init: sem_unlink (\"\") - EINVAL" );
281  status = sem_unlink( "" );
282  fatal_posix_service_status( status, -1, "sem_unlink error return status");
283  fatal_posix_service_status( errno, EINVAL, "sem_unlink errno value");
284
285  /*
286   * XXX - Cant' create location OBJECTS_ERROR or OBJECTS_REMOTE.
287   *       sem_close and sem_unlink.
288   */
289
290  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
291  status = sem_unlink("sem2");
292  fatal_posix_service_status( status, -1, "sem_unlink error return status");
293  fatal_posix_service_status( errno, ENOENT, "sem_unlink errno ENOENT");
294  assert( (status == -1) && (errno == ENOENT) );
295
296
297  /* Try adding in unlinking before closing... (can we still open?) */
298
299  puts( "*** END OF POSIX SEMAPHORE MANAGER TEST 1 ***" );
300  rtems_test_exit(0);
301
302  return NULL; /* just so the compiler thinks we returned something */
303}
304
305/* configuration information */
306#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
307#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
308
309#define CONFIGURE_POSIX_INIT_THREAD_TABLE
310
311#define CONFIGURE_MAXIMUM_POSIX_THREADS     1
312#define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES  MAX_SEMS
313
314#define CONFIGURE_POSIX_INIT_THREAD_TABLE
315#define CONFIGURE_POSIX_INIT_THREAD_STACK_SIZE \
316        (RTEMS_MINIMUM_STACK_SIZE * 4)
317
318#define CONFIGURE_INIT
319#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.