The following commit has been merged in the master branch: commit a9e259cceddb2d602bbaa4c3b0cbe7a06c770057 Author: Marek Lindner lindner_marek@yahoo.de Date: Sat Mar 10 13:09:37 2007 +0100
exit gracefully on sigsegv code cleanup
diff --git a/allocate.c b/allocate.c index fab9849..cd43162 100644 --- a/allocate.c +++ b/allocate.c @@ -147,7 +147,7 @@ void checkIntegrity(void) for ( memoryWalker = memoryList; memoryWalker != NULL; memoryWalker = memoryWalker->next ) {
if ( memoryWalker->counter != 0 ) - fprintf( stderr, " tag: %i, num malloc: %i, bytes per malloc: %i, total: %i\n", memoryWalker->tag, memoryWalker->counter, memoryWalker->length, memoryWalker->counter * memoryWalker->length ); + fprintf( stderr, " tag: %''4i, num malloc: %''4i, bytes per malloc: %''4i, total: %''6i\n", memoryWalker->tag, memoryWalker->counter, memoryWalker->length, memoryWalker->counter * memoryWalker->length );
}
diff --git a/batman.c b/batman.c index e89535e..6167373 100644 --- a/batman.c +++ b/batman.c @@ -558,19 +558,19 @@ int8_t batman() { struct neigh_node *neigh_node; struct hna_node *hna_node; struct forw_node *forw_node; - uint32_t neigh, hna, netmask, debug_timeout, select_timeout; + uint32_t neigh, hna, netmask, debug_timeout, select_timeout, curr_time; unsigned char in[1501], *hna_recv_buff; static char orig_str[ADDR_STR_LEN], neigh_str[ADDR_STR_LEN]; int16_t hna_buff_count, hna_buff_len; uint8_t forward_old, if_rp_filter_all_old, if_rp_filter_default_old; uint8_t is_my_addr, is_my_orig, is_broadcast, is_duplicate, is_bidirectional, forward_duplicate_packet; int8_t res; - uint32_t time_count = 0, curr_time; +
debug_timeout = get_time(); bidirectional_timeout = orginator_interval * 3;
- if ( NULL == ( orig_hash = hash_new( 128, orig_comp, orig_choose ) ) ) + if ( NULL == ( orig_hash = hash_new( 128, compare_orig, choose_orig ) ) ) return(-1);
/* for profiling the functions */ @@ -631,12 +631,6 @@ int8_t batman() {
debug_output( 4, " \n \n" );
- if(vis_if.sock && time_count == 50) - { - time_count = 0; - send_vis_packet(); - } - /* harden select_timeout against sudden time change (e.g. ntpdate) */ curr_time = get_time(); select_timeout = ( curr_time < ((struct forw_node *)forw_list.next)->send_time ? ((struct forw_node *)forw_list.next)->send_time - curr_time : 10 ); @@ -673,28 +667,6 @@ int8_t batman() { }
- addr_to_string( ((struct packet *)&in)->orig, orig_str, sizeof (orig_str) ); - addr_to_string( neigh, neigh_str, sizeof (neigh_str) ); - debug_output( 4, "new packet - orig: %s, sender: %s\n", orig_str, neigh_str ); - - /*if ( is_duplicate ) - output("Duplicate packet \n"); - - if ( in.orig == neigh ) - output("Originator packet from neighbour \n"); - - if ( is_my_orig == 1 ) - output("Originator packet from myself (via neighbour) \n"); - - if ( in.flags & UNIDIRECTIONAL ) - output("Packet with unidirectional flag \n"); - - if ( is_bidirectional ) - output("received via bidirectional link \n"); - - if ( !( in.flags & UNIDIRECTIONAL ) && ( !is_bidirectional ) ) - output("neighbour thinks connection is bidirectional - I disagree \n");*/ - if ( ((struct packet *)&in)->gwflags != 0 ) debug_output( 4, "Is an internet gateway (class %i) \n", ((struct packet *)&in)->gwflags );
@@ -722,10 +694,6 @@ int8_t batman() { }
- /*if ( ( ! is_my_addr ) && ( ! is_my_orig ) ) - debug_output( 3, "IP: %s send packet with sequence number: %i from %s\n", neigh_str, ((struct packet *)&in)->seqno, orig_str );*/ - - if ( ((struct packet *)&in)->version != COMPAT_VERSION ) {
debug_output( 4, "Drop packet: incompatible batman version (%i) \n", ((struct packet *)&in)->version ); @@ -771,7 +739,7 @@ int8_t batman() {
/* update ranking */ if ( ( is_bidirectional ) && ( !is_duplicate ) ) - update_originator( orig_node, (struct packet *)&in, neigh, if_incoming, hna_recv_buff, hna_buff_len ); + update_orig( orig_node, (struct packet *)&in, neigh, if_incoming, hna_recv_buff, hna_buff_len );
/* is single hop (direct) neighbour */ if ( ((struct packet *)&in)->orig == neigh ) { @@ -853,24 +821,30 @@ int8_t batman() {
}
+ send_outstanding_packets();
- purge_orginator( get_time() );
if ( debug_timeout + 1000 < get_time() ) {
debug_timeout = get_time();
- debug_orginator(); + purge_orig( get_time() ); + + debug_orig();
checkIntegrity();
+ if ( debug_clients.clients_num[2] > 0 ) + prof_print(); + if ( ( routing_class != 0 ) && ( curr_gateway == NULL ) ) choose_gw();
- } + if ( vis_if.sock ) + send_vis_packet();
- time_count++; + }
}
@@ -878,7 +852,7 @@ int8_t batman() { if ( debug_level > 0 ) printf( "Deleting all BATMAN routes\n" );
- purge_orginator( get_time() + ( 5 * TIMEOUT ) + orginator_interval ); + purge_orig( get_time() + ( 5 * TIMEOUT ) + orginator_interval );
hash_delete( orig_hash );
diff --git a/batman.h b/batman.h index 4edf79b..575b17c 100644 --- a/batman.h +++ b/batman.h @@ -223,10 +223,5 @@ void update_gw_list( struct orig_node *orig_node, uint8_t new_gwflags ); void choose_gw(); void del_default_route(); int8_t add_default_route(); -void debug_output( int8_t debug_prio, char *format, ... ); -uint32_t get_time( void ); -void addr_to_string( uint32_t addr, char *str, int32_t len ); -int32_t rand_num( int32_t limit ); -int8_t send_packet( unsigned char *packet_buff, int32_t packet_buff_len, struct sockaddr_in *broad, int32_t send_sock );
#endif diff --git a/orginator.c b/orginator.c index d720c87..6f3cae8 100644 --- a/orginator.c +++ b/orginator.c @@ -20,13 +20,14 @@
#include <string.h> +#include "os.h" #include "batman.h"
/* needed for hash, compares 2 struct orig_node, but only their ip-addresses. assumes that * the ip address is the first field in the struct */ -int orig_comp(void *data1, void *data2) { +int compare_orig(void *data1, void *data2) {
return(memcmp(data1, data2, 4));
@@ -36,7 +37,7 @@ int orig_comp(void *data1, void *data2) {
/* hashfunction to choose an entry in a hash table of given size */ /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */ -int orig_choose(void *data, int32_t size) { +int choose_orig(void *data, int32_t size) {
unsigned char *key= data; uint32_t hash = 0; @@ -108,7 +109,7 @@ struct orig_node *get_orig_node( uint32_t addr ) {
-void update_originator( struct orig_node *orig_node, struct packet *in, uint32_t neigh, struct batman_if *if_incoming, unsigned char *hna_recv_buff, int16_t hna_buff_len ) { +void update_orig( struct orig_node *orig_node, struct packet *in, uint32_t neigh, struct batman_if *if_incoming, unsigned char *hna_recv_buff, int16_t hna_buff_len ) {
prof_start( PROF_update_originator ); struct list_head *neigh_pos; @@ -199,7 +200,7 @@ void update_originator( struct orig_node *orig_node, struct packet *in, uint32_t
-void purge_orginator( uint32_t curr_time ) { +void purge_orig( uint32_t curr_time ) {
prof_start( PROF_purge_orginator ); struct hash_it_t *hashit = NULL; @@ -303,7 +304,7 @@ void purge_orginator( uint32_t curr_time ) {
-void debug_orginator() { +void debug_orig() {
struct hash_it_t *hashit = NULL; struct list_head *forw_pos, *orig_pos, *neigh_pos; @@ -409,9 +410,6 @@ void debug_orginator() {
}
- if ( debug_clients.clients_num[2] > 0 ) - prof_print(); - }
diff --git a/orginator.h b/orginator.h index ece749b..af1ea96 100644 --- a/orginator.h +++ b/orginator.h @@ -19,10 +19,10 @@
-int orig_comp(void *data1, void *data2); -int orig_choose(void *data, int32_t size); +int compare_orig(void *data1, void *data2); +int choose_orig(void *data, int32_t size); struct orig_node *get_orig_node( uint32_t addr ); -void update_originator( struct orig_node *orig_node, struct packet *in, uint32_t neigh, struct batman_if *if_incoming, unsigned char *hna_recv_buff, int16_t hna_buff_len ); -void purge_orginator( uint32_t curr_time ); -void debug_orginator(); +void update_orig( struct orig_node *orig_node, struct packet *in, uint32_t neigh, struct batman_if *if_incoming, unsigned char *hna_recv_buff, int16_t hna_buff_len ); +void purge_orig( uint32_t curr_time ); +void debug_orig();
diff --git a/os.h b/os.h index 50d2332..2ef174a 100644 --- a/os.h +++ b/os.h @@ -23,22 +23,23 @@ #include "batman-specific.h"
uint32_t get_time( void ); +int32_t rand_num( int32_t limit ); +void addr_to_string( uint32_t addr, char *str, int32_t len ); + void set_forwarding(int32_t state); int32_t get_forwarding(void); void set_rp_filter(int32_t state, char* dev); int32_t get_rp_filter(char *dev);
- - void add_del_route( uint32_t dest, uint16_t netmask, uint32_t router, int8_t del, char *dev, int32_t sock ); int8_t is_aborted(); -void addr_to_string( uint32_t addr, char *str, int32_t len ); +void handler( int32_t sig ); +void segmentation_fault( int32_t sig );
-int8_t receive_packet( unsigned char *packet_buff, int32_t packet_buff_len, int16_t *hna_buff_len, uint32_t *neigh, uint32_t timeout, struct batman_if **if_incoming );
+int8_t receive_packet( unsigned char *packet_buff, int32_t packet_buff_len, int16_t *hna_buff_len, uint32_t *neigh, uint32_t timeout, struct batman_if **if_incoming ); int8_t send_packet( unsigned char *packet_buff, int packet_buff_len, struct sockaddr_in *broad, int send_sock ); -int32_t rand_num( int32_t limit ); -int8_t bind_to_iface( int32_t sock, char *dev ); + int8_t probe_tun(); int8_t del_dev_tun( int32_t fd ); int8_t add_dev_tun( struct batman_if *batman_if, uint32_t dest_addr, char *tun_dev, size_t tun_dev_size, int32_t *fd ); @@ -46,9 +47,10 @@ int8_t add_dev_tun( struct batman_if *batman_if, uint32_t dest_addr, char *tun_d void apply_init_args( int argc, char *argv[] ); void init_interface ( struct batman_if *batman_if ); void init_interface_gw ( struct batman_if *batman_if ); +int8_t bind_to_iface( int32_t sock, char *dev );
void print_animation( void ); -void close_all_sockets(); +void restore_defaults(); void *gw_listen( void *arg );
void *client_to_gw_tun( void *arg ); @@ -58,5 +60,4 @@ void debug_output( int8_t debug_prio, char *format, ... ); void cleanup();
- #endif diff --git a/posix-specific.c b/posix-specific.c index f8d4695..9da3559 100644 --- a/posix-specific.c +++ b/posix-specific.c @@ -45,6 +45,27 @@ #include "allocate.h"
+ +static int8_t stop; + + + +int8_t is_aborted() { + + return stop != 0; + +} + + + +void handler( int32_t sig ) { + + stop = 1; + +} + + + void debug_output( int8_t debug_prio, char *format, ... ) {
struct list_head *debug_pos; @@ -337,6 +358,7 @@ void apply_init_args( int argc, char *argv[] ) { // fd_set wait_sockets, tmp_wait_sockets;
memset( &tmp_ip_holder, 0, sizeof (struct in_addr) ); + stop = 0;
printf( "WARNING: You are using the unstable batman branch. If you are interested in *using* batman get the latest stable release !\n" ); @@ -439,7 +461,7 @@ void apply_init_args( int argc, char *argv[] ) {
case 'H': verbose_usage(); - exit(0); + exit(EXIT_SUCCESS);
case 'o':
@@ -505,7 +527,7 @@ void apply_init_args( int argc, char *argv[] ) { case 'v':
printf( "B.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION ); - exit(0); + exit(EXIT_SUCCESS);
case 'V':
@@ -514,12 +536,12 @@ void apply_init_args( int argc, char *argv[] ) { printf( "\x1B[0;0HB.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION ); printf( "\x1B[9;0H \t May the bat guide your path ...\n\n\n" );
- exit(0); + exit(EXIT_SUCCESS);
case 'h': default: usage(); - exit(0); + exit(EXIT_SUCCESS);
}
@@ -552,7 +574,7 @@ void apply_init_args( int argc, char *argv[] ) { if ( argc <= found_args ) { fprintf( stderr, "Error - no interface specified\n" ); usage(); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
@@ -571,7 +593,7 @@ void apply_init_args( int argc, char *argv[] ) { if ( daemon( 0, 0 ) < 0 ) {
printf( "Error - can't fork to background: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -650,13 +672,13 @@ void apply_init_args( int argc, char *argv[] ) {
if ( bind ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof (struct sockaddr_un) ) < 0 ) { debug_output( 0, "Error - can't bind unix socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
if ( listen( unix_if.unix_sock, 10 ) < 0 ) { debug_output( 0, "Error - can't listen unix socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
@@ -855,14 +877,14 @@ void init_interface ( struct batman_if *batman_if )
if ( strlen( batman_if->dev ) > IFNAMSIZ - 1 ) { debug_output( 0, "Error - interface name too long: %s\n", batman_if->dev ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
batman_if->udp_send_sock = socket( PF_INET, SOCK_DGRAM, 0 ); if (batman_if->udp_send_sock < 0) { debug_output( 0, "Error - can't create send socket: %s", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
@@ -870,7 +892,7 @@ void init_interface ( struct batman_if *batman_if ) if ( batman_if->udp_recv_sock < 0 ) {
debug_output( 0, "Error - can't create recieve socket: %s", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -881,7 +903,7 @@ void init_interface ( struct batman_if *batman_if ) if ( ioctl( batman_if->udp_recv_sock, SIOCGIFADDR, &int_req ) < 0 ) {
debug_output( 0, "Error - can't get IP address of interface %s\n", batman_if->dev ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -893,7 +915,7 @@ void init_interface ( struct batman_if *batman_if ) if ( ioctl( batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req ) < 0 ) {
debug_output( 0, "Error - can't get broadcast IP address of interface %s\n", batman_if->dev ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -905,14 +927,14 @@ void init_interface ( struct batman_if *batman_if ) if ( setsockopt( batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (int) ) < 0 ) {
debug_output( 0, "Error - can't enable broadcasts: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
}
if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) {
- close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -920,14 +942,14 @@ void init_interface ( struct batman_if *batman_if ) if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in) ) < 0 ) {
debug_output( 0, "Error - can't bind send socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
}
if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) {
- close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -935,7 +957,7 @@ void init_interface ( struct batman_if *batman_if ) if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof (struct sockaddr_in) ) < 0 ) {
debug_output( 0, "Error - can't bind receive socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE);
} @@ -954,38 +976,38 @@ void init_interface_gw ( struct batman_if *batman_if )
if ( batman_if->tcp_gw_sock < 0 ) { debug_output( 0, "Error - can't create socket: %s", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
if ( setsockopt( batman_if->tcp_gw_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int) ) < 0 ) { debug_output( 0, "Error - can't enable reuse of address: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
if ( bind( batman_if->tcp_gw_sock, (struct sockaddr*)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) { debug_output( 0, "Error - can't bind socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
if ( listen( batman_if->tcp_gw_sock, 10 ) < 0 ) { debug_output( 0, "Error - can't listen socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
batman_if->tunnel_sock = socket(PF_INET, SOCK_DGRAM, 0); if ( batman_if->tunnel_sock < 0 ) { debug_output( 0, "Error - can't create tunnel socket: %s", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
if ( bind( batman_if->tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in) ) < 0 ) { debug_output( 0, "Error - can't bind tunnel socket: %s\n", strerror(errno) ); - close_all_sockets(); + restore_defaults(); exit(EXIT_FAILURE); }
@@ -1249,11 +1271,14 @@ int8_t add_default_route() {
-void close_all_sockets() { +void restore_defaults() {
struct list_head *if_pos, *if_pos_tmp; struct batman_if *batman_if;
+ + stop = 1; + list_for_each_safe( if_pos, if_pos_tmp, &if_list ) {
batman_if = list_entry( if_pos, struct batman_if, list ); @@ -1617,6 +1642,18 @@ void *gw_listen( void *arg ) {
+void segmentation_fault( int32_t sig ) { + + restore_defaults(); + + debug_output( 0, "Error - SIGSEGV received !\n" ); + + exit(EXIT_FAILURE); + +} + + + void cleanup() {
int8_t i; diff --git a/posix.c b/posix.c index ca145f2..df46f58 100644 --- a/posix.c +++ b/posix.c @@ -38,8 +38,6 @@
#include "os.h" #include "batman-specific.h" -#include "list.h" -#include "allocate.h"
@@ -51,7 +49,6 @@ extern struct vis_if vis_if;
static struct timeval start_time; -static int8_t stop;
static void get_time_internal( struct timeval *tv ) { @@ -213,14 +210,6 @@ void print_animation( void ) {
-int8_t is_aborted() { - - return stop != 0; - -} - - - void addr_to_string( uint32_t addr, char *str, int32_t len ) {
inet_ntop( AF_INET, &addr, str, len ); @@ -237,17 +226,9 @@ int32_t rand_num( int32_t limit ) {
-static void handler( int32_t sig ) { - - stop = 1; - -} - - int main( int argc, char *argv[] ) {
int8_t res; - stop = 0;
/* check if user is root */ @@ -258,20 +239,21 @@ int main( int argc, char *argv[] ) {
}
- apply_init_args( argc, argv ); -
signal( SIGINT, handler ); signal( SIGTERM, handler ); + signal( SIGSEGV, segmentation_fault ); + + + apply_init_args( argc, argv ); +
gettimeofday( &start_time, NULL ); srand( getpid() );
- res = batman();
- - close_all_sockets(); + restore_defaults(); cleanup(); checkLeak(); return res; diff --git a/profile.c b/profile.c index 64e6e6c..766e1ef 100644 --- a/profile.c +++ b/profile.c @@ -19,6 +19,7 @@
+#include "os.h" #include "batman.h"
@@ -64,7 +65,7 @@ void prof_print() {
for ( index = 0; index < PROF_COUNT; index++ ) {
- debug_output( 3, " %''30s: cpu time = %10.3f, calls = %i\n", prof_container[index].name, (float)prof_container[index].total_time/CLOCKS_PER_SEC, prof_container[index].calls ); + debug_output( 3, " %''30s: cpu time = %10.3f, calls = %''4i\n", prof_container[index].name, (float)prof_container[index].total_time/CLOCKS_PER_SEC, prof_container[index].calls );
}
diff --git a/schedule.c b/schedule.c index 0ea0136..a64a186 100644 --- a/schedule.c +++ b/schedule.c @@ -21,6 +21,7 @@
#include <string.h> #include <stdlib.h> +#include "os.h" #include "batman.h"
@@ -174,6 +175,7 @@ void send_outstanding_packets() { debug_output( 4, "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ntohs( ((struct packet *)forw_node->pack_buff)->seqno ), ((struct packet *)forw_node->pack_buff)->ttl, forw_node->if_outgoing->dev );
if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &forw_node->if_outgoing->broad, forw_node->if_outgoing->udp_send_sock ) < 0 ) { + restore_defaults(); exit(EXIT_FAILURE); }
@@ -189,6 +191,7 @@ void send_outstanding_packets() { if ( ( forw_node->if_outgoing != NULL ) ) {
if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &forw_node->if_outgoing->broad, forw_node->if_outgoing->udp_send_sock ) < 0 ) { + restore_defaults(); exit(EXIT_FAILURE); }
@@ -222,12 +225,14 @@ void send_outstanding_packets() { if ( ( forw_node->own ) && ( ((struct packet *)forw_node->pack_buff)->orig != ((struct batman_if *)if_list.next)->addr.sin_addr.s_addr ) ) {
if ( send_packet( forw_node->pack_buff, sizeof(struct packet), &batman_if->broad, batman_if->udp_send_sock ) < 0 ) { + restore_defaults(); exit(EXIT_FAILURE); }
} else {
if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &batman_if->broad, batman_if->udp_send_sock ) < 0 ) { + restore_defaults(); exit(EXIT_FAILURE); }