2005-09-20 13:26:39 +00:00
/** \file proc.c
Utilities for keeping track of jobs , processes and subshells , as
well as signal handling functions for tracking children . These
functions do not themselves launch new processes , the exec library
will call proc to create representations of the running jobs as
needed .
Some of the code in this file is based on code from the Glibc manual .
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
*/
# include "config.h"
# include <stdlib.h>
# include <stdio.h>
# include <sys/wait.h>
# include <wchar.h>
# include <string.h>
# include <errno.h>
# include <termios.h>
# include <sys/types.h>
# include <sys/stat.h>
2012-01-30 16:15:02 +00:00
# include <algorithm>
2006-08-09 22:53:38 +00:00
# ifdef HAVE_SYS_TERMIOS_H
# include <sys/termios.h>
# endif
# ifdef HAVE_SYS_IOCTL_H
2005-09-20 13:26:39 +00:00
# include <sys/ioctl.h>
2006-08-09 22:53:38 +00:00
# endif
2005-09-20 13:26:39 +00:00
# include <unistd.h>
# include <signal.h>
# include <dirent.h>
# include <sys/time.h>
# if HAVE_NCURSES_H
# include <ncurses.h>
# else
# include <curses.h>
# endif
# if HAVE_TERMIO_H
# include <termio.h>
# endif
2006-01-19 12:22:07 +00:00
# if HAVE_TERM_H
2005-09-20 13:26:39 +00:00
# include <term.h>
2006-01-19 12:22:07 +00:00
# elif HAVE_NCURSES_TERM_H
# include <ncurses/term.h>
# endif
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
2006-08-09 22:26:05 +00:00
# ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
# endif
2006-02-28 13:17:16 +00:00
# include "fallback.h"
2005-09-20 13:26:39 +00:00
# include "util.h"
2006-02-28 13:17:16 +00:00
2005-09-20 13:26:39 +00:00
# include "wutil.h"
# include "proc.h"
# include "common.h"
# include "reader.h"
# include "sanity.h"
# include "env.h"
2005-09-22 20:16:52 +00:00
# include "parser.h"
2005-10-05 22:37:08 +00:00
# include "signal.h"
2005-10-11 19:23:43 +00:00
# include "event.h"
2006-07-19 22:55:49 +00:00
2012-02-10 03:26:44 +00:00
# include <deque>
2006-02-16 13:36:32 +00:00
# include "output.h"
2005-09-20 13:26:39 +00:00
/**
2011-12-27 03:18:46 +00:00
Size of message buffer
2005-09-20 13:26:39 +00:00
*/
# define MESS_SIZE 256
/**
Size of buffer for reading buffered output
*/
# define BUFFER_SIZE 4096
2011-12-27 03:18:46 +00:00
/**
Status of last process to exit
2006-01-16 00:15:56 +00:00
*/
2005-09-20 13:26:39 +00:00
static int last_status = 0 ;
2006-01-16 00:15:56 +00:00
/**
2011-12-27 03:18:46 +00:00
Signal flag
2006-01-16 00:15:56 +00:00
*/
2005-09-20 13:26:39 +00:00
static sig_atomic_t got_signal = 0 ;
2012-02-28 02:43:24 +00:00
bool job_list_is_empty ( void )
{
ASSERT_IS_MAIN_THREAD ( ) ;
return parser_t : : principal_parser ( ) . job_list ( ) . empty ( ) ;
}
void job_iterator_t : : reset ( )
{
this - > current = job_list - > begin ( ) ;
this - > end = job_list - > end ( ) ;
}
2012-01-30 00:36:21 +00:00
2012-02-28 02:43:24 +00:00
job_iterator_t : : job_iterator_t ( job_list_t & jobs ) : job_list ( & jobs ) {
this - > reset ( ) ;
2012-01-30 00:36:21 +00:00
}
2012-02-28 02:43:24 +00:00
job_iterator_t : : job_iterator_t ( ) : job_list ( & parser_t : : principal_parser ( ) . job_list ( ) ) {
this - > reset ( ) ;
}
2005-09-20 13:26:39 +00:00
int is_interactive_session = 0 ;
int is_subshell = 0 ;
int is_block = 0 ;
int is_login = 0 ;
2005-10-05 22:37:08 +00:00
int is_event = 0 ;
2005-09-23 23:15:38 +00:00
int proc_had_barrier ;
2005-09-20 13:26:39 +00:00
pid_t proc_last_bg_pid = 0 ;
2006-01-30 17:54:26 +00:00
int job_control_mode = JOB_CONTROL_INTERACTIVE ;
2006-03-18 01:04:59 +00:00
int no_exec = 0 ;
2012-02-26 02:54:49 +00:00
static int is_interactive = - 1 ;
int get_is_interactive ( void ) {
ASSERT_IS_MAIN_THREAD ( ) ;
return is_interactive ;
}
2005-09-20 13:26:39 +00:00
2005-10-14 11:40:33 +00:00
/**
2005-12-11 22:21:01 +00:00
The event variable used to send all process event
2005-10-14 11:40:33 +00:00
*/
2012-02-09 03:02:25 +00:00
static event_t event ( 0 ) ;
2005-12-11 22:21:01 +00:00
2006-06-20 00:50:10 +00:00
/**
A stack containing the values of is_interactive . Used by proc_push_interactive and proc_pop_interactive .
*/
2012-02-10 03:26:44 +00:00
static std : : deque < int > interactive_stack ;
2005-10-14 11:40:33 +00:00
void proc_init ( )
{
2006-02-16 13:36:32 +00:00
proc_push_interactive ( 0 ) ;
2012-02-09 03:02:25 +00:00
event . arguments . reset ( new wcstring_list_t ) ;
2005-10-14 11:40:33 +00:00
}
2006-02-09 15:50:20 +00:00
2006-01-16 00:15:56 +00:00
/**
2011-12-27 03:18:46 +00:00
Remove job from list of jobs
2006-01-16 00:15:56 +00:00
*/
2005-09-20 13:26:39 +00:00
static int job_remove ( job_t * j )
{
2012-02-28 02:43:24 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
return parser_t : : principal_parser ( ) . job_remove ( j ) ;
2012-01-30 00:36:21 +00:00
}
void job_promote ( job_t * job )
{
2012-02-28 02:43:24 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
parser_t : : principal_parser ( ) . job_promote ( job ) ;
2005-09-20 13:26:39 +00:00
}
/*
Remove job from the job list and free all memory associated with
it .
*/
void job_free ( job_t * j )
{
job_remove ( j ) ;
2012-02-10 03:26:44 +00:00
delete j ;
2005-09-20 13:26:39 +00:00
}
void proc_destroy ( )
{
2012-02-09 03:02:25 +00:00
event . arguments . reset ( NULL ) ;
2012-02-28 02:43:24 +00:00
job_list_t & jobs = parser_t : : principal_parser ( ) . job_list ( ) ;
2012-01-30 00:36:21 +00:00
while ( ! jobs . empty ( ) )
2005-09-20 13:26:39 +00:00
{
2012-01-30 00:36:21 +00:00
job_t * job = jobs . front ( ) ;
2012-01-30 06:06:58 +00:00
debug ( 2 , L " freeing leaked job %ls " , job - > command_cstr ( ) ) ;
2012-01-30 00:36:21 +00:00
job_free ( job ) ;
2012-02-28 02:43:24 +00:00
}
2005-09-20 13:26:39 +00:00
}
void proc_set_last_status ( int s )
{
last_status = s ;
}
int proc_get_last_status ( )
{
return last_status ;
}
2012-02-28 02:43:24 +00:00
/* Basic thread safe job IDs. The vector consumed_job_ids has a true value wherever the job ID corresponding to that slot is in use. The job ID corresponding to slot 0 is 1. */
static pthread_mutex_t job_id_lock = PTHREAD_MUTEX_INITIALIZER ;
static std : : vector < bool > consumed_job_ids ;
job_id_t acquire_job_id ( void )
2005-09-20 13:26:39 +00:00
{
2012-02-28 02:43:24 +00:00
scoped_lock lock ( job_id_lock ) ;
2012-01-30 00:36:21 +00:00
2012-02-28 02:43:24 +00:00
/* Find the index of the first 0 slot */
std : : vector < bool > : : iterator slot = std : : find ( consumed_job_ids . begin ( ) , consumed_job_ids . end ( ) , false ) ;
if ( slot ! = consumed_job_ids . end ( ) )
{
/* We found a slot. Note that slot 0 corresponds to job ID 1. */
* slot = true ;
return slot - consumed_job_ids . begin ( ) + 1 ;
}
else
{
/* We did not find a slot; create a new slot. The size of the vector is now the job ID (since it is one larger than the slot). */
consumed_job_ids . push_back ( true ) ;
return ( job_id_t ) consumed_job_ids . size ( ) ;
}
}
2006-03-10 13:38:09 +00:00
2012-02-28 02:43:24 +00:00
void release_job_id ( job_id_t jid )
{
assert ( jid > 0 ) ;
scoped_lock lock ( job_id_lock ) ;
size_t slot = ( size_t ) ( jid - 1 ) , count = consumed_job_ids . size ( ) ;
/* Make sure this slot is within our vector and is currently set to consumed */
assert ( slot < count ) ;
assert ( consumed_job_ids . at ( slot ) = = true ) ;
/* Clear it and then resize the vector to eliminate unused trailing job IDs */
consumed_job_ids . at ( slot ) = false ;
while ( count - - ) {
if ( consumed_job_ids . at ( count ) )
break ;
}
consumed_job_ids . resize ( count + 1 ) ;
2005-09-20 13:26:39 +00:00
}
2012-02-28 02:43:24 +00:00
job_t * job_get ( job_id_t id )
2005-09-20 13:26:39 +00:00
{
2012-02-28 02:43:24 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
return parser_t : : principal_parser ( ) . job_get ( id ) ;
2005-09-20 13:26:39 +00:00
}
job_t * job_get_from_pid ( int pid )
{
2012-02-28 02:43:24 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
return parser_t : : principal_parser ( ) . job_get_from_pid ( pid ) ;
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
/*
Return true if all processes in the job have stopped or completed .
2006-01-16 00:15:56 +00:00
\ param j the job to test
2005-09-20 13:26:39 +00:00
*/
int job_is_stopped ( const job_t * j )
{
process_t * p ;
for ( p = j - > first_process ; p ; p = p - > next )
{
if ( ! p - > completed & & ! p - > stopped )
{
return 0 ;
}
}
return 1 ;
}
2011-12-27 03:18:46 +00:00
/*
Return true if the last processes in the job has completed .
2006-01-16 00:15:56 +00:00
\ param j the job to test
2005-09-20 13:26:39 +00:00
*/
int job_is_completed ( const job_t * j )
{
process_t * p ;
2011-12-27 03:18:46 +00:00
2006-07-07 15:35:39 +00:00
for ( p = j - > first_process ; p - > next ; p = p - > next )
;
2011-12-27 03:18:46 +00:00
2006-07-07 15:35:39 +00:00
return p - > completed ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
}
2006-10-25 20:47:59 +00:00
void job_set_flag ( job_t * j , int flag , int set )
{
if ( set )
j - > flags | = flag ;
else
j - > flags = j - > flags & ( 0xffffffff ^ flag ) ;
}
2012-02-28 02:43:24 +00:00
int job_get_flag ( const job_t * j , int flag )
2006-10-25 20:47:59 +00:00
{
return j - > flags & flag ? 1 : 0 ;
}
2006-11-20 13:12:24 +00:00
int job_signal ( job_t * j , int signal )
2011-12-27 03:18:46 +00:00
{
2006-11-20 13:12:24 +00:00
pid_t my_pid = getpid ( ) ;
int res = 0 ;
2011-12-27 03:18:46 +00:00
2006-11-20 13:12:24 +00:00
if ( j - > pgid ! = my_pid )
{
res = killpg ( j - > pgid , SIGHUP ) ;
}
else
{
process_t * p ;
for ( p = j - > first_process ; p ; p = p - > next )
{
if ( ! p - > completed )
{
if ( p - > pid )
{
if ( kill ( p - > pid , SIGHUP ) )
{
res = - 1 ;
break ;
}
}
}
}
}
return res ;
}
2006-10-25 20:47:59 +00:00
2005-09-20 13:26:39 +00:00
/**
Store the status of the process pid that was returned by waitpid .
2011-12-27 03:18:46 +00:00
Return 0 if all went well , nonzero otherwise .
2005-09-20 13:26:39 +00:00
*/
2012-02-28 02:43:24 +00:00
static void mark_process_status ( const job_t * j ,
2005-09-20 13:26:39 +00:00
process_t * p ,
int status )
{
2006-11-11 10:48:40 +00:00
// debug( 0, L"Process %ls %ls", p->argv[0], WIFSTOPPED (status)?L"stopped":(WIFEXITED( status )?L"exited":(WIFSIGNALED( status )?L"signaled to exit":L"BLARGH")) );
2009-02-21 16:46:56 +00:00
p - > status = status ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( WIFSTOPPED ( status ) )
{
p - > stopped = 1 ;
}
2011-12-27 03:18:46 +00:00
else if ( WIFSIGNALED ( status ) | | WIFEXITED ( status ) )
2009-02-21 16:46:56 +00:00
{
p - > completed = 1 ;
}
2005-09-20 13:26:39 +00:00
else
{
2009-02-22 20:28:52 +00:00
ssize_t ignore ;
2011-12-27 03:18:46 +00:00
2009-02-21 16:46:56 +00:00
/* This should never be reached */
2005-09-20 13:26:39 +00:00
p - > completed = 1 ;
2006-11-11 10:48:40 +00:00
2009-02-21 16:46:56 +00:00
char mess [ MESS_SIZE ] ;
snprintf ( mess ,
MESS_SIZE ,
" Process %d exited abnormally \n " ,
( int ) p - > pid ) ;
2009-02-22 20:28:52 +00:00
/*
If write fails , do nothing . We ' re in a signal handlers error
handler . If things aren ' t working properly , it ' s safer to
give up .
*/
ignore = write ( 2 , mess , strlen ( mess ) ) ;
2005-09-20 13:26:39 +00:00
}
}
/**
Handle status update for child \ c pid . This function is called by
2006-01-16 00:15:56 +00:00
the signal handler , so it mustn ' t use malloc or any such hitech
nonsense .
\ param pid the pid of the process whose status changes
\ param status the status as returned by wait
2005-09-20 13:26:39 +00:00
*/
static void handle_child_status ( pid_t pid , int status )
{
int found_proc = 0 ;
2012-02-28 02:43:24 +00:00
const job_t * j = 0 ;
2005-09-24 19:52:42 +00:00
process_t * p = 0 ;
2011-12-27 03:18:46 +00:00
// char mess[MESS_SIZE];
found_proc = 0 ;
2005-09-20 13:26:39 +00:00
/*
2011-12-27 03:18:46 +00:00
snprintf ( mess ,
2005-09-20 13:26:39 +00:00
MESS_SIZE ,
" Process %d \n " ,
( int ) pid ) ;
write ( 2 , mess , strlen ( mess ) ) ;
*/
2012-01-30 00:36:21 +00:00
job_iterator_t jobs ;
while ( ( j = jobs . next ( ) ) )
2005-09-20 13:26:39 +00:00
{
process_t * prev = 0 ;
for ( p = j - > first_process ; p ; p = p - > next )
{
if ( pid = = p - > pid )
{
/* snprintf( mess,
MESS_SIZE ,
" Process %d is %ls from job %ls \n " ,
( int ) pid , p - > actual_cmd , j - > command ) ;
write ( 2 , mess , strlen ( mess ) ) ;
2011-12-27 03:18:46 +00:00
*/
2005-09-20 13:26:39 +00:00
mark_process_status ( j , p , status ) ;
if ( p - > completed & & prev ! = 0 )
{
if ( ! prev - > completed & & prev - > pid )
{
/* snprintf( mess,
MESS_SIZE ,
" Kill previously uncompleted process %ls (%d) \n " ,
2011-12-27 03:18:46 +00:00
prev - > actual_cmd ,
2005-09-20 13:26:39 +00:00
prev - > pid ) ;
write ( 2 , mess , strlen ( mess ) ) ;
*/
kill ( prev - > pid , SIGPIPE ) ;
}
}
found_proc = 1 ;
break ;
}
prev = p ;
}
}
2011-12-27 03:18:46 +00:00
if ( WIFSIGNALED ( status ) & &
( WTERMSIG ( status ) = = SIGINT | |
2005-09-22 20:16:52 +00:00
WTERMSIG ( status ) = = SIGQUIT ) )
2005-09-20 13:26:39 +00:00
{
2005-09-22 20:16:52 +00:00
if ( ! is_interactive_session )
2011-12-27 03:18:46 +00:00
{
2005-09-20 13:26:39 +00:00
struct sigaction act ;
sigemptyset ( & act . sa_mask ) ;
2011-12-27 03:18:46 +00:00
act . sa_flags = 0 ;
2005-09-20 13:26:39 +00:00
act . sa_handler = SIG_DFL ;
sigaction ( SIGINT , & act , 0 ) ;
sigaction ( SIGQUIT , & act , 0 ) ;
kill ( getpid ( ) , WTERMSIG ( status ) ) ;
}
2005-09-22 20:16:52 +00:00
else
{
2012-01-23 05:40:08 +00:00
//PCA INSTANCED_PARSER what is this?
block_t * c = NULL ; //parser.current_block;
2005-09-24 19:52:42 +00:00
if ( p & & found_proc )
2005-09-22 20:16:52 +00:00
{
2005-09-24 19:52:42 +00:00
while ( c )
{
c - > skip = 1 ;
c = c - > outer ;
2011-12-27 03:18:46 +00:00
}
}
2005-09-22 20:16:52 +00:00
}
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( ! found_proc )
{
2011-12-27 03:18:46 +00:00
/*
2005-09-20 13:26:39 +00:00
A child we lost track of ?
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
There have been bugs in both subshell handling and in
builtin handling that have caused this previously . . .
*/
2011-12-27 03:18:46 +00:00
/* snprintf( mess,
2005-09-20 13:26:39 +00:00
MESS_SIZE ,
" Process %d not found by %d \n " ,
( int ) pid , ( int ) getpid ( ) ) ;
write ( 2 , mess , strlen ( mess ) ) ;
*/
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
return ;
}
void job_handle_signal ( int signal , siginfo_t * info , void * con )
{
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
int status ;
pid_t pid ;
int errno_old = errno ;
got_signal = 1 ;
// write( 2, "got signal\n", 11 );
while ( 1 )
{
switch ( pid = waitpid ( - 1 , & status , WUNTRACED | WNOHANG ) )
{
case 0 :
case - 1 :
{
errno = errno_old ;
return ;
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
default :
handle_child_status ( pid , status ) ;
break ;
}
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
kill ( 0 , SIGIO ) ;
errno = errno_old ;
}
2011-12-27 03:18:46 +00:00
/**
Format information about job status for the user to look at .
2006-01-16 00:15:56 +00:00
\ param j the job to test
\ param status a string description of the job exit type
2005-09-20 13:26:39 +00:00
*/
static void format_job_info ( const job_t * j , const wchar_t * status )
{
2006-01-05 13:54:11 +00:00
fwprintf ( stdout , L " \r " ) ;
2012-01-30 06:06:58 +00:00
fwprintf ( stdout , _ ( L " Job %d, \' %ls \' has %ls " ) , j - > job_id , j - > command_cstr ( ) , status ) ;
2005-09-20 13:26:39 +00:00
fflush ( stdout ) ;
tputs ( clr_eol , 1 , & writeb ) ;
fwprintf ( stdout , L " \n " ) ;
}
2005-12-03 19:46:18 +00:00
void proc_fire_event ( const wchar_t * msg , int type , pid_t pid , int status )
2005-10-11 19:23:43 +00:00
{
2011-12-27 03:18:46 +00:00
2005-12-11 22:21:01 +00:00
event . type = type ;
event . param1 . pid = pid ;
2011-12-27 03:18:46 +00:00
2011-12-27 08:06:07 +00:00
event . arguments - > push_back ( msg ) ;
2012-03-05 18:18:42 +00:00
event . arguments - > push_back ( to_string < int > ( pid ) ) ;
event . arguments - > push_back ( to_string < int > ( status ) ) ;
2005-12-11 22:21:01 +00:00
event_fire ( & event ) ;
2011-12-27 08:06:07 +00:00
event . arguments - > resize ( 0 ) ;
2011-12-27 03:18:46 +00:00
}
2005-10-11 19:23:43 +00:00
2012-02-16 08:24:27 +00:00
int job_reap ( bool interactive )
2005-09-20 13:26:39 +00:00
{
2012-02-16 08:24:27 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2012-01-30 00:36:21 +00:00
job_t * jnext ;
2005-09-20 13:26:39 +00:00
int found = 0 ;
2011-12-27 03:18:46 +00:00
2005-10-11 19:23:43 +00:00
static int locked = 0 ;
2011-12-27 03:18:46 +00:00
locked + + ;
2006-04-04 11:27:22 +00:00
/*
job_read may fire an event handler , we do not want to call
ourselves recursively ( to avoid infinite recursion ) .
*/
2005-10-11 19:23:43 +00:00
if ( locked > 1 )
2005-10-15 00:51:26 +00:00
return 0 ;
2012-01-30 00:36:21 +00:00
job_iterator_t jobs ;
jnext = jobs . next ( ) ;
while ( jnext )
{
job_t * j = jnext ;
jnext = jobs . next ( ) ;
2005-09-20 13:26:39 +00:00
process_t * p ;
2011-12-27 03:18:46 +00:00
2006-04-04 11:27:22 +00:00
/*
If we are reaping only jobs who do not need status messages
sent to the console , do not consider reaping jobs that need
status messages
*/
2006-10-25 20:47:59 +00:00
if ( ( ! job_get_flag ( j , JOB_SKIP_NOTIFICATION ) ) & & ( ! interactive ) & & ( ! job_get_flag ( j , JOB_FOREGROUND ) ) )
2005-10-11 19:23:43 +00:00
{
continue ;
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
for ( p = j - > first_process ; p ; p = p - > next )
{
2005-10-11 19:23:43 +00:00
int s ;
2005-09-20 13:26:39 +00:00
if ( ! p - > completed )
continue ;
2011-12-27 03:18:46 +00:00
2005-10-11 19:23:43 +00:00
if ( ! p - > pid )
2011-12-27 03:18:46 +00:00
continue ;
2005-10-11 19:23:43 +00:00
s = p - > status ;
2011-12-27 03:18:46 +00:00
proc_fire_event ( L " PROCESS_EXIT " , EVENT_EXIT , p - > pid , ( WIFSIGNALED ( s ) ? - 1 : WEXITSTATUS ( s ) ) ) ;
2005-10-11 19:23:43 +00:00
if ( WIFSIGNALED ( s ) )
2005-09-20 13:26:39 +00:00
{
2011-12-27 03:18:46 +00:00
/*
2005-09-20 13:26:39 +00:00
Ignore signal SIGPIPE . We issue it ourselves to the pipe
writer when the pipe reader dies .
*/
2005-10-11 19:23:43 +00:00
if ( WTERMSIG ( s ) ! = SIGPIPE )
2011-12-27 03:18:46 +00:00
{
2005-09-20 13:26:39 +00:00
int proc_is_job = ( ( p = = j - > first_process ) & & ( p - > next = = 0 ) ) ;
if ( proc_is_job )
2006-10-25 20:47:59 +00:00
job_set_flag ( j , JOB_NOTIFIED , 1 ) ;
if ( ! job_get_flag ( j , JOB_SKIP_NOTIFICATION ) )
2005-09-20 13:26:39 +00:00
{
2005-10-07 14:08:57 +00:00
if ( proc_is_job )
fwprintf ( stdout ,
2006-01-04 12:51:02 +00:00
_ ( L " %ls: Job %d, \' %ls \' terminated by signal %ls (%ls) " ) ,
program_name ,
2011-12-27 03:18:46 +00:00
j - > job_id ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ,
2005-10-07 14:08:57 +00:00
sig2wcs ( WTERMSIG ( p - > status ) ) ,
2006-02-01 15:49:11 +00:00
signal_get_desc ( WTERMSIG ( p - > status ) ) ) ;
2005-10-07 14:08:57 +00:00
else
fwprintf ( stdout ,
2006-01-04 12:51:02 +00:00
_ ( L " %ls: Process %d, \' %ls \' from job %d, \' %ls \' terminated by signal %ls (%ls) " ) ,
program_name ,
2005-10-07 14:08:57 +00:00
p - > pid ,
2012-01-30 06:06:58 +00:00
p - > argv0 ( ) ,
2005-10-07 14:08:57 +00:00
j - > job_id ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ,
2005-10-07 14:08:57 +00:00
sig2wcs ( WTERMSIG ( p - > status ) ) ,
2006-02-01 15:49:11 +00:00
signal_get_desc ( WTERMSIG ( p - > status ) ) ) ;
2005-09-20 13:26:39 +00:00
tputs ( clr_eol , 1 , & writeb ) ;
fwprintf ( stdout , L " \n " ) ;
2011-12-27 03:18:46 +00:00
found = 1 ;
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
/*
2005-09-20 13:26:39 +00:00
Clear status so it is not reported more than once
*/
p - > status = 0 ;
}
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
/*
2005-09-20 13:26:39 +00:00
If all processes have completed , tell the user the job has
2011-12-27 03:18:46 +00:00
completed and delete it from the active job list .
2005-09-20 13:26:39 +00:00
*/
2011-12-27 03:18:46 +00:00
if ( job_is_completed ( j ) )
2005-10-11 19:23:43 +00:00
{
2006-10-25 20:47:59 +00:00
if ( ! job_get_flag ( j , JOB_FOREGROUND ) & & ! job_get_flag ( j , JOB_NOTIFIED ) & & ! job_get_flag ( j , JOB_SKIP_NOTIFICATION ) )
2005-09-20 13:26:39 +00:00
{
2006-10-25 20:47:59 +00:00
format_job_info ( j , _ ( L " ended " ) ) ;
found = 1 ;
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
proc_fire_event ( L " JOB_EXIT " , EVENT_EXIT , - j - > pgid , 0 ) ;
proc_fire_event ( L " JOB_EXIT " , EVENT_JOB_ID , j - > job_id , 0 ) ;
2005-10-13 14:11:29 +00:00
job_free ( j ) ;
2011-12-27 03:18:46 +00:00
}
else if ( job_is_stopped ( j ) & & ! job_get_flag ( j , JOB_NOTIFIED ) )
2005-10-11 19:23:43 +00:00
{
2011-12-27 03:18:46 +00:00
/*
Notify the user about newly stopped jobs .
2005-09-20 13:26:39 +00:00
*/
2006-10-25 20:47:59 +00:00
if ( ! job_get_flag ( j , JOB_SKIP_NOTIFICATION ) )
2005-09-20 13:26:39 +00:00
{
2006-01-04 12:51:02 +00:00
format_job_info ( j , _ ( L " stopped " ) ) ;
2005-09-20 13:26:39 +00:00
found = 1 ;
2011-12-27 03:18:46 +00:00
}
2006-10-25 20:47:59 +00:00
job_set_flag ( j , JOB_NOTIFIED , 1 ) ;
2005-09-20 13:26:39 +00:00
}
}
2005-10-11 19:23:43 +00:00
2005-09-20 13:26:39 +00:00
if ( found )
fflush ( stdout ) ;
2005-10-11 19:23:43 +00:00
locked = 0 ;
2011-12-27 03:18:46 +00:00
return found ;
2005-09-20 13:26:39 +00:00
}
# ifdef HAVE__PROC_SELF_STAT
2006-01-20 14:27:21 +00:00
2006-01-23 20:40:14 +00:00
/**
Maximum length of a / proc / [ PID ] / stat filename
*/
2006-01-20 14:27:21 +00:00
# define FN_SIZE 256
2005-09-20 13:26:39 +00:00
/**
Get the CPU time for the specified process
*/
unsigned long proc_get_jiffies ( process_t * p )
{
2006-01-20 14:27:21 +00:00
wchar_t fn [ FN_SIZE ] ;
2005-09-20 13:26:39 +00:00
char state ;
2011-12-27 03:18:46 +00:00
int pid , ppid , pgrp ,
2005-09-20 13:26:39 +00:00
session , tty_nr , tpgid ,
2011-12-27 03:18:46 +00:00
exit_signal , processor ;
long int cutime , cstime , priority ,
nice , placeholder , itrealvalue ,
2005-09-20 13:26:39 +00:00
rss ;
2011-12-27 03:18:46 +00:00
unsigned long int flags , minflt , cminflt ,
majflt , cmajflt , utime ,
stime , starttime , vsize ,
2005-09-20 13:26:39 +00:00
rlim , startcode , endcode ,
startstack , kstkesp , kstkeip ,
signal , blocked , sigignore ,
sigcatch , wchan , nswap , cnswap ;
char comm [ 1024 ] ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( p - > pid < = 0 )
return 0 ;
2011-12-27 03:18:46 +00:00
2006-01-20 14:27:21 +00:00
swprintf ( fn , FN_SIZE , L " /proc/%d/stat " , p - > pid ) ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
FILE * f = wfopen ( fn , " r " ) ;
if ( ! f )
return 0 ;
2011-12-27 03:18:46 +00:00
int count = fscanf ( f ,
" %d %s %c "
2005-09-20 13:26:39 +00:00
" %d %d %d "
" %d %d %lu "
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
" %lu %lu %lu "
" %lu %lu %lu "
" %ld %ld %ld "
" %ld %ld %ld "
" %lu %lu %ld "
" %lu %lu %lu "
" %lu %lu %lu "
" %lu %lu %lu "
" %lu %lu %lu "
" %lu %d %d " ,
2011-12-27 03:18:46 +00:00
& pid , comm , & state ,
& ppid , & pgrp , & session ,
2005-09-20 13:26:39 +00:00
& tty_nr , & tpgid , & flags ,
& minflt , & cminflt , & majflt ,
& cmajflt , & utime , & stime ,
& cutime , & cstime , & priority ,
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
& nice , & placeholder , & itrealvalue ,
& starttime , & vsize , & rss ,
& rlim , & startcode , & endcode ,
& startstack , & kstkesp , & kstkeip ,
& signal , & blocked , & sigignore ,
& sigcatch , & wchan , & nswap ,
& cnswap , & exit_signal , & processor
) ;
if ( count < 17 )
{
return 0 ;
}
2006-06-21 09:54:30 +00:00
/*
Don ' t need to check exit status of fclose on read - only streams
*/
2005-09-20 13:26:39 +00:00
fclose ( f ) ;
return utime + stime + cutime + cstime ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
}
/**
Update the CPU time for all jobs
*/
void proc_update_jiffies ( )
{
2012-01-30 16:15:02 +00:00
job_t * job ;
2005-09-20 13:26:39 +00:00
process_t * p ;
2012-01-30 16:15:02 +00:00
job_iterator_t j ;
for ( job = j . next ( ) ; job ; job = j . next ( ) )
2005-09-20 13:26:39 +00:00
{
2012-01-30 16:15:02 +00:00
for ( p = job - > first_process ; p ; p = p - > next )
2005-09-20 13:26:39 +00:00
{
gettimeofday ( & p - > last_time , 0 ) ;
p - > last_jiffies = proc_get_jiffies ( p ) ;
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
}
# endif
/**
Check if there are buffers associated with the job , and select on
2011-12-27 03:18:46 +00:00
them for a while if available .
2006-01-16 00:15:56 +00:00
\ param j the job to test
2005-10-08 09:33:10 +00:00
\ return 1 if buffers were avaialble , zero otherwise
2005-09-20 13:26:39 +00:00
*/
static int select_try ( job_t * j )
{
fd_set fds ;
int maxfd = - 1 ;
io_data_t * d ;
FD_ZERO ( & fds ) ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
for ( d = j - > io ; d ; d = d - > next )
{
if ( d - > io_mode = = IO_BUFFER )
{
2005-10-11 19:31:16 +00:00
int fd = d - > param1 . pipe_fd [ 0 ] ;
2005-09-20 13:26:39 +00:00
// fwprintf( stderr, L"fd %d on job %ls\n", fd, j->command );
FD_SET ( fd , & fds ) ;
2005-10-11 19:31:16 +00:00
maxfd = maxi ( maxfd , d - > param1 . pipe_fd [ 0 ] ) ;
2005-09-20 13:26:39 +00:00
debug ( 3 , L " select_try on %d \n " , fd ) ;
}
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( maxfd > = 0 )
{
int retval ;
struct timeval tv ;
2011-12-27 03:18:46 +00:00
2009-07-14 09:53:20 +00:00
tv . tv_sec = 0 ;
tv . tv_usec = 10000 ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
retval = select ( maxfd + 1 , & fds , 0 , 0 , & tv ) ;
return retval > 0 ;
}
return - 1 ;
}
/**
2011-12-27 03:18:46 +00:00
Read from descriptors until they are empty .
2006-01-16 00:15:56 +00:00
\ param j the job to test
2005-09-20 13:26:39 +00:00
*/
static void read_try ( job_t * j )
{
io_data_t * d , * buff = 0 ;
/*
Find the last buffer , which is the one we want to read from
*/
for ( d = j - > io ; d ; d = d - > next )
{
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( d - > io_mode = = IO_BUFFER )
{
buff = d ;
}
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( buff )
{
2012-01-30 06:06:58 +00:00
debug ( 3 , L " proc::read_try('%ls') \n " , j - > command_cstr ( ) ) ;
2005-09-20 13:26:39 +00:00
while ( 1 )
{
char b [ BUFFER_SIZE ] ;
int l ;
2011-12-27 03:18:46 +00:00
2005-10-11 19:31:16 +00:00
l = read_blocked ( buff - > param1 . pipe_fd [ 0 ] ,
b , BUFFER_SIZE ) ;
2005-09-20 13:26:39 +00:00
if ( l = = 0 )
{
break ;
}
else if ( l < 0 )
{
if ( errno ! = EAGAIN )
{
2011-12-27 03:18:46 +00:00
debug ( 1 ,
2006-01-04 12:51:02 +00:00
_ ( L " An error occured while reading output from code block " ) ) ;
2011-12-27 03:18:46 +00:00
wperror ( L " read_try " ) ;
}
2005-09-20 13:26:39 +00:00
break ;
}
else
{
2012-03-04 10:35:30 +00:00
buff - > out_buffer_append ( b , l ) ;
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
}
}
2006-01-16 00:15:56 +00:00
2008-01-09 01:23:38 +00:00
/**
2011-12-27 03:18:46 +00:00
Give ownership of the terminal to the specified job .
2008-01-09 01:23:38 +00:00
\ param j The job to give the terminal to .
\ param cont If this variable is set , we are giving back control to
a job that has previously been stopped . In that case , we need to
set the terminal attributes to those saved in the job .
*/
2006-11-11 10:48:40 +00:00
static int terminal_give_to_job ( job_t * j , int cont )
{
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
if ( tcsetpgrp ( 0 , j - > pgid ) )
{
2011-12-27 03:18:46 +00:00
debug ( 1 ,
_ ( L " Could not send job %d ('%ls') to foreground " ) ,
j - > job_id ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ) ;
2006-11-11 10:48:40 +00:00
wperror ( L " tcsetpgrp " ) ;
return 0 ;
}
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
if ( cont )
2011-12-27 03:18:46 +00:00
{
2006-11-11 10:48:40 +00:00
if ( tcsetattr ( 0 , TCSADRAIN , & j - > tmodes ) )
{
debug ( 1 ,
_ ( L " Could not send job %d ('%ls') to foreground " ) ,
j - > job_id ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ) ;
2006-11-11 10:48:40 +00:00
wperror ( L " tcsetattr " ) ;
return 0 ;
}
}
return 1 ;
}
/**
2008-01-09 01:23:38 +00:00
Returns contol of the terminal to the shell , and saves the terminal
attribute state to the job , so that we can restore the terminal
2011-12-27 03:18:46 +00:00
ownership to the job at a later time .
2006-11-11 10:48:40 +00:00
*/
static int terminal_return_from_job ( job_t * j )
{
2011-12-27 03:18:46 +00:00
2011-07-28 01:50:39 +00:00
if ( tcsetpgrp ( 0 , getpgrp ( ) ) )
2006-11-11 10:48:40 +00:00
{
debug ( 1 , _ ( L " Could not return shell to foreground " ) ) ;
wperror ( L " tcsetpgrp " ) ;
return 0 ;
}
2011-12-27 03:18:46 +00:00
/*
Save jobs terminal modes .
2006-11-11 10:48:40 +00:00
*/
if ( tcgetattr ( 0 , & j - > tmodes ) )
{
debug ( 1 , _ ( L " Could not return shell to foreground " ) ) ;
wperror ( L " tcgetattr " ) ;
return 0 ;
}
2011-12-27 03:18:46 +00:00
/*
Restore the shell ' s terminal modes .
2006-11-11 10:48:40 +00:00
*/
if ( tcsetattr ( 0 , TCSADRAIN , & shell_modes ) )
{
debug ( 1 , _ ( L " Could not return shell to foreground " ) ) ;
wperror ( L " tcsetattr " ) ;
return 0 ;
}
return 1 ;
}
2005-09-20 13:26:39 +00:00
void job_continue ( job_t * j , int cont )
{
/*
Put job first in the job list
*/
2012-01-30 00:36:21 +00:00
job_promote ( j ) ;
2006-10-25 20:47:59 +00:00
job_set_flag ( j , JOB_NOTIFIED , 0 ) ;
2006-03-10 13:38:09 +00:00
2006-11-11 10:48:40 +00:00
CHECK_BLOCK ( ) ;
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
debug ( 4 ,
L " Continue job %d, gid %d (%ls), %ls, %ls " ,
2011-12-27 03:18:46 +00:00
j - > job_id ,
2006-11-11 10:48:40 +00:00
j - > pgid ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ,
2011-12-27 03:18:46 +00:00
job_is_completed ( j ) ? L " COMPLETED " : L " UNCOMPLETED " ,
2005-09-20 13:26:39 +00:00
is_interactive ? L " INTERACTIVE " : L " NON-INTERACTIVE " ) ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( ! job_is_completed ( j ) )
{
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_TERMINAL ) & & job_get_flag ( j , JOB_FOREGROUND ) )
2011-12-27 03:18:46 +00:00
{
2005-09-20 13:26:39 +00:00
/* Put the job into the foreground. */
2006-11-11 10:48:40 +00:00
int ok ;
2011-12-27 03:18:46 +00:00
2006-01-30 17:54:26 +00:00
signal_block ( ) ;
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
ok = terminal_give_to_job ( j , cont ) ;
2011-12-27 03:18:46 +00:00
signal_unblock ( ) ;
2006-11-11 10:48:40 +00:00
if ( ! ok )
return ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
/*
Send the job a continue signal , if necessary .
2005-09-20 13:26:39 +00:00
*/
2005-09-23 13:10:31 +00:00
if ( cont )
2005-09-20 13:26:39 +00:00
{
2005-09-23 13:10:31 +00:00
process_t * p ;
2006-02-01 12:27:15 +00:00
2005-09-23 13:10:31 +00:00
for ( p = j - > first_process ; p ; p = p - > next )
p - > stopped = 0 ;
2006-02-01 12:27:15 +00:00
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_CONTROL ) )
2005-09-20 13:26:39 +00:00
{
2006-02-01 12:27:15 +00:00
if ( killpg ( j - > pgid , SIGCONT ) )
2005-09-23 13:10:31 +00:00
{
2006-02-01 12:27:15 +00:00
wperror ( L " killpg (SIGCONT) " ) ;
2005-09-23 13:10:31 +00:00
return ;
2006-02-01 12:27:15 +00:00
}
}
else
{
for ( p = j - > first_process ; p ; p = p - > next )
{
if ( kill ( p - > pid , SIGCONT ) < 0 )
{
wperror ( L " kill (SIGCONT) " ) ;
return ;
2011-12-27 03:18:46 +00:00
}
2006-02-01 12:27:15 +00:00
}
2005-09-20 13:26:39 +00:00
}
2005-09-23 13:10:31 +00:00
}
2011-12-27 03:18:46 +00:00
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_FOREGROUND ) )
2005-09-23 13:10:31 +00:00
{
int quit = 0 ;
2011-12-27 03:18:46 +00:00
/*
2005-09-23 13:10:31 +00:00
Wait for job to report . Looks a bit ugly because it has to
handle the possibility that a signal is dispatched while
running job_is_stopped ( ) .
*/
while ( ! quit )
2005-09-20 13:26:39 +00:00
{
2005-09-23 13:10:31 +00:00
do
{
got_signal = 0 ;
2006-04-04 11:27:22 +00:00
quit = job_is_stopped ( j ) | | job_is_completed ( j ) ;
2005-09-23 13:10:31 +00:00
}
2007-04-16 20:10:53 +00:00
while ( got_signal & & ! quit )
;
2005-09-23 13:10:31 +00:00
if ( ! quit )
2005-09-20 13:26:39 +00:00
{
2011-12-27 03:18:46 +00:00
// debug( 1, L"select_try()" );
2005-09-23 13:10:31 +00:00
switch ( select_try ( j ) )
2005-09-20 13:26:39 +00:00
{
2011-12-27 03:18:46 +00:00
case 1 :
2005-09-23 13:10:31 +00:00
{
read_try ( j ) ;
break ;
}
2011-12-27 03:18:46 +00:00
2005-09-23 13:10:31 +00:00
case - 1 :
{
/*
If there is no funky IO magic , we can use
waitpid instead of handling child deaths
through signals . This gives a rather large
speed boost ( A factor 3 startup time
improvement on my 300 MHz machine ) on
short - lived jobs .
*/
2011-12-27 03:18:46 +00:00
int status ;
2005-09-23 13:10:31 +00:00
pid_t pid = waitpid ( - 1 , & status , WUNTRACED ) ;
if ( pid > 0 )
2006-11-11 10:48:40 +00:00
{
2005-09-23 13:10:31 +00:00
handle_child_status ( pid , status ) ;
2006-11-11 10:48:40 +00:00
}
else
{
/*
This probably means we got a
signal . A signal might mean that the
terminal emulator sent us a hup
signal to tell is to close . If so ,
we should exit .
*/
if ( reader_exit_forced ( ) )
{
quit = 1 ;
}
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
}
2005-09-23 13:10:31 +00:00
break ;
}
2011-12-27 03:18:46 +00:00
2005-09-23 13:10:31 +00:00
}
2011-12-27 03:18:46 +00:00
}
2005-09-23 13:10:31 +00:00
}
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_FOREGROUND ) )
2005-09-20 13:26:39 +00:00
{
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( job_is_completed ( j ) )
{
process_t * p = j - > first_process ;
while ( p - > next )
p = p - > next ;
2009-02-21 16:46:56 +00:00
if ( WIFEXITED ( p - > status ) | | WIFSIGNALED ( p - > status ) )
2005-09-20 13:26:39 +00:00
{
2011-12-27 03:18:46 +00:00
/*
2005-09-20 13:26:39 +00:00
Mark process status only if we are in the foreground
and the last process in a pipe , and it is not a short circuted builtin
*/
if ( p - > pid )
{
2009-02-21 16:46:56 +00:00
int status = proc_format_status ( p - > status ) ;
2012-01-02 21:40:03 +00:00
//wprintf(L"setting status %d for %ls\n", job_get_flag( j, JOB_NEGATE )?!status:status, j->command);
2009-02-21 16:46:56 +00:00
proc_set_last_status ( job_get_flag ( j , JOB_NEGATE ) ? ! status : status ) ;
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
/*
Put the shell back in the foreground .
2005-09-20 13:26:39 +00:00
*/
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_TERMINAL ) & & job_get_flag ( j , JOB_FOREGROUND ) )
2005-09-20 13:26:39 +00:00
{
2006-11-11 10:48:40 +00:00
int ok ;
2011-12-27 03:18:46 +00:00
2005-10-14 11:40:33 +00:00
signal_block ( ) ;
2006-11-11 10:48:40 +00:00
ok = terminal_return_from_job ( j ) ;
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
signal_unblock ( ) ;
2011-12-27 03:18:46 +00:00
2006-11-11 10:48:40 +00:00
if ( ! ok )
2005-10-14 11:40:33 +00:00
return ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
}
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
}
2011-12-27 03:18:46 +00:00
int proc_format_status ( int status )
2009-02-21 16:46:56 +00:00
{
2011-12-27 03:18:46 +00:00
if ( WIFSIGNALED ( status ) )
2009-02-21 16:46:56 +00:00
{
return 128 + WTERMSIG ( status ) ;
2011-12-27 03:18:46 +00:00
}
else if ( WIFEXITED ( status ) )
2009-02-21 16:46:56 +00:00
{
return WEXITSTATUS ( status ) ;
}
return status ;
2011-12-27 03:18:46 +00:00
2009-02-21 16:46:56 +00:00
}
2005-09-20 13:26:39 +00:00
void proc_sanity_check ( )
{
job_t * j ;
job_t * fg_job = 0 ;
2011-12-27 03:18:46 +00:00
2012-01-30 00:36:21 +00:00
job_iterator_t jobs ;
while ( ( j = jobs . next ( ) ) )
2005-09-20 13:26:39 +00:00
{
process_t * p ;
2006-10-25 20:47:59 +00:00
if ( ! job_get_flag ( j , JOB_CONSTRUCTED ) )
2005-09-20 13:26:39 +00:00
continue ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
validate_pointer ( j - > first_process ,
2006-01-04 12:51:02 +00:00
_ ( L " Process list pointer " ) ,
2005-09-20 13:26:39 +00:00
0 ) ;
2006-01-04 12:51:02 +00:00
2005-09-20 13:26:39 +00:00
/*
More than one foreground job ?
*/
2006-10-25 20:47:59 +00:00
if ( job_get_flag ( j , JOB_FOREGROUND ) & & ! ( job_is_stopped ( j ) | | job_is_completed ( j ) ) )
2005-09-20 13:26:39 +00:00
{
if ( fg_job ! = 0 )
{
2011-12-27 03:18:46 +00:00
debug ( 0 ,
2006-01-04 12:51:02 +00:00
_ ( L " More than one job in foreground: job 1: '%ls' job 2: '%ls' " ) ,
2012-01-30 06:06:58 +00:00
fg_job - > command_cstr ( ) ,
j - > command_cstr ( ) ) ;
2005-09-20 13:26:39 +00:00
sanity_lose ( ) ;
}
fg_job = j ;
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
p = j - > first_process ;
while ( p )
2011-12-27 03:18:46 +00:00
{
2012-01-30 06:06:58 +00:00
validate_pointer ( p - > get_argv ( ) , _ ( L " Process argument list " ) , 0 ) ;
validate_pointer ( p - > argv0 ( ) , _ ( L " Process name " ) , 0 ) ;
2006-01-04 12:51:02 +00:00
validate_pointer ( p - > next , _ ( L " Process list pointer " ) , 1 ) ;
validate_pointer ( p - > actual_cmd , _ ( L " Process command " ) , 1 ) ;
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( ( p - > stopped & ( ~ 0x00000001 ) ) ! = 0 )
{
debug ( 0 ,
2006-01-04 12:51:02 +00:00
_ ( L " Job '%ls', process '%ls' has inconsistent state \' stopped \' =%d " ) ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ,
p - > argv0 ( ) ,
2005-09-20 13:26:39 +00:00
p - > stopped ) ;
sanity_lose ( ) ;
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
if ( ( p - > completed & ( ~ 0x00000001 ) ) ! = 0 )
{
debug ( 0 ,
2006-01-04 12:51:02 +00:00
_ ( L " Job '%ls', process '%ls' has inconsistent state \' completed \' =%d " ) ,
2012-01-30 06:06:58 +00:00
j - > command_cstr ( ) ,
p - > argv0 ( ) ,
2005-09-20 13:26:39 +00:00
p - > completed ) ;
sanity_lose ( ) ;
}
2011-12-27 03:18:46 +00:00
2005-09-20 13:26:39 +00:00
p = p - > next ;
}
2011-12-27 03:18:46 +00:00
}
2005-09-20 13:26:39 +00:00
}
2006-02-16 13:36:32 +00:00
void proc_push_interactive ( int value )
{
2012-02-26 02:54:49 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2006-02-16 13:36:32 +00:00
int old = is_interactive ;
2012-02-10 03:26:44 +00:00
interactive_stack . push_back ( is_interactive ) ;
2006-02-16 13:36:32 +00:00
is_interactive = value ;
if ( old ! = value )
signal_set_handlers ( ) ;
}
void proc_pop_interactive ( )
{
2012-02-26 02:54:49 +00:00
ASSERT_IS_MAIN_THREAD ( ) ;
2006-02-16 13:36:32 +00:00
int old = is_interactive ;
2012-02-10 03:26:44 +00:00
is_interactive = interactive_stack . back ( ) ;
interactive_stack . pop_back ( ) ;
2006-02-16 13:36:32 +00:00
if ( is_interactive ! = old )
signal_set_handlers ( ) ;
}