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