source: rtems/testsuites/psxtests/psxsem01/init.c @ 91cee2ef

Last change on this file since 91cee2ef was 91cee2ef, checked in by Joel Sherrill <joel.sherrill@…>, on Nov 17, 2006 at 11:04:38 PM

2006-11-17 Joel Sherrill <joel@…>

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