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 "ipc.h" |
---|
32 | #include <signal.h> |
---|
33 | #include <unistd.h> |
---|
34 | #include <sys/time.h> |
---|
35 | #include <sys/wait.h> |
---|
36 | #include <pwd.h> |
---|
37 | |
---|
38 | global_t global; /* Against global namespace pollution */ |
---|
39 | |
---|
40 | static void sighandler( int signal ); |
---|
41 | |
---|
42 | int main( int argc, char *argv[] ) |
---|
43 | { |
---|
44 | int i = 0; |
---|
45 | char *old_cwd = NULL; |
---|
46 | struct sigaction sig, old; |
---|
47 | |
---|
48 | log_init(); |
---|
49 | global.conf_file = g_strdup( CONF_FILE_DEF ); |
---|
50 | global.conf = conf_load( argc, argv ); |
---|
51 | if( global.conf == NULL ) |
---|
52 | return( 1 ); |
---|
53 | |
---|
54 | b_main_init(); |
---|
55 | nogaim_init(); |
---|
56 | |
---|
57 | srand( time( NULL ) ^ getpid() ); |
---|
58 | global.helpfile = g_strdup( HELP_FILE ); |
---|
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 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
72 | { |
---|
73 | /* In case the operator requests a restart, we need this. */ |
---|
74 | old_cwd = g_malloc( 256 ); |
---|
75 | if( getcwd( old_cwd, 255 ) == NULL ) |
---|
76 | { |
---|
77 | log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) ); |
---|
78 | g_free( old_cwd ); |
---|
79 | old_cwd = NULL; |
---|
80 | } |
---|
81 | |
---|
82 | i = bitlbee_daemon_init(); |
---|
83 | log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION ); |
---|
84 | } |
---|
85 | if( i != 0 ) |
---|
86 | return( i ); |
---|
87 | |
---|
88 | if( ( global.conf->user && *global.conf->user ) && |
---|
89 | ( global.conf->runmode == RUNMODE_DAEMON || |
---|
90 | global.conf->runmode == RUNMODE_FORKDAEMON ) && |
---|
91 | ( !getuid() || !geteuid() ) ) |
---|
92 | { |
---|
93 | struct passwd *pw = NULL; |
---|
94 | pw = getpwnam( global.conf->user ); |
---|
95 | if( pw ) |
---|
96 | { |
---|
97 | setgid( pw->pw_gid ); |
---|
98 | setuid( pw->pw_uid ); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage ); |
---|
103 | if( global.storage == NULL ) |
---|
104 | { |
---|
105 | log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage ); |
---|
106 | return( 1 ); |
---|
107 | } |
---|
108 | |
---|
109 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
110 | memset( &sig, 0, sizeof( sig ) ); |
---|
111 | sig.sa_handler = sighandler; |
---|
112 | sigaction( SIGCHLD, &sig, &old ); |
---|
113 | sigaction( SIGPIPE, &sig, &old ); |
---|
114 | sig.sa_flags = SA_RESETHAND; |
---|
115 | sigaction( SIGINT, &sig, &old ); |
---|
116 | sigaction( SIGILL, &sig, &old ); |
---|
117 | sigaction( SIGBUS, &sig, &old ); |
---|
118 | sigaction( SIGFPE, &sig, &old ); |
---|
119 | sigaction( SIGSEGV, &sig, &old ); |
---|
120 | sigaction( SIGTERM, &sig, &old ); |
---|
121 | sigaction( SIGQUIT, &sig, &old ); |
---|
122 | sigaction( SIGXCPU, &sig, &old ); |
---|
123 | |
---|
124 | if( !getuid() || !geteuid() ) |
---|
125 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
126 | if( help_init( &global.help, global.helpfile ) == NULL ) |
---|
127 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
128 | |
---|
129 | b_main_run(); |
---|
130 | |
---|
131 | /* Mainly good for restarting, to make sure we close the help.txt fd. */ |
---|
132 | help_free( &global.help ); |
---|
133 | |
---|
134 | if( global.restart ) |
---|
135 | { |
---|
136 | char *fn = ipc_master_save_state(); |
---|
137 | |
---|
138 | chdir( old_cwd ); |
---|
139 | |
---|
140 | setenv( "_BITLBEE_RESTART_STATE", fn, 1 ); |
---|
141 | g_free( fn ); |
---|
142 | |
---|
143 | close( global.listen_socket ); |
---|
144 | |
---|
145 | if( execv( argv[0], argv ) == -1 ) |
---|
146 | /* Apparently the execve() failed, so let's just |
---|
147 | jump back into our own/current main(). */ |
---|
148 | /* Need more cleanup code to make this work. */ |
---|
149 | return 1; /* main( argc, argv ); */ |
---|
150 | } |
---|
151 | |
---|
152 | return( 0 ); |
---|
153 | } |
---|
154 | |
---|
155 | static void sighandler( int signal ) |
---|
156 | { |
---|
157 | /* FIXME: Calling log_message() here is not a very good idea! */ |
---|
158 | |
---|
159 | if( signal == SIGTERM ) |
---|
160 | { |
---|
161 | static int first = 1; |
---|
162 | |
---|
163 | if( first ) |
---|
164 | { |
---|
165 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
166 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
167 | |
---|
168 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
169 | b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL ); |
---|
170 | |
---|
171 | first = 0; |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
176 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
177 | But just in case we decide to change that: */ |
---|
178 | |
---|
179 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
180 | raise( signal ); |
---|
181 | } |
---|
182 | } |
---|
183 | else if( signal == SIGCHLD ) |
---|
184 | { |
---|
185 | pid_t pid; |
---|
186 | int st; |
---|
187 | |
---|
188 | while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 ) |
---|
189 | { |
---|
190 | if( WIFSIGNALED( st ) ) |
---|
191 | log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", (int) pid, WEXITSTATUS( st ) ); |
---|
192 | else if( WIFEXITED( st ) ) |
---|
193 | log_message( LOGLVL_INFO, "Client %d killed by signal %d.", (int) pid, WTERMSIG( st ) ); |
---|
194 | } |
---|
195 | } |
---|
196 | else if( signal != SIGPIPE ) |
---|
197 | { |
---|
198 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
199 | raise( signal ); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | double gettime() |
---|
204 | { |
---|
205 | struct timeval time[1]; |
---|
206 | |
---|
207 | gettimeofday( time, 0 ); |
---|
208 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
209 | } |
---|