Updated the list of USB device IDs for uvisor(4) and needed routines.
[dragonfly.git] / contrib / libarchive-2 / libarchive / filter_fork.c
blobc71cf6883ffb0e9ab65c49b12f5398d30f39635c
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: src/lib/libarchive/filter_fork.c,v 1.1 2007/05/29 01:00:20 kientzle Exp $");
30 #if defined(HAVE_POLL)
31 # if defined(HAVE_POLL_H)
32 # include <poll.h>
33 # elif defined(HAVE_SYS_POLL_H)
34 # include <sys/poll.h>
35 # endif
36 #elif defined(HAVE_SELECT)
37 # if defined(HAVE_SYS_SELECT_H)
38 # include <sys/select.h>
39 # elif defined(HAVE_UNISTD_H)
40 # include <unistd.h>
41 # endif
42 #endif
43 #ifdef HAVE_FCNTL_H
44 # include <fcntl.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
50 #include "filter_fork.h"
52 pid_t
53 __archive_create_child(const char *path, int *child_stdin, int *child_stdout)
55 pid_t child;
56 int stdin_pipe[2], stdout_pipe[2], tmp;
58 if (pipe(stdin_pipe) == -1)
59 goto state_allocated;
60 if (stdin_pipe[0] == STDOUT_FILENO) {
61 if ((tmp = dup(stdin_pipe[0])) == -1)
62 goto stdin_opened;
63 close(stdin_pipe[0]);
64 stdin_pipe[0] = tmp;
66 if (pipe(stdout_pipe) == -1)
67 goto stdin_opened;
68 if (stdout_pipe[1] == STDIN_FILENO) {
69 if ((tmp = dup(stdout_pipe[1])) == -1)
70 goto stdout_opened;
71 close(stdout_pipe[1]);
72 stdout_pipe[1] = tmp;
75 switch ((child = vfork())) {
76 case -1:
77 goto stdout_opened;
78 case 0:
79 close(stdin_pipe[1]);
80 close(stdout_pipe[0]);
81 if (dup2(stdin_pipe[0], STDIN_FILENO) == -1)
82 _exit(254);
83 if (stdin_pipe[0] != STDIN_FILENO)
84 close(stdin_pipe[0]);
85 if (dup2(stdout_pipe[1], STDOUT_FILENO) == -1)
86 _exit(254);
87 if (stdout_pipe[1] != STDOUT_FILENO)
88 close(stdout_pipe[1]);
89 execlp(path, path, (char *)NULL);
90 _exit(254);
91 default:
92 close(stdin_pipe[0]);
93 close(stdout_pipe[1]);
95 *child_stdin = stdin_pipe[1];
96 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
97 *child_stdout = stdout_pipe[0];
98 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
101 return child;
103 stdout_opened:
104 close(stdout_pipe[0]);
105 close(stdout_pipe[1]);
106 stdin_opened:
107 close(stdin_pipe[0]);
108 close(stdin_pipe[1]);
109 state_allocated:
110 return -1;
113 void
114 __archive_check_child(int in, int out)
116 #if defined(HAVE_POLL)
117 struct pollfd fds[2];
119 fds[0].fd = in;
120 fds[0].events = POLLOUT;
121 fds[1].fd = out;
122 fds[1].events = POLLIN;
124 poll(fds, 2, -1); /* -1 == INFTIM, wait forever */
125 #elif defined(HAVE_SELECT)
126 fd_set fds_in, fds_out, fds_error;
128 FD_ZERO(&fds_in);
129 FD_SET(out, &fds_in);
130 FD_ZERO(&fds_out);
131 FD_SET(in, &fds_out);
132 FD_ZERO(&fds_error);
133 FD_SET(in, &fds_error);
134 FD_SET(out, &fds_error);
135 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
136 #else
137 sleep(1);
138 #endif