2005-09-20 13:26:39 +00:00
/** \file exec.c
2012-11-18 10:23:22 +00:00
Functions for executing a program .
2005-09-20 13:26:39 +00:00
2012-11-18 10:23:22 +00:00
Some of the code in this file is based on code from the Glibc
2012-12-16 04:23:24 +00:00
manual , though the changes performed have been massive .
2005-09-20 13:26:39 +00:00
*/
2006-08-11 01:18:35 +00:00
# include "config.h"
2005-09-20 13:26:39 +00:00
# include <stdlib.h>
# include <stdio.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <termios.h>
# include <unistd.h>
# include <fcntl.h>
# include <errno.h>
# include <wchar.h>
# include <string.h>
# include <limits.h>
# include <signal.h>
# include <sys/wait.h>
# include <assert.h>
# include <dirent.h>
2006-11-15 13:30:46 +00:00
# include <time.h>
2011-12-27 03:18:46 +00:00
# include <vector>
2012-02-10 15:55:06 +00:00
# include <algorithm>
2012-02-22 03:33:11 +00:00
# include <memory>
2005-09-20 13:26:39 +00:00
2006-07-30 20:26:59 +00:00
# ifdef HAVE_SIGINFO_H
# include <siginfo.h>
# endif
2006-02-28 13:17:16 +00:00
# include "fallback.h"
2005-09-20 13:26:39 +00:00
# include "util.h"
2012-02-28 03:46:15 +00:00
# include "iothread.h"
2012-02-28 23:11:46 +00:00
# include "postfork.h"
2006-02-28 13:17:16 +00:00
2005-09-20 13:26:39 +00:00
# include "common.h"
# include "wutil.h"
# include "proc.h"
# include "exec.h"
# include "parser.h"
# include "builtin.h"
# include "function.h"
# include "env.h"
# include "wildcard.h"
# include "sanity.h"
# include "expand.h"
2005-10-08 11:20:51 +00:00
# include "signal.h"
2006-07-19 22:55:49 +00:00
2006-02-14 19:56:36 +00:00
# include "parse_util.h"
2005-09-20 13:26:39 +00:00
/**
file descriptor redirection error message
*/
2006-01-04 12:51:02 +00:00
# define FD_ERROR _( L"An error occurred while redirecting file descriptor %d" )
2007-10-26 18:42:32 +00:00
2009-02-22 20:28:52 +00:00
/**
file descriptor redirection error message
*/
# define WRITE_ERROR _( L"An error occurred while writing output" )
2005-09-20 13:26:39 +00:00
/**
file redirection error message
*/
2012-03-09 07:21:07 +00:00
# define FILE_ERROR _( L"An error occurred while redirecting file '%s'" )
2007-10-26 18:42:32 +00:00
2006-10-26 15:19:46 +00:00
/**
Base open mode to pass to calls to open
*/
2006-11-15 13:30:46 +00:00
# define OPEN_MASK 0666
2005-10-17 13:24:12 +00:00
2005-10-03 13:09:37 +00:00
/**
List of all pipes used by internal pipes . These must be closed in
many situations in order to make sure that stray fds aren ' t lying
around .
2012-11-18 10:23:22 +00:00
2012-02-28 23:11:46 +00:00
Note this is used after fork , so we must not do anything that may allocate memory . Hopefully methods like open_fds . at ( ) don ' t .
2005-10-03 13:09:37 +00:00
*/
2012-02-28 23:11:46 +00:00
static std : : vector < bool > open_fds ;
2006-02-03 21:50:31 +00:00
2012-02-28 23:11:46 +00:00
// Called in a forked child
2012-11-19 00:30:30 +00:00
static void exec_write_and_exit ( int fd , const char * buff , size_t count , int status )
2009-02-22 20:28:52 +00:00
{
2012-11-19 00:30:30 +00:00
if ( write_loop ( fd , buff , count ) = = - 1 )
{
debug ( 0 , WRITE_ERROR ) ;
wperror ( L " write " ) ;
exit_without_destructors ( status ) ;
}
2012-11-18 10:23:22 +00:00
exit_without_destructors ( status ) ;
2009-02-22 20:28:52 +00:00
}
2012-11-19 00:30:30 +00:00
void exec_close ( int fd )
2005-09-23 13:10:31 +00:00
{
2012-08-04 22:11:43 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2012-11-18 10:23:22 +00:00
2012-02-28 23:11:46 +00:00
/* This may be called in a child of fork(), so don't allocate memory */
2012-11-19 00:30:30 +00:00
if ( fd < 0 )
{
debug ( 0 , L " Called close on invalid file descriptor " ) ;
return ;
}
while ( close ( fd ) = = - 1 )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
if ( errno ! = EINTR )
{
debug ( 1 , FD_ERROR , fd ) ;
wperror ( L " close " ) ;
break ;
}
2012-11-18 10:23:22 +00:00
}
2012-02-28 23:11:46 +00:00
/* Maybe remove this from our set of open fds */
2012-11-19 00:30:30 +00:00
if ( ( size_t ) fd < open_fds . size ( ) )
{
2012-02-28 23:11:46 +00:00
open_fds [ fd ] = false ;
}
2005-10-03 13:09:37 +00:00
}
2012-11-19 00:30:30 +00:00
int exec_pipe ( int fd [ 2 ] )
2005-10-03 13:09:37 +00:00
{
2013-01-30 10:22:38 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2013-01-31 23:57:08 +00:00
2012-11-19 00:30:30 +00:00
int res ;
while ( ( res = pipe ( fd ) ) )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
if ( errno ! = EINTR )
{
2013-01-30 10:22:38 +00:00
// caller will call wperror
2012-11-19 00:30:30 +00:00
return res ;
}
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
debug ( 4 , L " Created pipe using fds %d and %d " , fd [ 0 ] , fd [ 1 ] ) ;
2012-11-18 10:23:22 +00:00
2012-02-28 23:11:46 +00:00
int max_fd = std : : max ( fd [ 0 ] , fd [ 1 ] ) ;
2012-11-19 00:30:30 +00:00
if ( max_fd > = 0 & & open_fds . size ( ) < = ( size_t ) max_fd )
{
2012-02-28 23:11:46 +00:00
open_fds . resize ( max_fd + 1 , false ) ;
}
open_fds . at ( fd [ 0 ] ) = true ;
open_fds . at ( fd [ 1 ] ) = true ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
return res ;
2005-10-03 13:09:37 +00:00
}
2013-06-17 06:26:43 +00:00
/* Returns true if the redirection is a file redirection to a file other than /dev/null */
static bool redirection_is_to_real_file ( const io_data_t * io )
{
bool result = false ;
if ( io ! = NULL & & io - > io_mode = = IO_FILE )
{
/* It's a file redirection. Compare the path to /dev/null */
CAST_INIT ( const io_file_t * , io_file , io ) ;
const char * path = io_file - > filename_cstr ;
if ( strcmp ( path , " /dev/null " ) ! = 0 )
{
/* It's not /dev/null */
result = true ;
}
}
return result ;
}
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
static bool chain_contains_redirection_to_real_file ( const io_chain_t & io_chain )
{
bool result = false ;
for ( size_t idx = 0 ; idx < io_chain . size ( ) ; idx + + )
{
const shared_ptr < const io_data_t > & io = io_chain . at ( idx ) ;
if ( redirection_is_to_real_file ( io . get ( ) ) )
{
result = true ;
break ;
}
}
return result ;
}
2013-01-30 10:22:38 +00:00
void print_open_fds ( void )
{
for ( size_t i = 0 ; i < open_fds . size ( ) ; + + i )
{
if ( open_fds . at ( i ) )
{
fprintf ( stderr , " fd %lu \n " , i ) ;
}
}
}
2005-10-03 13:09:37 +00:00
/**
Check if the specified fd is used as a part of a pipeline in the
specidied set of IO redirections .
2012-08-15 07:57:56 +00:00
This is called after fork ( ) .
2005-10-03 13:09:37 +00:00
\ param fd the fd to search for
2012-08-15 07:57:56 +00:00
\ param io_chain the set of io redirections to search in
2005-10-03 13:09:37 +00:00
*/
2012-11-19 00:30:30 +00:00
static bool use_fd_in_pipe ( int fd , const io_chain_t & io_chain )
2012-08-15 07:57:56 +00:00
{
for ( size_t idx = 0 ; idx < io_chain . size ( ) ; idx + + )
{
2013-01-07 15:04:55 +00:00
const shared_ptr < const io_data_t > & io = io_chain . at ( idx ) ;
2012-11-19 00:30:30 +00:00
if ( ( io - > io_mode = = IO_BUFFER ) | |
( io - > io_mode = = IO_PIPE ) )
2012-08-15 07:57:56 +00:00
{
2013-01-15 09:31:36 +00:00
CAST_INIT ( const io_pipe_t * , io_pipe , io . get ( ) ) ;
if ( io_pipe - > pipe_fd [ 0 ] = = fd | | io_pipe - > pipe_fd [ 1 ] = = fd )
2012-08-15 07:57:56 +00:00
return true ;
}
}
return false ;
2005-10-03 13:09:37 +00:00
}
/**
Close all fds in open_fds , except for those that are mentioned in
2005-10-21 11:59:45 +00:00
the redirection list io . This should make sure that there are no
stray opened file descriptors in the child .
2012-11-18 10:23:22 +00:00
2006-05-18 13:00:39 +00:00
\ param io the list of io redirections for this job . Pipes mentioned
here should not be closed .
2005-10-03 13:09:37 +00:00
*/
2012-11-19 00:30:30 +00:00
void close_unused_internal_pipes ( const io_chain_t & io )
2005-10-03 13:09:37 +00:00
{
2012-02-09 06:56:42 +00:00
/* A call to exec_close will modify open_fds, so be careful how we walk */
2012-11-19 00:30:30 +00:00
for ( size_t i = 0 ; i < open_fds . size ( ) ; i + + )
{
if ( open_fds [ i ] )
{
2012-02-28 23:11:46 +00:00
int fd = ( int ) i ;
2012-11-19 00:30:30 +00:00
if ( ! use_fd_in_pipe ( fd , io ) )
2012-02-28 23:11:46 +00:00
{
2012-11-19 00:30:30 +00:00
debug ( 4 , L " Close fd %d, used in other context " , fd ) ;
exec_close ( fd ) ;
2012-02-28 23:11:46 +00:00
i - - ;
}
2012-02-09 06:56:42 +00:00
}
}
2005-10-03 13:09:37 +00:00
}
2012-08-15 07:57:56 +00:00
void get_unused_internal_pipes ( std : : vector < int > & fds , const io_chain_t & io )
{
2012-11-19 00:30:30 +00:00
for ( size_t i = 0 ; i < open_fds . size ( ) ; i + + )
{
if ( open_fds [ i ] )
{
2012-08-15 07:57:56 +00:00
int fd = ( int ) i ;
2012-11-19 00:30:30 +00:00
if ( ! use_fd_in_pipe ( fd , io ) )
2012-08-15 07:57:56 +00:00
{
fds . push_back ( fd ) ;
}
}
}
}
2008-01-16 01:04:54 +00:00
/**
2012-02-29 19:27:14 +00:00
Returns the interpreter for the specified script . Returns NULL if file
2012-02-22 20:00:02 +00:00
is not a script with a shebang .
2008-01-16 01:04:54 +00:00
*/
2012-11-19 00:30:30 +00:00
char * get_interpreter ( const char * command , char * interpreter , size_t buff_size )
2008-01-16 01:04:54 +00:00
{
2012-03-02 08:27:40 +00:00
// OK to not use CLO_EXEC here because this is only called after fork
2012-11-19 00:30:30 +00:00
int fd = open ( command , O_RDONLY ) ;
if ( fd > = 0 )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
size_t idx = 0 ;
while ( idx + 1 < buff_size )
{
2012-02-29 19:27:14 +00:00
char ch ;
ssize_t amt = read ( fd , & ch , sizeof ch ) ;
2012-11-19 00:30:30 +00:00
if ( amt < = 0 )
break ;
if ( ch = = ' \n ' )
break ;
2012-02-29 19:27:14 +00:00
interpreter [ idx + + ] = ch ;
2012-11-19 00:30:30 +00:00
}
2012-02-29 19:27:14 +00:00
interpreter [ idx + + ] = ' \0 ' ;
close ( fd ) ;
2012-11-19 00:30:30 +00:00
}
if ( strncmp ( interpreter , " #! / " , 4 ) = = 0 )
{
2012-02-29 19:27:14 +00:00
return interpreter + 3 ;
2012-11-19 00:30:30 +00:00
}
else if ( strncmp ( interpreter , " #!/ " , 3 ) = = 0 )
{
2012-02-29 19:27:14 +00:00
return interpreter + 2 ;
2012-11-19 00:30:30 +00:00
}
else
{
2012-02-29 19:27:14 +00:00
return NULL ;
2012-02-22 20:00:02 +00:00
}
2008-01-16 01:04:54 +00:00
}
2005-09-20 13:26:39 +00:00
/**
This function is executed by the child process created by a call to
2005-10-09 11:48:16 +00:00
fork ( ) . It should be called after \ c setup_child_process . It calls
execve to replace the fish process image with the command specified
in \ c p . It never returns .
2005-09-20 13:26:39 +00:00
*/
2012-02-29 19:27:14 +00:00
/* Called in a forked child! Do not allocate memory, etc. */
2013-02-23 00:22:56 +00:00
static void safe_launch_process ( process_t * p , const char * actual_cmd , const char * const * cargv , const char * const * cenvv )
2005-09-20 13:26:39 +00:00
{
2012-11-19 00:30:30 +00:00
int err ;
2012-11-18 10:23:22 +00:00
// debug( 1, L"exec '%ls'", p->argv[0] );
2006-12-08 22:04:28 +00:00
2013-02-23 00:22:56 +00:00
// This function never returns, so we take certain liberties with constness
char * const * envv = const_cast < char * const * > ( cenvv ) ;
char * const * argv = const_cast < char * const * > ( cargv ) ;
2012-11-19 00:30:30 +00:00
execve ( actual_cmd , argv , envv ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
err = errno ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
Something went wrong with execve , check for a " : " , and run
/ bin / sh if encountered . This is a weird predecessor to the shebang
that is still sometimes used since it is supported on Windows .
*/
2012-03-02 08:27:40 +00:00
/* OK to not use CLO_EXEC here because this is called after fork and the file is immediately closed */
2012-11-19 00:30:30 +00:00
int fd = open ( actual_cmd , O_RDONLY ) ;
if ( fd > = 0 )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
char begin [ 1 ] = { 0 } ;
ssize_t amt_read = read ( fd , begin , 1 ) ;
close ( fd ) ;
if ( ( amt_read = = 1 ) & & ( begin [ 0 ] = = ' : ' ) )
{
2012-02-29 19:27:14 +00:00
// Relaunch it with /bin/sh. Don't allocate memory, so if you have more args than this, update your silly script! Maybe this should be changed to be based on ARG_MAX somehow.
char sh_command [ ] = " /bin/sh " ;
char * argv2 [ 128 ] ;
argv2 [ 0 ] = sh_command ;
2012-11-19 00:30:30 +00:00
for ( size_t i = 1 ; i < sizeof argv2 / sizeof * argv2 ; i + + )
{
2012-02-29 19:27:14 +00:00
argv2 [ i ] = argv [ i - 1 ] ;
if ( argv2 [ i ] = = NULL )
break ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
execve ( sh_command , argv2 , envv ) ;
}
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
errno = err ;
2012-08-15 07:57:56 +00:00
safe_report_exec_error ( errno , actual_cmd , argv , envv ) ;
2012-11-19 00:30:30 +00:00
exit_without_destructors ( STATUS_EXEC_FAIL ) ;
2012-02-29 19:27:14 +00:00
}
/**
2012-05-09 09:55:36 +00:00
This function is similar to launch_process , except it is not called after a fork ( i . e . it only calls exec ) and therefore it can allocate memory .
2012-02-29 19:27:14 +00:00
*/
2012-11-19 00:30:30 +00:00
static void launch_process_nofork ( process_t * p )
2012-02-29 19:27:14 +00:00
{
ASSERT_IS_MAIN_THREAD ( ) ;
ASSERT_IS_NOT_FORKED_CHILD ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
char * * argv = wcsv2strv ( p - > get_argv ( ) ) ;
2013-02-23 00:22:56 +00:00
const char * const * envv = env_export_arr ( false ) ;
2012-07-21 03:39:31 +00:00
char * actual_cmd = wcs2str ( p - > actual_cmd . c_str ( ) ) ;
2012-11-18 10:23:22 +00:00
2012-02-29 19:27:14 +00:00
/* Bounce to launch_process. This never returns. */
safe_launch_process ( p , actual_cmd , argv , envv ) ;
2005-09-20 13:26:39 +00:00
}
/**
2005-10-03 13:09:37 +00:00
Check if the IO redirection chains contains redirections for the
specified file descriptor
2005-09-20 13:26:39 +00:00
*/
2012-11-19 00:30:30 +00:00
static int has_fd ( const io_chain_t & d , int fd )
2005-09-20 13:26:39 +00:00
{
2012-11-19 00:30:30 +00:00
return io_chain_get ( d , fd ) ! = NULL ;
2005-09-20 13:26:39 +00:00
}
2012-08-22 20:41:21 +00:00
/**
2013-01-19 18:59:43 +00:00
Close a list of fds .
2012-08-22 20:41:21 +00:00
*/
2013-01-19 18:59:43 +00:00
static void io_cleanup_fds ( const std : : vector < int > & opened_fds )
2012-11-19 00:30:30 +00:00
{
2013-01-19 18:59:43 +00:00
std : : for_each ( opened_fds . begin ( ) , opened_fds . end ( ) , close ) ;
2012-08-22 20:41:21 +00:00
}
/**
Make a copy of the specified io redirection chain , but change file
redirection into fd redirection . This makes the redirection chain
suitable for use as block - level io , since the file won ' t be
repeatedly reopened for every command in the block , which would
reset the cursor position .
\ return the transmogrified chain on sucess , or 0 on failiure
*/
2012-11-19 00:30:30 +00:00
static bool io_transmogrify ( const io_chain_t & in_chain , io_chain_t & out_chain , std : : vector < int > & out_opened_fds )
{
2012-08-22 20:41:21 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
assert ( out_chain . empty ( ) ) ;
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
/* Just to be clear what we do for an empty chain */
2012-11-19 00:30:30 +00:00
if ( in_chain . empty ( ) )
{
2012-08-22 20:41:21 +00:00
return true ;
}
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
bool success = true ;
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
/* Make our chain of redirections */
io_chain_t result_chain ;
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
/* In the event we can't finish transmorgrifying, we'll have to close all the files we opened. */
std : : vector < int > opened_fds ;
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
for ( size_t idx = 0 ; idx < in_chain . size ( ) ; idx + + )
{
2013-01-07 15:04:55 +00:00
const shared_ptr < io_data_t > & in = in_chain . at ( idx ) ;
shared_ptr < io_data_t > out ; //gets allocated via new
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
switch ( in - > io_mode )
2012-08-22 20:41:21 +00:00
{
2012-11-19 08:31:03 +00:00
default :
/* Unknown type, should never happen */
fprintf ( stderr , " Unknown io_mode %ld \n " , ( long ) in - > io_mode ) ;
abort ( ) ;
break ;
/*
These redirections don ' t need transmogrification . They can be passed through .
*/
case IO_PIPE :
case IO_FD :
case IO_BUFFER :
case IO_CLOSE :
{
2013-01-07 15:04:55 +00:00
out = in ;
2012-11-19 08:31:03 +00:00
break ;
}
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
/*
2012-11-19 08:31:03 +00:00
Transmogrify file redirections
2012-08-22 20:41:21 +00:00
*/
2012-11-19 08:31:03 +00:00
case IO_FILE :
{
int fd ;
2013-01-15 08:18:03 +00:00
CAST_INIT ( io_file_t * , in_file , in . get ( ) ) ;
if ( ( fd = open ( in_file - > filename_cstr , in_file - > flags , OPEN_MASK ) ) = = - 1 )
2012-11-19 08:31:03 +00:00
{
debug ( 1 ,
FILE_ERROR ,
2013-01-15 08:18:03 +00:00
in_file - > filename_cstr ) ;
2012-08-22 20:41:21 +00:00
2012-11-19 08:31:03 +00:00
wperror ( L " open " ) ;
success = false ;
break ;
}
opened_fds . push_back ( fd ) ;
2013-01-15 07:37:33 +00:00
out . reset ( new io_fd_t ( in - > fd , fd , true ) ) ;
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
break ;
}
}
2012-11-18 10:23:22 +00:00
2013-02-20 20:25:01 +00:00
if ( out . get ( ) ! = NULL )
result_chain . push_back ( out ) ;
2012-11-18 10:23:22 +00:00
2013-02-20 20:25:01 +00:00
/* Don't go any further if we failed */
2012-11-19 00:30:30 +00:00
if ( ! success )
{
2012-08-22 20:41:21 +00:00
break ;
}
}
2012-11-18 10:23:22 +00:00
2012-08-22 20:41:21 +00:00
/* Now either return success, or clean up */
2012-11-19 00:30:30 +00:00
if ( success )
{
2012-08-22 20:41:21 +00:00
/* Yay */
out_chain . swap ( result_chain ) ;
out_opened_fds . swap ( opened_fds ) ;
2012-11-19 00:30:30 +00:00
}
else
{
2012-08-22 20:41:21 +00:00
/* No dice - clean up */
2013-01-19 18:59:43 +00:00
result_chain . clear ( ) ;
io_cleanup_fds ( opened_fds ) ;
2012-08-22 20:41:21 +00:00
}
return success ;
}
2005-09-20 13:26:39 +00:00
/**
Morph an io redirection chain into redirections suitable for
passing to eval , call eval , and clean up morphed redirections .
\ param def the code to evaluate
\ param block_type the type of block to push on evaluation
\ param io the io redirections to be performed on this block
*/
2012-11-19 00:30:30 +00:00
static void internal_exec_helper ( parser_t & parser ,
const wchar_t * def ,
enum block_type_t block_type ,
2013-08-18 23:55:01 +00:00
const io_chain_t & ios )
2005-09-20 13:26:39 +00:00
{
2012-08-22 20:41:21 +00:00
io_chain_t morphed_chain ;
std : : vector < int > opened_fds ;
bool transmorgrified = io_transmogrify ( ios , morphed_chain , opened_fds ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
int is_block_old = is_block ;
is_block = 1 ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
Did the transmogrification fail - if so , set error status and return
*/
if ( ! transmorgrified )
{
proc_set_last_status ( STATUS_EXEC_FAIL ) ;
return ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
signal_unblock ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
parser . eval ( def , morphed_chain , block_type ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
signal_block ( ) ;
2012-11-18 10:23:22 +00:00
2013-01-19 18:59:43 +00:00
morphed_chain . clear ( ) ;
io_cleanup_fds ( opened_fds ) ;
2012-11-19 00:30:30 +00:00
job_reap ( 0 ) ;
is_block = is_block_old ;
2005-09-20 13:26:39 +00:00
}
2012-10-29 08:45:51 +00:00
/* Returns whether we can use posix spawn for a given process in a given job.
2013-04-07 19:40:08 +00:00
Per https : //github.com/fish-shell/fish-shell/issues/364 , error handling for file redirections is too difficult with posix_spawn,
so in that case we use fork / exec .
2012-11-18 10:23:22 +00:00
2013-04-07 19:40:08 +00:00
Furthermore , to avoid the race between the caller calling tcsetpgrp ( ) and the client checking the foreground process group , we don ' t use posix_spawn if we ' re going to foreground the process . ( If we use fork ( ) , we can call tcsetpgrp after the fork , before the exec , and avoid the race ) .
2012-10-29 08:45:51 +00:00
*/
static bool can_use_posix_spawn_for_job ( const job_t * job , const process_t * process )
{
2012-11-19 00:30:30 +00:00
if ( job_get_flag ( job , JOB_CONTROL ) )
2012-11-04 23:45:52 +00:00
{
/* We are going to use job control; therefore when we launch this job it will get its own process group ID. But will it be foregrounded? */
2012-11-19 00:30:30 +00:00
if ( job_get_flag ( job , JOB_TERMINAL ) & & job_get_flag ( job , JOB_FOREGROUND ) )
2012-11-04 23:45:52 +00:00
{
/* It will be foregrounded, so we will call tcsetpgrp(), therefore do not use posix_spawn */
return false ;
}
}
2013-06-17 06:26:43 +00:00
/* Now see if we have a redirection involving a file. The only one we allow is /dev/null, which we assume will not fail. */
2012-10-29 08:45:51 +00:00
bool result = true ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
if ( chain_contains_redirection_to_real_file ( job - > block_io_chain ( ) ) | | chain_contains_redirection_to_real_file ( process - > io_chain ( ) ) )
2012-10-29 08:45:51 +00:00
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
result = false ;
2012-10-29 08:45:51 +00:00
}
return result ;
}
2013-03-25 23:06:12 +00:00
/* What exec does if no_exec is set. This only has to handle block pushing and popping. See #624. */
static void exec_no_exec ( parser_t & parser , const job_t * job )
{
/* Hack hack hack. If this is an 'end' job, then trigger a pop. If this is a job that would create a block, trigger a push. See #624 */
const process_t * p = job - > first_process ;
if ( p & & p - > type = = INTERNAL_BUILTIN )
{
const wchar_t * builtin_name_cstr = p - > argv0 ( ) ;
if ( builtin_name_cstr ! = NULL )
{
const wcstring builtin_name = builtin_name_cstr ;
if ( contains ( builtin_name , L " for " , L " function " , L " begin " , L " switch " ) )
{
// The above builtins are the ones that produce an unbalanced block from within their function implementation
// This list should be maintained somewhere else
parser . push_block ( new fake_block_t ( ) ) ;
}
else if ( builtin_name = = L " end " )
{
if ( parser . current_block = = NULL | | parser . current_block - > type ( ) = = TOP )
{
fprintf ( stderr , " Warning: not popping the root block \n " ) ;
}
else
{
parser . pop_block ( ) ;
}
}
}
}
}
2007-01-21 14:58:10 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
void exec_job ( parser_t & parser , job_t * j )
2005-09-20 13:26:39 +00:00
{
2012-11-19 00:30:30 +00:00
pid_t pid = 0 ;
sigset_t chldset ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
Set to true if something goes wrong while exec : ing the job , in
which case the cleanup code will kick in .
*/
bool exec_error = false ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
bool needs_keepalive = false ;
process_t keepalive ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
CHECK ( j , ) ;
CHECK_BLOCK ( ) ;
2012-11-18 10:23:22 +00:00
2013-05-05 09:33:17 +00:00
if ( no_exec )
{
2013-03-25 23:06:12 +00:00
exec_no_exec ( parser , j ) ;
2012-11-19 00:30:30 +00:00
return ;
2013-03-25 23:06:12 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
sigemptyset ( & chldset ) ;
sigaddset ( & chldset , SIGCHLD ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
debug ( 4 , L " Exec job '%ls' with id %d " , j - > command_wcstr ( ) , j - > job_id ) ;
2012-11-18 10:23:22 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
/* PCA Here we detect the special case of an input buffer redirection, i.e. we want a process to receive data that we hold in a buffer (it is an INPUT for the process, but an output for fish). This is extremely rare: I believe only run_pager creates these and it would be nice to dump it. So we can only have at most one.
2006-08-13 01:38:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
It would be great to wean fish_pager off of input redirections so that we can dump input redirections and the INTERNAL_BUFFER process type altogether .
*/
const io_buffer_t * single_magic_input_redirect = NULL ;
const io_chain_t all_ios = j - > all_io_redirections ( ) ;
for ( size_t idx = 0 ; idx < all_ios . size ( ) ; idx + + )
2012-11-19 00:30:30 +00:00
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
const shared_ptr < io_data_t > & io = all_ios . at ( idx ) ;
2012-11-18 10:23:22 +00:00
2013-01-16 03:55:57 +00:00
if ( ( io - > io_mode = = IO_BUFFER ) )
2012-11-19 00:30:30 +00:00
{
2013-01-16 03:55:57 +00:00
CAST_INIT ( io_buffer_t * , io_buffer , io . get ( ) ) ;
if ( io_buffer - > is_input )
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
/* We expect to have at most one of these, per the comment above. Note that this assertion is the only reason we don't break out of the loop below */
assert ( single_magic_input_redirect = = NULL & & " Should have at most one input IO_BUFFER " ) ;
2013-01-16 03:55:57 +00:00
/*
Input redirection - create a new gobetween process to take
care of buffering , save the redirection in input_redirect
*/
process_t * fake = new process_t ( ) ;
fake - > type = INTERNAL_BUFFER ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
fake - > pipe_write_fd = STDOUT_FILENO ;
2013-01-16 03:55:57 +00:00
j - > first_process - > pipe_read_fd = io - > fd ;
fake - > next = j - > first_process ;
j - > first_process = fake ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
single_magic_input_redirect = io_buffer ;
2013-01-16 03:55:57 +00:00
}
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
if ( j - > first_process - > type = = INTERNAL_EXEC )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
/*
Do a regular launch - but without forking first . . .
*/
signal_block ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
setup_child_process makes sure signals are properly set
up . It will also call signal_unblock
*/
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
/* PCA This is for handling exec. Passing all_ios here matches what fish 2.0.0 and 1.x did. It's known to be wrong - for example, it means that redirections bound for subsequent commands in the pipeline will apply to exec. However, using exec in a pipeline doesn't really make sense, so I'm not trying to fix it here. */
if ( ! setup_child_process ( j , 0 , all_ios ) )
2012-11-19 00:30:30 +00:00
{
/*
launch_process _never_ returns
*/
launch_process_nofork ( j - > first_process ) ;
}
else
{
job_set_flag ( j , JOB_CONSTRUCTED , 1 ) ;
j - > first_process - > completed = 1 ;
return ;
}
}
2013-01-31 23:57:08 +00:00
2012-11-19 00:30:30 +00:00
signal_block ( ) ;
/*
See if we need to create a group keepalive process . This is
a process that we create to make sure that the process group
doesn ' t die accidentally , and is often needed when a
builtin / block / function is inside a pipeline , since that
usually means we have to wait for one program to exit before
continuing in the pipeline , causing the group leader to
exit .
*/
if ( job_get_flag ( j , JOB_CONTROL ) )
2012-11-18 10:23:22 +00:00
{
2013-01-30 10:22:38 +00:00
for ( const process_t * p = j - > first_process ; p ; p = p - > next )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
if ( p - > type ! = EXTERNAL )
{
if ( p - > next )
{
needs_keepalive = true ;
break ;
}
if ( p ! = j - > first_process )
{
needs_keepalive = true ;
break ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
if ( needs_keepalive )
{
2012-02-28 23:11:46 +00:00
/* Call fork. No need to wait for threads since our use is confined and simple. */
2012-11-19 00:30:30 +00:00
if ( g_log_forks )
{
2012-04-22 03:08:08 +00:00
printf ( " fork #%d: Executing keepalive fork for '%ls' \n " , g_fork_count , j - > command_wcstr ( ) ) ;
2012-03-09 07:21:07 +00:00
}
2012-11-19 00:30:30 +00:00
keepalive . pid = execute_fork ( false ) ;
if ( keepalive . pid = = 0 )
{
2012-02-28 02:43:24 +00:00
/* Child */
2012-11-19 00:30:30 +00:00
keepalive . pid = getpid ( ) ;
set_child_group ( j , & keepalive , 1 ) ;
pause ( ) ;
exit_without_destructors ( 0 ) ;
}
else
{
2012-02-28 02:43:24 +00:00
/* Parent */
2012-11-19 00:30:30 +00:00
set_child_group ( j , & keepalive , 0 ) ;
}
2012-11-18 10:23:22 +00:00
}
2013-01-31 23:57:08 +00:00
2012-11-19 00:30:30 +00:00
/*
This loop loops over every process_t in the job , starting it as
appropriate . This turns out to be rather complex , since a
process_t can be one of many rather different things .
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
The loop also has to handle pipelining between the jobs .
*/
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
/* We can have up to three pipes "in flight" at a time:
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
1. The pipe the current process should read from ( courtesy of the previous process )
2. The pipe that the current process should write to
3. The pipe that the next process should read from ( courtesy of us )
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
We are careful to set these to - 1 when closed , so if we exit the loop abruptly , we can still close them .
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
*/
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
int pipe_current_read = - 1 , pipe_current_write = - 1 , pipe_next_read = - 1 ;
2013-01-30 10:22:38 +00:00
for ( process_t * p = j - > first_process ; p ; p = p - > next )
2012-11-19 00:30:30 +00:00
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
/* The IO chain for this process. It starts with the block IO, then pipes, and then gets any from the process */
io_chain_t process_net_io_chain = j - > block_io_chain ( ) ;
2013-01-30 11:08:06 +00:00
/* "Consume" any pipe_next_read by making it current */
assert ( pipe_current_read = = - 1 ) ;
pipe_current_read = pipe_next_read ;
pipe_next_read = - 1 ;
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
/* See if we need a pipe */
const bool pipes_to_next_command = ( p - > next ! = NULL ) ;
2012-11-18 10:23:22 +00:00
2013-02-04 12:07:16 +00:00
/* The pipes the current process write to and read from.
Unfortunately these can ' t be just allocated on the stack , since
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
j - > io wants shared_ptr .
The write pipe ( destined for stdout ) needs to occur before redirections . For example , with a redirection like this :
` foo 2 > & 1 | bar ` , what we want to happen is this :
dup2 ( pipe , stdout )
dup2 ( stdout , stderr )
so that stdout and stderr both wind up referencing the pipe .
The read pipe ( destined for stdin ) is more ambiguous . Imagine a pipeline like this :
echo alpha | cat < beta . txt
Should cat output alpha or beta ? bash and ksh output ' beta ' , tcsh gets it right and complains about ambiguity , and zsh outputs both ( ! ) . No shells appear to output ' alpha ' , so we match bash here . That means putting the pipe first , so that it gets trumped by the file redirection .
*/
2013-02-05 04:29:44 +00:00
shared_ptr < io_pipe_t > pipe_write ;
shared_ptr < io_pipe_t > pipe_read ;
2013-02-04 12:07:16 +00:00
if ( p ! = j - > first_process )
{
2013-02-05 04:29:44 +00:00
pipe_read . reset ( new io_pipe_t ( p - > pipe_read_fd , true ) ) ;
2013-02-07 12:39:08 +00:00
/* Record the current read in pipe_read */
pipe_read - > pipe_fd [ 0 ] = pipe_current_read ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
process_net_io_chain . push_back ( pipe_read ) ;
2013-02-04 12:07:16 +00:00
}
if ( p - > next )
{
2013-02-05 04:29:44 +00:00
pipe_write . reset ( new io_pipe_t ( p - > pipe_write_fd , false ) ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
process_net_io_chain . push_back ( pipe_write ) ;
2013-08-18 23:55:01 +00:00
2013-02-04 12:07:16 +00:00
}
2012-11-18 10:23:22 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
/* Now that we've added our pipes, add the rest of the IO redirections associated with that process */
process_net_io_chain . append ( p - > io_chain ( ) ) ;
2012-11-19 00:30:30 +00:00
/*
This call is used so the global environment variable array
is regenerated , if needed , before the fork . That way , we
avoid a lot of duplicate work where EVERY child would need
to generate it , since that result would not get written
back to the parent . This call could be safely removed , but
it would result in slightly lower performance - at least on
uniprocessor systems .
*/
if ( p - > type = = EXTERNAL )
env_export_arr ( true ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
Set up fd : s that will be used in the pipe
*/
2012-11-18 10:23:22 +00:00
2013-01-30 11:08:06 +00:00
if ( pipes_to_next_command )
2012-11-19 00:30:30 +00:00
{
2012-11-18 10:23:22 +00:00
// debug( 1, L"%ls|%ls" , p->argv[0], p->next->argv[0]);
2013-01-30 11:08:06 +00:00
int local_pipe [ 2 ] = { - 1 , - 1 } ;
if ( exec_pipe ( local_pipe ) = = - 1 )
2012-11-19 00:30:30 +00:00
{
debug ( 1 , PIPE_ERROR ) ;
wperror ( L " pipe " ) ;
exec_error = true ;
2013-01-30 10:22:38 +00:00
job_mark_process_as_failed ( j , p ) ;
2012-11-19 00:30:30 +00:00
break ;
}
2013-01-31 23:57:08 +00:00
2013-01-30 10:22:38 +00:00
// This tells the redirection about the fds, but the redirection does not close them
2013-01-30 11:08:06 +00:00
memcpy ( pipe_write - > pipe_fd , local_pipe , sizeof ( int ) * 2 ) ;
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
// Record our pipes
// The fds should be negative to indicate that we aren't overwriting an fd we failed to close
assert ( pipe_current_write = = - 1 ) ;
pipe_current_write = local_pipe [ 1 ] ;
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
assert ( pipe_next_read = = - 1 ) ;
pipe_next_read = local_pipe [ 0 ] ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2013-08-18 23:55:01 +00:00
//fprintf(stderr, "before IO: ");
//io_print(j->io);
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
// This is the IO buffer we use for storing the output of a block or function when it is in a pipeline
shared_ptr < io_buffer_t > block_output_io_buffer ;
2012-11-19 00:30:30 +00:00
switch ( p - > type )
{
2012-11-19 08:31:03 +00:00
case INTERNAL_FUNCTION :
{
/*
Calls to function_get_definition might need to
source a file as a part of autoloading , hence there
must be no blocks .
*/
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
signal_unblock ( ) ;
2013-01-30 10:22:38 +00:00
wcstring def ;
bool function_exists = function_get_definition ( p - > argv0 ( ) , & def ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
wcstring_list_t named_arguments = function_get_named_arguments ( p - > argv0 ( ) ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
bool shadows = function_get_shadows ( p - > argv0 ( ) ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
signal_block ( ) ;
2012-11-18 10:23:22 +00:00
2013-01-30 10:22:38 +00:00
if ( ! function_exists )
2012-11-19 08:31:03 +00:00
{
debug ( 0 , _ ( L " Unknown function '%ls' " ) , p - > argv0 ( ) ) ;
break ;
}
function_block_t * newv = new function_block_t ( p , p - > argv0 ( ) , shadows ) ;
parser . push_block ( newv ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
/*
set_argv might trigger an event
handler , hence we need to unblock
signals .
*/
signal_unblock ( ) ;
parse_util_set_argv ( p - > get_argv ( ) + 1 , named_arguments ) ;
signal_block ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
parser . forbid_function ( p - > argv0 ( ) ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
if ( p - > next )
{
2013-01-30 10:22:38 +00:00
// Be careful to handle failure, e.g. too many open fds
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
block_output_io_buffer . reset ( io_buffer_t : : create ( false /* = not input */ , STDOUT_FILENO ) ) ;
if ( block_output_io_buffer . get ( ) = = NULL )
2013-01-30 10:22:38 +00:00
{
exec_error = true ;
job_mark_process_as_failed ( j , p ) ;
}
else
{
2013-08-20 01:17:01 +00:00
/* This looks sketchy, because we're adding this io buffer locally - they aren't in the process or job redirection list. Therefore select_try won't be able to read them. However we call block_output_io_buffer->read() below, which reads until EOF. So there's no need to select on this. */
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
process_net_io_chain . push_back ( block_output_io_buffer ) ;
2013-01-30 10:22:38 +00:00
}
}
2013-01-31 23:57:08 +00:00
2013-01-30 10:22:38 +00:00
if ( ! exec_error )
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
internal_exec_helper ( parser , def . c_str ( ) , TOP , process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
parser . allow_function ( ) ;
parser . pop_block ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
break ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case INTERNAL_BLOCK :
{
if ( p - > next )
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
block_output_io_buffer . reset ( io_buffer_t : : create ( 0 ) ) ;
if ( block_output_io_buffer . get ( ) = = NULL )
2013-01-30 10:22:38 +00:00
{
exec_error = true ;
job_mark_process_as_failed ( j , p ) ;
}
else
{
2013-08-20 01:17:01 +00:00
/* See the comment above about it's OK to add an IO redirection to this local buffer, even though it won't be handled in select_try */
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
process_net_io_chain . push_back ( block_output_io_buffer ) ;
2013-01-30 10:22:38 +00:00
}
}
2013-01-31 23:57:08 +00:00
2013-01-30 10:22:38 +00:00
if ( ! exec_error )
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
internal_exec_helper ( parser , p - > argv0 ( ) , TOP , process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
}
break ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case INTERNAL_BUILTIN :
2012-11-18 10:23:22 +00:00
{
2012-11-19 08:31:03 +00:00
int builtin_stdin = 0 ;
2013-08-18 23:55:01 +00:00
bool close_stdin = false ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
/*
If this is the first process , check the io
redirections and see where we should be reading
from .
*/
if ( p = = j - > first_process )
2012-11-18 10:23:22 +00:00
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
const shared_ptr < const io_data_t > in = process_net_io_chain . get_io_for_fd ( STDIN_FILENO ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
if ( in )
2012-11-19 00:30:30 +00:00
{
2012-11-19 08:31:03 +00:00
switch ( in - > io_mode )
2012-11-19 00:30:30 +00:00
{
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case IO_FD :
{
2013-01-15 07:37:33 +00:00
CAST_INIT ( const io_fd_t * , in_fd , in . get ( ) ) ;
builtin_stdin = in_fd - > old_fd ;
2012-11-19 08:31:03 +00:00
break ;
}
case IO_PIPE :
{
2013-01-15 09:31:36 +00:00
CAST_INIT ( const io_pipe_t * , in_pipe , in . get ( ) ) ;
builtin_stdin = in_pipe - > pipe_fd [ 0 ] ;
2012-11-19 08:31:03 +00:00
break ;
}
case IO_FILE :
{
/* Do not set CLO_EXEC because child needs access */
2013-01-15 08:18:03 +00:00
CAST_INIT ( const io_file_t * , in_file , in . get ( ) ) ;
builtin_stdin = open ( in_file - > filename_cstr ,
in_file - > flags , OPEN_MASK ) ;
2012-11-19 08:31:03 +00:00
if ( builtin_stdin = = - 1 )
{
debug ( 1 ,
FILE_ERROR ,
2013-01-15 08:18:03 +00:00
in_file - > filename_cstr ) ;
2012-11-19 08:31:03 +00:00
wperror ( L " open " ) ;
}
else
{
2013-08-18 23:55:01 +00:00
close_stdin = true ;
2012-11-19 08:31:03 +00:00
}
break ;
}
case IO_CLOSE :
{
/*
FIXME :
2012-12-16 04:23:24 +00:00
When requesting that stdin be closed , we
really don ' t do anything . How should this be
2012-11-19 08:31:03 +00:00
handled ?
*/
builtin_stdin = - 1 ;
break ;
}
default :
{
builtin_stdin = - 1 ;
debug ( 1 ,
_ ( L " Unknown input redirection type %d " ) ,
in - > io_mode ) ;
break ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
}
2012-11-19 00:30:30 +00:00
}
2012-11-19 08:31:03 +00:00
}
else
{
2013-01-15 09:31:36 +00:00
builtin_stdin = pipe_read - > pipe_fd [ 0 ] ;
2012-11-19 08:31:03 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
if ( builtin_stdin = = - 1 )
{
exec_error = true ;
break ;
2012-11-19 00:30:30 +00:00
}
2012-11-19 08:31:03 +00:00
else
{
int old_out = builtin_out_redirect ;
int old_err = builtin_err_redirect ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
/*
Since this may be the foreground job , and since
a builtin may execute another foreground job ,
we need to pretend to suspend this job while
running the builtin , in order to avoid a
situation where two jobs are running at once .
The reason this is done here , and not by the
relevant builtins , is that this way , the
builtin does not need to know what job it is
part of . It could probably figure that out by
walking the job list , but it seems more robust
to make exec handle things .
*/
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
builtin_push_io ( parser , builtin_stdin ) ;
2012-11-18 10:23:22 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
builtin_out_redirect = has_fd ( process_net_io_chain , STDOUT_FILENO ) ;
builtin_err_redirect = has_fd ( process_net_io_chain , STDERR_FILENO ) ;
2012-11-18 10:23:22 +00:00
2013-01-30 10:22:38 +00:00
const int fg = job_get_flag ( j , JOB_FOREGROUND ) ;
2012-11-19 08:31:03 +00:00
job_set_flag ( j , JOB_FOREGROUND , 0 ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
signal_unblock ( ) ;
2012-11-18 10:23:22 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
p - > status = builtin_run ( parser , p - > get_argv ( ) , process_net_io_chain ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
builtin_out_redirect = old_out ;
builtin_err_redirect = old_err ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
signal_block ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
/*
Restore the fg flag , which is temporarily set to
false during builtin execution so as not to confuse
some job - handling builtins .
*/
job_set_flag ( j , JOB_FOREGROUND , fg ) ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
2012-11-19 08:31:03 +00:00
If stdin has been redirected , close the redirection
stream .
2012-11-19 00:30:30 +00:00
*/
2012-11-19 08:31:03 +00:00
if ( close_stdin )
{
exec_close ( builtin_stdin ) ;
}
break ;
2012-11-19 00:30:30 +00:00
}
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
if ( exec_error )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
break ;
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
switch ( p - > type )
{
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case INTERNAL_BLOCK :
case INTERNAL_FUNCTION :
2012-11-19 00:30:30 +00:00
{
2012-11-19 08:31:03 +00:00
int status = proc_get_last_status ( ) ;
2012-11-19 00:30:30 +00:00
/*
2012-11-19 08:31:03 +00:00
Handle output from a block or function . This usually
means do nothing , but in the case of pipes , we have
to buffer such io , since otherwise the internal pipe
buffer might overflow .
2012-11-19 00:30:30 +00:00
*/
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
if ( ! block_output_io_buffer . get ( ) )
2012-11-19 00:30:30 +00:00
{
2012-11-19 08:31:03 +00:00
/*
No buffer , so we exit directly . This means we
have to manually set the exit status .
*/
if ( p - > next = = 0 )
{
proc_set_last_status ( job_get_flag ( j , JOB_NEGATE ) ? ( ! status ) : status ) ;
}
p - > completed = 1 ;
break ;
2012-11-19 00:30:30 +00:00
}
2012-11-19 08:31:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
// Here we must have a non-NULL block_output_io_buffer
assert ( block_output_io_buffer . get ( ) ! = NULL ) ;
io_remove ( process_net_io_chain , block_output_io_buffer ) ;
2012-11-19 08:31:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
block_output_io_buffer - > read ( ) ;
2012-11-19 08:31:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
const char * buffer = block_output_io_buffer - > out_buffer_ptr ( ) ;
size_t count = block_output_io_buffer - > out_buffer_size ( ) ;
2012-11-19 08:31:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
if ( block_output_io_buffer - > out_buffer_size ( ) > 0 )
2012-11-19 08:31:03 +00:00
{
/* We don't have to drain threads here because our child process is simple */
if ( g_log_forks )
{
printf ( " Executing fork for internal block or function for '%ls' \n " , p - > argv0 ( ) ) ;
}
pid = execute_fork ( false ) ;
if ( pid = = 0 )
{
/*
This is the child process . Write out the contents of the pipeline .
*/
p - > pid = getpid ( ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
setup_child_process ( j , p , process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
exec_write_and_exit ( block_output_io_buffer - > fd , buffer , count , status ) ;
2012-11-19 08:31:03 +00:00
}
else
{
/*
This is the parent process . Store away
information on the child , and possibly give
it control over the terminal .
*/
p - > pid = pid ;
set_child_group ( j , p , 0 ) ;
}
}
else
{
if ( p - > next = = 0 )
{
proc_set_last_status ( job_get_flag ( j , JOB_NEGATE ) ? ( ! status ) : status ) ;
}
p - > completed = 1 ;
}
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
block_output_io_buffer . reset ( ) ;
2012-11-19 00:30:30 +00:00
break ;
2012-11-19 08:31:03 +00:00
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case INTERNAL_BUFFER :
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
assert ( single_magic_input_redirect ! = NULL ) ;
const char * buffer = single_magic_input_redirect - > out_buffer_ptr ( ) ;
size_t count = single_magic_input_redirect - > out_buffer_size ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/* We don't have to drain threads here because our child process is simple */
if ( g_log_forks )
{
2012-11-19 08:31:03 +00:00
printf ( " fork #%d: Executing fork for internal buffer for '%ls' \n " , g_fork_count , p - > argv0 ( ) ? p - > argv0 ( ) : L " (null) " ) ;
2012-11-19 00:30:30 +00:00
}
pid = execute_fork ( false ) ;
if ( pid = = 0 )
{
/*
2012-11-19 08:31:03 +00:00
This is the child process . Write out the
contents of the pipeline .
2012-11-19 00:30:30 +00:00
*/
p - > pid = getpid ( ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
setup_child_process ( j , p , process_net_io_chain ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
exec_write_and_exit ( 1 , buffer , count , 0 ) ;
2012-11-19 00:30:30 +00:00
}
else
{
/*
This is the parent process . Store away
information on the child , and possibly give
it control over the terminal .
*/
p - > pid = pid ;
set_child_group ( j , p , 0 ) ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
break ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case INTERNAL_BUILTIN :
2012-11-19 00:30:30 +00:00
{
/*
2012-11-19 08:31:03 +00:00
Handle output from builtin commands . In the general
case , this means forking of a worker process , that
will write out the contents of the stdout and stderr
buffers to the correct file descriptor . Since
2013-06-17 06:26:43 +00:00
forking is expensive , fish tries to avoid it when
2012-11-19 08:31:03 +00:00
possible .
2012-11-19 00:30:30 +00:00
*/
2013-06-17 06:26:43 +00:00
bool fork_was_skipped = false ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
const shared_ptr < io_data_t > stdout_io = process_net_io_chain . get_io_for_fd ( STDOUT_FILENO ) ;
const shared_ptr < io_data_t > stderr_io = process_net_io_chain . get_io_for_fd ( STDERR_FILENO ) ;
2013-06-17 06:26:43 +00:00
/* If we are outputting to a file, we have to actually do it, even if we have no output, so that we can truncate the file. Does not apply to /dev/null. */
bool must_fork = redirection_is_to_real_file ( stdout_io . get ( ) ) | | redirection_is_to_real_file ( stderr_io . get ( ) ) ;
if ( ! must_fork )
{
if ( p - > next = = NULL )
2013-01-11 23:09:33 +00:00
{
2013-06-17 06:26:43 +00:00
const bool stdout_is_to_buffer = stdout_io & & stdout_io - > io_mode = = IO_BUFFER ;
const bool no_stdout_output = get_stdout_buffer ( ) . empty ( ) ;
const bool no_stderr_output = get_stderr_buffer ( ) . empty ( ) ;
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
if ( no_stdout_output & & no_stderr_output )
2013-02-22 21:20:27 +00:00
{
2013-06-17 06:26:43 +00:00
/* The builtin produced no output and is not inside of a pipeline. No need to fork or even output anything. */
if ( g_log_forks )
{
// This one is very wordy
//printf("fork #-: Skipping fork due to no output for internal builtin for '%ls'\n", p->argv0());
}
fork_was_skipped = true ;
}
else if ( no_stderr_output & & stdout_is_to_buffer )
{
/* The builtin produced no stderr, and its stdout is going to an internal buffer. There is no need to fork. This helps out the performance quite a bit in complex completion code. */
if ( g_log_forks )
{
printf ( " fork #-: Skipping fork due to buffered output for internal builtin for '%ls' \n " , p - > argv0 ( ) ) ;
}
CAST_INIT ( io_buffer_t * , io_buffer , stdout_io . get ( ) ) ;
const std : : string res = wcs2string ( get_stdout_buffer ( ) ) ;
io_buffer - > out_buffer_append ( res . data ( ) , res . size ( ) ) ;
fork_was_skipped = true ;
}
else if ( stdout_io . get ( ) = = NULL & & stderr_io . get ( ) = = NULL )
{
/* We are writing to normal stdout and stderr. Just do it - no need to fork. */
if ( g_log_forks )
{
printf ( " fork #-: Skipping fork due to ordinary output for internal builtin for '%ls' \n " , p - > argv0 ( ) ) ;
}
const wcstring & out = get_stdout_buffer ( ) , & err = get_stderr_buffer ( ) ;
const std : : string outbuff = wcs2string ( out ) ;
const std : : string errbuff = wcs2string ( err ) ;
bool builtin_io_done = do_builtin_io ( outbuff . data ( ) , outbuff . size ( ) , errbuff . data ( ) , errbuff . size ( ) ) ;
if ( ! builtin_io_done )
{
show_stackframe ( ) ;
}
fork_was_skipped = true ;
2013-02-22 21:20:27 +00:00
}
2012-11-19 08:31:03 +00:00
}
2012-03-09 07:21:07 +00:00
}
2013-06-17 06:26:43 +00:00
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
if ( fork_was_skipped )
2012-11-19 00:30:30 +00:00
{
2012-11-19 08:31:03 +00:00
p - > completed = 1 ;
if ( p - > next = = 0 )
{
2013-01-24 11:17:55 +00:00
debug ( 3 , L " Set status of %ls to %d using short circuit " , j - > command_wcstr ( ) , p - > status ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
int status = p - > status ;
proc_set_last_status ( job_get_flag ( j , JOB_NEGATE ) ? ( ! status ) : status ) ;
}
2012-11-19 00:30:30 +00:00
}
2013-06-17 06:26:43 +00:00
else
{
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
/* Ok, unfortunately, we have to do a real fork. Bummer. We work hard to make sure we don't have to wait for all our threads to exit, by arranging things so that we don't have to allocate memory or do anything except system calls in the child. */
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
/* Get the strings we'll write before we fork (since they call malloc) */
const wcstring & out = get_stdout_buffer ( ) , & err = get_stderr_buffer ( ) ;
2012-12-19 21:31:06 +00:00
2013-06-17 06:26:43 +00:00
/* These strings may contain embedded nulls, so don't treat them as C strings */
const std : : string outbuff_str = wcs2string ( out ) ;
const char * outbuff = outbuff_str . data ( ) ;
size_t outbuff_len = outbuff_str . size ( ) ;
2012-12-19 21:31:06 +00:00
2013-06-17 06:26:43 +00:00
const std : : string errbuff_str = wcs2string ( err ) ;
const char * errbuff = errbuff_str . data ( ) ;
size_t errbuff_len = errbuff_str . size ( ) ;
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
fflush ( stdout ) ;
fflush ( stderr ) ;
if ( g_log_forks )
{
printf ( " fork #%d: Executing fork for internal builtin for '%ls' \n " , g_fork_count , p - > argv0 ( ) ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
io_print ( process_net_io_chain ) ;
2013-06-17 06:26:43 +00:00
}
pid = execute_fork ( false ) ;
if ( pid = = 0 )
{
/*
This is the child process . Setup redirections ,
print correct output to stdout and stderr , and
then exit .
*/
p - > pid = getpid ( ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
setup_child_process ( j , p , process_net_io_chain ) ;
2013-06-17 06:26:43 +00:00
do_builtin_io ( outbuff , outbuff_len , errbuff , errbuff_len ) ;
exit_without_destructors ( p - > status ) ;
}
else
{
/*
This is the parent process . Store away
information on the child , and possibly give
it control over the terminal .
*/
p - > pid = pid ;
2012-11-18 10:23:22 +00:00
2013-06-17 06:26:43 +00:00
set_child_group ( j , p , 0 ) ;
}
2012-11-19 08:31:03 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
break ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
case EXTERNAL :
{
/* Get argv and envv before we fork */
2013-02-23 00:22:56 +00:00
null_terminated_array_t < char > argv_array ;
convert_wide_array_to_narrow ( p - > get_argv_array ( ) , & argv_array ) ;
2013-05-05 09:33:17 +00:00
2013-04-07 19:40:08 +00:00
/* Ensure that stdin is blocking before we hand it off (see issue #176). It's a little strange that we only do this with stdin and not with stdout or stderr. However in practice, setting or clearing O_NONBLOCK on stdin also sets it for the other two fds, presumably because they refer to the same underlying file (/dev/tty?) */
make_fd_blocking ( STDIN_FILENO ) ;
2012-11-18 10:23:22 +00:00
2013-02-23 00:22:56 +00:00
const char * const * argv = argv_array . get ( ) ;
const char * const * envv = env_export_arr ( false ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 08:31:03 +00:00
std : : string actual_cmd_str = wcs2string ( p - > actual_cmd ) ;
const char * actual_cmd = actual_cmd_str . c_str ( ) ;
2012-11-18 10:23:22 +00:00
2013-01-15 09:52:03 +00:00
const wchar_t * reader_current_filename ( void ) ;
2012-11-19 08:31:03 +00:00
if ( g_log_forks )
{
const wchar_t * file = reader_current_filename ( ) ;
const wchar_t * func = parser_t : : principal_parser ( ) . is_function ( ) ;
printf ( " fork #%d: forking for '%s' in '%ls:%ls' \n " , g_fork_count , actual_cmd , file ? file : L " " , func ? func : L " ? " ) ;
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
fprintf ( stderr , " IO chain for %s: \n " , actual_cmd ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
io_print ( process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
}
2012-11-19 00:30:30 +00:00
# if FISH_USE_POSIX_SPAWN
2012-11-19 08:31:03 +00:00
/* Prefer to use posix_spawn, since it's faster on some systems like OS X */
bool use_posix_spawn = g_use_posix_spawn & & can_use_posix_spawn_for_job ( j , p ) ;
if ( use_posix_spawn )
2012-11-19 00:30:30 +00:00
{
2012-11-19 08:31:03 +00:00
/* Create posix spawn attributes and actions */
posix_spawnattr_t attr = posix_spawnattr_t ( ) ;
posix_spawn_file_actions_t actions = posix_spawn_file_actions_t ( ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
bool made_it = fork_actions_make_spawn_properties ( & attr , & actions , j , p , process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
if ( made_it )
{
/* We successfully made the attributes and actions; actually call posix_spawn */
2013-02-23 00:22:56 +00:00
int spawn_ret = posix_spawn ( & pid , actual_cmd , & actions , & attr , const_cast < char * const * > ( argv ) , const_cast < char * const * > ( envv ) ) ;
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
/* This usleep can be used to test for various race conditions (https://github.com/fish-shell/fish-shell/issues/360) */
//usleep(10000);
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
if ( spawn_ret ! = 0 )
{
safe_report_exec_error ( spawn_ret , actual_cmd , argv , envv ) ;
/* Make sure our pid isn't set */
pid = 0 ;
}
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
/* Clean up our actions */
posix_spawn_file_actions_destroy ( & actions ) ;
posix_spawnattr_destroy ( & attr ) ;
}
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
/* A 0 pid means we failed to posix_spawn. Since we have no pid, we'll never get told when it's exited, so we have to mark the process as failed. */
if ( pid = = 0 )
{
job_mark_process_as_failed ( j , p ) ;
exec_error = true ;
}
2012-11-19 00:30:30 +00:00
}
2012-11-19 08:31:03 +00:00
else
2012-08-15 07:57:56 +00:00
# endif
{
2012-11-19 08:31:03 +00:00
pid = execute_fork ( false ) ;
if ( pid = = 0 )
{
/* This is the child process. */
p - > pid = getpid ( ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-19 23:16:41 +00:00
setup_child_process ( j , p , process_net_io_chain ) ;
2012-11-19 08:31:03 +00:00
safe_launch_process ( p , actual_cmd , argv , envv ) ;
2012-11-19 00:30:30 +00:00
2012-11-19 08:31:03 +00:00
/*
safe_launch_process _never_ returns . . .
*/
}
2013-01-30 10:22:38 +00:00
else if ( pid < 0 )
{
job_mark_process_as_failed ( j , p ) ;
exec_error = true ;
}
2012-03-07 19:35:22 +00:00
}
2005-09-20 13:26:39 +00:00
2012-08-15 07:57:56 +00:00
2012-11-19 08:31:03 +00:00
/*
This is the parent process . Store away
information on the child , and possibly fice
it control over the terminal .
*/
p - > pid = pid ;
2012-08-15 07:57:56 +00:00
2012-11-19 08:31:03 +00:00
set_child_group ( j , p , 0 ) ;
2012-08-15 07:57:56 +00:00
2012-11-19 08:31:03 +00:00
break ;
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
if ( p - > type = = INTERNAL_BUILTIN )
builtin_pop_io ( parser ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
/*
Close the pipe the current process uses to read from the
previous process_t
*/
2013-01-30 11:08:06 +00:00
if ( pipe_current_read > = 0 )
2013-01-30 10:22:38 +00:00
{
2013-01-30 11:08:06 +00:00
exec_close ( pipe_current_read ) ;
pipe_current_read = - 1 ;
2013-01-30 10:22:38 +00:00
}
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
/* Close the write end too, since the curent child subprocess already has a copy of it. */
if ( pipe_current_write > = 0 )
2013-01-30 10:22:38 +00:00
{
2013-01-30 11:08:06 +00:00
exec_close ( pipe_current_write ) ;
pipe_current_write = - 1 ;
2012-11-19 00:30:30 +00:00
}
}
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
/* Clean up any file descriptors we left open */
if ( pipe_current_read > = 0 )
exec_close ( pipe_current_read ) ;
if ( pipe_current_write > = 0 )
exec_close ( pipe_current_write ) ;
if ( pipe_next_read > = 0 )
exec_close ( pipe_next_read ) ;
2013-01-31 23:57:08 +00:00
2013-01-30 11:08:06 +00:00
/* The keepalive process is no longer needed, so we terminate it with extreme prejudice */
2012-11-19 00:30:30 +00:00
if ( needs_keepalive )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
kill ( keepalive . pid , SIGKILL ) ;
2012-11-18 10:23:22 +00:00
}
2012-11-19 00:30:30 +00:00
signal_unblock ( ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
debug ( 3 , L " Job is constructed " ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
job_set_flag ( j , JOB_CONSTRUCTED , 1 ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
if ( ! job_get_flag ( j , JOB_FOREGROUND ) )
{
proc_last_bg_pid = j - > pgid ;
}
2012-11-18 10:23:22 +00:00
2013-01-30 10:22:38 +00:00
if ( ! exec_error )
{
job_continue ( j , false ) ;
}
else
2012-11-19 00:30:30 +00:00
{
2013-01-30 10:22:38 +00:00
/* Mark the errored job as not in the foreground. I can't fully justify whether this is the right place, but it prevents sanity_lose from complaining. */
job_set_flag ( j , JOB_FOREGROUND , 0 ) ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2011-12-27 03:18:46 +00:00
}
2010-09-18 01:51:16 +00:00
2005-09-20 13:26:39 +00:00
2013-01-31 23:57:08 +00:00
static int exec_subshell_internal ( const wcstring & cmd , wcstring_list_t * lst , bool apply_exit_status )
2005-09-20 13:26:39 +00:00
{
2012-02-26 21:27:31 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2012-11-19 00:30:30 +00:00
int prev_subshell = is_subshell ;
2013-01-31 23:57:08 +00:00
const int prev_status = proc_get_last_status ( ) ;
2012-11-19 00:30:30 +00:00
char sep = 0 ;
const env_var_t ifs = env_get_string ( L " IFS " ) ;
if ( ! ifs . missing_or_empty ( ) )
2012-11-18 10:23:22 +00:00
{
2012-11-19 00:30:30 +00:00
if ( ifs . at ( 0 ) < 128 )
{
sep = ' \n ' ; //ifs[0];
}
else
{
sep = 0 ;
2013-01-31 23:57:08 +00:00
debug ( 0 , L " Warning - invalid command substitution separator '%lc'. Please change the first character of IFS " , ifs [ 0 ] ) ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
is_subshell = 1 ;
2013-01-07 15:04:55 +00:00
2012-11-18 10:23:22 +00:00
2013-01-31 23:57:08 +00:00
int subcommand_status = - 1 ; //assume the worst
2013-01-30 10:22:38 +00:00
// IO buffer creation may fail (e.g. if we have too many open files to make a pipe), so this may be null
2013-01-31 23:57:08 +00:00
const shared_ptr < io_buffer_t > io_buffer ( io_buffer_t : : create ( 0 ) ) ;
if ( io_buffer . get ( ) ! = NULL )
2012-11-19 00:30:30 +00:00
{
2013-01-30 10:22:38 +00:00
parser_t & parser = parser_t : : principal_parser ( ) ;
2013-01-31 23:57:08 +00:00
if ( parser . eval ( cmd , io_chain_t ( io_buffer ) , SUBST ) = = 0 )
2013-01-30 10:22:38 +00:00
{
2013-01-31 23:57:08 +00:00
subcommand_status = proc_get_last_status ( ) ;
2013-01-30 10:22:38 +00:00
}
2013-01-31 23:57:08 +00:00
2013-01-30 10:22:38 +00:00
io_buffer - > read ( ) ;
2012-11-19 00:30:30 +00:00
}
2012-11-18 10:23:22 +00:00
2013-01-31 23:57:08 +00:00
// If the caller asked us to preserve the exit status, restore the old status
// Otherwise set the status of the subcommand
proc_set_last_status ( apply_exit_status ? subcommand_status : prev_status ) ;
2012-11-18 10:23:22 +00:00
2012-11-19 00:30:30 +00:00
is_subshell = prev_subshell ;
2012-12-20 20:25:35 +00:00
2013-01-30 10:22:38 +00:00
if ( lst ! = NULL & & io_buffer . get ( ) ! = NULL )
2012-11-18 10:23:22 +00:00
{
2012-12-20 00:40:10 +00:00
const char * begin = io_buffer - > out_buffer_ptr ( ) ;
const char * end = begin + io_buffer - > out_buffer_size ( ) ;
const char * cursor = begin ;
while ( cursor < end )
2012-11-18 10:23:22 +00:00
{
2012-12-20 00:40:10 +00:00
// Look for the next separator
const char * stop = ( const char * ) memchr ( cursor , sep , end - cursor ) ;
2012-12-21 02:10:08 +00:00
const bool hit_separator = ( stop ! = NULL ) ;
2012-12-20 00:40:10 +00:00
if ( ! hit_separator )
2012-11-19 00:30:30 +00:00
{
2012-12-20 00:40:10 +00:00
// If it's not found, just use the end
stop = end ;
2012-11-19 00:30:30 +00:00
}
2012-12-21 02:10:08 +00:00
// Stop now points at the first character we do not want to copy
2012-12-20 00:40:10 +00:00
const wcstring wc = str2wcstring ( cursor , stop - cursor ) ;
lst - > push_back ( wc ) ;
2012-12-20 20:25:35 +00:00
2012-12-20 00:40:10 +00:00
// If we hit a separator, skip over it; otherwise we're at the end
cursor = stop + ( hit_separator ? 1 : 0 ) ;
2012-11-18 10:23:22 +00:00
}
}
2012-12-20 20:25:35 +00:00
2013-01-31 23:57:08 +00:00
return subcommand_status ;
2005-09-20 13:26:39 +00:00
}
2012-02-08 07:35:41 +00:00
2013-01-31 23:57:08 +00:00
int exec_subshell ( const wcstring & cmd , std : : vector < wcstring > & outputs , bool apply_exit_status )
2012-02-08 07:35:41 +00:00
{
2012-02-26 21:27:31 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2013-01-31 23:57:08 +00:00
return exec_subshell_internal ( cmd , & outputs , apply_exit_status ) ;
2012-02-08 07:35:41 +00:00
}
2013-02-06 00:11:46 +00:00
int exec_subshell ( const wcstring & cmd , bool apply_exit_status )
2012-02-08 07:35:41 +00:00
{
2012-02-26 21:27:31 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2013-01-31 23:57:08 +00:00
return exec_subshell_internal ( cmd , NULL , apply_exit_status ) ;
2012-02-08 07:35:41 +00:00
}