Re-implement for unix target, address checking in ND_Address_Check: now use sigaction() and siglingjmp() instead of signal().

Minor code clean-up.
This commit is contained in:
agibert 2004-08-02 21:26:03 +00:00
parent 77330558f0
commit 64baaa1bc5
3 changed files with 112 additions and 48 deletions

View File

@ -1,9 +1,9 @@
/*---------------------------------------------------------------------------------*/
/* $RCSfile: node.h,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 2.7 $ */
/* $Revision: 2.8 $ */
/* $Name: $ */
/* $Date: 2004/08/01 23:18:37 $ */
/* $Date: 2004/08/02 21:26:03 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
@ -37,10 +37,9 @@
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#ifdef _LIBVER_SUPPORT

View File

@ -1,9 +1,9 @@
/*---------------------------------------------------------------------------------*/
/* $RCSfile: libnode.c,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 2.10 $ */
/* $Revision: 2.11 $ */
/* $Name: $ */
/* $Date: 2004/08/01 23:18:38 $ */
/* $Date: 2004/08/02 21:26:03 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
@ -34,7 +34,7 @@
#include <libnode.h>
#ifdef _LIBVER_SUPPORT
VER_INFO_EXPORT( libnode, "$Revision: 2.10 $", "$Name: $", __FILE__, "$Author: agibert $")
VER_INFO_EXPORT( libnode, "$Revision: 2.11 $", "$Name: $", __FILE__, "$Author: agibert $")
#endif
@ -63,7 +63,7 @@ NDT_Status ND_Default_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT
Command_Name = "NDD_CMD_MANAGER_VERSION";
*Version_Name_Ptr = "$Revision: 2.10 $ $Name: $ $Date: 2004/08/01 23:18:38 $ $Author: agibert $";
*Version_Name_Ptr = "$Revision: 2.11 $ $Name: $ $Date: 2004/08/02 21:26:03 $ $Author: agibert $";
return( NDS_OK);
}
@ -369,7 +369,7 @@ NDT_Status ND_OpenStruct_Manager( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id,
Command_Name = "NDD_CMD_MANAGER_VERSION";
*Version_Name_Ptr = "$Revision: 2.10 $ $Name: $ $Date: 2004/08/01 23:18:38 $ $Author: agibert $";
*Version_Name_Ptr = "$Revision: 2.11 $ $Name: $ $Date: 2004/08/02 21:26:03 $ $Author: agibert $";
return( NDS_OK);
}
@ -661,11 +661,11 @@ NDT_Status ND_Library_Close_I( void)
Symbol = NDG_Base.Symbol_First_Ptr;
while (Symbol)
while( Symbol)
{
Next_Symbol = Symbol->Next;
free (Symbol->Name);
free (Symbol);
free( Symbol->Name);
free( Symbol);
Symbol = Next_Symbol;
}
@ -4128,7 +4128,7 @@ NDT_Status ND_Node_Alloc( NDT_Root *Root_Ptr, NDT_Node **Node_Ptr_Ptr )
rc = ND_Allocator_Exec_I( Root_Ptr->Allocator_Ptr, (void **)Node_Ptr_Ptr, sizeof(NDT_Node), Root_Ptr->User);
if( ND_ERROR(rc)) return(rc);
if( ND_ERROR(rc)) return( rc);
(*Node_Ptr_Ptr)->Root = NULL;
(*Node_Ptr_Ptr)->Index = NDD_INDEX_UNKNOWN;
@ -4208,7 +4208,7 @@ NDT_Status ND_Node_Root_Alloc( NDT_Root **Root_Ptr_Ptr, NDT_Index_Nb Index_Nb,
NDT_Index_Id index_id;
status = ND_Allocator_Exec_I( Allocator_Ptr, (void **)Root_Ptr_Ptr, ( sizeof (NDT_Root) + sizeof(NDT_Index) * (Index_Nb + 1)), Data_Ptr);
status = ND_Allocator_Exec_I( Allocator_Ptr, (void **)Root_Ptr_Ptr, ( sizeof( NDT_Root) + sizeof(NDT_Index) * (Index_Nb + 1)), Data_Ptr);
if( ND_ERROR(status)) return( status);
if( strlen(Manager_Name) > NDD_MANAGER_NAME_LEN_MAX) return( NDS_ERRAPI);
@ -4245,7 +4245,7 @@ NDT_Status ND_Node_Root_Alloc( NDT_Root **Root_Ptr_Ptr, NDT_Index_Nb Index_Nb,
return( NDS_KO);
}
(*Root_Ptr_Ptr)->Index_Tab[index_id].Type = NDD_INDEX_STATUS_CLOSED;
( *Root_Ptr_Ptr)->Index_Tab[index_id].Type = NDD_INDEX_STATUS_CLOSED;
}
}
@ -4329,7 +4329,7 @@ NDT_Status ND_List_Node_Add( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, NDT_N
if(!New_Node_Ptr->Left && !New_Node_Ptr->Right)
{
if (!Root_Ptr->Index_Tab[Index_Id].Tail) Root_Ptr->Index_Tab[Index_Id].Head = Root_Ptr->Index_Tab[Index_Id].Tail = New_Node_Ptr;
if( !Root_Ptr->Index_Tab[Index_Id].Tail) Root_Ptr->Index_Tab[Index_Id].Head = Root_Ptr->Index_Tab[Index_Id].Tail = New_Node_Ptr;
else
{
Root_Ptr->Index_Tab[Index_Id].Tail->Right = New_Node_Ptr;
@ -4449,7 +4449,7 @@ NDT_Status ND_List_Value_Add( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, void
node_ptr->Value = Value_Ptr;
// return ND_List_Node_Add (Root, Node);
// return ND_List_Node_Add( Root, Node);
rc = ND_List_Node_Add( Root_Ptr, Index_Id, node_ptr);
if( ND_ERROR(rc))
@ -4484,7 +4484,7 @@ NDT_Status ND_Tree_Value_Add( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, void
node_ptr->Value = Value_Ptr;
// return ND_Tree_Node_Add (Root, Node);
// return ND_Tree_Node_Add( Root, Node);
rc = ND_Tree_Node_Add( Root_Ptr, Index_Id, node_ptr);
if( ND_ERROR(rc))
@ -4525,7 +4525,7 @@ NDT_Node *ND_List_Node_Find( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, void
{
rc = ND_Manager_Exec_I( Root_Ptr, Index_Id, node_ptr, NDD_CMD_VALUE_COMP, Value_Ptr, node_ptr->Value, User_Args);
switch (rc)
switch( rc)
{
case NDS_EQUAL:
{
@ -4739,7 +4739,7 @@ NDT_Node *ND_Tree_Recursive_Make( long Depth, long Node_Number, NDT_Node *No
NDT_Node *left_node_ptr, *middle_node_ptr, *right_node_ptr;
if( !Node_Ptr) return (NULL);
if( !Node_Ptr) return( NULL);
/* On calcule le milieu de la liste */
@ -4900,7 +4900,7 @@ long ND_Tree_MaxDepth_Get( NDT_Node *Node_Ptr)
long Max_Left, Max_Right;
if( !Node_Ptr) return ( 0);
if( !Node_Ptr) return( 0);
Max_Left = ND_Tree_MaxDepth_Get( Node_Ptr->Left);
@ -4975,7 +4975,7 @@ void ND_Tree_Node_Recursive_Add( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id, N
if( Max_Depth + Depth - 1 > Root_Ptr->Index_Tab[Index_Id].Max_Depth) Root_Ptr->Index_Tab[Index_Id].Max_Depth = Max_Depth + Depth - 1;
if (Min_Depth + Depth - 1 < Root_Ptr->Index_Tab[Index_Id].Min_Depth) Root_Ptr->Index_Tab[Index_Id].Min_Depth = Min_Depth + Depth - 1 ;
if( Min_Depth + Depth - 1 < Root_Ptr->Index_Tab[Index_Id].Min_Depth) Root_Ptr->Index_Tab[Index_Id].Min_Depth = Min_Depth + Depth - 1 ;
}
}
@ -5051,14 +5051,14 @@ void ND_Tree_Recursive_Print( NDT_Node *Node_Ptr, long Depth, FILE *Out)
/* Affichage des toutes les informations (noeud et valeur) :
if (Node->Root) fprintf (Out, "Root=%p ", Node->Root);
if (Node->Parent) fprintf (Out, "Parent=%p ", Node->Parent);
if (Node->Left) fprintf (Out, "Left=%p ", Node->Left);
if (Node->Right) fprintf (Out, "Right=%p ", Node->Right);
if( Node->Root) fprintf( Out, "Root=%p ", Node->Root);
if( Node->Parent) fprintf( Out, "Parent=%p ", Node->Parent);
if( Node->Left) fprintf( Out, "Left=%p ", Node->Left);
if( Node->Right) fprintf( Out, "Right=%p ", Node->Right);
fprintf (Out, "Value=[");
ND_Manager_Exec (Node->Root->Manager, NDD_CMD_PRINT_VALUE, Node->Value, Out);
fprintf (Out, "]\n");
fprintf( Out, "Value=[");
ND_Manager_Exec( Node->Root->Manager, NDD_CMD_PRINT_VALUE, Node->Value, Out);
fprintf( Out, "]\n");
*/
/* Affichage de la valeur seule : */
@ -5087,7 +5087,7 @@ int ND_Node_Compare( void **Node1_Ptr_Ptr, void **Node2_Ptr_Ptr)
// rc = ND_Manager_Exec_I( Tmp_Root->Manager, NDD_CMD_COMP_VALUE_COMP, ((NDT_Node *)(*Node1))->Value, ((NDT_Node *)(*Node2))->Value);
rc = ND_Manager_Exec_I( ((NDT_Node *)(*Node1_Ptr_Ptr))->Root, ((NDT_Node *)(*Node1_Ptr_Ptr))->Index, *Node1_Ptr_Ptr, NDD_CMD_VALUE_COMP, ((NDT_Node *)(*Node1_Ptr_Ptr))->Value, ((NDT_Node *)(*Node2_Ptr_Ptr))->Value, NULL);
rc = ND_Manager_Exec_I( ( ( NDT_Node *)( *Node1_Ptr_Ptr))->Root, ( ( NDT_Node *)(*Node1_Ptr_Ptr))->Index, *Node1_Ptr_Ptr, NDD_CMD_VALUE_COMP, ( (NDT_Node *) (*Node1_Ptr_Ptr))->Value, ( ( NDT_Node *)( *Node2_Ptr_Ptr))->Value, NULL);
switch( (int)rc)
{
@ -5097,7 +5097,7 @@ int ND_Node_Compare( void **Node1_Ptr_Ptr, void **Node2_Ptr_Ptr)
default:
sprintf( NDG_Base.Err_String, "Error ND_Node_Compare : incorrect return code from the manager: %d", rc);
ND_Error_Print ();
ND_Error_Print();
return 0;
}
}
@ -5141,12 +5141,12 @@ NDT_Status ND_List_Sort( NDT_Root *Root_Ptr, NDT_Index_Id Index_Id)
// Tmp_Root = Root;
// qsort( (void *)tab, (size_t)(Root_Ptr->Index_Tab[Index_Id].Node_Number), sizeof(NDT_Node *), (int (__cdecl *)(const void *, const void *))&ND_Node_Compare);
qsort( (void *)tab, (size_t)(Root_Ptr->Index_Tab[Index_Id].Node_Number), sizeof(NDT_Node *), (int (*)(const void *, const void *))&ND_Node_Compare);
// qsort( (void *)tab, (size_t)(Root_Ptr->Index_Tab[Index_Id].Node_Number), sizeof(NDT_Node *), ( int (__cdecl *)(const void *, const void *))&ND_Node_Compare);
qsort( (void *)tab, (size_t)( Root_Ptr->Index_Tab[Index_Id].Node_Number), sizeof( NDT_Node *), ( int (*)( const void *, const void *))&ND_Node_Compare);
/* On met à jour les liens entre les noeuds */
for (i = 0; i < Root_Ptr->Index_Tab[Index_Id].Node_Number; i++)
for( i = 0; i < Root_Ptr->Index_Tab[Index_Id].Node_Number; i++)
{
node_ptr = (NDT_Node *)tab[i];
@ -5207,7 +5207,7 @@ NDT_Node *ND_Tree_Node_Last_Recursive_Get( NDT_Node *Node_Ptr)
/*------------------------------------------------------------------------------*/
/* Redéfinition de la fonction malloc () avec retour de type NDT_Status */
/* Redéfinition de la fonction malloc() avec retour de type NDT_Status */
/*------------------------------------------------------------------------------*/
NDT_Status ND_Default_Allocator( size_t Size, void **Ptr_Ptr, void *Data_Ptr)
@ -5225,7 +5225,7 @@ NDT_Status ND_Default_Allocator( size_t Size, void **Ptr_Ptr, void *Data_Ptr
/*------------------------------------------------------------------------------*/
/* Redéfinition de la fonction free () avec retour de type NDT_Status */
/* Redéfinition de la fonction free() avec retour de type NDT_Status */
/*------------------------------------------------------------------------------*/
NDT_Status ND_Default_Desallocator( void *Ptr, void *Data_Ptr)
@ -5565,26 +5565,81 @@ void ND_Tree_Link_Recursive_Check( NDT_Node *Node_Ptr, int *Nb_Detected_Ptr,
NDT_Status ND_Address_Check( void *Address)
{
#if !defined(_MSC_VER)
NDT_Status status;
int rc;
int test;
struct sigaction act =
{
ND_Signal_Trap, //
0, //
0, // sa_mask
0, // sa_flags
0 //
}, oact_bus, oact_segv;
NDG_Base.Sig_Trapped = 0;
/* On trappe les signaux SIGBUS et SIGSEGV */
// signal( SIGBUS, NDG_Base.Signal_Trap); Win32
// signal( SIGSEGV, NDG_Base.Signal_Trap); Win32
/* Trap SIGBUS and SIGSEGV */
if( ( rc = sigaction( SIGBUS, &act, &oact_bus)) != 0)
{
sprintf( NDG_Base.Err_String, "Error ND_Address_Check: sigaction (Add SIGBUS Hdl) rc: %d errno: %d", rc, errno);
ND_Error_Print();
return( NDS_KO);
}
if( ( rc = sigaction( SIGSEGV, &act, &oact_segv)) != 0)
{
sprintf( NDG_Base.Err_String, "Error ND_Address_Check: sigaction (Add SIGSEGV Hdl) rc: %d errno: %d", rc, errno);
ND_Error_Print();
return( NDS_KO);
}
/* On tente d'accéder à l'adresse fournie */
test = *( (int *)Address);
if( sigsetjmp( NDG_Base.SigLongJmp_Env, 1) == 0 )
{
test = *( (int *)Address);
// sigrelse( SIGBUS); Win32
// sigrelse( SIGSEGV); Win32
status = NDS_OK;
}
else
{
status = NDS_KO;
}
if( NDG_Base.Sig_Trapped != 0) return( NDS_KO);
/* Untrap SIGBUS and SIGSEGV */
if( ( rc = sigaction( SIGBUS, &oact_bus, &act)) != 0)
{
sprintf( NDG_Base.Err_String, "Error ND_Address_Check: sigaction (Remove SIGBUS Hdl) rc: %d errno: %d", rc, errno);
ND_Error_Print();
return( NDS_KO);
}
if( ( rc = sigaction( SIGSEGV, &oact_segv, &act)) != 0)
{
sprintf( NDG_Base.Err_String, "Error ND_Address_Check: sigaction (Remove SIGSEGV Hdl) rc: %d errno: %d", rc, errno);
ND_Error_Print();
return( NDS_KO);
}
return( status);
#else
return( NDS_OK);
#endif
}
@ -5598,6 +5653,8 @@ NDT_Status ND_Address_Check( void *Address)
void ND_Signal_Trap( int Sig_Num)
{
NDG_Base.Sig_Trapped = Sig_Num;
siglongjmp( NDG_Base.SigLongJmp_Env, 1);
}

View File

@ -1,9 +1,9 @@
/*---------------------------------------------------------------------------------*/
/* $RCSfile: libnode.h,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 2.5 $ */
/* $Revision: 2.6 $ */
/* $Name: $ */
/* $Date: 2004/08/01 23:18:38 $ */
/* $Date: 2004/08/02 21:26:03 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
@ -29,11 +29,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#if !defined(_MSC_VER)
# include <errno.h>
# include <signal.h>
# include <setjmp.h>
#endif
//#include <dlfcn.h>
#include <node.h>
@ -78,6 +85,7 @@ typedef struct NDT_Base
char Err_String[512];
FILE *Err_Stream;
int Sig_Trapped;
sigjmp_buf SigLongJmp_Env;
NDT_Symbol *Symbol_First_Ptr;
NDT_Index_Type OpenStruct_Index_Type[1];
NDT_Root *OpenStruct_Ptr;
@ -89,7 +97,7 @@ NDT_Base NDG_Base =
NDD_TRUE,
"",
(FILE *)-1,
0,
0,{},
(NDT_Symbol *)NULL,
{ NDD_INDEX_STATUS_OPENED | NDD_INDEX_TYPE_LIST | NDD_INDEX_SUBTYPE_FIFO},
(NDT_Root *)NULL