[b7d3cc34] | 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" |
---|
[764c7d1] | 29 | #include "otr.h" |
---|
[b7d3cc34] | 30 | #include "protocols/nogaim.h" |
---|
| 31 | #include "help.h" |
---|
[54879ab] | 32 | #include "ipc.h" |
---|
[ba5add7] | 33 | #include "lib/ssl_client.h" |
---|
[b7d3cc34] | 34 | #include <signal.h> |
---|
| 35 | #include <unistd.h> |
---|
| 36 | #include <sys/time.h> |
---|
[d25f6fc] | 37 | #include <sys/wait.h> |
---|
[aaf92a9] | 38 | #include <pwd.h> |
---|
[b7d3cc34] | 39 | |
---|
| 40 | global_t global; /* Against global namespace pollution */ |
---|
| 41 | |
---|
| 42 | static void sighandler( int signal ); |
---|
| 43 | |
---|
[54879ab] | 44 | int main( int argc, char *argv[], char **envp ) |
---|
[b7d3cc34] | 45 | { |
---|
| 46 | int i = 0; |
---|
[58bc4e6] | 47 | char *old_cwd = NULL; |
---|
[b7d3cc34] | 48 | struct sigaction sig, old; |
---|
| 49 | |
---|
[d25f6fc] | 50 | log_init(); |
---|
[eeb85a8] | 51 | global.conf_file = g_strdup( CONF_FILE_DEF ); |
---|
[b7d3cc34] | 52 | global.conf = conf_load( argc, argv ); |
---|
| 53 | if( global.conf == NULL ) |
---|
| 54 | return( 1 ); |
---|
[6e1fed7] | 55 | |
---|
[60c1a4e] | 56 | b_main_init(); |
---|
| 57 | nogaim_init(); |
---|
[ba5add7] | 58 | /* Ugly Note: libotr and gnutls both use libgcrypt. libgcrypt |
---|
| 59 | has a process-global config state whose initialization happpens |
---|
| 60 | twice if libotr and gnutls are used together. libotr installs custom |
---|
| 61 | memory management functions for libgcrypt while our gnutls module |
---|
| 62 | uses the defaults. Therefore we initialize OTR after SSL. *sigh* */ |
---|
| 63 | ssl_init(); |
---|
[764c7d1] | 64 | otr_init(); |
---|
[60c1a4e] | 65 | |
---|
| 66 | srand( time( NULL ) ^ getpid() ); |
---|
| 67 | global.helpfile = g_strdup( HELP_FILE ); |
---|
| 68 | |
---|
[b7d3cc34] | 69 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
| 70 | { |
---|
| 71 | i = bitlbee_inetd_init(); |
---|
| 72 | log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); |
---|
| 73 | |
---|
| 74 | } |
---|
| 75 | else if( global.conf->runmode == RUNMODE_DAEMON ) |
---|
| 76 | { |
---|
| 77 | i = bitlbee_daemon_init(); |
---|
| 78 | log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); |
---|
| 79 | } |
---|
[d25f6fc] | 80 | else if( global.conf->runmode == RUNMODE_FORKDAEMON ) |
---|
| 81 | { |
---|
[54879ab] | 82 | /* In case the operator requests a restart, we need this. */ |
---|
| 83 | old_cwd = g_malloc( 256 ); |
---|
| 84 | if( getcwd( old_cwd, 255 ) == NULL ) |
---|
| 85 | { |
---|
| 86 | log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) ); |
---|
| 87 | g_free( old_cwd ); |
---|
| 88 | old_cwd = NULL; |
---|
| 89 | } |
---|
| 90 | |
---|
[d25f6fc] | 91 | i = bitlbee_daemon_init(); |
---|
| 92 | log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION ); |
---|
| 93 | } |
---|
[b7d3cc34] | 94 | if( i != 0 ) |
---|
| 95 | return( i ); |
---|
[6e1fed7] | 96 | |
---|
[aaf92a9] | 97 | if( ( global.conf->user && *global.conf->user ) && |
---|
| 98 | ( global.conf->runmode == RUNMODE_DAEMON || |
---|
| 99 | global.conf->runmode == RUNMODE_FORKDAEMON ) && |
---|
| 100 | ( !getuid() || !geteuid() ) ) |
---|
| 101 | { |
---|
| 102 | struct passwd *pw = NULL; |
---|
| 103 | pw = getpwnam( global.conf->user ); |
---|
| 104 | if( pw ) |
---|
| 105 | { |
---|
| 106 | setgid( pw->pw_gid ); |
---|
| 107 | setuid( pw->pw_uid ); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[d25f6fc] | 111 | global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage ); |
---|
[aaf92a9] | 112 | if( global.storage == NULL ) |
---|
| 113 | { |
---|
[b73ac9c] | 114 | log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage ); |
---|
[1ee6c18] | 115 | return( 1 ); |
---|
| 116 | } |
---|
[b7d3cc34] | 117 | |
---|
| 118 | /* Catch some signals to tell the user what's happening before quitting */ |
---|
| 119 | memset( &sig, 0, sizeof( sig ) ); |
---|
| 120 | sig.sa_handler = sighandler; |
---|
[d25f6fc] | 121 | sigaction( SIGCHLD, &sig, &old ); |
---|
[b7d3cc34] | 122 | sigaction( SIGPIPE, &sig, &old ); |
---|
| 123 | sig.sa_flags = SA_RESETHAND; |
---|
| 124 | sigaction( SIGINT, &sig, &old ); |
---|
| 125 | sigaction( SIGILL, &sig, &old ); |
---|
| 126 | sigaction( SIGBUS, &sig, &old ); |
---|
| 127 | sigaction( SIGFPE, &sig, &old ); |
---|
| 128 | sigaction( SIGSEGV, &sig, &old ); |
---|
| 129 | sigaction( SIGTERM, &sig, &old ); |
---|
| 130 | sigaction( SIGQUIT, &sig, &old ); |
---|
| 131 | sigaction( SIGXCPU, &sig, &old ); |
---|
| 132 | |
---|
| 133 | if( !getuid() || !geteuid() ) |
---|
| 134 | log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); |
---|
[0fbda193] | 135 | if( help_init( &global.help, global.helpfile ) == NULL ) |
---|
[b7d3cc34] | 136 | log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE ); |
---|
| 137 | |
---|
[ba9edaa] | 138 | b_main_run(); |
---|
[b7d3cc34] | 139 | |
---|
[0fbda193] | 140 | /* Mainly good for restarting, to make sure we close the help.txt fd. */ |
---|
| 141 | help_free( &global.help ); |
---|
| 142 | |
---|
[54879ab] | 143 | if( global.restart ) |
---|
| 144 | { |
---|
| 145 | char *fn = ipc_master_save_state(); |
---|
| 146 | char **args; |
---|
| 147 | int n, i; |
---|
| 148 | |
---|
| 149 | chdir( old_cwd ); |
---|
| 150 | |
---|
| 151 | n = 0; |
---|
| 152 | args = g_new0( char *, argc + 3 ); |
---|
| 153 | args[n++] = argv[0]; |
---|
| 154 | if( fn ) |
---|
| 155 | { |
---|
| 156 | args[n++] = "-R"; |
---|
| 157 | args[n++] = fn; |
---|
| 158 | } |
---|
| 159 | for( i = 1; argv[i] && i < argc; i ++ ) |
---|
| 160 | { |
---|
| 161 | if( strcmp( argv[i], "-R" ) == 0 ) |
---|
| 162 | i += 2; |
---|
| 163 | |
---|
| 164 | args[n++] = argv[i]; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | close( global.listen_socket ); |
---|
| 168 | |
---|
| 169 | execve( args[0], args, envp ); |
---|
| 170 | } |
---|
| 171 | |
---|
[b7d3cc34] | 172 | return( 0 ); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | static void sighandler( int signal ) |
---|
| 176 | { |
---|
[d25f6fc] | 177 | /* FIXME: Calling log_message() here is not a very good idea! */ |
---|
[b7d3cc34] | 178 | |
---|
| 179 | if( signal == SIGTERM ) |
---|
| 180 | { |
---|
| 181 | static int first = 1; |
---|
| 182 | |
---|
| 183 | if( first ) |
---|
| 184 | { |
---|
| 185 | /* We don't know what we were doing when this signal came in. It's not safe to touch |
---|
| 186 | the user data now (not to mention writing them to disk), so add a timer. */ |
---|
| 187 | |
---|
| 188 | log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); |
---|
[ba9edaa] | 189 | b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL ); |
---|
[b7d3cc34] | 190 | |
---|
| 191 | first = 0; |
---|
| 192 | } |
---|
| 193 | else |
---|
| 194 | { |
---|
| 195 | /* Well, actually, for now we'll never need this part because this signal handler |
---|
| 196 | will never be called more than once in a session for a non-SIGPIPE signal... |
---|
| 197 | But just in case we decide to change that: */ |
---|
| 198 | |
---|
| 199 | log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); |
---|
| 200 | raise( signal ); |
---|
| 201 | } |
---|
| 202 | } |
---|
[d25f6fc] | 203 | else if( signal == SIGCHLD ) |
---|
| 204 | { |
---|
| 205 | pid_t pid; |
---|
| 206 | int st; |
---|
| 207 | |
---|
| 208 | while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 ) |
---|
| 209 | { |
---|
| 210 | if( WIFSIGNALED( st ) ) |
---|
[52744f8] | 211 | log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", (int) pid, WEXITSTATUS( st ) ); |
---|
[d25f6fc] | 212 | else if( WIFEXITED( st ) ) |
---|
[52744f8] | 213 | log_message( LOGLVL_INFO, "Client %d killed by signal %d.", (int) pid, WTERMSIG( st ) ); |
---|
[d25f6fc] | 214 | } |
---|
| 215 | } |
---|
[b7d3cc34] | 216 | else if( signal != SIGPIPE ) |
---|
| 217 | { |
---|
| 218 | log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); |
---|
| 219 | raise( signal ); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | double gettime() |
---|
| 224 | { |
---|
| 225 | struct timeval time[1]; |
---|
| 226 | |
---|
| 227 | gettimeofday( time, 0 ); |
---|
| 228 | return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); |
---|
| 229 | } |
---|