Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • unix.c

    rb73ac9c r58bc4e6  
    2929#include "protocols/nogaim.h"
    3030#include "help.h"
     31#include "ipc.h"
    3132#include <signal.h>
    3233#include <unistd.h>
    3334#include <sys/time.h>
     35#include <sys/wait.h>
    3436
    3537global_t global;        /* Against global namespace pollution */
     
    3739static void sighandler( int signal );
    3840
    39 int main( int argc, char *argv[] )
     41int main( int argc, char *argv[], char **envp )
    4042{
    4143        int i = 0;
     44        char *old_cwd = NULL;
    4245        struct sigaction sig, old;
    4346       
     
    4649        global.loop = g_main_new( FALSE );
    4750       
    48         log_init( );
    49         nogaim_init( );
    50        
     51        log_init();
     52
     53        nogaim_init();
     54
    5155        CONF_FILE = g_strdup( CONF_FILE_DEF );
    5256       
     
    6973                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
    7074        }
     75        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
     76        {
     77                /* In case the operator requests a restart, we need this. */
     78                old_cwd = g_malloc( 256 );
     79                if( getcwd( old_cwd, 255 ) == NULL )
     80                {
     81                        log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) );
     82                        g_free( old_cwd );
     83                        old_cwd = NULL;
     84                }
     85               
     86                i = bitlbee_daemon_init();
     87                log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION );
     88        }
    7189        if( i != 0 )
    7290                return( i );
    7391
    74         global.storage = storage_init( global.conf->primary_storage,
    75                                                                    global.conf->migrate_storage );
     92        global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
    7693        if ( global.storage == NULL) {
    7794                log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
     
    83100        memset( &sig, 0, sizeof( sig ) );
    84101        sig.sa_handler = sighandler;
     102        sigaction( SIGCHLD, &sig, &old );
    85103        sigaction( SIGPIPE, &sig, &old );
    86104        sig.sa_flags = SA_RESETHAND;
     
    101119        g_main_run( global.loop );
    102120       
     121        if( global.restart )
     122        {
     123                char *fn = ipc_master_save_state();
     124                char **args;
     125                int n, i;
     126               
     127                chdir( old_cwd );
     128               
     129                n = 0;
     130                args = g_new0( char *, argc + 3 );
     131                args[n++] = argv[0];
     132                if( fn )
     133                {
     134                        args[n++] = "-R";
     135                        args[n++] = fn;
     136                }
     137                for( i = 1; argv[i] && i < argc; i ++ )
     138                {
     139                        if( strcmp( argv[i], "-R" ) == 0 )
     140                                i += 2;
     141                       
     142                        args[n++] = argv[i];
     143                }
     144               
     145                close( global.listen_socket );
     146               
     147                execve( args[0], args, envp );
     148        }
     149       
    103150        return( 0 );
    104151}
     
    106153static void sighandler( int signal )
    107154{
    108         /* FIXME: In fact, calling log_message() here can be dangerous. But well, let's take the risk for now. */
     155        /* FIXME: Calling log_message() here is not a very good idea! */
    109156       
    110157        if( signal == SIGTERM )
     
    132179                }
    133180        }
     181        else if( signal == SIGCHLD )
     182        {
     183                pid_t pid;
     184                int st;
     185               
     186                while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 )
     187                {
     188                        if( WIFSIGNALED( st ) )
     189                                log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", pid, WEXITSTATUS( st ) );
     190                        else if( WIFEXITED( st ) )
     191                                log_message( LOGLVL_INFO, "Client %d killed by signal %d.", pid, WTERMSIG( st ) );
     192                }
     193        }
    134194        else if( signal != SIGPIPE )
    135195        {
Note: See TracChangeset for help on using the changeset viewer.