ATF_TC_BODY (stream_init_inherit, tc) { atf_process_stream_t sb; RE (atf_process_stream_init_inherit (&sb)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_inherit); atf_process_stream_fini (&sb); }
ATF_TC_BODY (stream_init_redirect_fd, tc) { atf_process_stream_t sb; RE (atf_process_stream_init_redirect_fd (&sb, 1)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_redirect_fd); atf_process_stream_fini (&sb); }
ATF_TC_BODY (stream_init_connect, tc) { atf_process_stream_t sb; RE (atf_process_stream_init_connect (&sb, 1, 2)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_connect); atf_process_stream_fini (&sb); }
ATF_TC_BODY (stream_init_capture, tc) { atf_process_stream_t sb; RE (atf_process_stream_init_capture (&sb)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_capture); atf_process_stream_fini (&sb); }
ATF_TC_BODY (stream_init_redirect_path, tc) { atf_process_stream_t sb; atf_fs_path_t path; RE (atf_fs_path_init_fmt (&path, "foo")); RE (atf_process_stream_init_redirect_path (&sb, &path)); ATF_CHECK_EQ (atf_process_stream_type (&sb), atf_process_stream_type_redirect_path); atf_process_stream_fini (&sb); atf_fs_path_fini (&path); }
static void parent_connect(const stream_prepare_t *sp, int *fd) { const int type = atf_process_stream_type(sp->m_sb); if (type == atf_process_stream_type_capture) { close(sp->m_pipefds[1]); *fd = sp->m_pipefds[0]; } else if (type == atf_process_stream_type_connect) { /* Do nothing. */ } else if (type == atf_process_stream_type_inherit) { /* Do nothing. */ } else if (type == atf_process_stream_type_redirect_fd) { /* Do nothing. */ } else if (type == atf_process_stream_type_redirect_path) { /* Do nothing. */ } else { UNREACHABLE; } }
static atf_error_t child_connect(const stream_prepare_t *sp, int procfd) { atf_error_t err; const int type = atf_process_stream_type(sp->m_sb); if (type == atf_process_stream_type_capture) { close(sp->m_pipefds[0]); err = safe_dup(sp->m_pipefds[1], procfd); } else if (type == atf_process_stream_type_connect) { if (dup2(sp->m_sb->m_tgt_fd, sp->m_sb->m_src_fd) == -1) err = atf_libc_error(errno, "Cannot connect descriptor %d to %d", sp->m_sb->m_tgt_fd, sp->m_sb->m_src_fd); else err = atf_no_error(); } else if (type == atf_process_stream_type_inherit) { err = atf_no_error(); } else if (type == atf_process_stream_type_redirect_fd) { err = safe_dup(sp->m_sb->m_fd, procfd); } else if (type == atf_process_stream_type_redirect_path) { int aux = open(atf_fs_path_cstring(sp->m_sb->m_path), O_WRONLY | O_CREAT | O_TRUNC, 0644); if (aux == -1) err = atf_libc_error(errno, "Could not create %s", atf_fs_path_cstring(sp->m_sb->m_path)); else { err = safe_dup(aux, procfd); if (atf_is_error(err)) close(aux); } } else { UNREACHABLE; err = atf_no_error(); } return err; }
static atf_error_t stream_prepare_init(stream_prepare_t *sp, const atf_process_stream_t *sb) { atf_error_t err; const int type = atf_process_stream_type(sb); sp->m_sb = sb; sp->m_pipefds_ok = false; if (type == atf_process_stream_type_capture) { if (pipe(sp->m_pipefds) == -1) err = atf_libc_error(errno, "Failed to create pipe"); else { err = atf_no_error(); sp->m_pipefds_ok = true; } } else err = atf_no_error(); return err; }