Blame libarchive/filter_fork_posix.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2007 Joerg Sonnenberger
Packit Service 1d0348
 * Copyright (c) 2012 Michihiro NAKAJIMA 
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
Packit Service 1d0348
/* This capability is only available on POSIX systems. */
Packit Service 1d0348
#if defined(HAVE_PIPE) && defined(HAVE_FCNTL) && \
Packit Service 1d0348
    (defined(HAVE_FORK) || defined(HAVE_VFORK) || defined(HAVE_POSIX_SPAWNP))
Packit Service 1d0348
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/filter_fork.c 182958 2008-09-12 05:33:00Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#if defined(HAVE_SYS_TYPES_H)
Packit Service 1d0348
#  include <sys/types.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#  include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#  include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
Packit Service 1d0348
#  if defined(HAVE_POLL_H)
Packit Service 1d0348
#    include <poll.h>
Packit Service 1d0348
#  elif defined(HAVE_SYS_POLL_H)
Packit Service 1d0348
#    include <sys/poll.h>
Packit Service 1d0348
#  endif
Packit Service 1d0348
#elif defined(HAVE_SELECT)
Packit Service 1d0348
#  if defined(HAVE_SYS_SELECT_H)
Packit Service 1d0348
#    include <sys/select.h>
Packit Service 1d0348
#  elif defined(HAVE_UNISTD_H)
Packit Service 1d0348
#    include <unistd.h>
Packit Service 1d0348
#  endif
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_FCNTL_H
Packit Service 1d0348
#  include <fcntl.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_SPAWN_H
Packit Service 1d0348
#  include <spawn.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#  include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_UNISTD_H
Packit Service 1d0348
#  include <unistd.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_cmdline_private.h"
Packit Service 1d0348
Packit Service 1d0348
#include "filter_fork.h"
Packit Service 1d0348
Packit Service 1d0348
pid_t
Packit Service 1d0348
__archive_create_child(const char *cmd, int *child_stdin, int *child_stdout)
Packit Service 1d0348
{
Packit Service 1d0348
	pid_t child;
Packit Service 1d0348
	int stdin_pipe[2], stdout_pipe[2], tmp;
Packit Service 1d0348
#if HAVE_POSIX_SPAWNP
Packit Service 1d0348
	posix_spawn_file_actions_t actions;
Packit Service 1d0348
	int r;
Packit Service 1d0348
#endif
Packit Service 1d0348
	struct archive_cmdline *cmdline;
Packit Service 1d0348
Packit Service 1d0348
	cmdline = __archive_cmdline_allocate();
Packit Service 1d0348
	if (cmdline == NULL)
Packit Service 1d0348
		goto state_allocated;
Packit Service 1d0348
	if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
Packit Service 1d0348
		goto state_allocated;
Packit Service 1d0348
Packit Service 1d0348
	if (pipe(stdin_pipe) == -1)
Packit Service 1d0348
		goto state_allocated;
Packit Service 1d0348
	if (stdin_pipe[0] == 1 /* stdout */) {
Packit Service 1d0348
		if ((tmp = dup(stdin_pipe[0])) == -1)
Packit Service 1d0348
			goto stdin_opened;
Packit Service 1d0348
		close(stdin_pipe[0]);
Packit Service 1d0348
		stdin_pipe[0] = tmp;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (pipe(stdout_pipe) == -1)
Packit Service 1d0348
		goto stdin_opened;
Packit Service 1d0348
	if (stdout_pipe[1] == 0 /* stdin */) {
Packit Service 1d0348
		if ((tmp = dup(stdout_pipe[1])) == -1)
Packit Service 1d0348
			goto stdout_opened;
Packit Service 1d0348
		close(stdout_pipe[1]);
Packit Service 1d0348
		stdout_pipe[1] = tmp;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
#if HAVE_POSIX_SPAWNP
Packit Service 1d0348
Packit Service 1d0348
	r = posix_spawn_file_actions_init(&actions);
Packit Service 1d0348
	if (r != 0) {
Packit Service 1d0348
		errno = r;
Packit Service 1d0348
		goto stdout_opened;
Packit Service 1d0348
	}
Packit Service 1d0348
	r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
Packit Service 1d0348
	if (r != 0)
Packit Service 1d0348
		goto actions_inited;
Packit Service 1d0348
	r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
Packit Service 1d0348
	if (r != 0)
Packit Service 1d0348
		goto actions_inited;
Packit Service 1d0348
	/* Setup for stdin. */
Packit Service 1d0348
	r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
Packit Service 1d0348
	if (r != 0)
Packit Service 1d0348
		goto actions_inited;
Packit Service 1d0348
	if (stdin_pipe[0] != 0 /* stdin */) {
Packit Service 1d0348
		r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
Packit Service 1d0348
		if (r != 0)
Packit Service 1d0348
			goto actions_inited;
Packit Service 1d0348
	}
Packit Service 1d0348
	/* Setup for stdout. */
Packit Service 1d0348
	r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
Packit Service 1d0348
	if (r != 0)
Packit Service 1d0348
		goto actions_inited;
Packit Service 1d0348
	if (stdout_pipe[1] != 1 /* stdout */) {
Packit Service 1d0348
		r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
Packit Service 1d0348
		if (r != 0)
Packit Service 1d0348
			goto actions_inited;
Packit Service 1d0348
	}
Packit Service 1d0348
	r = posix_spawnp(&child, cmdline->path, &actions, NULL,
Packit Service 1d0348
		cmdline->argv, NULL);
Packit Service 1d0348
	if (r != 0)
Packit Service 1d0348
		goto actions_inited;
Packit Service 1d0348
	posix_spawn_file_actions_destroy(&actions);
Packit Service 1d0348
Packit Service 1d0348
#else /* HAVE_POSIX_SPAWNP */
Packit Service 1d0348
Packit Service 1d0348
#if HAVE_VFORK
Packit Service 1d0348
	child = vfork();
Packit Service 1d0348
#else
Packit Service 1d0348
	child = fork();
Packit Service 1d0348
#endif
Packit Service 1d0348
	if (child == -1)
Packit Service 1d0348
		goto stdout_opened;
Packit Service 1d0348
	if (child == 0) {
Packit Service 1d0348
		close(stdin_pipe[1]);
Packit Service 1d0348
		close(stdout_pipe[0]);
Packit Service 1d0348
		if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
Packit Service 1d0348
			_exit(254);
Packit Service 1d0348
		if (stdin_pipe[0] != 0 /* stdin */)
Packit Service 1d0348
			close(stdin_pipe[0]);
Packit Service 1d0348
		if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
Packit Service 1d0348
			_exit(254);
Packit Service 1d0348
		if (stdout_pipe[1] != 1 /* stdout */)
Packit Service 1d0348
			close(stdout_pipe[1]);
Packit Service 1d0348
		execvp(cmdline->path, cmdline->argv);
Packit Service 1d0348
		_exit(254);
Packit Service 1d0348
	}
Packit Service 1d0348
#endif /* HAVE_POSIX_SPAWNP */
Packit Service 1d0348
Packit Service 1d0348
	close(stdin_pipe[0]);
Packit Service 1d0348
	close(stdout_pipe[1]);
Packit Service 1d0348
Packit Service 1d0348
	*child_stdin = stdin_pipe[1];
Packit Service 1d0348
	fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
Packit Service 1d0348
	*child_stdout = stdout_pipe[0];
Packit Service 1d0348
	fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
Packit Service 1d0348
	__archive_cmdline_free(cmdline);
Packit Service 1d0348
Packit Service 1d0348
	return child;
Packit Service 1d0348
Packit Service 1d0348
#if HAVE_POSIX_SPAWNP
Packit Service 1d0348
actions_inited:
Packit Service 1d0348
	errno = r;
Packit Service 1d0348
	posix_spawn_file_actions_destroy(&actions);
Packit Service 1d0348
#endif
Packit Service 1d0348
stdout_opened:
Packit Service 1d0348
	close(stdout_pipe[0]);
Packit Service 1d0348
	close(stdout_pipe[1]);
Packit Service 1d0348
stdin_opened:
Packit Service 1d0348
	close(stdin_pipe[0]);
Packit Service 1d0348
	close(stdin_pipe[1]);
Packit Service 1d0348
state_allocated:
Packit Service 1d0348
	__archive_cmdline_free(cmdline);
Packit Service 1d0348
	return -1;
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
void
Packit Service 1d0348
__archive_check_child(int in, int out)
Packit Service 1d0348
{
Packit Service 1d0348
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
Packit Service 1d0348
	struct pollfd fds[2];
Packit Service 1d0348
	int idx;
Packit Service 1d0348
Packit Service 1d0348
	idx = 0;
Packit Service 1d0348
	if (in != -1) {
Packit Service 1d0348
		fds[idx].fd = in;
Packit Service 1d0348
		fds[idx].events = POLLOUT;
Packit Service 1d0348
		++idx;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (out != -1) {
Packit Service 1d0348
		fds[idx].fd = out;
Packit Service 1d0348
		fds[idx].events = POLLIN;
Packit Service 1d0348
		++idx;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	poll(fds, idx, -1); /* -1 == INFTIM, wait forever */
Packit Service 1d0348
#elif defined(HAVE_SELECT)
Packit Service 1d0348
	fd_set fds_in, fds_out, fds_error;
Packit Service 1d0348
Packit Service 1d0348
	FD_ZERO(&fds_in);
Packit Service 1d0348
	FD_ZERO(&fds_out);
Packit Service 1d0348
	FD_ZERO(&fds_error);
Packit Service 1d0348
	if (out != -1) {
Packit Service 1d0348
		FD_SET(out, &fds_in);
Packit Service 1d0348
		FD_SET(out, &fds_error);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (in != -1) {
Packit Service 1d0348
		FD_SET(in, &fds_out);
Packit Service 1d0348
		FD_SET(in, &fds_error);
Packit Service 1d0348
	}
Packit Service 1d0348
	select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
Packit Service 1d0348
#else
Packit Service 1d0348
	sleep(1);
Packit Service 1d0348
#endif
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
#endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */