source: rtems/testsuites/libtests/newlib01/init.c @ fe09d8d

4.11
Last change on this file since fe09d8d was fe09d8d, checked in by Chris Johns <chrisj@…>, on 02/14/20 at 04:16:46

libcsupport/newlib: Call newlib's sinit to force reent initialisation

  • Newlib overtites any FILE pointers set in stdin, stdout or stderr.

Closes #3870

  • 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/imfs.h>
23#include <rtems/libcsupport.h>
24
25#include "tmacros.h"
26
27const char rtems_test_name[] = "NEWLIB 1";
28
29static const char file_path[] = "/file";
30
31typedef enum {
32  INIT,
33  OPEN,
34  WRITTEN,
35  CLOSED
36} test_state;
37
38typedef struct {
39  rtems_id main_task_id;
40  rtems_id worker_task_id;
41  test_state current;
42} test_context;
43
44static test_context test_instance;
45
46static void wake_up_main(const test_context *ctx)
47{
48  rtems_status_code sc;
49
50  sc = rtems_event_transient_send(ctx->main_task_id);
51  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
52}
53
54static void wait(void)
55{
56  rtems_status_code sc;
57
58  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
59  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
60}
61
62static void worker_task(rtems_task_argument arg)
63{
64  test_context *ctx = &test_instance;
65  FILE *file = freopen(&file_path[0], "r+", stdout);
66  char buf[1] = { 'x' };
67  size_t n;
68
69  rtems_test_assert(file != NULL);
70
71  n = fwrite(&buf[0], sizeof(buf), 1, file);
72  rtems_test_assert(n == 1);
73
74  rtems_test_assert(ctx->current == OPEN);
75
76  wake_up_main(ctx);
77
78  rtems_test_assert(0);
79}
80
81static int handler_open(
82  rtems_libio_t *iop,
83  const char *path,
84  int oflag,
85  mode_t mode
86)
87{
88  test_context *ctx = &test_instance;
89
90  rtems_test_assert(ctx->current == INIT);
91  ctx->current = OPEN;
92
93  return 0;
94}
95
96static int handler_close(
97  rtems_libio_t *iop
98)
99{
100  test_context *ctx = &test_instance;
101
102  rtems_test_assert(ctx->current == WRITTEN);
103  ctx->current = CLOSED;
104
105  return 0;
106}
107
108static ssize_t handler_read(
109  rtems_libio_t *iop,
110  void *buffer,
111  size_t count
112)
113{
114  rtems_test_assert(0);
115
116  return 0;
117}
118
119static ssize_t handler_write(
120  rtems_libio_t *iop,
121  const void *buffer,
122  size_t count
123)
124{
125  test_context *ctx = &test_instance;
126
127  rtems_test_assert(ctx->current == OPEN);
128  ctx->current = WRITTEN;
129
130  iop->offset += count;
131
132  return (ssize_t) count;
133}
134
135static int handler_ioctl(
136  rtems_libio_t *iop,
137  uint32_t request,
138  void *buffer
139)
140{
141  rtems_test_assert(0);
142
143  return 0;
144}
145
146static off_t handler_lseek(
147  rtems_libio_t *iop,
148  off_t length,
149  int whence
150)
151{
152  rtems_test_assert(0);
153
154  return 0;
155}
156
157static int handler_ftruncate(
158  rtems_libio_t *iop,
159  off_t length
160)
161{
162  rtems_test_assert(0);
163
164  return 0;
165}
166
167static int handler_fsync(
168  rtems_libio_t *iop
169)
170{
171  rtems_test_assert(0);
172
173  return 0;
174}
175
176static int handler_fdatasync(
177  rtems_libio_t *iop
178)
179{
180  rtems_test_assert(0);
181
182  return 0;
183}
184
185static int handler_fcntl(
186  rtems_libio_t *iop,
187  int cmd
188)
189{
190  rtems_test_assert(0);
191
192  return 0;
193}
194
195static ssize_t handler_readv(
196  rtems_libio_t *iop,
197  const struct iovec *iov,
198  int iovcnt,
199  ssize_t total
200)
201{
202  rtems_test_assert(0);
203
204  return 0;
205}
206
207static ssize_t handler_writev(
208  rtems_libio_t *iop,
209  const struct iovec *iov,
210  int iovcnt,
211  ssize_t total
212)
213{
214  rtems_test_assert(0);
215
216  return 0;
217}
218
219static const rtems_filesystem_file_handlers_r node_handlers = {
220  .open_h = handler_open,
221  .close_h = handler_close,
222  .read_h = handler_read,
223  .write_h = handler_write,
224  .ioctl_h = handler_ioctl,
225  .lseek_h = handler_lseek,
226  .fstat_h = rtems_filesystem_default_fstat,
227  .ftruncate_h = handler_ftruncate,
228  .fsync_h = handler_fsync,
229  .fdatasync_h = handler_fdatasync,
230  .fcntl_h = handler_fcntl,
231  .readv_h = handler_readv,
232  .writev_h = handler_writev
233};
234
235static const IMFS_node_control node_control = IMFS_GENERIC_INITIALIZER(
236  &node_handlers,
237  IMFS_node_initialize_default,
238  IMFS_node_destroy_default
239);
240
241static void test(void)
242{
243  test_context *ctx = &test_instance;
244  rtems_status_code sc;
245  int rv;
246  rtems_resource_snapshot snapshot;
247
248  rtems_test_assert(fileno(stdout) >= 0);
249  rtems_test_assert(fileno(stdin) >= 0);
250  rtems_test_assert(fileno(stderr) >= 0);
251
252  ctx->main_task_id = rtems_task_self();
253
254  rtems_resource_snapshot_take(&snapshot);
255
256  rv = IMFS_make_generic_node(
257    &file_path[0],
258    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
259    &node_control,
260    NULL
261  );
262  rtems_test_assert(rv == 0);
263
264  sc = rtems_task_create(
265    rtems_build_name('W', 'O', 'R', 'K'),
266    2,
267    RTEMS_MINIMUM_STACK_SIZE,
268    RTEMS_DEFAULT_MODES,
269    RTEMS_DEFAULT_ATTRIBUTES,
270    &ctx->worker_task_id
271  );
272  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
273
274  sc = rtems_task_start(ctx->worker_task_id, worker_task, 0);
275  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
276
277  wait();
278
279  sc = rtems_task_delete(ctx->worker_task_id);
280  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
281
282  rtems_test_assert(fileno(stdout) >= 0);
283  rtems_test_assert(fileno(stdin) >= 0);
284  rtems_test_assert(fileno(stderr) >= 0);
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.