source: rtems/testsuites/libtests/newlib01/init.c @ 9d1522ed

5
Last change on this file since 9d1522ed was 9d1522ed, checked in by Sebastian Huber <sebastian.huber@…>, on 07/28/17 at 11:25:25

Fix IO control request type

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <stdio.h>
20
21#include <rtems.h>
22#include <rtems/console.h>
23#include <rtems/imfs.h>
24#include <rtems/libcsupport.h>
25
26#include "tmacros.h"
27
28const char rtems_test_name[] = "NEWLIB 1";
29
30static const char file_path[] = "/file";
31
32typedef enum {
33  INIT,
34  OPEN,
35  WRITTEN,
36  CLOSED
37} test_state;
38
39typedef struct {
40  rtems_id main_task_id;
41  rtems_id worker_task_id;
42  test_state current;
43} test_context;
44
45static test_context test_instance;
46
47static void wake_up_main(const test_context *ctx)
48{
49  rtems_status_code sc;
50
51  sc = rtems_event_transient_send(ctx->main_task_id);
52  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
53}
54
55static void wait(void)
56{
57  rtems_status_code sc;
58
59  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
60  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
61}
62
63static void worker_task(rtems_task_argument arg)
64{
65  test_context *ctx = &test_instance;
66  char buf[1] = { 'x' };
67  size_t n;
68
69  stdout = fopen(&file_path[0], "r+");
70  rtems_test_assert(stdout != NULL);
71
72  n = fwrite(&buf[0], sizeof(buf), 1, stdout);
73  rtems_test_assert(n == 1);
74
75  rtems_test_assert(ctx->current == OPEN);
76
77  wake_up_main(ctx);
78
79  rtems_test_assert(0);
80}
81
82static int handler_open(
83  rtems_libio_t *iop,
84  const char *path,
85  int oflag,
86  mode_t mode
87)
88{
89  test_context *ctx = &test_instance;
90
91  rtems_test_assert(ctx->current == INIT);
92  ctx->current = OPEN;
93
94  return 0;
95}
96
97static int handler_close(
98  rtems_libio_t *iop
99)
100{
101  test_context *ctx = &test_instance;
102
103  rtems_test_assert(ctx->current == WRITTEN);
104  ctx->current = CLOSED;
105
106  return 0;
107}
108
109static ssize_t handler_read(
110  rtems_libio_t *iop,
111  void *buffer,
112  size_t count
113)
114{
115  rtems_test_assert(0);
116
117  return 0;
118}
119
120static ssize_t handler_write(
121  rtems_libio_t *iop,
122  const void *buffer,
123  size_t count
124)
125{
126  test_context *ctx = &test_instance;
127
128  rtems_test_assert(ctx->current == OPEN);
129  ctx->current = WRITTEN;
130
131  iop->offset += count;
132
133  return (ssize_t) count;
134}
135
136static int handler_ioctl(
137  rtems_libio_t *iop,
138  ioctl_command_t request,
139  void *buffer
140)
141{
142  rtems_test_assert(0);
143
144  return 0;
145}
146
147static off_t handler_lseek(
148  rtems_libio_t *iop,
149  off_t length,
150  int whence
151)
152{
153  rtems_test_assert(0);
154
155  return 0;
156}
157
158static int handler_ftruncate(
159  rtems_libio_t *iop,
160  off_t length
161)
162{
163  rtems_test_assert(0);
164
165  return 0;
166}
167
168static int handler_fsync(
169  rtems_libio_t *iop
170)
171{
172  rtems_test_assert(0);
173
174  return 0;
175}
176
177static int handler_fdatasync(
178  rtems_libio_t *iop
179)
180{
181  rtems_test_assert(0);
182
183  return 0;
184}
185
186static int handler_fcntl(
187  rtems_libio_t *iop,
188  int cmd
189)
190{
191  rtems_test_assert(0);
192
193  return 0;
194}
195
196static ssize_t handler_readv(
197  rtems_libio_t *iop,
198  const struct iovec *iov,
199  int iovcnt,
200  ssize_t total
201)
202{
203  rtems_test_assert(0);
204
205  return 0;
206}
207
208static ssize_t handler_writev(
209  rtems_libio_t *iop,
210  const struct iovec *iov,
211  int iovcnt,
212  ssize_t total
213)
214{
215  rtems_test_assert(0);
216
217  return 0;
218}
219
220static const rtems_filesystem_file_handlers_r node_handlers = {
221  .open_h = handler_open,
222  .close_h = handler_close,
223  .read_h = handler_read,
224  .write_h = handler_write,
225  .ioctl_h = handler_ioctl,
226  .lseek_h = handler_lseek,
227  .fstat_h = rtems_filesystem_default_fstat,
228  .ftruncate_h = handler_ftruncate,
229  .fsync_h = handler_fsync,
230  .fdatasync_h = handler_fdatasync,
231  .fcntl_h = handler_fcntl,
232  .readv_h = handler_readv,
233  .writev_h = handler_writev
234};
235
236static const IMFS_node_control node_control = IMFS_GENERIC_INITIALIZER(
237  &node_handlers,
238  IMFS_node_initialize_default,
239  IMFS_node_destroy_default
240);
241
242static void test(void)
243{
244  test_context *ctx = &test_instance;
245  rtems_status_code sc;
246  int rv;
247  rtems_resource_snapshot snapshot;
248  FILE *file;
249
250  ctx->main_task_id = rtems_task_self();
251
252  /* Fill dynamic file pool in Newlib _GLOBAL_REENT */
253  file = fopen(CONSOLE_DEVICE_NAME, "r+");
254  rtems_test_assert(file != NULL);
255  rv = fclose(file);
256  rtems_test_assert(rv == 0);
257
258  rtems_resource_snapshot_take(&snapshot);
259
260  rv = IMFS_make_generic_node(
261    &file_path[0],
262    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
263    &node_control,
264    NULL
265  );
266  rtems_test_assert(rv == 0);
267
268  sc = rtems_task_create(
269    rtems_build_name('W', 'O', 'R', 'K'),
270    2,
271    RTEMS_MINIMUM_STACK_SIZE,
272    RTEMS_DEFAULT_MODES,
273    RTEMS_DEFAULT_ATTRIBUTES,
274    &ctx->worker_task_id
275  );
276  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
277
278  sc = rtems_task_start(ctx->worker_task_id, worker_task, 0);
279  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
280
281  wait();
282
283  sc = rtems_task_delete(ctx->worker_task_id);
284  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
285
286  rv = unlink(&file_path[0]);
287  rtems_test_assert(rv == 0);
288
289  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
290}
291
292static void Init(rtems_task_argument arg)
293{
294  TEST_BEGIN();
295
296  test();
297
298  TEST_END();
299  rtems_test_exit(0);
300}
301
302#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
303#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
304
305#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
306
307#define CONFIGURE_MAXIMUM_TASKS 2
308
309#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
310
311#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
312
313#define CONFIGURE_INIT
314
315#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.