1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Main file (Unix specific part) */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "bitlbee.h" |
---|
27 | #include "commands.h" |
---|
28 | #include "crypting.h" |
---|
29 | #include "protocols/nogaim.h" |
---|
30 | #include "help.h" |
---|
31 | #include <signal.h> |
---|
32 | #include <unistd.h> |
---|
33 | #include <sys/time.h> |
---|
34 | |
---|
35 | global_t global; /* Against global namespace pollution */ |
---|
36 | |
---|
37 | static void sighandler( int signal ); |
---|
38 | |
---|
39 | int main( int argc, char *argv[] ) |
---|
40 | { |
---|
41 | int i = 0; |
---|
42 | struct sigaction sig, old; |
---|
43 | |
---|
44 | memset( &global, 0, sizeof( global_t ) ); |
---|
45 | |
---|
46 | global.loop = g_main_new( FALSE ); |
---|
47 | |
---|
48 | log_init( ); |
---|
49 | nogaim_init( ); |
---|
50 | |
---|
51 | CONF_FILE = g_strdup( CONF_FILE_DEF ); |
---|
52 | |
---|
53 | global.helpfile = g_strdup( HELP_FILE ); |
---|
54 | |
---|
55 | global.conf = conf_load( argc, argv ); |
---|
56 | if( global.conf == NULL ) |
---|
57 | return( 1 ); |
---|
58 | |
---|
59 | |
---|
60 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
61 | { |
---|
62 | i = bitlbee_inetd_init(); |
---|
63 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
64 | |
---|
65 | } |
---|
66 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
67 | { |
---|
68 | i = bitlbee_daemon_init(); |
---|
69 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
70 | } |
---|
71 | if( i != 0 ) |
---|
72 | return( i ); |
---|
73 | |
---|
74 | global.storage = storage_init( global.conf->primary_storage, |
---|
75 | global.conf->migrate_storage ); |
---|
76 | if ( global.storage == NULL) { |
---|
77 | log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage ); |
---|
78 | return( 1 ); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
83 | memset( &sig, 0, sizeof( sig ) ); |
---|
84 | sig.sa_handler = sighandler; |
---|
85 | sigaction( SIGPIPE, &sig, &old ); |
---|
86 | sig.sa_flags = SA_RESETHAND; |
---|
87 | sigaction( SIGINT, &sig, &old ); |
---|
88 | sigaction( SIGILL, &sig, &old ); |
---|
89 | sigaction( SIGBUS, &sig, &old ); |
---|
90 | sigaction( SIGFPE, &sig, &old ); |
---|
91 | sigaction( SIGSEGV, &sig, &old ); |
---|
92 | sigaction( SIGTERM, &sig, &old ); |
---|
93 | sigaction( SIGQUIT, &sig, &old ); |
---|
94 | sigaction( SIGXCPU, &sig, &old ); |
---|
95 | |
---|
96 | if( !getuid() || !geteuid() ) |
---|
97 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
98 | if( help_init( &(global.help) ) == NULL ) |
---|
99 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
100 | |
---|
101 | g_main_run( global.loop ); |
---|
102 | |
---|
103 | return( 0 ); |
---|
104 | } |
---|
105 | |
---|
106 | static void sighandler( int signal ) |
---|
107 | { |
---|
108 | /* FIXME: In fact, calling log_message() here can be dangerous. But well, let's take the risk for now. */ |
---|
109 | |
---|
110 | if( signal == SIGTERM ) |
---|
111 | { |
---|
112 | static int first = 1; |
---|
113 | |
---|
114 | if( first ) |
---|
115 | { |
---|
116 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
117 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
118 | |
---|
119 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
120 | g_timeout_add_full( G_PRIORITY_LOW, 1, (GSourceFunc) bitlbee_shutdown, NULL, NULL ); |
---|
121 | |
---|
122 | first = 0; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
127 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
128 | But just in case we decide to change that: */ |
---|
129 | |
---|
130 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
131 | raise( signal ); |
---|
132 | } |
---|
133 | } |
---|
134 | else if( signal != SIGPIPE ) |
---|
135 | { |
---|
136 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
137 | raise( signal ); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | double gettime() |
---|
142 | { |
---|
143 | struct timeval time[1]; |
---|
144 | |
---|
145 | gettimeofday( time, 0 ); |
---|
146 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
147 | } |
---|