--- main.c.orig 2009-04-14 14:04:20.000000000 -0700 +++ main.c.new 2009-04-14 14:06:04.000000000 -0700 @@ -36,7 +36,7 @@ extern char *optarg; extern int optind, opterr, optopt; int inBackground = 0; - int rc, pid, c; + int rc, pid, c, i; int message_mask_set = 0; int message_stderr_set = 0; int fdlimit, fd; @@ -217,13 +217,47 @@ conference(jcr->jcr_i, NULL); log_warn(JDBG, "Main loop starting."); - jcr_main_new_stream(); - g_main_loop_run(gmain); - log_warn(JDBG, "Main loop exiting."); + // Attempt to connect 1 time within 10 seconds. + for (i = 0; i < 10; i++) { + jcr_main_new_stream(); + if (jcr->stream_state == _STREAM_ERROR) + sleep(1); + else + break; + } + + if (jcr->stream_state == _STREAM_ERROR) { + log_warn(JDBG, "Error initializing a socket, exiting."); + } else { + g_main_loop_run(gmain); + log_warn(JDBG, "Main loop exiting."); + + jcr->in_shutdown = 1; + if (jcr->shandler != NULL) + (jcr->shandler)((void *)jcr->sdata); + + g_thread_join(jcr->dthread); + g_io_channel_shutdown(jcr->gio, TRUE, NULL); + } + + jcr->stream_state = _STREAM_SHUTDOWN; + pool_free(jcr->jcr_i->p); + free(jcr->spool_dir); + free(jcr->log_dir); + free(jcr->recv_buffer); + xmlnode_free(jcr->config); + XML_ParserFree(jcr->parser); + + if (jcr->pid_file) + { + unlink(jcr->pid_file); + free(jcr->pid_file); + } #ifdef LIBIDN /* free stringprep caches */ - jid_stop_caching(); + // Drop this for now as it triggers and abort() in glib/g_logv(), and we're exiting anyway. + //jid_stop_caching(); #endif return 0;