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 | gboolean bitlbee_dirty_workaround( gpointer data ); |
---|
39 | |
---|
40 | int main( int argc, char *argv[] ) |
---|
41 | { |
---|
42 | int i = 0; |
---|
43 | struct sigaction sig, old; |
---|
44 | |
---|
45 | memset( &global, 0, sizeof( global_t ) ); |
---|
46 | |
---|
47 | global.loop = g_main_new( FALSE ); |
---|
48 | |
---|
49 | log_init( ); |
---|
50 | |
---|
51 | nogaim_init(); |
---|
52 | |
---|
53 | CONF_FILE = g_strdup( CONF_FILE_DEF ); |
---|
54 | |
---|
55 | global.helpfile = g_strdup( HELP_FILE ); |
---|
56 | |
---|
57 | global.conf = conf_load( argc, argv ); |
---|
58 | if( global.conf == NULL ) |
---|
59 | return( 1 ); |
---|
60 | |
---|
61 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
62 | { |
---|
63 | i = bitlbee_inetd_init(); |
---|
64 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
65 | |
---|
66 | } |
---|
67 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
68 | { |
---|
69 | i = bitlbee_daemon_init(); |
---|
70 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
71 | } |
---|
72 | if( i != 0 ) |
---|
73 | return( i ); |
---|
74 | |
---|
75 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
76 | memset( &sig, 0, sizeof( sig ) ); |
---|
77 | sig.sa_handler = sighandler; |
---|
78 | sigaction( SIGPIPE, &sig, &old ); |
---|
79 | sig.sa_flags = SA_RESETHAND; |
---|
80 | sigaction( SIGINT, &sig, &old ); |
---|
81 | sigaction( SIGILL, &sig, &old ); |
---|
82 | sigaction( SIGBUS, &sig, &old ); |
---|
83 | sigaction( SIGFPE, &sig, &old ); |
---|
84 | sigaction( SIGSEGV, &sig, &old ); |
---|
85 | sigaction( SIGTERM, &sig, &old ); |
---|
86 | sigaction( SIGQUIT, &sig, &old ); |
---|
87 | sigaction( SIGXCPU, &sig, &old ); |
---|
88 | |
---|
89 | if( !getuid() || !geteuid() ) |
---|
90 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
91 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
92 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG ); |
---|
93 | else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 ) |
---|
94 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
95 | if( help_init( &(global.help) ) == NULL ) |
---|
96 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
97 | |
---|
98 | /* Workaround against runaway problems. Bah, this is really dirty, |
---|
99 | but in the end not really different from the <=0.91 situation, |
---|
100 | which makes it an acceptable temporary "solution". */ |
---|
101 | // g_timeout_add( 0, bitlbee_dirty_workaround, NULL ); |
---|
102 | |
---|
103 | g_main_run( global.loop ); |
---|
104 | |
---|
105 | return( 0 ); |
---|
106 | } |
---|
107 | |
---|
108 | gboolean bitlbee_dirty_workaround( gpointer data ) |
---|
109 | { |
---|
110 | usleep( 50000 ); |
---|
111 | return( TRUE ); |
---|
112 | } |
---|
113 | |
---|
114 | void proxyprofiler_dump(); |
---|
115 | |
---|
116 | static void sighandler( int signal ) |
---|
117 | { |
---|
118 | /* FIXME: In fact, calling log_message() here can be dangerous. But well, let's take the risk for now. */ |
---|
119 | |
---|
120 | if( signal == SIGTERM ) |
---|
121 | { |
---|
122 | static int first = 1; |
---|
123 | |
---|
124 | if( first ) |
---|
125 | { |
---|
126 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
127 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
128 | |
---|
129 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
130 | g_timeout_add_full( G_PRIORITY_LOW, 1, (GSourceFunc) bitlbee_shutdown, NULL, NULL ); |
---|
131 | |
---|
132 | first = 0; |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
137 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
138 | But just in case we decide to change that: */ |
---|
139 | |
---|
140 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
141 | raise( signal ); |
---|
142 | } |
---|
143 | } |
---|
144 | #ifdef PROXYPROFILER |
---|
145 | else if( signal == SIGXCPU ) |
---|
146 | { |
---|
147 | write_io_activity(); |
---|
148 | proxyprofiler_dump(); |
---|
149 | log_message( LOGLVL_ERROR, "Received SIGXCPU, dumping some debugging info." ); |
---|
150 | exit( 1 ); |
---|
151 | } |
---|
152 | #endif |
---|
153 | else if( signal != SIGPIPE ) |
---|
154 | { |
---|
155 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
156 | raise( signal ); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | double gettime() |
---|
161 | { |
---|
162 | struct timeval time[1]; |
---|
163 | |
---|
164 | gettimeofday( time, 0 ); |
---|
165 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
166 | } |
---|