static tr_benc* getPrefs( void ) { static tr_benc settings; static gboolean loaded = FALSE; if( !loaded ) { tr_bencInitDict( &settings, 0 ); tr_prefs_init_defaults( &settings ); tr_sessionLoadSettings( &settings, gl_confdir, MY_CONFIG_NAME ); loaded = TRUE; } return &settings; }
static void gotsig (int sig) { switch (sig) { case SIGHUP: { if (!mySession) { tr_logAddInfo ("Deferring reload until session is fully started."); seenHUP = true; } else { tr_variant settings; const char * configDir; /* reopen the logfile to allow for log rotation */ if (logfileName) { logfile = freopen (logfileName, LOGFILE_MODE_STR, logfile); if (!logfile) fprintf (stderr, "Couldn't reopen \"%s\": %s\n", logfileName, tr_strerror (errno)); } configDir = tr_sessionGetConfigDir (mySession); tr_logAddInfo ("Reloading settings from \"%s\"", configDir); tr_variantInitDict (&settings, 0); tr_variantDictAddBool (&settings, TR_KEY_rpc_enabled, true); tr_sessionLoadSettings (&settings, configDir, MY_NAME); tr_sessionSet (mySession, &settings); tr_variantFree (&settings); tr_sessionReloadBlocklists (mySession); } break; } default: tr_logAddError ("Unexpected signal (%d) in daemon, closing.", sig); /* no break */ case SIGINT: case SIGTERM: event_base_loopexit(ev_base, NULL); break; } }
static void gotsig( int sig ) { switch( sig ) { case SIGHUP: { tr_benc settings; const char * configDir = tr_sessionGetConfigDir( mySession ); tr_inf( "Reloading settings from \"%s\"", configDir ); tr_bencInitDict( &settings, 0 ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE ); tr_sessionLoadSettings( &settings, configDir, MY_NAME ); tr_sessionSet( mySession, &settings ); tr_bencFree( &settings ); tr_sessionReloadBlocklists( mySession ); break; } default: closing = TRUE; break; } }
int main( int argc, char ** argv ) { int c; const char * optarg; tr_benc settings; tr_bool boolVal; tr_bool loaded; tr_bool foreground = FALSE; tr_bool dumpSettings = FALSE; const char * configDir = NULL; const char * pid_filename; dtr_watchdir * watchdir = NULL; FILE * logfile = NULL; tr_bool pidfile_created = FALSE; signal( SIGINT, gotsig ); signal( SIGTERM, gotsig ); #ifndef WIN32 signal( SIGHUP, gotsig ); #endif /* load settings from defaults + config file */ tr_bencInitDict( &settings, 0 ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE ); configDir = getConfigDir( argc, (const char**)argv ); loaded = tr_sessionLoadSettings( &settings, configDir, MY_NAME ); /* overwrite settings from the comamndline */ tr_optind = 1; while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) { switch( c ) { case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, TRUE ); break; case 'b': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE ); break; case 'B': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE ); break; case 'c': tr_bencDictAddStr( &settings, PREF_KEY_DIR_WATCH, optarg ); tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, TRUE ); break; case 'C': tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, FALSE ); break; case 941: tr_bencDictAddStr( &settings, TR_PREFS_KEY_INCOMPLETE_DIR, optarg ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, TRUE ); break; case 942: tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, FALSE ); break; case 'd': dumpSettings = TRUE; break; case 'e': logfile = fopen( optarg, "a+" ); if( logfile == NULL ) fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) ); break; case 'f': foreground = TRUE; break; case 'g': /* handled above */ break; case 'V': /* version */ fprintf(stderr, "%s %s\n", MY_NAME, LONG_VERSION_STRING); exit( 0 ); case 'o': tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, TRUE ); break; case 'O': tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, FALSE ); break; case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) ); break; case 't': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, TRUE ); break; case 'T': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, FALSE ); break; case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg ); break; case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg ); break; case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); break; case 'P': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); break; case 'm': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, TRUE ); break; case 'M': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, FALSE ); break; case 'L': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( optarg ) ); break; case 'l': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( optarg ) ); break; case 800: paused = TRUE; break; case 910: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); break; case 911: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); break; case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); break; case 'i': tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, optarg ); break; case 'I': tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, optarg ); break; case 'r': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, optarg ); break; case 953: tr_bencDictAddReal( &settings, TR_PREFS_KEY_RATIO, atof(optarg) ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, TRUE ); break; case 954: tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, FALSE ); break; case 'x': tr_bencDictAddStr( &settings, PREF_KEY_PIDFILE, optarg ); break; case 'y': tr_bencDictAddBool( &settings, TR_PREFS_KEY_LPD_ENABLED, TRUE ); break; case 'Y': tr_bencDictAddBool( &settings, TR_PREFS_KEY_LPD_ENABLED, FALSE ); break; case 810: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_ERR ); break; case 811: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_INF ); break; case 812: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_DBG ); break; default: showUsage( ); break; } } if( foreground && !logfile ) logfile = stderr; if( !loaded ) { printMessage( logfile, TR_MSG_ERR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__ ); return -1; } if( dumpSettings ) { char * str = tr_bencToStr( &settings, TR_FMT_JSON, NULL ); fprintf( stderr, "%s", str ); tr_free( str ); return 0; } if( !foreground && tr_daemon( TRUE, FALSE ) < 0 ) { char buf[256]; tr_snprintf( buf, sizeof( buf ), "Failed to daemonize: %s", tr_strerror( errno ) ); printMessage( logfile, TR_MSG_ERR, MY_NAME, buf, __FILE__, __LINE__ ); exit( 1 ); } /* start the session */ tr_formatter_mem_init( MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR ); tr_formatter_size_init( DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR ); tr_formatter_speed_init( SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR ); mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings ); tr_ninf( NULL, "Using settings from \"%s\"", configDir ); tr_sessionSaveSettings( mySession, configDir, &settings ); pid_filename = NULL; tr_bencDictFindStr( &settings, PREF_KEY_PIDFILE, &pid_filename ); if( pid_filename && *pid_filename ) { FILE * fp = fopen( pid_filename, "w+" ); if( fp != NULL ) { fprintf( fp, "%d", (int)getpid() ); fclose( fp ); tr_inf( "Saved pidfile \"%s\"", pid_filename ); pidfile_created = TRUE; } else tr_err( "Unable to save pidfile \"%s\": %s", pid_filename, strerror( errno ) ); } if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal ) tr_ninf( MY_NAME, "requiring authentication" ); /* maybe add a watchdir */ { const char * dir; if( tr_bencDictFindBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, &boolVal ) && boolVal && tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &dir ) && dir && *dir ) { tr_inf( "Watching \"%s\" for new .torrent files", dir ); watchdir = dtr_watchdir_new( mySession, dir, onFileAdded ); } } /* load the torrents */ { tr_torrent ** torrents; tr_ctor * ctor = tr_ctorNew( mySession ); if( paused ) tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); torrents = tr_sessionLoadTorrents( mySession, ctor, NULL ); tr_free( torrents ); tr_ctorFree( ctor ); } #ifdef HAVE_SYSLOG if( !foreground ) openlog( MY_NAME, LOG_CONS|LOG_PID, LOG_DAEMON ); #endif while( !closing ) { tr_wait_msec( 1000 ); /* sleep one second */ dtr_watchdir_update( watchdir ); pumpLogMessages( logfile ); } /* shutdown */ #if HAVE_SYSLOG if( !foreground ) { syslog( LOG_INFO, "%s", "Closing session" ); closelog( ); } #endif printf( "Closing transmission session..." ); tr_sessionSaveSettings( mySession, configDir, &settings ); dtr_watchdir_free( watchdir ); tr_sessionClose( mySession ); printf( " done.\n" ); /* cleanup */ if( pidfile_created ) remove( pid_filename ); tr_bencFree( &settings ); return 0; }