Changeset 5ebe625 for unix.c


Ignore:
Timestamp:
2006-02-12T07:26:20Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
f665dab
Parents:
a323a22 (diff), 58bc4e6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge

File:
1 edited

Legend:

Unmodified
Added
Removed
  • unix.c

    ra323a22 r5ebe625  
    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( );
     51        log_init();
    4952
    5053        nogaim_init();
     
    7073                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
    7174        }
     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        }
    7289        if( i != 0 )
    7390                return( i );
    7491
    75         global.storage = storage_init( global.conf->primary_storage,
    76                                                                    global.conf->migrate_storage );
     92        global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
    7793        if ( global.storage == NULL) {
    7894                log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
     
    84100        memset( &sig, 0, sizeof( sig ) );
    85101        sig.sa_handler = sighandler;
     102        sigaction( SIGCHLD, &sig, &old );
    86103        sigaction( SIGPIPE, &sig, &old );
    87104        sig.sa_flags = SA_RESETHAND;
     
    102119        g_main_run( global.loop );
    103120       
     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       
    104150        return( 0 );
    105151}
     
    107153static void sighandler( int signal )
    108154{
    109         /* 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! */
    110156       
    111157        if( signal == SIGTERM )
     
    133179                }
    134180        }
     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        }
    135194        else if( signal != SIGPIPE )
    136195        {
Note: See TracChangeset for help on using the changeset viewer.