2005-10-24 15:26:25 +00:00
|
|
|
/** \file event.c
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2005-10-24 15:26:25 +00:00
|
|
|
Functions for handling event triggers
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
*/
|
2006-08-11 01:18:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2005-10-05 22:37:08 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2006-01-20 14:27:21 +00:00
|
|
|
#include "wutil.h"
|
2005-10-05 22:37:08 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "signal.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2006-02-09 15:50:20 +00:00
|
|
|
#include "halloc_util.h"
|
2005-10-05 22:37:08 +00: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 15:26:25 +00:00
|
|
|
/**
|
|
|
|
Number of delivered signals
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
int count;
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
|
|
|
Whether signals have been skipped
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
int overflow;
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
|
|
|
Array of signal events
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
int signal[SIG_UNHANDLED_MAX];
|
|
|
|
}
|
|
|
|
signal_list_t;
|
|
|
|
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
2005-10-05 22:37:08 +00: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 13:00:39 +00:00
|
|
|
static signal_list_t sig_list[]={{0,0},{0,0}};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
The index of sig_list that is the list of signals currently written to
|
|
|
|
*/
|
|
|
|
static int active_list=0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
List of event handlers
|
|
|
|
*/
|
|
|
|
static array_list_t *events;
|
|
|
|
/**
|
|
|
|
List of event handlers that should be removed
|
|
|
|
*/
|
|
|
|
static array_list_t *killme;
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
/**
|
|
|
|
List of events that have been sent but have not yet been delivered because they are blocked.
|
|
|
|
*/
|
|
|
|
static array_list_t *blocked;
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
/**
|
|
|
|
Tests if one event instance matches the definition of a event
|
2006-02-02 15:33:30 +00:00
|
|
|
class. If both the class and the instance name a function,
|
|
|
|
they must name the same function.
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
static int event_match( event_t *class, event_t *instance )
|
|
|
|
{
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
if( class->function_name && instance->function_name )
|
|
|
|
{
|
|
|
|
if( wcscmp( class->function_name, instance->function_name ) != 0 )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( class->type == EVENT_ANY )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if( class->type != instance->type )
|
|
|
|
return 0;
|
|
|
|
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
switch( class->type )
|
|
|
|
{
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
case EVENT_SIGNAL:
|
2005-10-11 19:31:16 +00:00
|
|
|
if( class->param1.signal == EVENT_ANY_SIGNAL )
|
2005-10-05 22:37:08 +00:00
|
|
|
return 1;
|
2005-10-11 19:31:16 +00:00
|
|
|
return class->param1.signal == instance->param1.signal;
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
case EVENT_VARIABLE:
|
2005-10-11 19:31:16 +00:00
|
|
|
return wcscmp( instance->param1.variable,
|
|
|
|
class->param1.variable )==0;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
case EVENT_EXIT:
|
2005-10-11 19:31:16 +00:00
|
|
|
if( class->param1.pid == EVENT_ANY_PID )
|
2005-10-05 22:37:08 +00:00
|
|
|
return 1;
|
2005-10-11 19:31:16 +00:00
|
|
|
return class->param1.pid == instance->param1.pid;
|
2005-10-15 00:51:26 +00:00
|
|
|
|
|
|
|
case EVENT_JOB_ID:
|
|
|
|
return class->param1.job_id == instance->param1.job_id;
|
2007-08-19 16:42:30 +00:00
|
|
|
|
|
|
|
case EVENT_GENERIC:
|
|
|
|
return wcscmp( instance->param1.param,
|
|
|
|
class->param1.param )==0;
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
This should never be reached
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create an identical copy of an event. Use deep copying, i.e. make
|
|
|
|
duplicates of any strings used as well.
|
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
static event_t *event_copy( event_t *event, int copy_arguments )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
|
|
|
event_t *e = malloc( sizeof( event_t ) );
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
if( !e )
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
memcpy( e, event, sizeof(event_t));
|
|
|
|
|
|
|
|
if( e->function_name )
|
|
|
|
e->function_name = wcsdup( e->function_name );
|
|
|
|
|
|
|
|
if( e->type == EVENT_VARIABLE )
|
2005-10-11 19:31:16 +00:00
|
|
|
e->param1.variable = wcsdup( e->param1.variable );
|
2007-08-19 16:42:30 +00:00
|
|
|
else if( e->type == EVENT_GENERIC )
|
|
|
|
e->param1.param = wcsdup( e->param1.param );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
al_init( &e->arguments );
|
|
|
|
if( copy_arguments )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0; i<al_get_count( &event->arguments ); i++ )
|
|
|
|
{
|
|
|
|
al_push( &e->arguments, wcsdup( (wchar_t *)al_get( &event->arguments, i ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2006-01-23 20:40:14 +00:00
|
|
|
/**
|
|
|
|
Test if specified event is blocked
|
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
static int event_is_blocked( event_t *e )
|
|
|
|
{
|
|
|
|
block_t *block;
|
|
|
|
event_block_t *eb;
|
|
|
|
|
|
|
|
for( block = current_block; block; block = block->outer )
|
|
|
|
{
|
|
|
|
for( eb = block->first_event_block; eb; eb=eb->next )
|
|
|
|
{
|
|
|
|
if( eb->type & (1<<EVENT_ANY ) )
|
|
|
|
return 1;
|
|
|
|
if( eb->type & (1<<e->type) )
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( eb = global_event_block; eb; eb=eb->next )
|
|
|
|
{
|
|
|
|
if( eb->type & (1<<EVENT_ANY ) )
|
|
|
|
return 1;
|
|
|
|
if( eb->type & (1<<e->type) )
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-01 15:49:11 +00:00
|
|
|
const wchar_t *event_get_desc( event_t *e )
|
|
|
|
{
|
2006-06-21 10:07:46 +00:00
|
|
|
|
2007-10-02 10:09:37 +00:00
|
|
|
/*
|
|
|
|
String buffer used for formating event descriptions in event_get_desc()
|
|
|
|
*/
|
|
|
|
static string_buffer_t *get_desc_buff=0;
|
|
|
|
|
2006-06-21 10:07:46 +00:00
|
|
|
CHECK( e, 0 );
|
|
|
|
|
2006-02-01 15:49:11 +00:00
|
|
|
if( !get_desc_buff )
|
|
|
|
{
|
2006-02-09 15:50:20 +00:00
|
|
|
get_desc_buff=sb_halloc( global_context );
|
2006-02-01 15:49:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sb_clear( get_desc_buff );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( e->type )
|
|
|
|
{
|
|
|
|
|
|
|
|
case EVENT_SIGNAL:
|
|
|
|
sb_printf( get_desc_buff, _(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal ), signal_get_desc( e->param1.signal ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_VARIABLE:
|
|
|
|
sb_printf( get_desc_buff, _(L"handler for variable '%ls'"), e->param1.variable );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_EXIT:
|
|
|
|
if( e->param1.pid > 0 )
|
|
|
|
{
|
|
|
|
sb_printf( get_desc_buff, _(L"exit handler for process %d"), e->param1.pid );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
job_t *j = job_get_from_pid( -e->param1.pid );
|
|
|
|
if( j )
|
|
|
|
sb_printf( get_desc_buff, _(L"exit handler for job %d, '%ls'"), j->job_id, j->command );
|
|
|
|
else
|
|
|
|
sb_printf( get_desc_buff, _(L"exit handler for job with process group %d"), -e->param1.pid );
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_JOB_ID:
|
|
|
|
{
|
|
|
|
job_t *j = job_get( e->param1.job_id );
|
|
|
|
if( j )
|
|
|
|
sb_printf( get_desc_buff, _(L"exit handler for job %d, '%ls'"), j->job_id, j->command );
|
|
|
|
else
|
|
|
|
sb_printf( get_desc_buff, _(L"exit handler for job with job id %d"), j->job_id );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-08-19 16:42:30 +00:00
|
|
|
case EVENT_GENERIC:
|
|
|
|
sb_printf( get_desc_buff, _(L"handler for generic event '%ls'"), e->param1.param );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
sb_printf( get_desc_buff, _(L"Unknown event type") );
|
|
|
|
break;
|
|
|
|
|
2006-02-01 15:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (const wchar_t *)get_desc_buff->buff;
|
|
|
|
}
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
void event_add_handler( event_t *event )
|
|
|
|
{
|
2005-12-11 22:21:01 +00:00
|
|
|
event_t *e;
|
|
|
|
|
2006-06-21 10:07:46 +00:00
|
|
|
CHECK( event, );
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
e = event_copy( event, 0 );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
if( !events )
|
2006-02-09 15:50:20 +00:00
|
|
|
events = al_new();
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2005-10-06 11:54:16 +00:00
|
|
|
if( e->type == EVENT_SIGNAL )
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
signal_handle( e->param1.signal, 1 );
|
2005-10-06 11:54:16 +00:00
|
|
|
}
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
al_push( events, e );
|
|
|
|
}
|
|
|
|
|
|
|
|
void event_remove( event_t *criterion )
|
|
|
|
{
|
|
|
|
int i;
|
2005-10-06 11:54:16 +00:00
|
|
|
array_list_t *new_list=0;
|
|
|
|
event_t e;
|
|
|
|
|
2006-06-21 10:07:46 +00:00
|
|
|
CHECK( criterion, );
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
/*
|
2006-05-18 13:00:39 +00: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-05 22:37:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if( !events )
|
2006-06-21 10:07:46 +00:00
|
|
|
return;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
for( i=0; i<al_get_count( events); i++ )
|
|
|
|
{
|
|
|
|
event_t *n = (event_t *)al_get( events, i );
|
|
|
|
if( event_match( criterion, n ) )
|
|
|
|
{
|
|
|
|
if( !killme )
|
|
|
|
killme = al_new();
|
|
|
|
|
|
|
|
al_push( killme, n );
|
2005-10-06 11:54:16 +00: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 )
|
|
|
|
{
|
|
|
|
e.type = EVENT_SIGNAL;
|
2005-10-11 19:31:16 +00:00
|
|
|
e.param1.signal = n->param1.signal;
|
2005-10-06 11:54:16 +00:00
|
|
|
e.function_name = 0;
|
|
|
|
|
|
|
|
if( event_get( &e, 0 ) == 1 )
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
signal_handle( e.param1.signal, 0 );
|
2005-10-06 11:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-06 11:54:16 +00:00
|
|
|
if( !new_list )
|
|
|
|
new_list = al_new();
|
2005-10-05 22:37:08 +00:00
|
|
|
al_push( new_list, n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
al_destroy( events );
|
2005-10-06 11:54:16 +00:00
|
|
|
free( events );
|
2005-10-05 22:37:08 +00:00
|
|
|
events = new_list;
|
|
|
|
}
|
|
|
|
|
2005-10-06 11:54:16 +00:00
|
|
|
int event_get( event_t *criterion, array_list_t *out )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
2005-10-06 11:54:16 +00:00
|
|
|
int found = 0;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
if( !events )
|
2005-10-06 11:54:16 +00:00
|
|
|
return 0;
|
2006-06-21 10:07:46 +00:00
|
|
|
|
|
|
|
CHECK( criterion, 0 );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
for( i=0; i<al_get_count( events); i++ )
|
|
|
|
{
|
|
|
|
event_t *n = (event_t *)al_get( events, i );
|
|
|
|
if( event_match(criterion, n ) )
|
2005-10-06 11:54:16 +00:00
|
|
|
{
|
|
|
|
found++;
|
|
|
|
if( out )
|
|
|
|
al_push( out, n );
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
2005-10-06 11:54:16 +00:00
|
|
|
return found;
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Free all events in the kill list
|
|
|
|
*/
|
|
|
|
static void event_free_kills()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if( !killme )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for( i=0; i<al_get_count( killme ); i++ )
|
|
|
|
{
|
2005-10-06 11:54:16 +00:00
|
|
|
event_t *roadkill = (event_t *)al_get( killme, i );
|
2005-10-05 22:37:08 +00:00
|
|
|
event_free( roadkill );
|
|
|
|
}
|
|
|
|
al_truncate( killme, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test if the specified event is waiting to be killed
|
|
|
|
*/
|
|
|
|
static int event_is_killed( event_t *e )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if( !killme )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for( i=0; i<al_get_count( killme ); i++ )
|
|
|
|
{
|
|
|
|
event_t *roadkill = (event_t *)al_get( events, i );
|
|
|
|
if( roadkill ==e )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Perform the specified event. Since almost all event firings will
|
2006-07-29 17:31:59 +00: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-05 22:37:08 +00:00
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
static void event_fire_internal( event_t *event )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
string_buffer_t *b=0;
|
|
|
|
array_list_t *fire=0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
First we free all events that have been removed
|
|
|
|
*/
|
|
|
|
event_free_kills();
|
|
|
|
|
2005-10-06 11:54:16 +00:00
|
|
|
if( !events )
|
|
|
|
return;
|
|
|
|
|
2005-10-05 22:37:08 +00: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.
|
|
|
|
*/
|
|
|
|
for( i=0; i<al_get_count( events ); i++ )
|
|
|
|
{
|
|
|
|
event_t *criterion = (event_t *)al_get( events, i );
|
2005-10-17 13:24:12 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
/*
|
|
|
|
Check if this event is a match
|
|
|
|
*/
|
|
|
|
if(event_match( criterion, event ) )
|
|
|
|
{
|
|
|
|
if( !fire )
|
|
|
|
fire = al_new();
|
|
|
|
al_push( fire, criterion );
|
|
|
|
}
|
|
|
|
}
|
2005-10-15 00:51:26 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
/*
|
|
|
|
No matches. Time to return.
|
|
|
|
*/
|
|
|
|
if( !fire )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Iterate over our list of matching events
|
|
|
|
*/
|
|
|
|
|
|
|
|
for( i=0; i<al_get_count( fire ); i++ )
|
|
|
|
{
|
|
|
|
event_t *criterion = (event_t *)al_get( fire, i );
|
2006-11-02 13:48:59 +00:00
|
|
|
int prev_status;
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
/*
|
|
|
|
Check if this event has been removed, if so, dont fire it
|
|
|
|
*/
|
|
|
|
if( event_is_killed( criterion ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fire event
|
|
|
|
*/
|
|
|
|
if( !b )
|
|
|
|
b = sb_new();
|
|
|
|
else
|
|
|
|
sb_clear( b );
|
|
|
|
|
|
|
|
sb_append( b, criterion->function_name );
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
for( j=0; j<al_get_count(&event->arguments); j++ )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
2006-02-14 11:47:39 +00:00
|
|
|
wchar_t *arg_esc = escape( (wchar_t *)al_get( &event->arguments, j), 1 );
|
2005-10-05 22:37:08 +00:00
|
|
|
sb_append( b, L" " );
|
|
|
|
sb_append( b, arg_esc );
|
|
|
|
free( arg_esc );
|
|
|
|
}
|
2005-10-11 19:23:43 +00:00
|
|
|
|
|
|
|
// debug( 1, L"Event handler fires command '%ls'", (wchar_t *)b->buff );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2005-12-03 19:46:18 +00:00
|
|
|
/*
|
|
|
|
Event handlers are not part of the main flow of code, so
|
2006-07-29 17:31:59 +00:00
|
|
|
they are marked as non-interactive
|
2005-12-03 19:46:18 +00:00
|
|
|
*/
|
2006-02-16 13:36:32 +00:00
|
|
|
proc_push_interactive(0);
|
2006-11-02 13:48:59 +00:00
|
|
|
prev_status = proc_get_last_status();
|
2006-02-01 15:49:11 +00:00
|
|
|
parser_push_block( EVENT );
|
|
|
|
current_block->param1.event = event;
|
2005-10-05 22:37:08 +00:00
|
|
|
eval( (wchar_t *)b->buff, 0, TOP );
|
2006-02-01 15:49:11 +00:00
|
|
|
parser_pop_block();
|
2006-02-16 13:36:32 +00:00
|
|
|
proc_pop_interactive();
|
2006-11-02 13:48:59 +00:00
|
|
|
proc_set_last_status( prev_status );
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( b )
|
|
|
|
{
|
|
|
|
sb_destroy( b );
|
|
|
|
free( b );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fire )
|
|
|
|
{
|
|
|
|
al_destroy( fire );
|
|
|
|
free( fire );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free killed events
|
|
|
|
*/
|
|
|
|
event_free_kills();
|
2005-10-11 19:23:43 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-15 00:51:26 +00:00
|
|
|
Handle all pending signal events
|
2005-10-05 22:37:08 +00:00
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
static void event_fire_delayed()
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
2005-12-11 22:21:01 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2006-07-29 17:31:59 +00:00
|
|
|
/*
|
|
|
|
If is_event is one, we are running the event-handler non-recursively.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
if( blocked && is_event==1)
|
|
|
|
{
|
|
|
|
array_list_t *new_blocked = 0;
|
|
|
|
|
|
|
|
for( i=0; i<al_get_count( blocked ); i++ )
|
|
|
|
{
|
|
|
|
event_t *e = (event_t *)al_get( blocked, i );
|
|
|
|
if( event_is_blocked( e ) )
|
|
|
|
{
|
|
|
|
if( !new_blocked )
|
|
|
|
new_blocked = al_new();
|
|
|
|
al_push( new_blocked, e );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( e );
|
|
|
|
event_free( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
al_destroy( blocked );
|
|
|
|
free( blocked );
|
|
|
|
blocked = new_blocked;
|
|
|
|
}
|
2006-08-13 01:38:03 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
while( sig_list[active_list].count > 0 )
|
|
|
|
{
|
|
|
|
signal_list_t *lst;
|
|
|
|
event_t e;
|
2005-12-11 22:21:01 +00:00
|
|
|
al_init( &e.arguments );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2005-10-14 11:40:33 +00:00
|
|
|
/*
|
|
|
|
Switch signal lists
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
sig_list[1-active_list].count=0;
|
|
|
|
sig_list[1-active_list].overflow=0;
|
|
|
|
active_list=1-active_list;
|
2005-10-14 11:40:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Set up
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
e.type=EVENT_SIGNAL;
|
|
|
|
e.function_name=0;
|
|
|
|
|
|
|
|
lst = &sig_list[1-active_list];
|
|
|
|
|
|
|
|
if( lst->overflow )
|
|
|
|
{
|
2006-01-04 12:51:02 +00:00
|
|
|
debug( 0, _( L"Signal list overflow. Signals have been ignored." ) );
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
2005-10-14 11:40:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Send all signals in our private list
|
|
|
|
*/
|
2005-10-05 22:37:08 +00:00
|
|
|
for( i=0; i<lst->count; i++ )
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
e.param1.signal = lst->signal[i];
|
2005-12-11 22:21:01 +00:00
|
|
|
al_set( &e.arguments, 0, sig2wcs( e.param1.signal ) );
|
|
|
|
if( event_is_blocked( &e ) )
|
|
|
|
{
|
|
|
|
if( !blocked )
|
|
|
|
blocked = al_new();
|
|
|
|
al_push( blocked, event_copy(&e, 1) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( &e );
|
|
|
|
}
|
2005-10-14 11:40:33 +00:00
|
|
|
}
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
al_destroy( &e.arguments );
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
void event_fire( event_t *event )
|
2005-10-05 22:37:08 +00:00
|
|
|
{
|
2005-10-11 19:23:43 +00:00
|
|
|
is_event++;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
|
|
|
if( event && (event->type == EVENT_SIGNAL) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This means we are in a signal handler. We must be very
|
|
|
|
careful not do do anything that could cause a memory
|
2006-07-29 17:31:59 +00:00
|
|
|
allocation or something else that might be bad when in a
|
2005-10-05 22:37:08 +00:00
|
|
|
signal handler.
|
|
|
|
*/
|
|
|
|
if( sig_list[active_list].count < SIG_UNHANDLED_MAX )
|
2005-10-11 19:31:16 +00:00
|
|
|
sig_list[active_list].signal[sig_list[active_list].count++]=event->param1.signal;
|
2005-10-05 22:37:08 +00:00
|
|
|
else
|
|
|
|
sig_list[active_list].overflow=1;
|
2005-10-11 19:23:43 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
else
|
|
|
|
{
|
2007-04-21 08:11:22 +00:00
|
|
|
/*
|
|
|
|
Fire events triggered by signals
|
|
|
|
*/
|
2005-12-11 22:21:01 +00:00
|
|
|
event_fire_delayed();
|
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
if( event )
|
2005-12-11 22:21:01 +00:00
|
|
|
{
|
|
|
|
if( event_is_blocked( event ) )
|
|
|
|
{
|
|
|
|
if( !blocked )
|
|
|
|
blocked = al_new();
|
|
|
|
|
|
|
|
al_push( blocked, event_copy(event, 1) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_fire_internal( event );
|
|
|
|
}
|
|
|
|
}
|
2005-10-11 19:23:43 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
2005-12-11 23:30:01 +00:00
|
|
|
is_event--;
|
2005-10-05 22:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void event_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void event_destroy()
|
|
|
|
{
|
2006-02-06 13:45:32 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
if( events )
|
|
|
|
{
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( events, (void (*)(void *))&event_free );
|
2005-10-05 22:37:08 +00:00
|
|
|
al_destroy( events );
|
2005-10-06 11:54:16 +00:00
|
|
|
free( events );
|
2005-10-05 22:37:08 +00:00
|
|
|
events=0;
|
|
|
|
}
|
2006-02-06 13:45:32 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
if( killme )
|
|
|
|
{
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( killme, (void (*)(void *))&event_free );
|
2005-10-05 22:37:08 +00:00
|
|
|
al_destroy( killme );
|
2005-10-06 11:54:16 +00:00
|
|
|
free( killme );
|
2005-10-05 22:37:08 +00:00
|
|
|
killme=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void event_free( event_t *e )
|
|
|
|
{
|
2006-06-21 10:07:46 +00:00
|
|
|
CHECK( e, );
|
|
|
|
|
2005-12-11 22:21:01 +00:00
|
|
|
/*
|
|
|
|
When apropriate, we clear the argument vector
|
|
|
|
*/
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( &e->arguments, &free );
|
2005-12-11 22:21:01 +00:00
|
|
|
al_destroy( &e->arguments );
|
|
|
|
|
2005-10-06 11:54:16 +00:00
|
|
|
free( (void *)e->function_name );
|
2005-10-05 22:37:08 +00:00
|
|
|
if( e->type == EVENT_VARIABLE )
|
2007-08-19 16:42:30 +00:00
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
free( (void *)e->param1.variable );
|
2007-08-19 16:42:30 +00:00
|
|
|
}
|
|
|
|
else if( e->type == EVENT_GENERIC )
|
|
|
|
{
|
|
|
|
free( (void *)e->param1.param );
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
free( e );
|
|
|
|
}
|
2006-05-14 10:16:23 +00:00
|
|
|
|
2007-08-19 16:42:30 +00:00
|
|
|
|
|
|
|
void event_fire_generic(const wchar_t *name)
|
|
|
|
{
|
|
|
|
event_t ev;
|
|
|
|
|
|
|
|
CHECK( name, );
|
|
|
|
|
|
|
|
ev.type = EVENT_GENERIC;
|
|
|
|
ev.param1.param = name;
|
|
|
|
ev.function_name=0;
|
|
|
|
|
|
|
|
al_init( &ev.arguments );
|
|
|
|
event_fire( &ev );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|