]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/filter_fork.c
Portability: We can get away with the older and better-supported
[FreeBSD/FreeBSD.git] / lib / libarchive / filter_fork.c
1 /*-
2  * Copyright (c) 2007 Joerg Sonnenberger
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27
28 /* This capability is only available on POSIX systems. */
29 #if defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL)
30
31 __FBSDID("$FreeBSD$");
32
33 #if defined(HAVE_POLL)
34 #  if defined(HAVE_POLL_H)
35 #    include <poll.h>
36 #  elif defined(HAVE_SYS_POLL_H)
37 #    include <sys/poll.h>
38 #  endif
39 #elif defined(HAVE_SELECT)
40 #  if defined(HAVE_SYS_SELECT_H)
41 #    include <sys/select.h>
42 #  elif defined(HAVE_UNISTD_H)
43 #    include <unistd.h>
44 #  endif
45 #endif
46 #ifdef HAVE_FCNTL_H
47 #  include <fcntl.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #  include <unistd.h>
51 #endif
52
53 #include "filter_fork.h"
54
55 pid_t
56 __archive_create_child(const char *path, int *child_stdin, int *child_stdout)
57 {
58         pid_t child;
59         int stdin_pipe[2], stdout_pipe[2], tmp;
60
61         if (pipe(stdin_pipe) == -1)
62                 goto state_allocated;
63         if (stdin_pipe[0] == STDOUT_FILENO) {
64                 if ((tmp = dup(stdin_pipe[0])) == -1)
65                         goto stdin_opened;
66                 close(stdin_pipe[0]);
67                 stdin_pipe[0] = tmp;
68         }
69         if (pipe(stdout_pipe) == -1)
70                 goto stdin_opened;
71         if (stdout_pipe[1] == STDIN_FILENO) {
72                 if ((tmp = dup(stdout_pipe[1])) == -1)
73                         goto stdout_opened;
74                 close(stdout_pipe[1]);
75                 stdout_pipe[1] = tmp;
76         }
77
78         switch ((child = vfork())) {
79         case -1:
80                 goto stdout_opened;
81         case 0:
82                 close(stdin_pipe[1]);
83                 close(stdout_pipe[0]);
84                 if (dup2(stdin_pipe[0], STDIN_FILENO) == -1)
85                         _exit(254);
86                 if (stdin_pipe[0] != STDIN_FILENO)
87                         close(stdin_pipe[0]);
88                 if (dup2(stdout_pipe[1], STDOUT_FILENO) == -1)
89                         _exit(254);
90                 if (stdout_pipe[1] != STDOUT_FILENO)
91                         close(stdout_pipe[1]);
92                 execlp(path, path, (char *)NULL);
93                 _exit(254);             
94         default:
95                 close(stdin_pipe[0]);
96                 close(stdout_pipe[1]);
97
98                 *child_stdin = stdin_pipe[1];
99                 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
100                 *child_stdout = stdout_pipe[0];
101                 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
102         }
103
104         return child;
105
106 stdout_opened:
107         close(stdout_pipe[0]);
108         close(stdout_pipe[1]);
109 stdin_opened:
110         close(stdin_pipe[0]);
111         close(stdin_pipe[1]);
112 state_allocated:
113         return -1;
114 }
115
116 void
117 __archive_check_child(int in, int out)
118 {
119 #if defined(HAVE_POLL)
120         struct pollfd fds[2];
121
122         fds[0].fd = in;
123         fds[0].events = POLLOUT;
124         fds[1].fd = out;
125         fds[1].events = POLLIN;
126
127         poll(fds, 2, -1); /* -1 == INFTIM, wait forever */
128 #elif defined(HAVE_SELECT)
129         fd_set fds_in, fds_out, fds_error;
130
131         FD_ZERO(&fds_in);
132         FD_SET(out, &fds_in);
133         FD_ZERO(&fds_out);
134         FD_SET(in, &fds_out);
135         FD_ZERO(&fds_error);
136         FD_SET(in, &fds_error);
137         FD_SET(out, &fds_error);
138         select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
139 #else
140         sleep(1);
141 #endif
142 }
143
144 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */