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