aboutsummaryrefslogblamecommitdiffstats
path: root/libexec_add_output_document.c
blob: 9eb1f8d26c0f30d9d453205e301f7a9dd771b487 (plain) (tree)












































                                                                                                         
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libexec_add_output_document(struct libexec_command *cmd, int fd, struct libexec_document *doc, int flags)
{
	int fd_flags, fl_flags, fds[2];

	if (!cmd || fd < 0 || !doc) {
		errno = EINVAL;
		return -1;
	}

	fd_flags = (flags & O_CLOEXEC);
	fl_flags = (flags ^ fd_flags);
	if (fd_flags)
		fd_flags = FD_CLOEXEC;

	if (pipe(fds))
		return -1;
	if (fd_flags && fcntl(fds[0], F_SETFD, fd_flags))
		goto fail;
	if (fl_flags && fcntl(fds[0], F_SETFL, fl_flags))
		goto fail;

	doc->fd = fds[0];

	if (libexec_dup(cmd, fd, fds[1]))
		goto fail;
	cmd->plumings[cmd->nplumings - 1].type = LIBEXEC_PLUMING_PIPE;

	return 0;

fail:
	close(fds[0]);
	close(fds[1]);
	return -1;
}


#else
LIBEXEC_CONST__ int main(void) {return 0;} /* TODO test */
#endif