1 | /* |
---|
2 | * Copyright (c) 2013 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 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #include <err.h> |
---|
33 | #include <assert.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <stdlib.h> |
---|
36 | #include <unistd.h> |
---|
37 | #include <syslog.h> |
---|
38 | #include <sys/socket.h> |
---|
39 | #include <fcntl.h> |
---|
40 | |
---|
41 | #define RTEMS_BSD_PROGRAM_NO_EXIT_WRAP |
---|
42 | #define RTEMS_BSD_PROGRAM_NO_PRINTF_WRAP |
---|
43 | #include <machine/rtems-bsd-program.h> |
---|
44 | #include <machine/pcpu.h> |
---|
45 | |
---|
46 | #include <rtems.h> |
---|
47 | #include <rtems/libcsupport.h> |
---|
48 | |
---|
49 | #define TEST_NAME "LIBBSD SYSCALLS 1" |
---|
50 | |
---|
51 | typedef void (*no_mem_test_body)(int fd); |
---|
52 | |
---|
53 | typedef struct { |
---|
54 | no_mem_test_body body; |
---|
55 | int fd; |
---|
56 | rtems_id master_task; |
---|
57 | } no_mem_test; |
---|
58 | |
---|
59 | static void |
---|
60 | no_mem_task(rtems_task_argument arg) |
---|
61 | { |
---|
62 | const no_mem_test *self = (const no_mem_test *) arg; |
---|
63 | rtems_status_code sc; |
---|
64 | void *greedy; |
---|
65 | |
---|
66 | assert(rtems_configuration_get_unified_work_area()); |
---|
67 | |
---|
68 | greedy = rtems_workspace_greedy_allocate(NULL, 0); |
---|
69 | (*self->body)(self->fd); |
---|
70 | rtems_workspace_greedy_free(greedy); |
---|
71 | |
---|
72 | sc = rtems_event_transient_send(self->master_task); |
---|
73 | assert(sc == RTEMS_SUCCESSFUL); |
---|
74 | |
---|
75 | sc = rtems_task_suspend(RTEMS_SELF); |
---|
76 | assert(sc == RTEMS_SUCCESSFUL); |
---|
77 | } |
---|
78 | |
---|
79 | void |
---|
80 | do_no_mem_test(no_mem_test_body body, int fd) |
---|
81 | { |
---|
82 | no_mem_test test = { |
---|
83 | .body = body, |
---|
84 | .fd = fd, |
---|
85 | .master_task = rtems_task_self() |
---|
86 | }; |
---|
87 | rtems_status_code sc; |
---|
88 | rtems_id id; |
---|
89 | rtems_resource_snapshot snapshot; |
---|
90 | |
---|
91 | rtems_resource_snapshot_take(&snapshot); |
---|
92 | |
---|
93 | sc = rtems_task_create( |
---|
94 | rtems_build_name('N', 'M', 'E', 'M'), |
---|
95 | RTEMS_MINIMUM_PRIORITY, |
---|
96 | RTEMS_MINIMUM_STACK_SIZE, |
---|
97 | RTEMS_DEFAULT_MODES, |
---|
98 | RTEMS_FLOATING_POINT, |
---|
99 | &id |
---|
100 | ); |
---|
101 | assert(sc == RTEMS_SUCCESSFUL); |
---|
102 | |
---|
103 | sc = rtems_task_start(id, no_mem_task, (rtems_task_argument) &test); |
---|
104 | assert(sc == RTEMS_SUCCESSFUL); |
---|
105 | |
---|
106 | sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
107 | assert(sc == RTEMS_SUCCESSFUL); |
---|
108 | |
---|
109 | sc = rtems_task_delete(id); |
---|
110 | assert(sc == RTEMS_SUCCESSFUL); |
---|
111 | |
---|
112 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
113 | } |
---|
114 | |
---|
115 | static const char prog_name[] = "prog"; |
---|
116 | |
---|
117 | static int |
---|
118 | invalid_prog(void *ctx) |
---|
119 | { |
---|
120 | (void) ctx; |
---|
121 | |
---|
122 | assert(0); |
---|
123 | } |
---|
124 | |
---|
125 | static int |
---|
126 | invalid_main(int argc, char **argv) |
---|
127 | { |
---|
128 | (void) argc; |
---|
129 | (void) argv; |
---|
130 | |
---|
131 | assert(0); |
---|
132 | } |
---|
133 | |
---|
134 | static void *const some_context = (void *) 0xcafe; |
---|
135 | |
---|
136 | static int |
---|
137 | some_prog(void *ctx) |
---|
138 | { |
---|
139 | assert(ctx == some_context); |
---|
140 | assert(strcmp(rtems_bsd_program_get_name(), prog_name) == 0); |
---|
141 | assert(rtems_bsd_program_get_context() == some_context); |
---|
142 | errno = 0; |
---|
143 | rtems_bsd_program_exit(456); |
---|
144 | } |
---|
145 | |
---|
146 | static const int some_argc = 1; |
---|
147 | |
---|
148 | static char *some_argv[] = { "a", NULL }; |
---|
149 | |
---|
150 | static int |
---|
151 | some_main(int argc, char **argv) |
---|
152 | { |
---|
153 | assert(argc == some_argc); |
---|
154 | assert(argv == some_argv); |
---|
155 | assert(strcmp(rtems_bsd_program_get_name(), prog_name) == 0); |
---|
156 | errno = 0; |
---|
157 | rtems_bsd_program_exit(789); |
---|
158 | } |
---|
159 | |
---|
160 | static void |
---|
161 | no_mem_bsd_program(int fd) |
---|
162 | { |
---|
163 | (void) fd; |
---|
164 | |
---|
165 | assert(rtems_bsd_program_call(prog_name, invalid_prog, NULL) |
---|
166 | == EXIT_FAILURE); |
---|
167 | assert(rtems_bsd_program_call_main(prog_name, invalid_main, some_argc, |
---|
168 | some_argv) == EXIT_FAILURE); |
---|
169 | assert(strcmp(rtems_bsd_program_get_name(), "?") == 0); |
---|
170 | assert(rtems_bsd_program_get_context() == NULL); |
---|
171 | } |
---|
172 | |
---|
173 | static void |
---|
174 | test_bsd_program(void) |
---|
175 | { |
---|
176 | rtems_resource_snapshot snapshot; |
---|
177 | int exit_code; |
---|
178 | void *greedy; |
---|
179 | char *invalid_argv[2] = { "a", "b" }; |
---|
180 | struct thread *td; |
---|
181 | |
---|
182 | assert(rtems_configuration_get_unified_work_area()); |
---|
183 | |
---|
184 | puts("test BSD program"); |
---|
185 | |
---|
186 | /* Needs to be called once before the snapshot is taken so that the |
---|
187 | * thread structure is already created. */ |
---|
188 | td = rtems_bsd_get_curthread_or_null(); |
---|
189 | assert(td != NULL); |
---|
190 | |
---|
191 | rtems_resource_snapshot_take(&snapshot); |
---|
192 | |
---|
193 | do_no_mem_test(no_mem_bsd_program, -1); |
---|
194 | |
---|
195 | greedy = rtems_workspace_greedy_allocate(NULL, 0); |
---|
196 | no_mem_bsd_program(-1); |
---|
197 | rtems_workspace_greedy_free(greedy); |
---|
198 | |
---|
199 | errno = 0; |
---|
200 | exit_code = rtems_bsd_program_call_main(prog_name, NULL, 1, invalid_argv); |
---|
201 | assert(errno == EFAULT); |
---|
202 | assert(exit_code == EXIT_FAILURE); |
---|
203 | |
---|
204 | errno = EINVAL; |
---|
205 | exit_code = rtems_bsd_program_call(prog_name, some_prog, some_context); |
---|
206 | assert(errno == 0); |
---|
207 | assert(exit_code == 456); |
---|
208 | assert(strcmp(rtems_bsd_program_get_name(), "?") == 0); |
---|
209 | assert(rtems_bsd_program_get_context() == NULL); |
---|
210 | |
---|
211 | errno = EINVAL; |
---|
212 | exit_code = rtems_bsd_program_call_main(prog_name, some_main, |
---|
213 | some_argc, some_argv); |
---|
214 | assert(errno == 0); |
---|
215 | assert(exit_code == 789); |
---|
216 | assert(strcmp(rtems_bsd_program_get_name(), "?") == 0); |
---|
217 | assert(rtems_bsd_program_get_context() == NULL); |
---|
218 | |
---|
219 | assert(rtems_resource_snapshot_check(&snapshot)); |
---|
220 | } |
---|
221 | |
---|
222 | static void |
---|
223 | test_warn(void) |
---|
224 | { |
---|
225 | puts("test warn"); |
---|
226 | |
---|
227 | errno = EAGAIN; |
---|
228 | warn("%s", "warn"); |
---|
229 | |
---|
230 | errno = ENAMETOOLONG; |
---|
231 | warn(NULL); |
---|
232 | |
---|
233 | errno = 0; |
---|
234 | warnc(EDOM, "%s", "warnc"); |
---|
235 | |
---|
236 | errno = 0; |
---|
237 | warnc(ERANGE, NULL); |
---|
238 | |
---|
239 | warnx("%s", "warnx"); |
---|
240 | |
---|
241 | warnx(NULL); |
---|
242 | } |
---|
243 | |
---|
244 | static int |
---|
245 | call_err(void *ctx) |
---|
246 | { |
---|
247 | errno = EAGAIN; |
---|
248 | err(10, "%s", "call_err"); |
---|
249 | } |
---|
250 | |
---|
251 | static int |
---|
252 | call_err_null(void *ctx) |
---|
253 | { |
---|
254 | errno = ENAMETOOLONG; |
---|
255 | err(11, NULL); |
---|
256 | } |
---|
257 | |
---|
258 | static int |
---|
259 | call_errc(void *ctx) |
---|
260 | { |
---|
261 | errc(12, EDOM, "%s", "call_errc"); |
---|
262 | } |
---|
263 | |
---|
264 | static int |
---|
265 | call_errc_null(void *ctx) |
---|
266 | { |
---|
267 | errc(13, ERANGE, NULL); |
---|
268 | } |
---|
269 | |
---|
270 | static int |
---|
271 | call_errx(void *ctx) |
---|
272 | { |
---|
273 | errx(14, "%s", "call_errx"); |
---|
274 | } |
---|
275 | |
---|
276 | static int |
---|
277 | call_errx_null(void *ctx) |
---|
278 | { |
---|
279 | errx(15, NULL); |
---|
280 | } |
---|
281 | |
---|
282 | static void |
---|
283 | test_err(void) |
---|
284 | { |
---|
285 | int exit_code; |
---|
286 | |
---|
287 | puts("test err"); |
---|
288 | |
---|
289 | exit_code = rtems_bsd_program_call("err", call_err, NULL); |
---|
290 | assert(exit_code == 10); |
---|
291 | |
---|
292 | exit_code = rtems_bsd_program_call("err", call_err_null, NULL); |
---|
293 | assert(exit_code == 11); |
---|
294 | |
---|
295 | exit_code = rtems_bsd_program_call("errc", call_errc, NULL); |
---|
296 | assert(exit_code == 12); |
---|
297 | |
---|
298 | exit_code = rtems_bsd_program_call("errc", call_errc_null, NULL); |
---|
299 | assert(exit_code == 13); |
---|
300 | |
---|
301 | exit_code = rtems_bsd_program_call("errx", call_errx, NULL); |
---|
302 | assert(exit_code == 14); |
---|
303 | |
---|
304 | exit_code = rtems_bsd_program_call("errx", call_errx_null, NULL); |
---|
305 | assert(exit_code == 15); |
---|
306 | } |
---|
307 | |
---|
308 | static void |
---|
309 | test_main(void) |
---|
310 | { |
---|
311 | test_bsd_program(); |
---|
312 | test_warn(); |
---|
313 | test_err(); |
---|
314 | |
---|
315 | exit(0); |
---|
316 | } |
---|
317 | |
---|
318 | #include <rtems/bsd/test/default-init.h> |
---|