2005-09-20 13:26:39 +00:00
|
|
|
/** \file exec.c
|
|
|
|
Functions for executing a program.
|
|
|
|
|
|
|
|
Some of the code in this file is based on code from the Glibc
|
|
|
|
manual, though I the changes performed have been massive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "util.h"
|
|
|
|
#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"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "env_universal.h"
|
2006-01-04 12:51:02 +00:00
|
|
|
#include "translate.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Prototype for the getpgid library function. The prototype for this
|
|
|
|
function seems to be missing in glibc, at least I've not found any
|
2005-10-24 15:26:25 +00:00
|
|
|
combination of includes, macros and compiler switches that will
|
2005-09-20 13:26:39 +00:00
|
|
|
include it.
|
|
|
|
*/
|
|
|
|
pid_t getpgid( pid_t pid );
|
|
|
|
|
|
|
|
/**
|
|
|
|
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" )
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
file redirection error message
|
|
|
|
*/
|
2006-01-04 12:51:02 +00:00
|
|
|
#define FILE_ERROR _( L"An error occurred while redirecting file '%ls'" )
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
fork error message
|
|
|
|
*/
|
2006-01-04 12:51:02 +00:00
|
|
|
#define FORK_ERROR _( L"Could not create child process - exiting" )
|
2005-09-20 13:26:39 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2005-10-17 13:24:12 +00:00
|
|
|
static array_list_t *open_fds=0;
|
|
|
|
|
2006-02-04 11:33:20 +00:00
|
|
|
static int set_child_group( job_t *j, process_t *p, int print_errors );
|
2006-02-03 21:50:31 +00:00
|
|
|
|
2005-09-23 13:10:31 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
void exec_close( int fd )
|
2005-09-23 13:10:31 +00:00
|
|
|
{
|
2005-10-03 13:09:37 +00:00
|
|
|
int i;
|
|
|
|
|
2005-09-23 13:10:31 +00:00
|
|
|
while( close(fd) == -1 )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
|
|
|
debug( 1, FD_ERROR, fd );
|
|
|
|
wperror( L"close" );
|
|
|
|
}
|
|
|
|
}
|
2005-10-03 13:09:37 +00:00
|
|
|
|
|
|
|
if( open_fds )
|
|
|
|
{
|
|
|
|
for( i=0; i<al_get_count( open_fds ); i++ )
|
|
|
|
{
|
|
|
|
int n = (int)(long)al_get( open_fds, i );
|
|
|
|
if( n == fd )
|
|
|
|
{
|
|
|
|
al_set( open_fds,
|
|
|
|
i,
|
|
|
|
al_get( open_fds,
|
|
|
|
al_get_count( open_fds ) -1 ) );
|
|
|
|
al_truncate( open_fds,
|
|
|
|
al_get_count( open_fds ) -1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
int exec_pipe( int fd[2])
|
2005-10-03 13:09:37 +00:00
|
|
|
{
|
2005-10-08 11:20:51 +00:00
|
|
|
int res;
|
2005-11-02 15:41:59 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
while( ( res=pipe( fd ) ) )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
|
|
|
wperror(L"pipe");
|
|
|
|
return res;
|
|
|
|
}
|
2005-11-02 15:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug( 4, L"Created pipe using fds %d and %d", fd[0], fd[1]);
|
2005-10-08 11:20:51 +00:00
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
if( open_fds == 0 )
|
|
|
|
{
|
2005-11-02 15:41:59 +00:00
|
|
|
open_fds = al_new();
|
2005-10-03 13:09:37 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
al_push( open_fds, (void *)(long)fd[0] );
|
|
|
|
al_push( open_fds, (void *)(long)fd[1] );
|
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check if the specified fd is used as a part of a pipeline in the
|
|
|
|
specidied set of IO redirections.
|
|
|
|
|
|
|
|
\param fd the fd to search for
|
|
|
|
\param io the set of io redirections to search in
|
|
|
|
*/
|
|
|
|
static int use_fd_in_pipe( int fd, io_data_t *io )
|
|
|
|
{
|
|
|
|
if( !io )
|
|
|
|
return 0;
|
|
|
|
|
2005-10-07 14:08:57 +00:00
|
|
|
if( ( io->io_mode == IO_BUFFER ) ||
|
|
|
|
( io->io_mode == IO_PIPE ) )
|
2005-10-03 13:09:37 +00:00
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
if( io->param1.pipe_fd[0] == fd ||
|
|
|
|
io->param1.pipe_fd[1] == fd )
|
2005-10-03 13:09:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2005-10-07 14:08:57 +00:00
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
return use_fd_in_pipe( fd, io->next );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
\param io the list of io redirections for this job. Pipes mentioned here should not be closed.
|
|
|
|
*/
|
|
|
|
static void close_unused_internal_pipes( io_data_t *io )
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
|
|
|
|
if( open_fds )
|
|
|
|
{
|
|
|
|
for( ;i<al_get_count( open_fds ); i++ )
|
|
|
|
{
|
|
|
|
int n = (int)(long)al_get( open_fds, i );
|
|
|
|
if( !use_fd_in_pipe( n, io) )
|
|
|
|
{
|
2005-10-03 13:58:48 +00:00
|
|
|
debug( 4, L"Close fd %d, used in other context", n );
|
2005-10-08 11:20:51 +00:00
|
|
|
exec_close( n );
|
2005-10-03 13:09:37 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void exec_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void exec_destroy()
|
|
|
|
{
|
|
|
|
if( open_fds )
|
|
|
|
{
|
|
|
|
al_destroy( open_fds );
|
|
|
|
free( open_fds );
|
|
|
|
}
|
2005-09-23 13:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Make sure the fd used by this redirection is not used by i.e. a pipe.
|
|
|
|
*/
|
|
|
|
void free_fd( io_data_t *io, int fd )
|
|
|
|
{
|
|
|
|
if( !io )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( io->io_mode == IO_PIPE )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0; i<2; i++ )
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
if(io->param1.pipe_fd[i] == fd )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
if( (io->param1.pipe_fd[i] = dup(fd)) == -1)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
|
|
|
debug( 1,
|
|
|
|
FD_ERROR,
|
|
|
|
fd );
|
|
|
|
wperror( L"dup" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-21 11:59:45 +00:00
|
|
|
/**
|
|
|
|
Set up a childs io redirections. Should only be called by
|
|
|
|
setup_child_process(). Does the following: First it closes any open
|
|
|
|
file descriptors not related to the child by calling
|
|
|
|
close_unused_internal_pipes() and closing the universal variable
|
|
|
|
server file descriptor. It then goes on to perform all the
|
|
|
|
redirections described by \c io.
|
|
|
|
|
|
|
|
\param io the list of IO redirections for the child
|
2006-01-15 22:41:48 +00:00
|
|
|
\param exit_on_error whether to call exit() on errors
|
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
\return 0 on sucess, -1 on failiure
|
2005-10-21 11:59:45 +00:00
|
|
|
*/
|
2006-01-15 22:41:48 +00:00
|
|
|
static int handle_child_io( io_data_t *io, int exit_on_error )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
close_unused_internal_pipes( io );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
for( ; io; io=io->next )
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
if( io->io_mode == IO_FD && io->fd == io->param1.old_fd )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( io->fd > 2 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Make sure the fd used by this redirection is not used by i.e. a pipe.
|
|
|
|
*/
|
|
|
|
free_fd( io, io->fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( io->io_mode )
|
|
|
|
{
|
|
|
|
case IO_CLOSE:
|
2005-11-29 10:12:06 +00:00
|
|
|
close(io->fd);
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
case IO_FILE:
|
2005-11-29 10:12:06 +00:00
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
if( (tmp=wopen( io->param1.filename,
|
2005-10-22 10:06:05 +00:00
|
|
|
io->param2.flags, 0777 ) )==-1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1,
|
|
|
|
FILE_ERROR,
|
2005-10-11 19:31:16 +00:00
|
|
|
io->param1.filename );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
wperror( L"open" );
|
2006-01-15 22:41:48 +00:00
|
|
|
if( exit_on_error )
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
return -1;
|
2006-01-15 22:41:48 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else if( tmp != io->fd)
|
|
|
|
{
|
2005-11-29 10:12:06 +00:00
|
|
|
close(io->fd);
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if(dup2( tmp, io->fd ) == -1 )
|
|
|
|
{
|
|
|
|
debug( 1,
|
|
|
|
FD_ERROR,
|
|
|
|
io->fd );
|
|
|
|
wperror( L"dup2" );
|
2006-01-15 22:41:48 +00:00
|
|
|
if( exit_on_error )
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
return -1;
|
2006-01-15 22:41:48 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2005-10-08 11:20:51 +00:00
|
|
|
exec_close( tmp );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-11-29 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case IO_FD:
|
2005-11-29 10:12:06 +00:00
|
|
|
{
|
|
|
|
close(io->fd);
|
2006-01-15 22:41:48 +00:00
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
if( dup2( io->param1.old_fd, io->fd ) == -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1,
|
|
|
|
FD_ERROR,
|
|
|
|
io->fd );
|
|
|
|
wperror( L"dup2" );
|
2006-01-15 22:41:48 +00:00
|
|
|
if( exit_on_error )
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
return -1;
|
2006-01-15 22:41:48 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-11-29 10:12:06 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
case IO_BUFFER:
|
|
|
|
case IO_PIPE:
|
|
|
|
{
|
2005-10-07 14:08:57 +00:00
|
|
|
int fd_to_dup = io->fd;
|
2006-01-15 22:41:48 +00:00
|
|
|
|
|
|
|
close(io->fd);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
if( dup2( io->param1.pipe_fd[fd_to_dup?1:0], io->fd ) == -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1, PIPE_ERROR );
|
|
|
|
wperror( L"dup2" );
|
2006-01-15 22:41:48 +00:00
|
|
|
if( exit_on_error )
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
return -1;
|
2006-01-15 22:41:48 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 14:08:57 +00:00
|
|
|
if( fd_to_dup != 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
exec_close( io->param1.pipe_fd[0]);
|
|
|
|
exec_close( io->param1.pipe_fd[1]);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
2006-01-15 22:41:48 +00:00
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
exec_close( io->param1.pipe_fd[0] );
|
2006-01-15 22:41:48 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2006-01-15 22:41:48 +00:00
|
|
|
|
|
|
|
if( env_universal_server.fd >= 0 )
|
|
|
|
exec_close( env_universal_server.fd );
|
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
return 0;
|
2006-01-15 22:41:48 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-10-09 11:48:16 +00:00
|
|
|
/**
|
|
|
|
Initialize a new child process. This should be called right away
|
|
|
|
after forking in the child process. If job control is suitable, the
|
|
|
|
process is put in the jobs group, all signal handlers are reset,
|
2005-10-21 11:59:45 +00:00
|
|
|
SIGCHLD is unblocked (the exec call blocks blocks SIGCHLD), and all
|
|
|
|
IO redirections and other file descriptor actions are performed.
|
2006-01-15 22:41:48 +00:00
|
|
|
|
|
|
|
\param j the job to set up the IO for
|
2006-01-23 20:40:14 +00:00
|
|
|
\param p the child process to set up
|
2006-01-15 22:41:48 +00:00
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
\return 0 on sucess, -1 on failiure
|
2005-10-09 11:48:16 +00:00
|
|
|
*/
|
2006-01-16 00:15:56 +00:00
|
|
|
static int setup_child_process( job_t *j, process_t *p )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
int res=0;
|
2006-02-03 17:27:36 +00:00
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
if( p )
|
|
|
|
{
|
2006-02-04 11:33:20 +00:00
|
|
|
res = set_child_group( j, p, 1 );
|
2006-02-01 12:27:15 +00:00
|
|
|
}
|
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
if( !res )
|
2006-02-01 12:27:15 +00:00
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
res = handle_child_io( j->io, (p==0) );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-01-15 22:41:48 +00:00
|
|
|
/* Set the handling for job control signals back to the default. */
|
2006-02-03 21:50:31 +00:00
|
|
|
if( !res )
|
2006-01-15 22:41:48 +00:00
|
|
|
{
|
|
|
|
signal_reset_handlers();
|
|
|
|
}
|
|
|
|
|
2005-10-14 11:40:33 +00:00
|
|
|
/* Remove all signal blocks */
|
|
|
|
signal_unblock();
|
|
|
|
|
2006-01-15 22:41:48 +00:00
|
|
|
return res;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
static void launch_process( process_t *p )
|
|
|
|
{
|
2006-01-18 12:42:48 +00:00
|
|
|
// debug( 1, L"exec '%ls'", p->argv[0] );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
execve (wcs2str(p->actual_cmd), wcsv2strv( (const wchar_t **) p->argv), env_export_arr( 0 ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
debug( 0,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Failed to execute process '%ls'" ),
|
2005-09-20 13:26:39 +00:00
|
|
|
p->actual_cmd );
|
|
|
|
wperror( L"execve" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
static int has_fd( io_data_t *d, int fd )
|
|
|
|
{
|
|
|
|
return io_get( d, fd ) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-11 14:40:20 +00:00
|
|
|
/**
|
|
|
|
Free a transmogrified io chain. Only the chain itself and resources
|
|
|
|
used by a transmogrified IO_FILE redirection are freed, since the
|
|
|
|
original chain may still be needed.
|
|
|
|
*/
|
|
|
|
static void io_untransmogrify( io_data_t * in, io_data_t *out )
|
|
|
|
{
|
|
|
|
if( !out )
|
|
|
|
return;
|
|
|
|
io_untransmogrify( in->next, out->next );
|
|
|
|
switch( in->io_mode )
|
|
|
|
{
|
|
|
|
case IO_FILE:
|
|
|
|
exec_close( out->param1.old_fd );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2005-10-09 11:48:16 +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.
|
2006-01-11 14:40:20 +00:00
|
|
|
|
|
|
|
\return the transmogrified chain on sucess, or 0 on failiure
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
static io_data_t *io_transmogrify( io_data_t * in )
|
|
|
|
{
|
|
|
|
io_data_t *out;
|
|
|
|
|
|
|
|
if( !in )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out = malloc( sizeof( io_data_t ) );
|
|
|
|
if( !out )
|
|
|
|
die_mem();
|
|
|
|
|
|
|
|
out->fd = in->fd;
|
|
|
|
out->io_mode = IO_FD;
|
2005-10-11 19:31:16 +00:00
|
|
|
out->param2.close_old = 1;
|
2006-01-11 14:40:20 +00:00
|
|
|
out->next=0;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
switch( in->io_mode )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
These redirections don't need transmogrification. They can be passed through.
|
|
|
|
*/
|
|
|
|
case IO_FD:
|
|
|
|
case IO_CLOSE:
|
|
|
|
case IO_BUFFER:
|
|
|
|
case IO_PIPE:
|
|
|
|
{
|
|
|
|
memcpy( out, in, sizeof(io_data_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Transmogrify file redirections
|
|
|
|
*/
|
|
|
|
case IO_FILE:
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2005-10-22 10:06:05 +00:00
|
|
|
if( (fd=wopen( in->param1.filename, in->param2.flags, 0777 ) )==-1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1,
|
|
|
|
FILE_ERROR,
|
2005-10-11 19:31:16 +00:00
|
|
|
in->param1.filename );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
wperror( L"open" );
|
2006-01-11 14:40:20 +00:00
|
|
|
free( out );
|
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
out->param1.old_fd = fd;
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-11 14:40:20 +00:00
|
|
|
if( in->next)
|
|
|
|
{
|
|
|
|
out->next = io_transmogrify( in->next );
|
|
|
|
if( !out->next )
|
|
|
|
{
|
|
|
|
io_untransmogrify( in, out );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2006-01-11 14:40:20 +00:00
|
|
|
static void internal_exec_helper( const wchar_t *def,
|
2005-09-20 13:26:39 +00:00
|
|
|
int block_type,
|
|
|
|
io_data_t *io )
|
|
|
|
{
|
|
|
|
io_data_t *io_internal = io_transmogrify( io );
|
|
|
|
int is_block_old=is_block;
|
|
|
|
is_block=1;
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2006-01-11 14:40:20 +00:00
|
|
|
/*
|
|
|
|
Did the transmogrification fail - if so, set error status and return
|
|
|
|
*/
|
|
|
|
if( io && !io_internal )
|
|
|
|
{
|
|
|
|
proc_set_last_status( 1 );
|
|
|
|
return;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-01-11 14:40:20 +00:00
|
|
|
signal_unblock();
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
eval( def, io_internal, block_type );
|
2006-01-11 14:40:20 +00:00
|
|
|
|
2005-10-14 11:40:33 +00:00
|
|
|
signal_block();
|
2006-01-11 14:40:20 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
io_untransmogrify( io, io_internal );
|
2005-10-11 19:23:43 +00:00
|
|
|
job_reap( 0 );
|
2005-09-20 13:26:39 +00:00
|
|
|
is_block=is_block_old;
|
|
|
|
}
|
|
|
|
|
2005-10-09 11:48:16 +00:00
|
|
|
/**
|
2006-02-04 11:33:20 +00:00
|
|
|
This function should be called by both the parent process and the
|
|
|
|
child right after fork() has been called. If job control is
|
|
|
|
enabled, the child is put in the jobs group, and if the child is
|
|
|
|
also in the foreground, it is also given control of the
|
|
|
|
terminal. When called in the parent process, this function may
|
|
|
|
fail, since the child might have already finished and called
|
|
|
|
exit. The parent process may safely ignore the exit status of this
|
|
|
|
call.
|
|
|
|
|
|
|
|
Returns 0 on sucess, -1 on failiure.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-02-04 11:33:20 +00:00
|
|
|
static int set_child_group( job_t *j, process_t *p, int print_errors )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-02-03 21:50:31 +00:00
|
|
|
int res = 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-01-30 17:54:26 +00:00
|
|
|
if( j->job_control )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int new_pgid=0;
|
|
|
|
|
|
|
|
if (!j->pgid)
|
|
|
|
{
|
|
|
|
new_pgid=1;
|
|
|
|
j->pgid = p->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( setpgid (p->pid, j->pgid) )
|
|
|
|
{
|
2006-02-04 11:33:20 +00:00
|
|
|
if( getpgid( p->pid) != j->pgid && print_errors )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Could not send process %d from group %d to group %d" ),
|
2005-09-20 13:26:39 +00:00
|
|
|
p->pid,
|
|
|
|
getpgid( p->pid),
|
|
|
|
j->pgid );
|
|
|
|
wperror( L"setpgid" );
|
2006-02-03 21:50:31 +00:00
|
|
|
res = -1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j->pgid = getpid();
|
|
|
|
}
|
2006-02-04 11:33:20 +00:00
|
|
|
|
2006-02-01 12:27:15 +00:00
|
|
|
if( j->terminal && j->fg )
|
|
|
|
{
|
2006-02-04 11:33:20 +00:00
|
|
|
if( tcsetpgrp (0, j->pgid) && print_errors )
|
2006-02-01 12:27:15 +00:00
|
|
|
{
|
|
|
|
debug( 1, _( L"Could not send job %d ('%ls') to foreground" ),
|
|
|
|
j->job_id,
|
|
|
|
j->command );
|
|
|
|
wperror( L"tcsetpgrp" );
|
2006-02-03 21:50:31 +00:00
|
|
|
res = -1;
|
2006-02-01 12:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-03 21:50:31 +00:00
|
|
|
return res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void exec( job_t *j )
|
|
|
|
{
|
|
|
|
process_t *p;
|
|
|
|
pid_t pid;
|
|
|
|
int mypipe[2];
|
|
|
|
sigset_t chldset;
|
|
|
|
sigemptyset( &chldset );
|
|
|
|
sigaddset( &chldset, SIGCHLD );
|
|
|
|
int skip_fork;
|
|
|
|
|
|
|
|
io_data_t pipe_read, pipe_write;
|
|
|
|
io_data_t *tmp;
|
|
|
|
|
|
|
|
io_data_t *io_buffer =0;
|
2005-10-17 13:24:12 +00:00
|
|
|
|
2005-10-20 23:56:51 +00:00
|
|
|
/*
|
|
|
|
Set to 1 if something goes wrong while exec:ing the job, in which case the cleanup code will kick in.
|
|
|
|
*/
|
|
|
|
int exec_error=0;
|
|
|
|
|
2005-10-17 13:24:12 +00:00
|
|
|
|
2006-01-18 12:42:48 +00:00
|
|
|
debug( 4, L"Exec job '%ls' with id %d", j->command, j->job_id );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
if( j->first_process->type==INTERNAL_EXEC )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Do a regular launch - but without forking first...
|
|
|
|
*/
|
2005-10-14 11:40:33 +00:00
|
|
|
signal_block();
|
|
|
|
|
|
|
|
/*
|
2006-02-05 13:12:53 +00:00
|
|
|
setup_child_process make sure signals are properly set
|
|
|
|
up. It will also call signal_unblock
|
2005-10-14 11:40:33 +00:00
|
|
|
*/
|
2006-02-03 21:50:31 +00:00
|
|
|
if( !setup_child_process( j, 0 ) )
|
2006-01-15 22:41:48 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
launch_process never returns
|
|
|
|
*/
|
|
|
|
launch_process( j->first_process );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j->constructed=1;
|
|
|
|
j->first_process->completed=1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-01-18 12:42:48 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
pipe_read.fd=0;
|
|
|
|
pipe_write.fd=1;
|
|
|
|
pipe_read.io_mode=IO_PIPE;
|
2005-10-11 19:31:16 +00:00
|
|
|
pipe_read.param1.pipe_fd[0] = -1;
|
|
|
|
pipe_read.param1.pipe_fd[1] = -1;
|
2005-09-20 13:26:39 +00:00
|
|
|
pipe_write.io_mode=IO_PIPE;
|
|
|
|
pipe_read.next=0;
|
|
|
|
pipe_write.next=0;
|
2005-11-02 15:41:59 +00:00
|
|
|
pipe_write.param1.pipe_fd[0]=pipe_write.param1.pipe_fd[1]=-1;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
//fwprintf( stderr, L"Run command %ls\n", j->command );
|
|
|
|
|
|
|
|
if( block_io )
|
|
|
|
{
|
|
|
|
if( j->io )
|
|
|
|
j->io = io_add( io_duplicate(block_io), j->io );
|
|
|
|
else
|
|
|
|
j->io=io_duplicate(block_io);
|
|
|
|
}
|
|
|
|
|
|
|
|
j->io = io_add( j->io, &pipe_write );
|
|
|
|
|
2005-10-14 11:40:33 +00:00
|
|
|
signal_block();
|
|
|
|
|
2005-10-21 11:59:45 +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.
|
2005-10-20 23:56:51 +00:00
|
|
|
|
2005-10-21 11:59:45 +00:00
|
|
|
The loop also has to handle pipelining between the jobs.
|
|
|
|
*/
|
2005-10-20 23:56:51 +00:00
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
for( p=j->first_process; p; p = p->next )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
mypipe[1]=-1;
|
|
|
|
skip_fork=0;
|
2005-09-22 20:16:52 +00:00
|
|
|
|
2005-10-07 14:08:57 +00:00
|
|
|
pipe_write.fd = p->pipe_fd;
|
|
|
|
|
2005-10-03 13:09:37 +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
|
|
|
|
*/
|
2005-09-22 20:16:52 +00:00
|
|
|
if( p->type == EXTERNAL )
|
|
|
|
env_export_arr( 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-03 13:09:37 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Set up fd:s that will be used in the pipe
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( p == j->first_process->next )
|
|
|
|
{
|
|
|
|
j->io = io_add( j->io, &pipe_read );
|
|
|
|
}
|
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
if( p->next )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-11-02 15:41:59 +00:00
|
|
|
// debug( 1, L"%ls|%ls" , p->argv[0], p->next->argv[0]);
|
|
|
|
|
|
|
|
if( exec_pipe( mypipe ) == -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
debug( 1, PIPE_ERROR );
|
|
|
|
wperror (L"pipe");
|
2005-10-20 23:56:51 +00:00
|
|
|
exec_error=1;
|
|
|
|
break;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fwprintf( stderr,
|
|
|
|
L"Make pipe from %ls to %ls using fds %d and %d\n",
|
|
|
|
p->actual_cmd,
|
|
|
|
p->next->actual_cmd,
|
|
|
|
mypipe[0],
|
|
|
|
mypipe[1] );
|
|
|
|
*/
|
2005-10-11 19:31:16 +00:00
|
|
|
memcpy( pipe_write.param1.pipe_fd, mypipe, sizeof(int)*2);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the last element of the pipeline.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove the io redirection for pipe output.
|
|
|
|
*/
|
|
|
|
j->io = io_remove( j->io, &pipe_write );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( p->type )
|
|
|
|
{
|
|
|
|
case INTERNAL_FUNCTION:
|
|
|
|
{
|
|
|
|
wchar_t **arg;
|
|
|
|
int i;
|
|
|
|
string_buffer_t sb;
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
const wchar_t * def = function_get_definition( p->argv[0] );
|
2006-02-01 15:49:11 +00:00
|
|
|
//fwprintf( stderr, L"run function %ls\n", argv[0] );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( def == 0 )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
debug( 0, _( L"Unknown function '%ls'" ), p->argv[0] );
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
parser_push_block( FUNCTION_CALL );
|
|
|
|
|
2006-02-01 15:49:11 +00:00
|
|
|
current_block->param2.function_call_process = p;
|
2006-01-26 14:48:10 +00:00
|
|
|
current_block->param1.function_name = wcsdup( p->argv[0] );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( builtin_count_args(p->argv)>1 )
|
|
|
|
{
|
|
|
|
sb_init( &sb );
|
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
for( i=1, arg=p->argv+1; *arg; i++, arg++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
if( i != 1 )
|
|
|
|
sb_append( &sb, ARRAY_SEP_STR );
|
|
|
|
sb_append( &sb, *arg );
|
|
|
|
}
|
|
|
|
|
|
|
|
env_set( L"argv", (wchar_t *)sb.buff, ENV_LOCAL );
|
|
|
|
sb_destroy( &sb );
|
|
|
|
}
|
2006-01-26 14:48:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
env_set( L"argv", 0, ENV_LOCAL );
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
parser_forbid_function( p->argv[0] );
|
|
|
|
|
|
|
|
if( p->next )
|
|
|
|
{
|
|
|
|
// fwprintf( stderr, L"Function %ls\n", def );
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer = io_buffer_create();
|
2005-09-20 13:26:39 +00:00
|
|
|
j->io = io_add( j->io, io_buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_exec_helper( def, TOP, j->io );
|
|
|
|
|
|
|
|
parser_allow_function();
|
|
|
|
parser_pop_block();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case INTERNAL_BLOCK:
|
|
|
|
{
|
|
|
|
if( p->next )
|
|
|
|
{
|
|
|
|
// fwprintf( stderr, L"Block %ls\n", p->argv[0] );
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer = io_buffer_create();
|
2005-09-20 13:26:39 +00:00
|
|
|
j->io = io_add( j->io, io_buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_exec_helper( p->argv[0], TOP, j->io );
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case INTERNAL_BUILTIN:
|
|
|
|
{
|
|
|
|
int builtin_stdin=0;
|
|
|
|
int fg;
|
|
|
|
int close_stdin=0;
|
|
|
|
|
|
|
|
if( p == j->first_process )
|
|
|
|
{
|
|
|
|
io_data_t *in = io_get( j->io, 0 );
|
|
|
|
if( in )
|
|
|
|
{
|
|
|
|
switch( in->io_mode )
|
|
|
|
{
|
|
|
|
|
|
|
|
case IO_FD:
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
builtin_stdin = in->param1.old_fd;
|
2005-09-20 13:26:39 +00:00
|
|
|
/* fwprintf( stderr,
|
|
|
|
L"Input redirection for builtin '%ls' from fd %d\n",
|
|
|
|
p->argv[0],
|
|
|
|
in->old_fd );
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IO_PIPE:
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
builtin_stdin = in->param1.pipe_fd[0];
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IO_FILE:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
fwprintf( stderr,
|
|
|
|
L"Input redirection for builtin from file %ls\n",
|
|
|
|
in->filename);
|
|
|
|
*/
|
2005-10-20 23:56:51 +00:00
|
|
|
builtin_stdin=wopen( in->param1.filename,
|
2005-10-22 10:06:05 +00:00
|
|
|
in->param2.flags, 0777 );
|
2005-10-20 23:56:51 +00:00
|
|
|
if( builtin_stdin == -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-10-20 23:56:51 +00:00
|
|
|
debug( 1,
|
|
|
|
FILE_ERROR,
|
|
|
|
in->param1.filename );
|
|
|
|
wperror( L"open" );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_stdin = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2005-10-20 23:56:51 +00:00
|
|
|
builtin_stdin=-1;
|
2005-09-20 13:26:39 +00:00
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Unknown input redirection type %d" ),
|
2005-09-20 13:26:39 +00:00
|
|
|
in->io_mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
builtin_stdin = pipe_read.param1.pipe_fd[0];
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2005-10-20 23:56:51 +00:00
|
|
|
if( builtin_stdin == -1 )
|
|
|
|
{
|
|
|
|
exec_error=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
builtin_push_io( builtin_stdin );
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
builtin_out_redirect = has_fd( j->io, 1 );
|
|
|
|
builtin_err_redirect = has_fd( j->io, 2 );
|
|
|
|
fg = j->fg;
|
|
|
|
j->fg = 0;
|
2005-10-14 11:40:33 +00:00
|
|
|
|
|
|
|
signal_unblock();
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
p->status = builtin_run( p->argv );
|
2005-10-14 11:40:33 +00:00
|
|
|
|
|
|
|
signal_block();
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Restore the fg flag, which is temporarily set to
|
|
|
|
false during builtin execution so as not to confuse
|
|
|
|
some job-handling builtins.
|
|
|
|
*/
|
|
|
|
j->fg = fg;
|
|
|
|
|
|
|
|
|
|
|
|
/* if( is_interactive )
|
|
|
|
fwprintf( stderr, L"Builtin '%ls' finished\n", j->command );
|
|
|
|
*/
|
|
|
|
if( close_stdin )
|
|
|
|
{
|
|
|
|
// fwprintf( stderr, L"Close builtin_stdin\n" );
|
2005-10-08 11:20:51 +00:00
|
|
|
exec_close( builtin_stdin );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-10-20 23:56:51 +00:00
|
|
|
|
|
|
|
if( exec_error )
|
|
|
|
break;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
switch( p->type )
|
|
|
|
{
|
|
|
|
case INTERNAL_BLOCK:
|
|
|
|
case INTERNAL_FUNCTION:
|
|
|
|
{
|
|
|
|
int status = proc_get_last_status();
|
2005-11-29 19:50:30 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Handle output from a block or function. This usually
|
|
|
|
means do nothing, but in the case of pipes, we have
|
2005-11-29 19:50:30 +00:00
|
|
|
to buffer such io, since otherwise the internal pipe
|
2005-09-20 13:26:39 +00:00
|
|
|
buffer might overflow.
|
|
|
|
*/
|
2005-11-02 15:41:59 +00:00
|
|
|
if( !io_buffer )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-11-29 19:50:30 +00:00
|
|
|
/*
|
2006-01-29 22:04:14 +00:00
|
|
|
No buffer, so we exit directly. This means we
|
2005-11-29 19:50:30 +00:00
|
|
|
have to manually set the exit status.
|
|
|
|
*/
|
|
|
|
if( p->next == 0 )
|
|
|
|
{
|
|
|
|
proc_set_last_status( j->negate?(status?0:1):status);
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
p->completed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
j->io = io_remove( j->io, io_buffer );
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_read( io_buffer );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
if( io_buffer->param2.out_buffer->used != 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-11-02 15:41:59 +00:00
|
|
|
pid = fork();
|
2006-01-18 12:42:48 +00:00
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
if( pid == 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the child process. Write out the contents of the pipeline.
|
|
|
|
*/
|
|
|
|
p->pid = getpid();
|
2006-01-16 00:15:56 +00:00
|
|
|
setup_child_process( j, p );
|
2005-09-20 13:26:39 +00:00
|
|
|
write( io_buffer->fd,
|
2005-10-11 19:31:16 +00:00
|
|
|
io_buffer->param2.out_buffer->buff,
|
|
|
|
io_buffer->param2.out_buffer->used );
|
2005-09-20 13:26:39 +00:00
|
|
|
exit( status );
|
|
|
|
}
|
2005-11-02 15:41:59 +00:00
|
|
|
else if( pid < 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/* The fork failed. */
|
|
|
|
debug( 0, FORK_ERROR );
|
|
|
|
wperror (L"fork");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the parent process. Store away
|
2005-10-14 11:40:33 +00:00
|
|
|
information on the child, and possibly give
|
2005-09-20 13:26:39 +00:00
|
|
|
it control over the terminal.
|
|
|
|
*/
|
|
|
|
p->pid = pid;
|
2006-02-04 11:33:20 +00:00
|
|
|
set_child_group( j, p, 0 );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-01-29 22:04:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( p->next == 0 )
|
|
|
|
{
|
|
|
|
proc_set_last_status( j->negate?(status?0:1):status);
|
|
|
|
}
|
|
|
|
p->completed = 1;
|
|
|
|
}
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_destroy( io_buffer );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
io_buffer=0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case INTERNAL_BUILTIN:
|
|
|
|
{
|
|
|
|
int skip_fork=0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
If a builtin didn't produce any output, and it is not inside a pipeline, there is no need to fork
|
|
|
|
*/
|
|
|
|
skip_fork =
|
|
|
|
( !sb_out->used ) &&
|
|
|
|
( !sb_err->used ) &&
|
|
|
|
( !p->next );
|
|
|
|
|
|
|
|
/*
|
|
|
|
If the output of a builtin is to be sent to an internal
|
|
|
|
buffer, there is no need to fork. This helps out the
|
|
|
|
performance quite a bit in complex completion code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
io_data_t *io = io_get( j->io, 1 );
|
|
|
|
int buffer_stdout = io && io->io_mode == IO_BUFFER;
|
2005-11-02 15:41:59 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( ( !sb_err->used ) &&
|
|
|
|
( !p->next ) &&
|
|
|
|
( sb_out->used ) &&
|
|
|
|
( buffer_stdout ) )
|
|
|
|
{
|
|
|
|
// fwprintf( stderr, L"Skip fork of %ls\n", j->command );
|
|
|
|
char *res = wcs2str( (wchar_t *)sb_out->buff );
|
2005-10-11 19:31:16 +00:00
|
|
|
b_append( io->param2.out_buffer, res, strlen( res ) );
|
2005-09-20 13:26:39 +00:00
|
|
|
skip_fork = 1;
|
|
|
|
free( res );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( skip_fork )
|
|
|
|
{
|
|
|
|
p->completed=1;
|
|
|
|
if( p->next == 0 )
|
|
|
|
{
|
|
|
|
debug( 3, L"Set status of %ls to %d using short circut", j->command, p->status );
|
|
|
|
|
|
|
|
proc_set_last_status( j->negate?(p->status?0:1):p->status );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
pid = fork();
|
|
|
|
if( pid == 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the child process.
|
|
|
|
*/
|
|
|
|
p->pid = getpid();
|
2006-01-16 00:15:56 +00:00
|
|
|
setup_child_process( j, p );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( sb_out->used )
|
|
|
|
fwprintf( stdout, L"%ls", sb_out->buff );
|
|
|
|
if( sb_err->used )
|
|
|
|
fwprintf( stderr, L"%ls", sb_err->buff );
|
|
|
|
|
|
|
|
exit( p->status );
|
|
|
|
|
|
|
|
}
|
2005-11-02 15:41:59 +00:00
|
|
|
else if( pid < 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/* The fork failed. */
|
|
|
|
debug( 0, FORK_ERROR );
|
|
|
|
wperror (L"fork");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the parent process. Store away
|
|
|
|
information on the child, and possibly fice
|
|
|
|
it control over the terminal.
|
|
|
|
*/
|
|
|
|
p->pid = pid;
|
|
|
|
|
2006-02-04 11:33:20 +00:00
|
|
|
set_child_group( j, p, 0 );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EXTERNAL:
|
|
|
|
{
|
2005-11-29 19:50:30 +00:00
|
|
|
pid = fork();
|
2005-11-02 15:41:59 +00:00
|
|
|
if( pid == 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the child process.
|
|
|
|
*/
|
|
|
|
p->pid = getpid();
|
2006-01-16 00:15:56 +00:00
|
|
|
setup_child_process( j, p );
|
2005-09-20 13:26:39 +00:00
|
|
|
launch_process( p );
|
|
|
|
|
|
|
|
/*
|
|
|
|
launch_process _never_ returns...
|
|
|
|
*/
|
|
|
|
}
|
2005-11-02 15:41:59 +00:00
|
|
|
else if( pid < 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/* The fork failed. */
|
|
|
|
debug( 0, FORK_ERROR );
|
2005-11-29 19:50:30 +00:00
|
|
|
wperror( L"fork" );
|
|
|
|
exit( 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the parent process. Store away
|
|
|
|
information on the child, and possibly fice
|
|
|
|
it control over the terminal.
|
|
|
|
*/
|
|
|
|
p->pid = pid;
|
|
|
|
|
2006-02-04 11:33:20 +00:00
|
|
|
set_child_group( j, p, 0 );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-11-02 15:41:59 +00:00
|
|
|
if( p->type == INTERNAL_BUILTIN )
|
2006-02-04 11:33:20 +00:00
|
|
|
builtin_pop_io();
|
2005-11-02 15:41:59 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Close the pipe the current process uses to read from the previous process_t
|
|
|
|
*/
|
2005-10-11 19:31:16 +00:00
|
|
|
if( pipe_read.param1.pipe_fd[0] >= 0 )
|
|
|
|
exec_close( pipe_read.param1.pipe_fd[0] );
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
Set up the pipe the next process uses to read from the current process_t
|
|
|
|
*/
|
|
|
|
if( p->next )
|
2005-10-11 19:31:16 +00:00
|
|
|
pipe_read.param1.pipe_fd[0] = mypipe[0];
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If there is a next process, close the output end of the
|
|
|
|
pipe (the child subprocess already has a copy of the pipe).
|
|
|
|
*/
|
|
|
|
if( p->next )
|
|
|
|
{
|
2005-10-08 11:20:51 +00:00
|
|
|
exec_close(mypipe[1]);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-21 11:59:45 +00:00
|
|
|
signal_unblock();
|
2005-10-20 23:56:51 +00:00
|
|
|
|
2005-09-23 13:10:31 +00:00
|
|
|
debug( 3, L"Job is constructed" );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
j->io = io_remove( j->io, &pipe_read );
|
|
|
|
|
|
|
|
for( tmp = block_io; tmp; tmp=tmp->next )
|
|
|
|
j->io = io_remove( j->io, tmp );
|
|
|
|
|
|
|
|
j->constructed = 1;
|
|
|
|
|
|
|
|
if( !j->fg )
|
|
|
|
{
|
|
|
|
proc_last_bg_pid = j->pgid;
|
|
|
|
}
|
|
|
|
|
2005-10-20 23:56:51 +00:00
|
|
|
if( !exec_error )
|
|
|
|
{
|
|
|
|
job_continue (j, 0);
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
debug( 3, L"End of exec()" );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int exec_subshell( const wchar_t *cmd,
|
|
|
|
array_list_t *l )
|
|
|
|
{
|
|
|
|
char *begin, *end;
|
|
|
|
char z=0;
|
|
|
|
int prev_subshell = is_subshell;
|
|
|
|
int status, prev_status;
|
|
|
|
io_data_t *io_buffer;
|
|
|
|
|
|
|
|
if( !cmd )
|
|
|
|
{
|
2005-10-09 11:48:16 +00:00
|
|
|
debug( 1,
|
2006-01-04 12:51:02 +00:00
|
|
|
_( L"Sent null command to subshell. This is a fish bug. If it can be reproduced, please send a bug report to %s." ),
|
2005-10-09 11:48:16 +00:00
|
|
|
PACKAGE_BUGREPORT );
|
2005-09-20 13:26:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-14 11:40:33 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
is_subshell=1;
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer= io_buffer_create();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
prev_status = proc_get_last_status();
|
|
|
|
|
|
|
|
eval( cmd, io_buffer, SUBST );
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_read( io_buffer );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
status = proc_get_last_status();
|
|
|
|
proc_set_last_status( prev_status );
|
|
|
|
|
|
|
|
is_subshell = prev_subshell;
|
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
b_append( io_buffer->param2.out_buffer, &z, 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
begin=end=io_buffer->param2.out_buffer->buff;
|
2005-11-02 15:41:59 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( l )
|
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
switch( *end )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
if( begin != end )
|
|
|
|
{
|
|
|
|
wchar_t *el = str2wcs( begin );
|
|
|
|
if( el )
|
|
|
|
al_push( l, el );
|
|
|
|
}
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_destroy( io_buffer );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
{
|
|
|
|
wchar_t *el;
|
|
|
|
*end=0;
|
2006-01-28 02:03:29 +00:00
|
|
|
el = str2wcs( begin );
|
|
|
|
al_push( l, el );
|
2005-09-20 13:26:39 +00:00
|
|
|
begin = end+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_buffer_destroy( io_buffer );
|
2006-01-18 12:42:48 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return status;
|
|
|
|
}
|