Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / libarchive-2.1 / libarchive / filter_fork.c
bloba7ee48b4630944aa00426ef685ce78f73cc3de15
1 /*-
2 * Copyright (c) 2007 Joerg Sonnenberger
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #if defined(HAVE_POLL)
31 # if defined(HAVE_POLL_H)
32 # include <poll.h>
33 # endif
34 #elif defined(HAVE_SELECT)
35 # if defined(HAVE_SYS_SELECT_H)
36 # include <sys/select.h>
37 # elif defined(HAVE_UNISTD_H)
38 # include <unistd.h>
39 # endif
40 #endif
41 #ifdef HAVE_FCNTL_H
42 # include <fcntl.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
48 #include "filter_fork.h"
50 pid_t
51 __archive_create_child(const char *path, int *child_stdin, int *child_stdout)
53 pid_t child;
54 int stdin_pipe[2], stdout_pipe[2], tmp;
56 if (pipe(stdin_pipe) == -1)
57 goto state_allocated;
58 if (stdin_pipe[0] == STDOUT_FILENO) {
59 if ((tmp = dup(stdin_pipe[0])) == -1)
60 goto stdin_opened;
61 close(stdin_pipe[0]);
62 stdin_pipe[0] = tmp;
64 if (pipe(stdout_pipe) == -1)
65 goto stdin_opened;
66 if (stdout_pipe[1] == STDIN_FILENO) {
67 if ((tmp = dup(stdout_pipe[1])) == -1)
68 goto stdout_opened;
69 close(stdout_pipe[1]);
70 stdout_pipe[1] = tmp;
73 switch ((child = vfork())) {
74 case -1:
75 goto stdout_opened;
76 case 0:
77 close(stdin_pipe[1]);
78 close(stdout_pipe[0]);
79 if (dup2(stdin_pipe[0], STDIN_FILENO) == -1)
80 _exit(254);
81 if (stdin_pipe[0] != STDIN_FILENO)
82 close(stdin_pipe[0]);
83 if (dup2(stdout_pipe[1], STDOUT_FILENO) == -1)
84 _exit(254);
85 if (stdout_pipe[1] != STDOUT_FILENO)
86 close(stdout_pipe[1]);
87 execlp(path, path, (char *)NULL);
88 _exit(254);
89 default:
90 close(stdin_pipe[0]);
91 close(stdout_pipe[1]);
93 *child_stdin = stdin_pipe[1];
94 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
95 *child_stdout = stdout_pipe[0];
96 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
99 return child;
101 stdout_opened:
102 close(stdout_pipe[0]);
103 close(stdout_pipe[1]);
104 stdin_opened:
105 close(stdin_pipe[0]);
106 close(stdin_pipe[1]);
107 state_allocated:
108 return -1;
111 void
112 __archive_check_child(int in, int out)
114 #if defined(HAVE_POLL)
115 struct pollfd fds[2];
117 fds[0].fd = in;
118 fds[0].events = POLLOUT;
119 fds[1].fd = out;
120 fds[1].events = POLLIN;
122 poll(fds, 2, -1); /* -1 == INFTIM, wait forever */
123 #elif defined(HAVE_SELECT)
124 fd_set fds_in, fds_out, fds_error;
126 FD_ZERO(&fds_in);
127 FD_SET(out, &fds_in);
128 FD_ZERO(&fds_out);
129 FD_SET(in, &fds_out);
130 FD_ZERO(&fds_error);
131 FD_SET(in, &fds_error);
132 FD_SET(out, &fds_error);
133 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
134 #else
135 sleep(1);
136 #endif