telepathygabble/src/exegabble.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2  * gabble.h - entry point and utility functions for telepathy-gabble
       
     3  * Copyright (C) 2005 Collabora Ltd.
       
     4  * 
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Lesser General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2.1 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with this library; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    19  */
       
    20 
       
    21 #include "config.h"
       
    22 
       
    23 #include <dbus/dbus-glib.h>
       
    24 #include <stdlib.h>
       
    25 #include <string.h>
       
    26 #include <unistd.h>
       
    27 
       
    28 #ifdef HAVE_EXECINFO_H
       
    29 #include <execinfo.h>
       
    30 #endif /* HAVE_EXECINFO_H */
       
    31 
       
    32 #ifdef HAVE_SIGNAL_H
       
    33 #include <signal.h>
       
    34 #endif /* HAVE_SIGNAL_H */
       
    35 
       
    36 #include "debug.h"
       
    37 #include "gabble-connection-manager.h"
       
    38 #include "telepathy-errors.h"
       
    39 #include "telepathy-errors-enumtypes.h"
       
    40 
       
    41 GSource *timeout = NULL;
       
    42 GMainLoop *mainloop = NULL;
       
    43 GabbleConnectionManager *manager = NULL;
       
    44 gboolean connections_exist = FALSE;
       
    45 guint timeout_id;
       
    46 
       
    47 #define DIE_TIME 5000
       
    48 
       
    49 static gboolean
       
    50 kill_connection_manager (gpointer data)
       
    51 {
       
    52 #ifdef ENABLE_DEBUG
       
    53   if (!gabble_debug_flag_is_set (GABBLE_DEBUG_PERSIST) && !connections_exist)
       
    54 #else
       
    55   if (!connections_exist)
       
    56 #endif
       
    57     {
       
    58       g_debug ("no connections, and timed out");
       
    59       g_object_unref (manager);
       
    60       g_main_loop_quit (mainloop);
       
    61     }
       
    62 
       
    63   timeout_id = 0;
       
    64   return FALSE;
       
    65 }
       
    66 
       
    67 static void
       
    68 new_connection (GabbleConnectionManager *conn,
       
    69                 gchar *bus_name,
       
    70                 gchar *object_path,
       
    71                 gchar *proto)
       
    72 {
       
    73   connections_exist = TRUE;
       
    74 
       
    75   if (0 != timeout_id)
       
    76     {
       
    77       g_source_remove (timeout_id);
       
    78     }
       
    79 }
       
    80 
       
    81 static void
       
    82 no_more_connections (GabbleConnectionManager *conn)
       
    83 {
       
    84   connections_exist = FALSE;
       
    85 
       
    86   if (0 != timeout_id)
       
    87     {
       
    88       g_source_remove (timeout_id);
       
    89     }
       
    90 
       
    91   timeout_id = g_timeout_add (DIE_TIME, kill_connection_manager, NULL);
       
    92 }
       
    93 
       
    94 #ifdef ENABLE_BACKTRACE
       
    95 static void
       
    96 print_backtrace (void)
       
    97 {
       
    98 #if defined (HAVE_BACKTRACE) && defined (HAVE_BACKTRACE_SYMBOLS_FD)
       
    99   void *array[20];
       
   100   size_t size;
       
   101 
       
   102 #define MSG "\n########## Backtrace (version " VERSION ") ##########\n"
       
   103   write (STDERR_FILENO, MSG, strlen (MSG));
       
   104 #undef MSG
       
   105 
       
   106   size = backtrace (array, 20);
       
   107   backtrace_symbols_fd (array, size, STDERR_FILENO);
       
   108 #endif /* HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS_FD */
       
   109 }
       
   110 
       
   111 static void
       
   112 critical_handler (const gchar *log_domain,
       
   113                   GLogLevelFlags log_level,
       
   114                   const gchar *message,
       
   115                   gpointer user_data)
       
   116 {
       
   117   g_log_default_handler (log_domain, log_level, message, user_data);
       
   118   print_backtrace ();
       
   119 }
       
   120 
       
   121 #ifdef HAVE_SIGNAL
       
   122 static void
       
   123 segv_handler (int sig)
       
   124 {
       
   125 #define MSG "caught SIGSEGV\n"
       
   126   write (STDERR_FILENO, MSG, strlen (MSG));
       
   127 #undef MSG
       
   128 
       
   129   print_backtrace ();
       
   130   abort ();
       
   131 }
       
   132 #endif /* HAVE_SIGNAL */
       
   133 
       
   134 #endif /* ENABLE_BACKTRACE */
       
   135 
       
   136 static void
       
   137 add_signal_handlers (void)
       
   138 {
       
   139 #if defined(HAVE_SIGNAL) && defined(ENABLE_BACKTRACE)
       
   140   signal (SIGSEGV, segv_handler);
       
   141 #endif /* HAVE_SIGNAL && ENABLE_BACKTRACE */
       
   142 }
       
   143 
       
   144 int
       
   145 main (int argc,
       
   146       char **argv)
       
   147 {
       
   148   add_signal_handlers ();
       
   149 
       
   150   g_type_init();
       
   151 
       
   152   g_set_prgname("telepathy-gabble");
       
   153 
       
   154 /* bsr #ifdef ENABLE_DEBUG*/
       
   155   gabble_debug_set_flags_from_env ();
       
   156 
       
   157   if (g_getenv ("GABBLE_PERSIST"))
       
   158     gabble_debug_set_flags (0xffff);
       
   159 /*#endif bsr */
       
   160 
       
   161     {
       
   162       GLogLevelFlags fatal_mask;
       
   163 
       
   164       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
       
   165       fatal_mask |= G_LOG_LEVEL_CRITICAL;
       
   166       g_log_set_always_fatal (fatal_mask);
       
   167 
       
   168 #ifdef ENABLE_BACKTRACE
       
   169       g_log_set_handler ("GLib-GObject",
       
   170           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   171           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   172           critical_handler, NULL);
       
   173       g_log_set_handler ("GLib",
       
   174           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   175           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   176           critical_handler, NULL);
       
   177       g_log_set_handler (NULL,
       
   178           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   179           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   180           critical_handler, NULL);
       
   181 #endif /* ENABLE_BACKTRACE */
       
   182     }
       
   183 
       
   184   g_message("before gabble mainloop new \n");
       
   185   getchar();
       
   186   exit(1);
       
   187   mainloop = g_main_loop_new (NULL, FALSE);
       
   188 
       
   189   dbus_g_error_domain_register (TELEPATHY_ERRORS,
       
   190       "org.freedesktop.Telepathy.Error", TELEPATHY_TYPE_ERRORS);
       
   191 
       
   192   manager = g_object_new (GABBLE_TYPE_CONNECTION_MANAGER, NULL);
       
   193 
       
   194   g_signal_connect (manager, "new-connection",
       
   195       (GCallback) new_connection, NULL);
       
   196 
       
   197   g_signal_connect (manager, "no-more-connections",
       
   198       (GCallback) no_more_connections, NULL);
       
   199 
       
   200   g_message("calling gabble_connection_manager_register \n");
       
   201   _gabble_connection_manager_register (manager);
       
   202 
       
   203   g_message("started version " VERSION);
       
   204 
       
   205   //timeout_id = g_timeout_add (DIE_TIME, kill_connection_manager, NULL);
       
   206 
       
   207   g_main_loop_run (mainloop);
       
   208 
       
   209   return 0;
       
   210 }