Cleanup of subsystem states
authorHenning Rogge <hrogge@googlemail.com>
Sat, 15 Oct 2011 15:07:40 +0000 (17:07 +0200)
committerHenning Rogge <hrogge@googlemail.com>
Sat, 15 Oct 2011 15:07:40 +0000 (17:07 +0200)
src/core/olsr_cfg.c
src/core/olsr_clock.c
src/core/olsr_http.c
src/core/olsr_logging.c
src/core/olsr_logging_cfg.c
src/core/olsr_packet_socket.c
src/core/olsr_plugins.c
src/core/olsr_socket.c
src/core/olsr_stream_socket.c
src/core/olsr_telnet.c
src/core/olsr_timer.c

index e5919d4..5dcadfd 100644 (file)
@@ -67,7 +67,7 @@ static struct cfg_delta _olsr_delta;
 static bool _first_apply;
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_cfg_state);
+OLSR_SUBSYSTEM_STATE(_cfg_state);
 
 /* define global configuration template */
 static struct cfg_schema_section global_section = {
@@ -95,7 +95,7 @@ static struct cfg_schema_entry global_entries[] = {
  */
 int
 olsr_cfg_init(void) {
-  if (olsr_subsystem_init(&olsr_cfg_state))
+  if (olsr_subsystem_is_initialized(&_cfg_state))
     return 0;
 
   cfg_add(&_olsr_cfg_instance);
@@ -109,7 +109,6 @@ olsr_cfg_init(void) {
   if ((_olsr_raw_db = cfg_db_add()) == NULL) {
     OLSR_WARN(LOG_CONFIG, "Cannot create raw configuration database.");
     cfg_remove(&_olsr_cfg_instance);
-    olsr_subsystem_cleanup(&olsr_cfg_state);
     return -1;
   }
 
@@ -118,7 +117,6 @@ olsr_cfg_init(void) {
     OLSR_WARN(LOG_CONFIG, "Cannot create configuration database.");
     cfg_db_remove(_olsr_raw_db);
     cfg_remove(&_olsr_cfg_instance);
-    olsr_subsystem_cleanup(&olsr_cfg_state);
     return -1;
   }
 
@@ -131,6 +129,7 @@ olsr_cfg_init(void) {
   memset(&config_global, 0, sizeof(config_global));
   _first_apply = true;
 
+  olsr_subsystem_init(&_cfg_state);
   return 0;
 }
 
@@ -139,7 +138,7 @@ olsr_cfg_init(void) {
  */
 void
 olsr_cfg_cleanup(void) {
-  if (olsr_subsystem_cleanup(&olsr_cfg_state))
+  if (olsr_subsystem_cleanup(&_cfg_state))
     return;
 
   free(config_global.plugin.value);
index 3f96fe6..dce6e8e 100644 (file)
@@ -56,7 +56,7 @@ static struct timeval first_tv;        /* timevalue during startup */
 static struct timeval last_tv;         /* timevalue used for last olsr_times() calculation */
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_clock_state);
+OLSR_SUBSYSTEM_STATE(_clock_state);
 
 static int olsr_get_timezone(void);
 
@@ -66,7 +66,7 @@ static int olsr_get_timezone(void);
  */
 int
 olsr_clock_init(void) {
-  if (olsr_subsystem_init(&olsr_clock_state))
+  if (olsr_subsystem_is_initialized(&_clock_state))
     return 0;
 
   /* Grab initial timestamp */
@@ -80,6 +80,7 @@ olsr_clock_init(void) {
     return -1;
   }
 
+  olsr_subsystem_init(&_clock_state);
   return 0;
 }
 
@@ -88,7 +89,7 @@ olsr_clock_init(void) {
  */
 void
 olsr_clock_cleanup(void) {
-  olsr_subsystem_cleanup(&olsr_clock_state);
+  olsr_subsystem_cleanup(&_clock_state);
 }
 
 /**
index a1284eb..0adf090 100644 (file)
@@ -96,14 +96,14 @@ static struct avl_tree _http_site_tree;
 static struct olsr_stream_managed _http_managed_socket;
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(_olsr_http_state);
+OLSR_SUBSYSTEM_STATE(_http_state);
 
 /**
  * Initialize http subsystem
  */
 void
 olsr_http_init(void) {
-  if (olsr_subsystem_init(&_olsr_http_state))
+  if (olsr_subsystem_init(&_http_state))
     return;
 
   cfg_schema_add_section(olsr_cfg_get_schema(), &_http_section);
@@ -126,7 +126,7 @@ olsr_http_init(void) {
  */
 void
 olsr_http_cleanup(void) {
-  if (olsr_subsystem_cleanup(&_olsr_http_state))
+  if (olsr_subsystem_cleanup(&_http_state))
     return;
 
   olsr_stream_remove_managed(&_http_managed_socket);
index 2390d28..4068533 100644 (file)
@@ -70,7 +70,7 @@ const char *LOG_SEVERITY_NAMES[LOG_SEVERITY_COUNT] = {
 const char OUT_OF_MEMORY_ERROR[] = "Out of memory error!";
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_logging_state);
+OLSR_SUBSYSTEM_STATE(_logging_state);
 
 /**
  * Initialize logging system
@@ -83,14 +83,14 @@ olsr_log_init(enum log_severity def_severity)
   enum log_severity j;
   enum log_source i;
 
-  if (olsr_subsystem_init(&olsr_logging_state))
+  if (olsr_subsystem_is_initialized(&_logging_state))
     return 0;
 
   list_init_head(&log_handler_list);
 
   if (abuf_init(&logbuffer, 4096)) {
     fputs("Not enough memory for logging buffer\n", stderr);
-    olsr_subsystem_cleanup(&olsr_logging_state);
+    olsr_subsystem_cleanup(&_logging_state);
     return -1;
   }
 
@@ -100,6 +100,8 @@ olsr_log_init(enum log_severity def_severity)
       log_global_mask.mask[j][i] = j >= def_severity;
     }
   }
+
+  olsr_subsystem_init(&_logging_state);
   return 0;
 }
 
@@ -111,7 +113,7 @@ olsr_log_cleanup(void)
 {
   struct log_handler_entry *h, *iterator;
 
-  if (olsr_subsystem_cleanup(&olsr_logging_state))
+  if (olsr_subsystem_cleanup(&_logging_state))
     return;
 
   /* remove all handlers */
index fa8ec02..25971da 100644 (file)
@@ -112,7 +112,7 @@ static struct log_handler_entry file_handler = {
 };
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_logcfg_state);
+OLSR_SUBSYSTEM_STATE(_logcfg_state);
 
 /**
  * Initialize logging configuration
@@ -121,7 +121,7 @@ OLSR_SUBSYSTEM_STATE(olsr_logcfg_state);
  */
 void
 olsr_logcfg_init(enum log_source *debug_lvl_1_ptr, size_t length) {
-  if (olsr_subsystem_init(&olsr_logcfg_state))
+  if (olsr_subsystem_init(&_logcfg_state))
     return;
 
   debug_lvl_1 = debug_lvl_1_ptr;
@@ -138,7 +138,7 @@ olsr_logcfg_init(enum log_source *debug_lvl_1_ptr, size_t length) {
  */
 void
 olsr_logcfg_cleanup(void) {
-  if (olsr_subsystem_cleanup(&olsr_logcfg_state))
+  if (olsr_subsystem_cleanup(&_logcfg_state))
     return;
 
   /* cleanup delta handler */
index 5cda971..9304f9c 100644 (file)
@@ -55,7 +55,7 @@ static struct list_entity packet_sockets = { NULL, NULL };
 static char input_buffer[65536];
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_packet_state);
+OLSR_SUBSYSTEM_STATE(_packet_state);
 
 static void _cb_packet_event(int fd, void *data, bool r, bool w);
 
@@ -64,7 +64,7 @@ static void _cb_packet_event(int fd, void *data, bool r, bool w);
  */
 void
 olsr_packet_init(void) {
-  if (olsr_subsystem_init(&olsr_packet_state))
+  if (olsr_subsystem_init(&_packet_state))
     return;
 
   list_init_head(&packet_sockets);
@@ -77,7 +77,7 @@ void
 olsr_packet_cleanup(void) {
   struct olsr_packet_socket *skt;
 
-  if (olsr_subsystem_cleanup(&olsr_packet_state))
+  if (olsr_subsystem_cleanup(&_packet_state))
     return;
 
   while (!list_is_empty(&packet_sockets)) {
index 6c63e49..ae7c9b8 100644 (file)
@@ -86,14 +86,14 @@ static int _unload_plugin(struct olsr_plugin *plugin, bool cleanup);
 static void *_open_plugin(const char *filename);
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_plugins_state);
+OLSR_SUBSYSTEM_STATE(_plugins_state);
 
 /**
  * Initialize the plugin loader system
  */
 void
 olsr_plugins_init(void) {
-  if (olsr_subsystem_init(&olsr_plugins_state))
+  if (olsr_subsystem_init(&_plugins_state))
     return;
 
   avl_init(&plugin_tree, avl_comp_strcasecmp, false, NULL);
@@ -113,7 +113,7 @@ void
 olsr_plugins_cleanup(void) {
   struct olsr_plugin *plugin, *iterator;
 
-  if (olsr_subsystem_cleanup(&olsr_plugins_state))
+  if (olsr_subsystem_cleanup(&_plugins_state))
     return;
 
   OLSR_FOR_ALL_PLUGIN_ENTRIES(plugin, iterator) {
index 136b5ae..ebcc792 100644 (file)
 struct list_entity socket_head;
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_socket_state);
+OLSR_SUBSYSTEM_STATE(_socket_state);
 
 /**
  * Initialize olsr socket scheduler
  */
 void
 olsr_socket_init(void) {
-  if (olsr_subsystem_init(&olsr_socket_state))
+  if (olsr_subsystem_init(&_socket_state))
     return;
 
   list_init_head(&socket_head);
@@ -80,7 +80,7 @@ olsr_socket_cleanup(void)
 {
   struct olsr_socket_entry *entry, *iterator;
 
-  if (olsr_subsystem_cleanup(&olsr_socket_state))
+  if (olsr_subsystem_cleanup(&_socket_state))
     return;
 
   OLSR_FOR_ALL_SOCKETS(entry, iterator) {
index 7abef25..58d8c28 100644 (file)
@@ -66,7 +66,7 @@ static struct olsr_memcookie_info *connection_cookie;
 static struct olsr_timer_info *connection_timeout;
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_stream_state);
+OLSR_SUBSYSTEM_STATE(_stream_state);
 
 static int _apply_managed_socket(struct olsr_stream_managed *managed,
     struct olsr_stream_socket *stream, struct netaddr *bindto, uint16_t port);
@@ -83,14 +83,13 @@ static void _cb_timeout_handler(void *);
  */
 int
 olsr_stream_init(void) {
-  if (olsr_subsystem_init(&olsr_stream_state))
+  if (olsr_subsystem_is_initialized(&_stream_state))
     return 0;
 
   connection_cookie = olsr_memcookie_add("stream socket connections",
       sizeof(struct olsr_stream_session));
   if (connection_cookie == NULL) {
     OLSR_WARN_OOM(LOG_SOCKET_STREAM);
-    olsr_subsystem_cleanup(&olsr_stream_state);
     return -1;
   }
 
@@ -99,11 +98,11 @@ olsr_stream_init(void) {
   if (connection_timeout == NULL) {
     OLSR_WARN_OOM(LOG_SOCKET_STREAM);
     olsr_memcookie_remove(connection_cookie);
-    olsr_subsystem_cleanup(&olsr_stream_state);
     return -1;
   }
 
   list_init_head(&olsr_stream_head);
+  olsr_subsystem_init(&_stream_state);
   return 0;
 }
 
@@ -114,7 +113,7 @@ void
 olsr_stream_cleanup(void) {
   struct olsr_stream_socket *comport;
 
-  if (olsr_subsystem_cleanup(&olsr_stream_state))
+  if (olsr_subsystem_cleanup(&_stream_state))
     return;
 
   while (!list_is_empty(&olsr_stream_head)) {
index c4ee4ac..11a5a31 100644 (file)
@@ -90,7 +90,7 @@ static struct olsr_telnet_command _builtin[] = {
 };
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_telnet_state);
+OLSR_SUBSYSTEM_STATE(_telnet_state);
 
 /* telnet session handling */
 static struct olsr_memcookie_info *_telnet_memcookie;
@@ -107,20 +107,18 @@ int
 olsr_telnet_init(void) {
   size_t i;
 
-  if (olsr_subsystem_init(&olsr_telnet_state))
+  if (olsr_subsystem_is_initialized(&_telnet_state))
     return 0;
 
   _telnet_memcookie = olsr_memcookie_add("telnet session",
       sizeof(struct olsr_telnet_session));
   if (_telnet_memcookie == NULL) {
-    olsr_subsystem_cleanup(&olsr_telnet_state);
     return -1;
   }
 
   _telnet_repeat_timerinfo = olsr_timer_add("txt repeat timer", _cb_telnet_repeat_timer, true);
   if (_telnet_repeat_timerinfo == NULL) {
-    olsr_memcookie_remove(_telnet_memcookie);
-    olsr_subsystem_cleanup(&olsr_telnet_state);
+    olsr_subsystem_cleanup(&_telnet_state);
     return -1;
   }
 
@@ -145,6 +143,7 @@ olsr_telnet_init(void) {
     olsr_telnet_add(&_builtin[i]);
   }
 
+  olsr_subsystem_init(&_telnet_state);
   return 0;
 }
 
@@ -153,7 +152,7 @@ olsr_telnet_init(void) {
  */
 void
 olsr_telnet_cleanup(void) {
-  if (olsr_subsystem_cleanup(&olsr_telnet_state))
+  if (olsr_subsystem_cleanup(&_telnet_state))
     return;
 
   olsr_stream_remove_managed(&_telnet_managed);
index 68dcd51..83264b6 100644 (file)
@@ -62,7 +62,7 @@ static struct olsr_memcookie_info *timer_mem_cookie = NULL;
 static struct olsr_memcookie_info *timerinfo_cookie = NULL;
 
 /* remember if initialized or not */
-OLSR_SUBSYSTEM_STATE(olsr_timer_state);
+OLSR_SUBSYSTEM_STATE(_timer_state);
 
 /* Prototypes */
 static uint32_t calc_jitter(unsigned int rel_time, uint8_t jitter_pct, unsigned int random_val);
@@ -76,7 +76,7 @@ olsr_timer_init(void)
 {
   int idx;
 
-  if (olsr_subsystem_init(&olsr_timer_state))
+  if (olsr_subsystem_is_initialized(&_timer_state))
     return 0;
 
   OLSR_INFO(LOG_SOCKET, "Initializing scheduler.\n");
@@ -95,7 +95,6 @@ olsr_timer_init(void)
   timer_mem_cookie = olsr_memcookie_add("timer_entry", sizeof(struct olsr_timer_entry));
   if (timer_mem_cookie == NULL) {
     OLSR_WARN_OOM(LOG_SOCKET);
-    olsr_timer_state--;
     return -1;
   }
 
@@ -103,10 +102,10 @@ olsr_timer_init(void)
   timerinfo_cookie = olsr_memcookie_add("timerinfo", sizeof(struct olsr_timer_info));
   if (timerinfo_cookie == NULL) {
     olsr_memcookie_remove(timer_mem_cookie);
-    olsr_timer_state--;
     return -1;
   }
 
+  olsr_subsystem_init(&_timer_state);
   return 0;
 }
 
@@ -120,7 +119,7 @@ olsr_timer_cleanup(void)
   struct list_entity *timer_head_node;
   unsigned int wheel_slot = 0;
 
-  if (olsr_subsystem_cleanup(&olsr_timer_state))
+  if (olsr_subsystem_cleanup(&_timer_state))
     return;
 
   for (wheel_slot = 0; wheel_slot < TIMER_WHEEL_SLOTS; wheel_slot++) {