2005-10-24 23:26:25 +08:00
|
|
|
/** \file event.c
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
Functions for handling event triggers
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
*/
|
2006-08-11 09:18:35 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
2011-12-27 14:18:16 +08:00
|
|
|
#include <algorithm>
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2005-10-06 06:37:08 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-01-20 22:27:21 +08:00
|
|
|
#include "wutil.h"
|
2005-10-06 06:37:08 +08:00
|
|
|
#include "function.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "signal.h"
|
2006-07-20 06:55:49 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
|
|
|
Number of signals that can be queued before an overflow occurs
|
|
|
|
*/
|
|
|
|
#define SIG_UNHANDLED_MAX 64
|
|
|
|
|
|
|
|
/**
|
|
|
|
This struct contains a list of generated signals waiting to be
|
|
|
|
dispatched
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Number of delivered signals
|
|
|
|
*/
|
2005-10-06 06:37:08 +08:00
|
|
|
int count;
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Whether signals have been skipped
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
int overflow;
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Array of signal events
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
int signal[SIG_UNHANDLED_MAX];
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
signal_list_t;
|
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
2005-10-06 06:37:08 +08:00
|
|
|
The signal event list. Actually two separate lists. One which is
|
|
|
|
active, which is the one that new events is written to. The inactive
|
|
|
|
one contains the events that are currently beeing performed.
|
|
|
|
*/
|
2006-05-18 21:00:39 +08:00
|
|
|
static signal_list_t sig_list[]={{0,0},{0,0}};
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The index of sig_list that is the list of signals currently written to
|
|
|
|
*/
|
|
|
|
static int active_list=0;
|
|
|
|
|
2011-12-27 14:27:58 +08:00
|
|
|
typedef std::vector<event_t *> event_list_t;
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
2012-06-05 05:20:01 +08:00
|
|
|
List of event handlers.
|
|
|
|
Note this is inspected by our signal handler, so we must block signals around manipulating it.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2011-12-27 14:27:58 +08:00
|
|
|
static event_list_t events;
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
|
|
|
List of event handlers that should be removed
|
|
|
|
*/
|
2011-12-27 14:27:58 +08:00
|
|
|
static event_list_t killme;
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2005-12-12 06:21:01 +08:00
|
|
|
/**
|
|
|
|
List of events that have been sent but have not yet been delivered because they are blocked.
|
|
|
|
*/
|
2011-12-27 14:27:58 +08:00
|
|
|
static event_list_t blocked;
|
2005-12-12 06:21:01 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
|
|
|
Tests if one event instance matches the definition of a event
|
2006-02-02 23:33:30 +08:00
|
|
|
class. If both the class and the instance name a function,
|
|
|
|
they must name the same function.
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
*/
|
2012-02-18 03:36:49 +08:00
|
|
|
static int event_match( const event_t *classv, const event_t *instance )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2012-03-06 06:18:16 +08:00
|
|
|
/* If the function names are both non-empty and different, then it's not a match */
|
|
|
|
if( ! classv->function_name.empty() &&
|
|
|
|
! instance->function_name.empty() &&
|
|
|
|
classv->function_name != instance->function_name)
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2012-03-06 06:18:16 +08:00
|
|
|
return 0;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
if( classv->type == EVENT_ANY )
|
2005-10-06 06:37:08 +08:00
|
|
|
return 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
if( classv->type != instance->type )
|
2005-10-06 06:37:08 +08:00
|
|
|
return 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2010-09-18 09:51:16 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
switch( classv->type )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
case EVENT_SIGNAL:
|
2011-12-27 11:18:46 +08:00
|
|
|
if( classv->param1.signal == EVENT_ANY_SIGNAL )
|
2005-10-06 06:37:08 +08:00
|
|
|
return 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
return classv->param1.signal == instance->param1.signal;
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
case EVENT_VARIABLE:
|
2012-02-09 11:02:25 +08:00
|
|
|
return instance->str_param1 == classv->str_param1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
case EVENT_EXIT:
|
2011-12-27 11:18:46 +08:00
|
|
|
if( classv->param1.pid == EVENT_ANY_PID )
|
2005-10-06 06:37:08 +08:00
|
|
|
return 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
return classv->param1.pid == instance->param1.pid;
|
2005-10-15 08:51:26 +08:00
|
|
|
|
|
|
|
case EVENT_JOB_ID:
|
2011-12-27 11:18:46 +08:00
|
|
|
return classv->param1.job_id == instance->param1.job_id;
|
2007-08-20 00:42:30 +08:00
|
|
|
|
|
|
|
case EVENT_GENERIC:
|
2012-02-09 11:02:25 +08:00
|
|
|
return instance->str_param1 == classv->str_param1;
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
|
|
|
This should never be reached
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
return 0;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create an identical copy of an event. Use deep copying, i.e. make
|
|
|
|
duplicates of any strings used as well.
|
|
|
|
*/
|
2012-03-04 07:20:30 +08:00
|
|
|
static event_t *event_copy( const event_t *event, int copy_arguments )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2012-02-09 11:02:25 +08:00
|
|
|
event_t *e = new event_t(*event);
|
2011-12-27 16:06:07 +08:00
|
|
|
|
2012-02-09 11:02:25 +08:00
|
|
|
e->arguments.reset(new wcstring_list_t);
|
|
|
|
if( copy_arguments && event->arguments.get() != NULL )
|
2005-12-12 06:21:01 +08:00
|
|
|
{
|
2011-12-27 16:06:07 +08:00
|
|
|
*(e->arguments) = *(event->arguments);
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Test if specified event is blocked
|
|
|
|
*/
|
2005-12-12 06:21:01 +08:00
|
|
|
static int event_is_blocked( event_t *e )
|
|
|
|
{
|
|
|
|
block_t *block;
|
2012-01-23 13:40:08 +08:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
2012-01-17 04:10:08 +08:00
|
|
|
for( block = parser.current_block; block; block = block->outer )
|
2005-12-12 06:21:01 +08:00
|
|
|
{
|
2012-02-08 13:04:51 +08:00
|
|
|
if (event_block_list_blocks_type(block->event_blocks, e->type))
|
|
|
|
return true;
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
2012-02-08 13:04:51 +08:00
|
|
|
return event_block_list_blocks_type(parser.global_event_blocks, e->type);
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
|
|
|
|
2012-02-18 03:36:49 +08:00
|
|
|
wcstring event_get_desc( const event_t *e )
|
2006-02-01 23:49:11 +08:00
|
|
|
{
|
2006-06-21 18:07:46 +08:00
|
|
|
|
|
|
|
CHECK( e, 0 );
|
|
|
|
|
2012-02-09 08:20:48 +08:00
|
|
|
wcstring result;
|
2006-02-01 23:49:11 +08:00
|
|
|
switch( e->type )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
case EVENT_SIGNAL:
|
2012-02-09 08:20:48 +08:00
|
|
|
result = format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal ), signal_get_desc( e->param1.signal ));
|
2006-02-01 23:49:11 +08:00
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
case EVENT_VARIABLE:
|
2012-02-09 11:02:25 +08:00
|
|
|
result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str() );
|
2006-02-01 23:49:11 +08:00
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
case EVENT_EXIT:
|
|
|
|
if( e->param1.pid > 0 )
|
|
|
|
{
|
2012-02-09 08:20:48 +08:00
|
|
|
result = format_string(_(L"exit handler for process %d"), e->param1.pid );
|
2006-02-01 23:49:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
job_t *j = job_get_from_pid( -e->param1.pid );
|
|
|
|
if( j )
|
2012-03-09 15:21:07 +08:00
|
|
|
result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr() );
|
2006-02-01 23:49:11 +08:00
|
|
|
else
|
2012-02-09 08:20:48 +08:00
|
|
|
result = format_string(_(L"exit handler for job with process group %d"), -e->param1.pid );
|
2006-02-01 23:49:11 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
case EVENT_JOB_ID:
|
|
|
|
{
|
|
|
|
job_t *j = job_get( e->param1.job_id );
|
|
|
|
if( j )
|
2012-03-09 15:21:07 +08:00
|
|
|
result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr() );
|
2006-02-01 23:49:11 +08:00
|
|
|
else
|
2012-03-26 16:21:10 +08:00
|
|
|
result = format_string(_(L"exit handler for job with job id %d"), e->param1.job_id );
|
2006-02-01 23:49:11 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-08-20 00:42:30 +08:00
|
|
|
case EVENT_GENERIC:
|
2012-02-09 11:02:25 +08:00
|
|
|
result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str() );
|
2007-08-20 00:42:30 +08:00
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-08-20 00:42:30 +08:00
|
|
|
default:
|
2012-02-09 08:20:48 +08:00
|
|
|
result = format_string(_(L"Unknown event type") );
|
2007-08-20 00:42:30 +08:00
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-09 08:20:48 +08:00
|
|
|
return result;
|
2006-02-01 23:49:11 +08:00
|
|
|
}
|
|
|
|
|
2012-02-18 07:58:02 +08:00
|
|
|
#if 0
|
2012-02-18 03:36:49 +08:00
|
|
|
static void show_all_handlers(void) {
|
|
|
|
puts("event handlers:");
|
2012-03-02 06:56:34 +08:00
|
|
|
for (event_list_t::const_iterator iter = events.begin(); iter != events.end(); ++iter) {
|
2012-02-18 03:36:49 +08:00
|
|
|
const event_t *foo = *iter;
|
|
|
|
wcstring tmp = event_get_desc(foo);
|
|
|
|
printf(" handler now %ls\n", tmp.c_str());
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 07:58:02 +08:00
|
|
|
#endif
|
2005-12-12 06:21:01 +08:00
|
|
|
|
2012-03-04 07:20:30 +08:00
|
|
|
void event_add_handler( const event_t *event )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2005-12-12 06:21:01 +08:00
|
|
|
event_t *e;
|
|
|
|
|
2006-06-21 18:07:46 +08:00
|
|
|
CHECK( event, );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-12-12 06:21:01 +08:00
|
|
|
e = event_copy( event, 0 );
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2005-10-06 19:54:16 +08:00
|
|
|
if( e->type == EVENT_SIGNAL )
|
|
|
|
{
|
2005-10-12 03:31:16 +08:00
|
|
|
signal_handle( e->param1.signal, 1 );
|
2005-10-06 19:54:16 +08:00
|
|
|
}
|
2012-06-05 05:20:01 +08:00
|
|
|
|
|
|
|
// Block around updating the events vector
|
|
|
|
signal_block();
|
|
|
|
events.push_back(e);
|
|
|
|
signal_unblock();
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void event_remove( event_t *criterion )
|
|
|
|
{
|
2012-02-18 03:36:49 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
size_t i;
|
2011-12-27 14:27:58 +08:00
|
|
|
event_list_t new_list;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-21 18:07:46 +08:00
|
|
|
CHECK( criterion, );
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
2006-05-18 21:00:39 +08:00
|
|
|
Because of concurrency issues (env_remove could remove an event
|
|
|
|
that is currently being executed), env_remove does not actually
|
|
|
|
free any events - instead it simply moves all events that should
|
|
|
|
be removed from the event list to the killme list, and the ones
|
|
|
|
that shouldn't be killed to new_list, and then drops the empty
|
|
|
|
events-list.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
if( events.empty() )
|
2006-06-21 18:07:46 +08:00
|
|
|
return;
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
for( i=0; i<events.size(); i++ )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 14:18:16 +08:00
|
|
|
event_t *n = events.at(i);
|
2005-10-06 06:37:08 +08:00
|
|
|
if( event_match( criterion, n ) )
|
|
|
|
{
|
2011-12-27 14:22:55 +08:00
|
|
|
killme.push_back(n);
|
2005-10-06 19:54:16 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
If this event was a signal handler and no other handler handles
|
|
|
|
the specified signal type, do not handle that type of signal any
|
|
|
|
more.
|
|
|
|
*/
|
|
|
|
if( n->type == EVENT_SIGNAL )
|
|
|
|
{
|
2012-02-09 11:02:25 +08:00
|
|
|
event_t e = event_t::signal_event(n->param1.signal);
|
2005-10-06 19:54:16 +08:00
|
|
|
if( event_get( &e, 0 ) == 1 )
|
|
|
|
{
|
2005-10-12 03:31:16 +08:00
|
|
|
signal_handle( e.param1.signal, 0 );
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-10-06 19:54:16 +08:00
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 14:18:16 +08:00
|
|
|
new_list.push_back(n);
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-05 05:20:01 +08:00
|
|
|
signal_block();
|
2011-12-27 14:18:16 +08:00
|
|
|
events.swap(new_list);
|
2012-06-05 05:20:01 +08:00
|
|
|
signal_unblock();
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
2012-02-08 09:06:45 +08:00
|
|
|
int event_get( event_t *criterion, std::vector<event_t *> *out )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 14:18:16 +08:00
|
|
|
size_t i;
|
2005-10-06 19:54:16 +08:00
|
|
|
int found = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
if( events.empty() )
|
2011-12-27 11:18:46 +08:00
|
|
|
return 0;
|
2006-06-21 18:07:46 +08:00
|
|
|
|
|
|
|
CHECK( criterion, 0 );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
for( i=0; i<events.size(); i++ )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 14:18:16 +08:00
|
|
|
event_t *n = events.at(i);
|
2005-10-06 06:37:08 +08:00
|
|
|
if( event_match(criterion, n ) )
|
2005-10-06 19:54:16 +08:00
|
|
|
{
|
|
|
|
found++;
|
|
|
|
if( out )
|
2012-02-08 09:06:45 +08:00
|
|
|
out->push_back(n);
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
2005-10-06 19:54:16 +08:00
|
|
|
return found;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 05:20:01 +08:00
|
|
|
bool event_is_signal_observed(int sig)
|
|
|
|
{
|
|
|
|
/* We are in a signal handler! Don't allocate memory, etc.
|
|
|
|
This does what event_match does, except it doesn't require passing in an event_t.
|
|
|
|
*/
|
|
|
|
size_t i, max = events.size();
|
|
|
|
for (i=0; i < max; i++)
|
|
|
|
{
|
|
|
|
const event_t *event = events[i];
|
|
|
|
if (event->type == EVENT_ANY)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event->type == EVENT_SIGNAL)
|
|
|
|
{
|
|
|
|
if( event->param1.signal == EVENT_ANY_SIGNAL || event->param1.signal == sig)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/**
|
|
|
|
Free all events in the kill list
|
|
|
|
*/
|
|
|
|
static void event_free_kills()
|
|
|
|
{
|
2011-12-27 14:22:55 +08:00
|
|
|
for_each(killme.begin(), killme.end(), event_free);
|
|
|
|
killme.resize(0);
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test if the specified event is waiting to be killed
|
|
|
|
*/
|
|
|
|
static int event_is_killed( event_t *e )
|
|
|
|
{
|
2011-12-27 14:22:55 +08:00
|
|
|
return std::find(killme.begin(), killme.end(), e) != killme.end();
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Perform the specified event. Since almost all event firings will
|
2006-07-30 01:31:59 +08:00
|
|
|
not be matched by even a single event handler, we make sure to
|
|
|
|
optimize the 'no matches' path. This means that nothing is
|
|
|
|
allocated/initialized unless needed.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2012-02-18 03:36:49 +08:00
|
|
|
static void event_fire_internal( const event_t *event )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2012-02-18 03:36:49 +08:00
|
|
|
|
2011-12-27 16:06:07 +08:00
|
|
|
size_t i, j;
|
2011-12-27 14:51:34 +08:00
|
|
|
event_list_t fire;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
First we free all events that have been removed
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
event_free_kills();
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
if( events.empty() )
|
2005-10-06 19:54:16 +08:00
|
|
|
return;
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
Then we iterate over all events, adding events that should be
|
|
|
|
fired to a second list. We need to do this in a separate step
|
|
|
|
since an event handler might call event_remove or
|
|
|
|
event_add_handler, which will change the contents of the \c
|
|
|
|
events list.
|
|
|
|
*/
|
2011-12-27 14:18:16 +08:00
|
|
|
for( i=0; i<events.size(); i++ )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 14:18:16 +08:00
|
|
|
event_t *criterion = events.at(i);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
Check if this event is a match
|
|
|
|
*/
|
|
|
|
if(event_match( criterion, event ) )
|
|
|
|
{
|
2011-12-27 14:51:34 +08:00
|
|
|
fire.push_back(criterion);
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
No matches. Time to return.
|
|
|
|
*/
|
2011-12-27 14:51:34 +08:00
|
|
|
if( fire.empty() )
|
2005-10-06 06:37:08 +08:00
|
|
|
return;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
Iterate over our list of matching events
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 14:51:34 +08:00
|
|
|
for( i=0; i<fire.size(); i++ )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 14:51:34 +08:00
|
|
|
event_t *criterion = fire.at(i);
|
2006-11-02 21:48:59 +08:00
|
|
|
int prev_status;
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
/*
|
|
|
|
Check if this event has been removed, if so, dont fire it
|
|
|
|
*/
|
|
|
|
if( event_is_killed( criterion ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fire event
|
|
|
|
*/
|
2011-12-27 16:06:07 +08:00
|
|
|
wcstring buffer = criterion->function_name;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-09 11:02:25 +08:00
|
|
|
if (event->arguments.get())
|
2011-12-27 16:06:07 +08:00
|
|
|
{
|
|
|
|
for( j=0; j< event->arguments->size(); j++ )
|
|
|
|
{
|
|
|
|
wcstring arg_esc = escape_string( event->arguments->at(j), 1 );
|
|
|
|
buffer += L" ";
|
|
|
|
buffer += arg_esc;
|
|
|
|
}
|
|
|
|
}
|
2005-10-12 03:23:43 +08:00
|
|
|
|
2012-07-07 06:30:27 +08:00
|
|
|
// debug( 1, L"Event handler fires command '%ls'", buffer.c_str() );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-12-04 03:46:18 +08:00
|
|
|
/*
|
|
|
|
Event handlers are not part of the main flow of code, so
|
2006-07-30 01:31:59 +08:00
|
|
|
they are marked as non-interactive
|
2005-12-04 03:46:18 +08:00
|
|
|
*/
|
2006-02-16 21:36:32 +08:00
|
|
|
proc_push_interactive(0);
|
2006-11-02 21:48:59 +08:00
|
|
|
prev_status = proc_get_last_status();
|
2012-01-23 13:40:08 +08:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
2012-01-17 04:10:08 +08:00
|
|
|
parser.push_block( EVENT );
|
2012-02-18 03:36:49 +08:00
|
|
|
parser.current_block->state1<const event_t *>() = event;
|
2012-08-15 15:57:56 +08:00
|
|
|
parser.eval( buffer, io_chain_t(), TOP );
|
2012-01-17 04:10:08 +08:00
|
|
|
parser.pop_block();
|
2011-12-27 11:18:46 +08:00
|
|
|
proc_pop_interactive();
|
2006-11-02 21:48:59 +08:00
|
|
|
proc_set_last_status( prev_status );
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free killed events
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
event_free_kills();
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-15 08:51:26 +08:00
|
|
|
Handle all pending signal events
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2005-12-12 06:21:01 +08:00
|
|
|
static void event_fire_delayed()
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2005-12-12 06:21:01 +08:00
|
|
|
|
2011-12-27 16:06:07 +08:00
|
|
|
size_t i;
|
2005-12-12 06:21:01 +08:00
|
|
|
|
2006-07-30 01:31:59 +08:00
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
If is_event is one, we are running the event-handler non-recursively.
|
2006-07-30 01:31:59 +08:00
|
|
|
|
|
|
|
When the event handler has called a piece of code that triggers
|
|
|
|
another event, we do not want to fire delayed events because of
|
|
|
|
concurrency problems.
|
|
|
|
*/
|
2011-12-27 14:27:58 +08:00
|
|
|
if( ! blocked.empty() && is_event==1)
|
2005-12-12 06:21:01 +08:00
|
|
|
{
|
2011-12-27 14:27:58 +08:00
|
|
|
event_list_t new_blocked;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 14:27:58 +08:00
|
|
|
for( i=0; i<blocked.size(); i++ )
|
2005-12-12 06:21:01 +08:00
|
|
|
{
|
2011-12-27 14:27:58 +08:00
|
|
|
event_t *e = blocked.at(i);
|
2005-12-12 06:21:01 +08:00
|
|
|
if( event_is_blocked( e ) )
|
|
|
|
{
|
2011-12-27 14:27:58 +08:00
|
|
|
new_blocked.push_back(e);
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( e );
|
|
|
|
event_free( e );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2011-12-27 14:27:58 +08:00
|
|
|
blocked.swap(new_blocked);
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
while( sig_list[active_list].count > 0 )
|
|
|
|
{
|
|
|
|
signal_list_t *lst;
|
|
|
|
|
2005-10-14 19:40:33 +08:00
|
|
|
/*
|
|
|
|
Switch signal lists
|
|
|
|
*/
|
2005-10-06 06:37:08 +08:00
|
|
|
sig_list[1-active_list].count=0;
|
|
|
|
sig_list[1-active_list].overflow=0;
|
|
|
|
active_list=1-active_list;
|
2005-10-14 19:40:33 +08:00
|
|
|
|
|
|
|
/*
|
2011-12-27 11:18:46 +08:00
|
|
|
Set up
|
2005-10-14 19:40:33 +08:00
|
|
|
*/
|
2012-02-09 11:02:25 +08:00
|
|
|
event_t e = event_t::signal_event(0);
|
|
|
|
e.arguments.reset(new wcstring_list_t(1)); //one element
|
2005-10-06 06:37:08 +08:00
|
|
|
lst = &sig_list[1-active_list];
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
if( lst->overflow )
|
|
|
|
{
|
2006-01-04 20:51:02 +08:00
|
|
|
debug( 0, _( L"Signal list overflow. Signals have been ignored." ) );
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-14 19:40:33 +08:00
|
|
|
/*
|
|
|
|
Send all signals in our private list
|
|
|
|
*/
|
2012-08-05 06:11:43 +08:00
|
|
|
for( int i=0; i < lst->count; i++ )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2005-10-12 03:31:16 +08:00
|
|
|
e.param1.signal = lst->signal[i];
|
2011-12-27 16:06:07 +08:00
|
|
|
e.arguments->at(0) = sig2wcs( e.param1.signal );
|
2005-12-12 06:21:01 +08:00
|
|
|
if( event_is_blocked( &e ) )
|
|
|
|
{
|
2011-12-27 14:27:58 +08:00
|
|
|
blocked.push_back(event_copy(&e, 1));
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( &e );
|
|
|
|
}
|
2005-10-14 19:40:33 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-09 11:02:25 +08:00
|
|
|
e.arguments.reset(NULL);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 05:20:01 +08:00
|
|
|
void event_fire_signal(int signal)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This means we are in a signal handler. We must be very
|
|
|
|
careful not do do anything that could cause a memory
|
|
|
|
allocation or something else that might be bad when in a
|
|
|
|
signal handler.
|
|
|
|
*/
|
|
|
|
if( sig_list[active_list].count < SIG_UNHANDLED_MAX )
|
|
|
|
sig_list[active_list].signal[sig_list[active_list].count++]=signal;
|
|
|
|
else
|
|
|
|
sig_list[active_list].overflow=1;
|
|
|
|
}
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2005-12-12 06:21:01 +08:00
|
|
|
void event_fire( event_t *event )
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
if( event && (event->type == EVENT_SIGNAL) )
|
|
|
|
{
|
2012-06-05 05:20:01 +08:00
|
|
|
event_fire_signal(event->param1.signal);
|
2005-10-12 03:23:43 +08:00
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
else
|
|
|
|
{
|
2012-06-05 05:20:01 +08:00
|
|
|
is_event++;
|
|
|
|
|
2007-04-21 16:11:22 +08:00
|
|
|
/*
|
|
|
|
Fire events triggered by signals
|
|
|
|
*/
|
2005-12-12 06:21:01 +08:00
|
|
|
event_fire_delayed();
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
if( event )
|
2005-12-12 06:21:01 +08:00
|
|
|
{
|
|
|
|
if( event_is_blocked( event ) )
|
|
|
|
{
|
2011-12-27 14:27:58 +08:00
|
|
|
blocked.push_back(event_copy(event, 1));
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( event );
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 05:20:01 +08:00
|
|
|
is_event--;
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void event_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void event_destroy()
|
|
|
|
{
|
2006-02-06 21:45:32 +08:00
|
|
|
|
2011-12-27 14:18:16 +08:00
|
|
|
for_each(events.begin(), events.end(), event_free);
|
2012-06-05 05:20:01 +08:00
|
|
|
events.clear();
|
2011-12-27 14:22:55 +08:00
|
|
|
|
|
|
|
for_each(killme.begin(), killme.end(), event_free);
|
2012-06-05 05:20:01 +08:00
|
|
|
killme.clear();
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void event_free( event_t *e )
|
|
|
|
{
|
2006-06-21 18:07:46 +08:00
|
|
|
CHECK( e, );
|
2012-02-09 11:02:25 +08:00
|
|
|
delete e;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
2006-05-14 18:16:23 +08:00
|
|
|
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2008-01-15 06:31:24 +08:00
|
|
|
void event_fire_generic_internal(const wchar_t *name, ...)
|
2007-08-20 00:42:30 +08:00
|
|
|
{
|
2008-01-15 06:31:24 +08:00
|
|
|
va_list va;
|
|
|
|
wchar_t *arg;
|
2007-08-20 00:42:30 +08:00
|
|
|
|
|
|
|
CHECK( name, );
|
2012-02-09 11:02:25 +08:00
|
|
|
|
|
|
|
event_t ev(EVENT_GENERIC);
|
|
|
|
ev.str_param1 = name;
|
|
|
|
ev.arguments.reset(new wcstring_list_t);
|
2008-01-15 06:31:24 +08:00
|
|
|
va_start( va, name );
|
|
|
|
while( (arg=va_arg(va, wchar_t *) )!= 0 )
|
|
|
|
{
|
2011-12-27 16:06:07 +08:00
|
|
|
ev.arguments->push_back(arg);
|
2008-01-15 06:31:24 +08:00
|
|
|
}
|
|
|
|
va_end( va );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-08-20 00:42:30 +08:00
|
|
|
event_fire( &ev );
|
2012-02-09 11:02:25 +08:00
|
|
|
ev.arguments.reset(NULL);
|
2007-08-20 00:42:30 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 11:02:25 +08:00
|
|
|
event_t event_t::signal_event(int sig) {
|
|
|
|
event_t event(EVENT_SIGNAL);
|
|
|
|
event.param1.signal = sig;
|
|
|
|
return event;
|
|
|
|
}
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2012-02-09 11:02:25 +08:00
|
|
|
event_t event_t::variable_event(const wcstring &str) {
|
|
|
|
event_t event(EVENT_VARIABLE);
|
|
|
|
event.str_param1 = str;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_t event_t::generic_event(const wcstring &str) {
|
|
|
|
event_t event(EVENT_GENERIC);
|
|
|
|
event.str_param1 = str;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_t::event_t(const event_t &x) :
|
|
|
|
type(x.type),
|
|
|
|
param1(x.param1),
|
|
|
|
str_param1(x.str_param1),
|
|
|
|
function_name(x.function_name)
|
|
|
|
{
|
|
|
|
const wcstring_list_t *ptr = x.arguments.get();
|
|
|
|
if (ptr)
|
|
|
|
arguments.reset(new wcstring_list_t(*ptr));
|
|
|
|
}
|