summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spfifo02/init.c
blob: aa1f66753aaa586a6d47177af1d63ca6f9ac904c (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *  COPYRIGHT (c) 1989-2014.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tmacros.h>
#include "test_support.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <rtems/libcsupport.h>

/* forward declarations to avoid warnings */
rtems_task Init(rtems_task_argument argument);
void create_all_barriers(void);
void create_all_semaphores(void);
void delete_barrier(void);
void delete_semaphore(void);
void create_fifo(void);
void open_fifo(int expected, int flags);

#define MAXIMUM 10
#define NUM_OPEN_REQ 26

rtems_id Barriers[MAXIMUM];
int BarrierCount;

rtems_id Semaphores[MAXIMUM];
int SemaphoreCount;

void create_all_barriers(void)
{
  rtems_status_code status;
  int               i;

  BarrierCount = 0;

  memset( Barriers, 0, sizeof(Barriers) );
  for ( i=0 ; i<MAXIMUM ; i++ ) {
    status = rtems_barrier_create(
      rtems_build_name( 'B', 'A', 'R', 0x30+i ),
      RTEMS_BARRIER_MANUAL_RELEASE,
      0,
      &Barriers[i]
    );
    if ( status == RTEMS_TOO_MANY ) {
      printf( "%d Barriers created\n", BarrierCount+1 );
      return;
    } 

    directive_failed( status, "barrier create" );
    BarrierCount++;
  }
}

void create_all_semaphores(void)
{
  rtems_status_code status;
  int               i;

  SemaphoreCount = 0;

  for ( i=0 ; i<MAXIMUM ; i++ ) {
    status = rtems_semaphore_create(
      rtems_build_name( 'S', 'E', 'M', 0x30+i ),
      0,
      RTEMS_DEFAULT_ATTRIBUTES,
      0,
      &Semaphores[i]
    );
    if ( status == RTEMS_TOO_MANY ) {
      printf( "%d Semaphores created\n", SemaphoreCount+1 );
      return;
    } 

    directive_failed( status, "semaphore create" );
    SemaphoreCount++;
  }
}

void delete_barrier(void)
{
  rtems_status_code status;
  
  BarrierCount--;
  printf( "Deleting barrier id=0x%08x\n",
    (unsigned int)Barriers[BarrierCount] );
  status = rtems_barrier_delete( Barriers[BarrierCount] );
  directive_failed( status, "barrier delete" );
}

void delete_semaphore(void)
{
  rtems_status_code status;
  
  SemaphoreCount--;
  printf( "Deleting semaphore id=0x%08x\n",
    (unsigned int) Semaphores[SemaphoreCount] );
  status = rtems_semaphore_delete( Semaphores[SemaphoreCount] );
  directive_failed( status, "semaphore delete" );
}

void create_fifo(void)
{
  int status;

  status = mkfifo("/fifo01", 0777);
  rtems_test_assert(status == 0);
}

void open_fifo(int expected, int flags)
{
  int fd;

  fd = open("/fifo01", flags);
  printf( "expect status=%d errno=%d/(%s)\n", fd, errno, strerror(errno) );
  if ( expected ) {
    rtems_test_assert(fd == -1);
    rtems_test_assert(errno == expected); 
  } else {
    rtems_test_assert(fd != -1);
    close( fd );
  }
}

rtems_task Init(
  rtems_task_argument argument
)
{
  void *alloc_ptr = (void *)0;
  int num_opens = 0;
  int index = 0;

  puts( "\n\n*** TEST FIFO 02 ***" );

  puts( "Creating all barriers" );
  create_all_barriers();

  puts( "Creating all semaphores" );
  create_all_semaphores();

  puts( "Creating FIFO" );
  create_fifo();

  puts( "Opening FIFO.. expect ENOMEM (semaphore for pipe could not be created)" );
  open_fifo(ENOMEM, O_RDWR);

  delete_semaphore();

  alloc_ptr = malloc( malloc_free_space() - 4 );
  puts("Opening FIFO.. expect ENOMEM since no memory is available");
  open_fifo(ENOMEM, O_RDWR);

  free(alloc_ptr);
  puts( "Opening FIFO.. expect ENOMEM (barrier-1 for pipe could not be created)" );
  open_fifo(ENOMEM, O_RDWR);

  delete_barrier();
  puts( "Opening FIFO.. expect ENOMEM (barrier-2 for pipe could not be created" );
  open_fifo(ENOMEM, O_RDWR);

  delete_barrier();
  puts( "Opening FIFO.. expect ENOMEM (semaphore-1 for pipe could not be created" );
  open_fifo(ENOMEM, O_RDWR);

  delete_semaphore();
  puts( "Opening FIFO in RDWR mode. Expect OK" );
  open_fifo(0, O_RDWR);
  ++num_opens;

  puts( "Opening FIFO in non blocking RDONLY mode. Expect OK");
  open_fifo(0, O_RDONLY | O_NONBLOCK);
  ++num_opens;

  puts( "Opening FIFO in non blocking WRONLY mode. Expect ENXIO");
  open_fifo(ENXIO, O_WRONLY | O_NONBLOCK);
  ++num_opens;

  puts("\nMultiple opens\n");
  index = 0;
  do {

    printf("%d... ", index+1);
    open_fifo(0, O_RDONLY | O_NONBLOCK);
    ++index;
  } while ( index < NUM_OPEN_REQ - num_opens );

  puts( "*** END OF TEST FIFO 08 ***" );

  rtems_test_exit(0);
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_MAXIMUM_TASKS             1
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5
#define CONFIGURE_FIFOS_ENABLED
#define CONFIGURE_MAXIMUM_FIFOS 1

#define CONFIGURE_INIT

#include <rtems/confdefs.h>
/* end of file */