]> granicus.if.org Git - transmission/commitdiff
(trunmk libT) #4162 "daemon should reopen logfile on SIGHUP" -- patch from gvdl to...
authorJordan Lee <jordan@transmissionbt.com>
Wed, 30 May 2012 17:59:52 +0000 (17:59 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Wed, 30 May 2012 17:59:52 +0000 (17:59 +0000)
daemon/daemon.c

index 28b77991af98a9cbcd8f79a89e2f2ff8503206a8..d7a3c63b2f29cf8fc02af76328f79a7c64ad9855 100644 (file)
 #define SPEED_G_STR "GB/s"
 #define SPEED_T_STR "TB/s"
 
+#define LOGFILE_MODE_STR "a+"
+
 static bool paused = false;
 static bool closing = false;
 static bool seenHUP = false;
+static const char *logfileName = NULL;
+static FILE *logfile = NULL;
 static tr_session * mySession = NULL;
 
 /***
@@ -150,7 +154,16 @@ gotsig( int sig )
             else
             {
                 tr_benc settings;
-                const char * configDir = tr_sessionGetConfigDir( mySession );
+                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_inf( "Reloading settings from \"%s\"", configDir );
                 tr_bencInitDict( &settings, 0 );
                 tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, true );
@@ -353,7 +366,6 @@ main( int argc, char ** argv )
     const char * configDir = NULL;
     const char * pid_filename;
     dtr_watchdir * watchdir = NULL;
-    FILE * logfile = NULL;
     bool pidfile_created = false;
     tr_session * session = NULL;
 
@@ -392,8 +404,10 @@ main( int argc, char ** argv )
                       break;
             case 'd': dumpSettings = true;
                       break;
-            case 'e': logfile = fopen( optarg, "a+" );
-                      if( logfile == NULL )
+            case 'e': logfile = fopen( optarg, LOGFILE_MODE_STR );
+                      if( logfile )
+                          logfileName = optarg;
+                      else
                           fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) );
                       break;
             case 'f': foreground = true;