From 99318adcb88fa3f1dd21882ec364e682fec96c5e Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 7 Nov 2005 17:32:40 +0100 Subject: Import work on services-based Win32 port --- unix.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'unix.c') diff --git a/unix.c b/unix.c index 140be68e..159b3126 100644 --- a/unix.c +++ b/unix.c @@ -60,12 +60,18 @@ int main( int argc, char *argv[] ) if( global.conf->runmode == RUNMODE_INETD ) { + log_link( LOGLVL_ERROR, LOGOUTPUT_IRC ); + log_link( LOGLVL_WARNING, LOGOUTPUT_IRC ); + i = bitlbee_inetd_init(); log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION ); } else if( global.conf->runmode == RUNMODE_DAEMON ) { + log_link( LOGLVL_ERROR, LOGOUTPUT_SYSLOG ); + log_link( LOGLVL_WARNING, LOGOUTPUT_SYSLOG ); + i = bitlbee_daemon_init(); log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION ); } -- cgit v1.2.3 From 5be87b2e736962dce2576012b7f1cf215f169f34 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 2 Apr 2008 17:03:02 +0200 Subject: Move unix-specific random_bytes() implementation to unix.c. --- unix.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'unix.c') diff --git a/unix.c b/unix.c index d25aeb2e..39ca5732 100644 --- a/unix.c +++ b/unix.c @@ -218,3 +218,70 @@ double gettime() gettimeofday( time, 0 ); return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); } + +/* A pretty reliable random number generator. Tries to use the /dev/random + devices first, and falls back to the random number generator from libc + when it fails. Opens randomizer devices with O_NONBLOCK to make sure a + lack of entropy won't halt BitlBee. */ +void random_bytes( unsigned char *buf, int count ) +{ + static int use_dev = -1; + + /* Actually this probing code isn't really necessary, is it? */ + if( use_dev == -1 ) + { + if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) + use_dev = 1; + else + { + use_dev = 0; + srand( ( getpid() << 16 ) ^ time( NULL ) ); + } + } + + if( use_dev ) + { + int fd; + + /* At least on Linux, /dev/random can block if there's not + enough entropy. We really don't want that, so if it can't + give anything, use /dev/urandom instead. */ + if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) + if( read( fd, buf, count ) == count ) + { + close( fd ); + return; + } + close( fd ); + + /* urandom isn't supposed to block at all, but just to be + sure. If it blocks, we'll disable use_dev and use the libc + randomizer instead. */ + if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) + if( read( fd, buf, count ) == count ) + { + close( fd ); + return; + } + close( fd ); + + /* If /dev/random blocks once, we'll still try to use it + again next time. If /dev/urandom also fails for some + reason, stick with libc during this session. */ + + use_dev = 0; + srand( ( getpid() << 16 ) ^ time( NULL ) ); + } + + if( !use_dev ) + { + int i; + + /* Possibly the LSB of rand() isn't very random on some + platforms. Seems okay on at least Linux and OSX though. */ + for( i = 0; i < count; i ++ ) + buf[i] = rand() & 0xff; + } +} + + -- cgit v1.2.3 From 7f49a8642e162611cf20ab95955098597d1f4472 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 10 Jun 2008 05:09:49 +0200 Subject: Move random_bytes() back to lib/ --- unix.c | 65 ----------------------------------------------------------------- 1 file changed, 65 deletions(-) (limited to 'unix.c') diff --git a/unix.c b/unix.c index f90f03b9..0566fa56 100644 --- a/unix.c +++ b/unix.c @@ -225,69 +225,4 @@ double gettime() return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); } -/* A pretty reliable random number generator. Tries to use the /dev/random - devices first, and falls back to the random number generator from libc - when it fails. Opens randomizer devices with O_NONBLOCK to make sure a - lack of entropy won't halt BitlBee. */ -void random_bytes( unsigned char *buf, int count ) -{ - static int use_dev = -1; - - /* Actually this probing code isn't really necessary, is it? */ - if( use_dev == -1 ) - { - if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) - use_dev = 1; - else - { - use_dev = 0; - srand( ( getpid() << 16 ) ^ time( NULL ) ); - } - } - - if( use_dev ) - { - int fd; - - /* At least on Linux, /dev/random can block if there's not - enough entropy. We really don't want that, so if it can't - give anything, use /dev/urandom instead. */ - if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) - if( read( fd, buf, count ) == count ) - { - close( fd ); - return; - } - close( fd ); - - /* urandom isn't supposed to block at all, but just to be - sure. If it blocks, we'll disable use_dev and use the libc - randomizer instead. */ - if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) - if( read( fd, buf, count ) == count ) - { - close( fd ); - return; - } - close( fd ); - - /* If /dev/random blocks once, we'll still try to use it - again next time. If /dev/urandom also fails for some - reason, stick with libc during this session. */ - - use_dev = 0; - srand( ( getpid() << 16 ) ^ time( NULL ) ); - } - - if( !use_dev ) - { - int i; - - /* Possibly the LSB of rand() isn't very random on some - platforms. Seems okay on at least Linux and OSX though. */ - for( i = 0; i < count; i ++ ) - buf[i] = rand() & 0xff; - } -} - -- cgit v1.2.3 From cd63d5822e76a6126bb3017567c9ce2869a44e0b Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 29 Jun 2008 13:47:39 +0100 Subject: Now using an environment variable instead of a flag to pass state info when restarting the ForkDaemon. Preparing for a proper fallback when execv() fails. (Bug #425) --- unix.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) (limited to 'unix.c') diff --git a/unix.c b/unix.c index d25aeb2e..336b49c4 100644 --- a/unix.c +++ b/unix.c @@ -39,7 +39,7 @@ global_t global; /* Against global namespace pollution */ static void sighandler( int signal ); -int main( int argc, char *argv[], char **envp ) +int main( int argc, char *argv[] ) { int i = 0; char *old_cwd = NULL; @@ -134,30 +134,19 @@ int main( int argc, char *argv[], char **envp ) if( global.restart ) { char *fn = ipc_master_save_state(); - char **args; - int n, i; chdir( old_cwd ); - n = 0; - args = g_new0( char *, argc + 3 ); - args[n++] = argv[0]; - if( fn ) - { - args[n++] = "-R"; - args[n++] = fn; - } - for( i = 1; argv[i] && i < argc; i ++ ) - { - if( strcmp( argv[i], "-R" ) == 0 ) - i += 2; - - args[n++] = argv[i]; - } + setenv( "_BITLBEE_RESTART_STATE", fn, 1 ); + g_free( fn ); close( global.listen_socket ); - execve( args[0], args, envp ); + if( execv( argv[0], argv ) == -1 ) + /* Apparently the execve() failed, so let's just + jump back into our own/current main(). */ + /* Need more cleanup code to make this work. */ + return 1; /* main( argc, argv ); */ } return( 0 ); -- cgit v1.2.3