Changeset 10135


Ignore:
Timestamp:
Feb 7, 2010, 10:17:42 PM (12 years ago)
Author:
charles
Message:

(trunk daemon) #2877 "Don't let the daemon launch if settings.json is corrupt" -- implemented in trunk for 1.90

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r10132 r10135  
    229229
    230230static void
     231printMessage( tr_bool foreground, int level, const char * name, const char * message, const char * file, int line )
     232{
     233#ifdef HAVE_SYSLOG
     234    if( foreground )
     235    {
     236        char timestr[64];
     237        tr_getLogTimeStr( timestr, sizeof( timestr ) );
     238        if( name )
     239            fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line );
     240        else
     241            fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, message, file, line );
     242    }
     243    else /* daemon... write to syslog */
     244    {
     245        int priority;
     246
     247        /* figure out the syslog priority */
     248        switch( level ) {
     249            case TR_MSG_ERR: priority = LOG_ERR; break;
     250            case TR_MSG_DBG: priority = LOG_DEBUG; break;
     251            default: priority = LOG_INFO; break;
     252        }
     253
     254        if( name )
     255            syslog( priority, "%s %s (%s:%d)", name, message, file, line );
     256        else
     257            syslog( priority, "%s (%s:%d)", message, file, line );
     258    }
     259#else
     260    {
     261        char timestr[64];
     262        tr_getLogTimeStr( timestr, sizeof( timestr ) );
     263        if( name )
     264            fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line );
     265        else
     266            fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, message, file, line );
     267    }
     268#endif
     269}
     270
     271static void
    231272pumpLogMessages( tr_bool foreground )
    232273{
     
    235276
    236277    for( l=list; l!=NULL; l=l->next )
    237     {
    238 #ifdef HAVE_SYSLOG
    239         if( foreground )
    240         {
    241             char timestr[64];
    242             tr_getLogTimeStr( timestr, sizeof( timestr ) );
    243             if( l->name )
    244                 fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
    245             else
    246                 fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
    247         }
    248         else /* daemon... write to syslog */
    249         {
    250             int priority;
    251 
    252             /* figure out the syslog priority */
    253             switch( l->level ) {
    254                 case TR_MSG_ERR: priority = LOG_ERR; break;
    255                 case TR_MSG_DBG: priority = LOG_DEBUG; break;
    256                 default: priority = LOG_INFO; break;
    257             }
    258 
    259             if( l->name )
    260                 syslog( priority, "%s %s (%s:%d)", l->name, l->message, l->file, l->line );
    261             else
    262                 syslog( priority, "%s (%s:%d)", l->message, l->file, l->line );
    263         }
    264 #else
    265         {
    266             char timestr[64];
    267             tr_getLogTimeStr( timestr, sizeof( timestr ) );
    268             if( l->name )
    269                 fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
    270             else
    271                 fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
    272         }
    273 #endif
    274     }
     278        printMessage( foreground, l->level, l->name, l->message, l->file, l->line );
    275279
    276280    tr_freeMessageList( list );
     
    300304    configDir = getConfigDir( argc, (const char**)argv );
    301305    loaded = tr_sessionLoadSettings( &settings, configDir, MY_NAME );
    302     tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE );
    303306
    304307    /* overwrite settings from the comamndline */
     
    391394    }
    392395
     396    if( !loaded )
     397    {
     398        printMessage( foreground, TR_MSG_ERR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__ );
     399        return -1;
     400    }
     401
    393402    if( dumpSettings )
    394403    {
     
    401410    if( !foreground && tr_daemon( TRUE, FALSE ) < 0 )
    402411    {
    403         fprintf( stderr, "failed to daemonize: %s\n", strerror( errno ) );
     412        char buf[256];
     413        tr_snprintf( buf, sizeof( buf ), "Failed to dameonize: %s", tr_strerror( errno ) );
     414        printMessage( foreground, TR_MSG_ERR, MY_NAME, buf, __FILE__, __LINE__ );
    404415        exit( 1 );
    405416    }
     
    407418    /* start the session */
    408419    mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings );
    409 
    410     if( loaded )
    411         tr_ninf( NULL, "Using settings from \"%s\"", configDir );
    412     else
    413         tr_nerr( NULL, "Couldn't find settings in \"%s\"; using defaults", configDir );
    414 
     420    tr_ninf( NULL, "Using settings from \"%s\"", configDir );
    415421    tr_sessionSaveSettings( mySession, configDir, &settings );
    416422
  • trunk/libtransmission/session.c

    r10129 r10135  
    1212
    1313#include <assert.h>
     14#include <errno.h> /* ENOENT */
    1415#include <stdlib.h>
    1516#include <string.h> /* memcpy */
     
    363364tr_sessionLoadSettings( tr_benc * d, const char * configDir, const char * appName )
    364365{
     366    int err = 0;
    365367    char * filename;
    366368    tr_benc fileSettings;
     
    384386    /* file settings override the defaults */
    385387    filename = tr_buildPath( configDir, "settings.json", NULL );
    386     if( !tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename ) ) {
     388    err = tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename );
     389    if( !err ) {
    387390        tr_bencMergeDicts( d, &fileSettings );
    388391        tr_bencFree( &fileSettings );
    389         success = TRUE;
    390392    }
    391393
     
    393395    tr_bencFree( &sessionDefaults );
    394396    tr_free( filename );
     397    success = (err==0) || (err==ENOENT);
    395398    return success;
    396399}
     
    411414    {
    412415        tr_benc fileSettings;
    413         if( !tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename ) )
     416        const int err = tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename );
     417        if( !err )
    414418        {
    415419            tr_bencMergeDicts( &settings, &fileSettings );
     
    432436    /* save the result */
    433437    tr_bencToFile( &settings, TR_FMT_JSON, filename );
    434     tr_inf( "Saved \"%s\"", filename );
    435438
    436439    /* cleanup */
  • trunk/libtransmission/utils.c

    r10069 r10135  
    4747
    4848
    49 int                   messageLevel = 0;
     49int                   messageLevel = TR_MSG_INF;
    5050static tr_lock *      messageLock = NULL;
    5151static tr_bool        messageQueuing = FALSE;
     
    285285    va_list ap;
    286286
    287     tr_lockLock( messageLock );
     287    if( messageLock != NULL )
     288        tr_lockLock( messageLock );
    288289
    289290    /* build the text message */
     
    343344    }
    344345
    345     tr_lockUnlock( messageLock );
     346    if( messageLock != NULL )
     347        tr_lockUnlock( messageLock );
     348
    346349    errno = err;
    347350}
Note: See TracChangeset for help on using the changeset viewer.