1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/check.h"
38 #include "atf-c/build.h"
39 #include "atf-c/defs.h"
40 #include "atf-c/detail/dynstr.h"
41 #include "atf-c/detail/env.h"
42 #include "atf-c/detail/fs.h"
43 #include "atf-c/detail/list.h"
44 #include "atf-c/detail/process.h"
45 #include "atf-c/detail/sanity.h"
46 #include "atf-c/error.h"
47 #include "atf-c/utils.h"
49 /* ---------------------------------------------------------------------
50 * Auxiliary functions.
51 * --------------------------------------------------------------------- */
55 create_tmpdir(atf_fs_path_t *dir)
59 err = atf_fs_path_init_fmt(dir, "%s/check.XXXXXX",
60 atf_env_get_with_default("TMPDIR", "/tmp"));
61 if (atf_is_error(err))
64 err = atf_fs_mkdtemp(dir);
65 if (atf_is_error(err)) {
66 atf_fs_path_fini(dir);
70 INV(!atf_is_error(err));
77 cleanup_tmpdir(const atf_fs_path_t *dir, const atf_fs_path_t *outfile,
78 const atf_fs_path_t *errfile)
81 atf_error_t err = atf_fs_unlink(outfile);
82 if (atf_is_error(err)) {
83 INV(atf_error_is(err, "libc") &&
84 atf_libc_error_code(err) == ENOENT);
87 INV(!atf_is_error(err));
91 atf_error_t err = atf_fs_unlink(errfile);
92 if (atf_is_error(err)) {
93 INV(atf_error_is(err, "libc") &&
94 atf_libc_error_code(err) == ENOENT);
97 INV(!atf_is_error(err));
101 atf_error_t err = atf_fs_rmdir(dir);
102 INV(!atf_is_error(err));
108 const_execvp(const char *file, const char *const *argv)
110 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
111 return execvp(file, UNCONST(argv));
117 init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb)
122 err = atf_process_stream_init_inherit(sb);
124 err = atf_process_stream_init_redirect_path(sb, path);
131 init_sbs(const atf_fs_path_t *outfile, atf_process_stream_t *outsb,
132 const atf_fs_path_t *errfile, atf_process_stream_t *errsb)
136 err = init_sb(outfile, outsb);
137 if (atf_is_error(err))
140 err = init_sb(errfile, errsb);
141 if (atf_is_error(err)) {
142 atf_process_stream_fini(outsb);
151 const char *const *m_argv;
154 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
160 struct exec_data *ea = v;
162 const_execvp(ea->m_argv[0], ea->m_argv);
163 fprintf(stderr, "execvp(%s) failed: %s\n", ea->m_argv[0], strerror(errno));
169 fork_and_wait(const char *const *argv, const atf_fs_path_t *outfile,
170 const atf_fs_path_t *errfile, atf_process_status_t *status)
173 atf_process_child_t child;
174 atf_process_stream_t outsb, errsb;
175 struct exec_data ea = { argv };
177 err = init_sbs(outfile, &outsb, errfile, &errsb);
178 if (atf_is_error(err))
181 err = atf_process_fork(&child, exec_child, &outsb, &errsb, &ea);
182 if (atf_is_error(err))
185 err = atf_process_child_wait(&child, status);
188 atf_process_stream_fini(&errsb);
189 atf_process_stream_fini(&outsb);
196 update_success_from_status(const char *progname,
197 const atf_process_status_t *status, bool *success)
199 bool s = atf_process_status_exited(status) &&
200 atf_process_status_exitstatus(status) == EXIT_SUCCESS;
202 if (atf_process_status_exited(status)) {
203 if (atf_process_status_exitstatus(status) == EXIT_SUCCESS)
207 fprintf(stderr, "%s failed with exit code %d\n", progname,
208 atf_process_status_exitstatus(status));
210 } else if (atf_process_status_signaled(status)) {
212 fprintf(stderr, "%s failed due to signal %d%s\n", progname,
213 atf_process_status_termsig(status),
214 atf_process_status_coredump(status) ? " (core dumped)" : "");
217 fprintf(stderr, "%s failed due to unknown reason\n", progname);
225 array_to_list(const char *const *a, atf_list_t *l)
229 err = atf_list_init(l);
230 if (atf_is_error(err))
234 char *item = strdup(*a);
236 err = atf_no_memory_error();
240 err = atf_list_append(l, item, true);
241 if (atf_is_error(err))
252 print_array(const char *const *array, const char *pfx)
254 const char *const *ptr;
257 for (ptr = array; *ptr != NULL; ptr++)
264 check_build_run(const char *const *argv, bool *success)
267 atf_process_status_t status;
269 print_array(argv, ">");
271 err = fork_and_wait(argv, NULL, NULL, &status);
272 if (atf_is_error(err))
275 update_success_from_status(argv[0], &status, success);
276 atf_process_status_fini(&status);
278 INV(!atf_is_error(err));
283 /* ---------------------------------------------------------------------
284 * The "atf_check_result" type.
285 * --------------------------------------------------------------------- */
287 struct atf_check_result_impl {
290 atf_fs_path_t m_stdout;
291 atf_fs_path_t m_stderr;
292 atf_process_status_t m_status;
297 atf_check_result_init(atf_check_result_t *r, const char *const *argv,
298 const atf_fs_path_t *dir)
302 r->pimpl = malloc(sizeof(struct atf_check_result_impl));
303 if (r->pimpl == NULL)
304 return atf_no_memory_error();
306 err = array_to_list(argv, &r->pimpl->m_argv);
307 if (atf_is_error(err))
310 err = atf_fs_path_copy(&r->pimpl->m_dir, dir);
311 if (atf_is_error(err))
314 err = atf_fs_path_init_fmt(&r->pimpl->m_stdout, "%s/stdout",
315 atf_fs_path_cstring(dir));
316 if (atf_is_error(err))
319 err = atf_fs_path_init_fmt(&r->pimpl->m_stderr, "%s/stderr",
320 atf_fs_path_cstring(dir));
321 if (atf_is_error(err))
324 INV(!atf_is_error(err));
328 atf_fs_path_fini(&r->pimpl->m_stdout);
330 atf_fs_path_fini(&r->pimpl->m_dir);
332 atf_list_fini(&r->pimpl->m_argv);
338 atf_check_result_fini(atf_check_result_t *r)
340 atf_process_status_fini(&r->pimpl->m_status);
342 cleanup_tmpdir(&r->pimpl->m_dir, &r->pimpl->m_stdout,
343 &r->pimpl->m_stderr);
344 atf_fs_path_fini(&r->pimpl->m_stdout);
345 atf_fs_path_fini(&r->pimpl->m_stderr);
346 atf_fs_path_fini(&r->pimpl->m_dir);
348 atf_list_fini(&r->pimpl->m_argv);
354 atf_check_result_stdout(const atf_check_result_t *r)
356 return atf_fs_path_cstring(&r->pimpl->m_stdout);
360 atf_check_result_stderr(const atf_check_result_t *r)
362 return atf_fs_path_cstring(&r->pimpl->m_stderr);
366 atf_check_result_exited(const atf_check_result_t *r)
368 return atf_process_status_exited(&r->pimpl->m_status);
372 atf_check_result_exitcode(const atf_check_result_t *r)
374 return atf_process_status_exitstatus(&r->pimpl->m_status);
378 atf_check_result_signaled(const atf_check_result_t *r)
380 return atf_process_status_signaled(&r->pimpl->m_status);
384 atf_check_result_termsig(const atf_check_result_t *r)
386 return atf_process_status_termsig(&r->pimpl->m_status);
389 /* ---------------------------------------------------------------------
391 * --------------------------------------------------------------------- */
393 /* XXX: This function shouldn't be in this module. It messes with stdout
394 * and stderr, and it provides a very high-end interface. This belongs,
395 * probably, somewhere related to test cases (such as in the tc module). */
397 atf_check_build_c_o(const char *sfile,
399 const char *const optargs[],
405 err = atf_build_c_o(sfile, ofile, optargs, &argv);
406 if (atf_is_error(err))
409 err = check_build_run((const char *const *)argv, success);
411 atf_utils_free_charpp(argv);
417 atf_check_build_cpp(const char *sfile,
419 const char *const optargs[],
425 err = atf_build_cpp(sfile, ofile, optargs, &argv);
426 if (atf_is_error(err))
429 err = check_build_run((const char *const *)argv, success);
431 atf_utils_free_charpp(argv);
437 atf_check_build_cxx_o(const char *sfile,
439 const char *const optargs[],
445 err = atf_build_cxx_o(sfile, ofile, optargs, &argv);
446 if (atf_is_error(err))
449 err = check_build_run((const char *const *)argv, success);
451 atf_utils_free_charpp(argv);
457 atf_check_exec_array(const char *const *argv, atf_check_result_t *r)
462 err = create_tmpdir(&dir);
463 if (atf_is_error(err))
466 err = atf_check_result_init(r, argv, &dir);
467 if (atf_is_error(err)) {
468 atf_error_t err2 = atf_fs_rmdir(&dir);
469 INV(!atf_is_error(err2));
473 err = fork_and_wait(argv, &r->pimpl->m_stdout, &r->pimpl->m_stderr,
474 &r->pimpl->m_status);
475 if (atf_is_error(err)) {
476 atf_check_result_fini(r);
480 INV(!atf_is_error(err));
482 atf_fs_path_fini(&dir);