summaryrefslogtreecommitdiffstats
path: root/c/src/tests/psxtests/psxsem01/init.c
blob: 2f694bd1aa5498405f863bd0f75709905c72f365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#define CONFIGURE_INIT
#include "system.h"
#include <sched.h>
#include <fcntl.h>
#include <time.h>
#include <tmacros.h>

void *POSIX_Init(
  void *argument
)
{
  int             status;
  int             value;
  int             i;
  sem_t           sems[CONFIGURE_MAXIMUM_POSIX_SEMAPHORES];
  sem_t           sem2;
  sem_t           *n_sem1;
  sem_t           *n_sem2;
  sem_t           testsem;
  struct timespec waittime;
  char            failure_msg[80];

  puts( "\n\n*** POSIX SEMAPHORE MANAGER TEST 1 ***" );

  puts( "Init: sem_init - SUCCESSFUL" );
  for (i = 0; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
    status = sem_init(&sems[i], 0, i);
    sprintf(failure_msg, "sem_init %d", i );
    fatal_directive_status( status, 0, failure_msg);
  }
  puts( "Init: sem_init - UNSUCCESSFUL (ENOSPC)" );
  status = sem_init(&sem2, 0, 1);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, ENOSPC, "sem_init errorno ENOSPC" );
 
  puts( "Init: sem_init - UNSUCCESSFUL (ENOSYS -- pshared not supported)" );
  status = sem_init(&sem2, 1, 1);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, ENOSYS, "sem_init errno set to ENOSYS");
  
  puts( "Init: sem_getvalue - SUCCESSFUL ");
  for (i = 0; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
    status = sem_getvalue(&sems[i], &value);
    sprintf( failure_msg, "sem_getvalue %d", i );
    fatal_directive_status( status, 0, failure_msg );
    fatal_directive_status( value, i, "sem_getvalue correct value" );
  }
  puts( "Init: sem_getvalue - UNSUCCESSFUL ");
  status = sem_getvalue(&sem2, &value);
  fatal_directive_status( status, -1, "sem_init error return status"); 
  fatal_directive_status( errno, EINVAL, "sem_getvalue errno EINVAL");

  puts( "Init: sem_destroy - SUCCESSFUL" );
  status = sem_destroy(&sems[0]);
  fatal_directive_status( status, 0, "sem_destroy semaphore 0"); 

  puts( "Init: sem_destroy - UNSUCCESSFUL (EINVAL)" );
  status = sem_destroy(&sem2);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_destroy errno EINVAL");

  puts( "Init: sem_wait - SUCCESSFUL" );
  status = sem_wait(&sems[1]);
  fatal_directive_status( status, 0, "sem_wait semaphore 1"); 

  puts( "Init: sem_wait - UNSUCCESSFUL (EINVAL)" );
  status = sem_wait(&sem2);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_wait errno EINVAL");

  puts( "Init: sem_post - SUCCESSFUL" );
  status = sem_post(&sems[1]);
  fatal_directive_status( status, 0, "sem_post semaphore 1"); 

  puts( "Init: sem_wait - SUCCESSFUL (after a sem_post)" );
  status = sem_wait(&sems[1]);
  fatal_directive_status( status, 0, "sem_wait semaphore 1"); 

  puts( "Init: sem_trywait - SUCCESSFUL" );
  status = sem_trywait(&sems[2]);
  fatal_directive_status( status, 0, "sem_trywait semaphore 2"); 

  puts( "Init: sem_trywait - UNSUCCESSFUL (EAGAIN)" );
  status = sem_trywait(&sems[1]);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EAGAIN, "sem_trywait errno EAGAIN");

  puts( "Init: sem_trywait - UNSUCCESSFUL (EINVAL)" );
  status = sem_trywait(&sem2);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_trywait errno EINVAL");

  puts( "Init: sem_timedwait - SUCCESSFUL" );
  waittime.tv_sec = 0;
  waittime.tv_nsec = 100;
  status = sem_timedwait(&sems[2], &waittime);
  fatal_directive_status( status, 0, "sem_timedwait semaphore 2"); 

  puts( "Init: sem_timedwait - UNSUCCESSFUL (ETIMEDOUT)" );
  status = sem_timedwait(&sems[1], &waittime);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, ETIMEDOUT, "sem_init errno ETIMEDOUT");

  puts( "Init: sem_timedwait - UNSUCCESSFUL (EINVAL)" );
  status = sem_timedwait(&sem2, &waittime);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_init errno EINVAL");

  puts( "Init: sem_post - UNSUCCESSFUL (EINVAL)" );
  status = sem_post(&sem2);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_post errno EINVAL");

  puts( "Init: sem_destroy - SUCCESSFUL" );
  for (i = 1; i < CONFIGURE_MAXIMUM_POSIX_SEMAPHORES; i++) {
    status = sem_destroy(&sems[i]);
    sprintf( failure_msg, "sem_destroy %d", i );
    fatal_directive_status( status, 0, failure_msg ); 
  }

  /* Modes are currently unsupported */

  /*
   * Validate all sem_open return paths.
   */

  puts( "Init: sem_open - sem1 SUCCESSFUL" );
  n_sem1 = sem_open("sem1", O_CREAT, 00777, 1);
  assert( n_sem1 != SEM_FAILED );

  puts( "Init: sem_open - UNSUCCESSFUL (EEXIST)" );
  n_sem2 = sem_open("sem1", O_CREAT | O_EXCL, 00777, 1);
  fatal_directive_status( 
    (int) n_sem2, (int ) SEM_FAILED, "sem_open error return status" );
  fatal_directive_status( errno, EEXIST,  "sem_open errno EEXIST"); 

  /*
   * Validate we can wait on a semaphore opened with sem_open.
   */

  puts( "Init: sem_wait on sem1" );
  status = sem_wait(n_sem1);
  fatal_directive_status( status, 0, "sem_wait opened semaphore"); 

#if 0
  puts( "Init: sem_unlink - sem1 SUCCESSFUL" );
  status = sem_unlink( "sem1" );
  fatal_directive_status( status, 0, "sem_unlink locked semaphore"); 

  puts( "Init: sem_open - Reopen sem1 SUCCESSFUL with a different id" );
  n_sem2 = sem_open( "sem1", O_CREAT | O_EXCL, 00777, 1);
  assert( n_sem2 != SEM_FAILED );
#endif

  /*
   * Validate we can call close on a semaphore opened with sem_open.
   */

  puts( "Init: sem_close - SUCCESSFUL" );
  status = sem_close( n_sem1 );
  fatal_directive_status( status, 0, "sem_close semaphore"); 

  /*
   * Notes:  
   *  Calls to sem_close when sem_link has not been previously called shall
   *  have no effect on the state of the semaphore.
   *
   *
  
  puts( "Init: sem_close - UNSUCCESSFUL (EINVAL)" );
  status = sem_close(n_sem2);
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, EINVAL, "sem_close errno EINVAL");

  puts( "Init: sem_unlink - SUCCESSFUL" );
  status = sem_unlink("sem1");
  fatal_directive_status( status, 0, "sem_unlink semaphore"); 

  puts( "Init: sem_unlink - UNSUCCESSFUL (ENOENT)" );
  status = sem_unlink("sem2");
  fatal_directive_status( status, -1, "sem_init error return status");
  fatal_directive_status( errno, ENOENT, "sem_unlink errno ENOENT");
  assert( (status == -1) && (errno == ENOENT) );
  */

  /* Try adding in unlinking before closing... (can we still open?) */

  puts( "*** POSIX SEMAPHORE MANAGER TEST 1 COMPLETED ***" );
  exit(0);

  return NULL; /* just so the compiler thinks we returned something */
}