Remove debugging code.
[trust_router.git] / tr / tr_main.c
index e81aab4..5eb825e 100644 (file)
 #include <event2/event.h>
 #include <talloc.h>
 #include <sys/time.h>
+#include <signal.h>
+#include <pthread.h>
 
-#include <tr.h>
 #include <tid_internal.h>
 #include <tr_tid.h>
+#include <tr_trp.h>
 #include <tr_config.h>
 #include <tr_event.h>
 #include <tr_cfgwatch.h>
+#include <tr.h>
 #include <tr_debug.h>
 
+#define TALLOC_DEBUG_ENABLE 1
+
 /***** command-line option handling / setup *****/
 
 /* Strip trailing / from a path name.*/
@@ -105,16 +110,49 @@ static error_t parse_option(int key, char *arg, struct argp_state *state)
 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
 
 
-int main (int argc, char *argv[])
+/***** talloc error handling *****/
+/* called when talloc tries to abort */
+static void tr_abort(const char *reason)
 {
-  TALLOC_CTX *main_ctx=talloc_new(NULL);
+  tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
+  abort();
+}
+
+#if TALLOC_DEBUG_ENABLE
+static void tr_talloc_log(const char *msg)
+{
+  tr_debug("talloc: %s", msg);
+}
+#endif /* TALLOC_DEBUG_ENABLE */
+
+static void configure_signals(void)
+{
+  sigset_t signals;
+  /* ignore SIGPIPE */
+  sigemptyset(&signals);
+  sigaddset(&signals, SIGPIPE);
+  pthread_sigmask(SIG_BLOCK, &signals, NULL);
+}
+
+int main(int argc, char *argv[])
+{
+  TALLOC_CTX *main_ctx=NULL;
 
   TR_INSTANCE *tr = NULL;
   struct cmdline_args opts;
   struct event_base *ev_base;
   struct tr_socket_event tids_ev;
   struct event *cfgwatch_ev;
-  TR_CFGWATCH *cfgwatch; /* config file watcher status */
+
+  configure_signals();
+
+  /* we're going to be multithreaded, so disable null context tracking */
+  talloc_set_abort_fn(tr_abort);
+  talloc_disable_null_tracking();
+#if TALLOC_DEBUG_ENABLE
+  talloc_set_log_fn(tr_talloc_log);
+#endif /* TALLOC_DEBUG_ENABLE */
+  main_ctx=talloc_new(NULL);
 
   /* Use standalone logging */
   tr_log_open();
@@ -129,31 +167,36 @@ int main (int argc, char *argv[])
   /* process options */
   remove_trailing_slash(opts.config_dir);
 
-  /* Get a configuration status object */
-  cfgwatch=tr_cfgwatch_create(main_ctx);
-  if (cfgwatch == NULL) {
-    tr_crit("Unable to create configuration watcher object, exiting.");
-    return 1;
-  }
-  
   /***** create a Trust Router instance *****/
-  if (NULL == (tr = tr_create())) {
+  if (NULL == (tr = tr_create(main_ctx))) {
     tr_crit("Unable to create Trust Router instance, exiting.");
     return 1;
   }
 
-  /***** process configuration *****/
-  cfgwatch->config_dir=opts.config_dir;
-  cfgwatch->ctx=main_ctx;
-  cfgwatch->tr=tr;
-  if (0 != tr_read_and_apply_config(cfgwatch)) {
-    tr_crit("Error reading configuration, exiting.");
+  /***** initialize the trust path query server instance *****/
+  if (NULL == (tr->tids = tids_create (tr))) {
+    tr_crit("Error initializing Trust Path Query Server instance.");
     return 1;
   }
 
-  /***** initialize the trust path query server instance *****/
-  if (0 == (tr->tids = tids_create ())) {
-    tr_crit("Error initializing Trust Path Query Server instance.");
+  /***** initialize the trust router protocol server instance *****/
+  if (NULL == (tr->trps = trps_new(tr))) {
+    tr_crit("Error initializing Trust Router Protocol Server instance.");
+    return 1;
+  }
+
+  /***** process configuration *****/
+  tr->cfgwatch=tr_cfgwatch_create(tr);
+  if (tr->cfgwatch == NULL) {
+    tr_crit("Unable to create configuration watcher object, exiting.");
+    return 1;
+  }
+  tr->cfgwatch->config_dir=opts.config_dir;
+  tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
+  tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
+  tr->cfgwatch->update_cookie=(void *)tr;
+  if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
+    tr_crit("Error reading configuration, exiting.");
     return 1;
   }
 
@@ -164,12 +207,8 @@ int main (int argc, char *argv[])
     return 1;
   }
 
-  /* install configuration file watching events */
-  cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
-  cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
-  
   /* already set config_dir, fstat_list and n_files earlier */
-  if (0 != tr_cfgwatch_event_init(ev_base, cfgwatch, &cfgwatch_ev)) {
+  if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
     tr_crit("Error initializing configuration file watcher.");
     return 1;
   }
@@ -177,19 +216,24 @@ int main (int argc, char *argv[])
   /*tr_status_event_init();*/ /* install status reporting events */
 
   /* install TID server events */
-  if (0 != tr_tids_event_init(ev_base, tr, &tids_ev)) {
+  if (0 != tr_tids_event_init(ev_base,
+                              tr->tids,
+                              tr->cfg_mgr,
+                              tr->trps,
+                             &tids_ev)) {
     tr_crit("Error initializing Trust Path Query Server instance.");
     return 1;
   }
 
-  /*tr_trp_event_init();*/ /* install TRP handler events */
+  /* install TRP handler events */
+  if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
+    tr_crit("Error initializing Trust Path Query Server instance.");
+    return 1;
+  }
 
-  fflush(stdout); fflush(stderr);
   tr_event_loop_run(ev_base); /* does not return until we are done */
 
-  /* TODO: update the cleanup code */
-  tids_destroy(tr->tids);
-  tr_destroy(tr);
+  tr_destroy(tr); /* thanks to talloc, should destroy everything */
 
   talloc_free(main_ctx);
   return 0;