2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
3
Copyright (C) 2005- The University of Notre Dame
4
This software is distributed under the GNU General Public License.
5
See the file COPYING for details.
8
#include "timed_exec.h"
9
#include "ftsh_error.h"
10
#include "stringtools.h"
12
#include "multi_fork.h"
25
timed_exec_t timed_exec( int line, const char *path, char **argv, int fds[3], pid_t *pid, int *status, time_t stoptime )
31
struct multi_fork_status s;
34
if(actual!=0) return TIMED_EXEC_NOEXEC;
36
fresult = multi_fork(1,&s,stoptime,line);
38
/* Move our standard I/O streams into the expected places. */
39
/* It seems that cygwin doesn't like dup2 on the same fd. */
43
for( i=0; i<=2; i++ ) {
45
if( dup2(fds[i],i) != i ) {
46
ftsh_error(FTSH_ERROR_PROCESS,line,"failure to dup2(%d,%d): %s\n",fds[i],i,strerror(errno));
52
/* Close all of the file descriptors that we don't need. */
54
maxfd = sysconf( _SC_OPEN_MAX );
55
if(maxfd<=0) maxfd = 255;
56
for(i=3;i<maxfd;i++) {
57
if(i==pfds[1]) continue;
61
/* Set the pipe to automatically close after exec. */
63
if( fcntl(pfds[1],F_SETFL,FD_CLOEXEC)==0 ) {
69
If anything goes wrong, write the errno to the pipe,
70
where the parent process can collect and print it.
75
full_write(pfds[1],&child_errno,sizeof(child_errno));
81
Now clear the pipe. If it contains an int, then the process
82
forked, but was unable to exec. Set the reason appropriately.
83
Otherwise, live with what we have.
87
actual = full_read(pfds[0],&child_errno,sizeof(int));
93
if(actual==sizeof(int)) {
94
return TIMED_EXEC_NOEXEC;
95
} else if(fresult==MULTI_FORK_SUCCESS) {
96
return TIMED_EXEC_SUCCESS;
97
} else if(fresult==MULTI_FORK_TIMEOUT) {
98
return TIMED_EXEC_TIMEOUT;
100
return TIMED_EXEC_FAILURE;