source: rtems/testsuites/psxtests/psxsem01/init.c @ e02db20c

4.104.114.84.9
Last change on this file since e02db20c was e02db20c, checked in by Joel Sherrill <joel.sherrill@…>, on May 11, 2007 at 7:44:22 PM

2007-05-11 Joel Sherrill <joel.sherrill@…>

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