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

4.104.114.9
Last change on this file since ce7803fe was ce7803fe, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 4, 2008 at 4:13:10 PM

2008-08-04 Joel Sherrill <joel.sherrill@…>

  • psxsem01/init.c: Spacing.
  • 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   * To do this case, we must be blocking when we want the semaphore.
119   * POSIX doesn't want you to check the error if you can get the resource.
120   */
121
122#if 1
123  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL) -- skipping" );
124#else
125  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL)" );
126  waittime.tv_sec = 0;
127  waittime.tv_nsec = 0x7FFFFFFF;
128  status = sem_timedwait(&sems[2], &waittime);
129  fatal_posix_service_status( status, -1, "sem_timedwait error return status");
130  fatal_posix_service_status( errno, EINVAL, "sem_init errno EINVAL");
131#endif
132
133  puts( "Init: sem_post - UNSUCCESSFUL (EINVAL)" );
134  status = sem_post(&sem2);
135  fatal_posix_service_status( status, -1, "sem_post error return status");
136  fatal_posix_service_status( errno, EINVAL, "sem_post errno EINVAL");
137
138  puts( "Init: sem_destroy - SUCCESSFUL" );
139  for (i = 1; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
140    status = sem_destroy(&sems[i]);
141    sprintf( failure_msg, "sem_destroy %d", i );
142    fatal_posix_service_status( status, 0, failure_msg );
143  }
144
145  /* Modes are currently unsupported */
146
147  /*
148   * Validate all sem_open return paths.
149   */
150
151  puts( "Init: sem_open - sem1 SUCCESSFUL" );
152  n_sem1 = sem_open( "sem1", O_CREAT, 00777, 1 );
153  assert( n_sem1 != SEM_FAILED );
154
155  puts( "Init: sem_open - Create an Existing sem (EEXIST)" );
156  n_sem2 = sem_open("sem1", O_CREAT | O_EXCL, 00777, 1);
157  fatal_posix_service_status(
158    (int) n_sem2, (int ) SEM_FAILED, "sem_open error return status" );
159  fatal_posix_service_status( errno, EEXIST,  "sem_open errno EEXIST");
160
161  puts( "Init: sem_open - Open new sem without create flag (ENOENT)" );
162  n_sem2 = sem_open("sem3", O_EXCL, 00777, 1);
163  fatal_posix_service_status(
164    (int) n_sem2, (int ) SEM_FAILED, "sem_open error return status" );
165  fatal_posix_service_status( errno, ENOENT,  "sem_open errno EEXIST");
166
167  /*
168   * XXX - Could not hit the following errors:
169   *   E_POSIX_Semaphore_Create_support only fails if
170   *     ENOSYS - When semaphore is shared between processes.
171   *     ENOSPC - When out of memory.
172   */
173
174  /*
175   * Validate we can wait on a semaphore opened with sem_open.
176   */
177
178  puts( "Init: sem_wait on sem1" );
179  status = sem_wait(n_sem1);
180  fatal_posix_service_status( status, 0, "sem_wait opened semaphore");
181
182  /*
183   * Validate a second open returns the same semaphore.
184   */
185
186  puts( "Init: sem_open - Open an existing sem ( same id )" );
187  n_sem2 = sem_open("sem1", 0 );
188  fatal_posix_service_status(
189    (int) n_sem2, (int ) n_sem1, "sem_open error return status" );
190
191  /*
192   * Unlink the semaphore, then verify an open of the same name produces a
193   * different semaphore.
194   */
195
196  puts( "Init: sem_unlink - sem1 SUCCESSFUL" );
197  status = sem_unlink( "sem1" );
198  fatal_posix_service_status( status, 0, "sem_unlink locked semaphore");
199
200  puts( "Init: sem_open - Reopen sem1 SUCCESSFUL with a different id" );
201  n_sem2 = sem_open( "sem1", O_CREAT | O_EXCL, 00777, 1);
202  assert( n_sem2 != SEM_FAILED );
203  assert( n_sem2 != n_sem1 );
204
205  /*
206   * Validate we can call close on a semaphore opened with sem_open.
207   */
208
209  puts( "Init: sem_close (1) - SUCCESSFUL" );
210  status = sem_close( n_sem1 );
211  fatal_posix_service_status( status, 0, "sem_close semaphore");
212
213
214  /*
215   * Validate it n_sem2 (the last open for sem1 name can be
216   * correctly closed and unlinked.
217   */
218
219  puts( "Init: sem_close (2) - SUCCESSFUL" );
220  status = sem_close( n_sem2 );
221  fatal_posix_service_status( status, 0, "sem_close semaphore");
222
223  puts( "Init: sem_unlink - sem1 (2) SUCCESSFUL" );
224  status = sem_unlink( "sem1" );
225  fatal_posix_service_status( status, 0, "sem_unlink locked semaphore");
226
227  puts( "Init: sem_close - UNSUCCESSFUL (EINVAL)" );
228  status = sem_close(n_sem2);
229  fatal_posix_service_status( status, -1, "sem_close error return status");
230  fatal_posix_service_status( errno, EINVAL, "sem_close errno EINVAL");
231
232  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
233  status = sem_unlink("sem1");
234  fatal_posix_service_status( status, -1, "sem_unlink error return status");
235  fatal_posix_service_status( errno, ENOENT, "sem_close errno EINVAL");
236
237
238  /*
239   * Validate we can unlink (2)
240   */
241
242  puts( "Init: sem_unlink (NULL) - EINVAL" );
243  status = sem_unlink( NULL );
244  fatal_posix_service_status( status, -1, "sem_unlink error return status");
245  fatal_posix_service_status( errno, EINVAL, "sem_unlink errno value");
246
247  puts( "Init: sem_unlink (\"\") - EINVAL" );
248  status = sem_unlink( "" );
249  fatal_posix_service_status( status, -1, "sem_unlink error return status");
250  fatal_posix_service_status( errno, EINVAL, "sem_unlink errno value");
251
252  /*
253   * XXX - Cant' create location OBJECTS_ERROR or OBJECTS_REMOTE.
254   *       sem_close and sem_unlink.
255   */
256
257  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
258  status = sem_unlink("sem2");
259  fatal_posix_service_status( status, -1, "sem_unlink error return status");
260  fatal_posix_service_status( errno, ENOENT, "sem_unlink errno ENOENT");
261  assert( (status == -1) && (errno == ENOENT) );
262
263
264  /* Try adding in unlinking before closing... (can we still open?) */
265
266  puts( "*** END OF POSIX SEMAPHORE MANAGER TEST 1 ***" );
267  rtems_test_exit(0);
268
269  return NULL; /* just so the compiler thinks we returned something */
270}
Note: See TracBrowser for help on using the repository browser.