4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2007 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* This prototype is not in the header file because this is a private
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * function; however, we need to access it during testing. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t atf_process_status_init(atf_process_status_t *, int);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "stream_prepare" auxiliary type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstream_prepare_init(stream_prepare_t *sp, const atf_process_stream_t *sb)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(errno, "Failed to create pipe");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_process_stream" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntconst int atf_process_stream_type_redirect_path = 5;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (sb->m_type == atf_process_stream_type_capture) ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (sb->m_type == atf_process_stream_type_connect) ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (sb->m_type == atf_process_stream_type_inherit) ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (sb->m_type == atf_process_stream_type_redirect_fd) ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (sb->m_type == atf_process_stream_type_redirect_path);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_init_capture(atf_process_stream_t *sb)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_init_connect(atf_process_stream_t *sb,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_init_inherit(atf_process_stream_t *sb)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_init_redirect_fd(atf_process_stream_t *sb,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_init_redirect_path(atf_process_stream_t *sb,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sb->m_type = atf_process_stream_type_redirect_path;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_stream_type(const atf_process_stream_t *sb)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_process_status" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_init(atf_process_status_t *s, int status)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntatf_process_status_fini(atf_process_status_t *s ATF_DEFS_ATTRIBUTE_UNUSED)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_exited(const atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_exitstatus(const atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_signaled(const atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_termsig(const atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_status_coredump(const atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * The "atf_process_child" type.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_child_wait(atf_process_child_t *c, atf_process_status_t *s)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(errno, "Failed waiting for process %d",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_process_child_pid(const atf_process_child_t *c)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(errno, "Could not allocate file descriptor");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntchild_connect(const stream_prepare_t *sp, int procfd)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const int type = atf_process_stream_type(sp->m_sb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_connect) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (dup2(sp->m_sb->m_tgt_fd, sp->m_sb->m_src_fd) == -1)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(errno, "Cannot connect descriptor %d to %d",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_inherit) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_redirect_fd) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_redirect_path) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int aux = open(atf_fs_path_cstring(sp->m_sb->m_path),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_libc_error(errno, "Could not create %s",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntparent_connect(const stream_prepare_t *sp, int *fd)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const int type = atf_process_stream_type(sp->m_sb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_connect) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* Do nothing. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_inherit) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* Do nothing. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_redirect_fd) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* Do nothing. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } else if (type == atf_process_stream_type_redirect_path) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* Do nothing. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdo_child(void (*)(void *),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const stream_prepare_t *) ATF_DEFS_ATTRIBUTE_NORETURN;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt void (*start)(void *),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (pid == 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntinit_stream_w_default(const atf_process_stream_t *usersb,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt void (*start)(void *),
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_t inherit_outsb, inherit_errsb;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const atf_process_stream_t *real_outsb, *real_errsb;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = init_stream_w_default(outsb, &inherit_outsb, &real_outsb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = init_stream_w_default(errsb, &inherit_errsb, &real_errsb);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = fork_with_streams(c, start, real_outsb, real_errsb, v);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntconst_execvp(const char *file, const char *const *argv)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntlist_to_array(const atf_list_t *l, const char ***ap)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char **a;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt a = (const char **)malloc((atf_list_size(l) + 1) * sizeof(const char *));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *aiter = (const char *)atf_list_citer_data(liter);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const int ret = const_execvp(atf_fs_path_cstring(ea->m_prog), ea->m_argv);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_fs_path_cstring(ea->m_prog), strerror(errnocopy));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *const *argv,
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt void (*prehook)(void))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_type(outsb) != atf_process_stream_type_capture);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_type(errsb) != atf_process_stream_type_capture);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_process_fork(&c, do_exec, outsb, errsb, &ea);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INV(atf_error_is(err, "libc") && atf_libc_error_code(err) == EINTR);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt void (*prehook)(void))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_type(outsb) != atf_process_stream_type_capture);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_process_stream_type(errsb) != atf_process_stream_type_capture);