Ticket #33: mythtv.logrotate.diff

File mythtv.logrotate.diff, 2.5 KB (added by Robert Tsai <rtsai1111>, 19 years ago)
  • mythtv/programs/mythbackend/main.cpp

     
    4040QString pidfile;
    4141QString lockfile_location;
    4242HouseKeeper *housekeeping = NULL;
     43char *logfile_c = NULL;
    4344
    4445bool setupTVs(bool ismaster, bool &error)
    4546{
     
    168169        unlink(pidfile.ascii());
    169170
    170171    unlink(lockfile_location.ascii());
     172
     173    signal(SIGHUP, SIG_DFL);
     174
     175    if (logfile_c)
     176        free(logfile_c);
    171177}
    172    
     178
     179int log_rotate(int report_error)
     180{
     181    /* http://www.gossamer-threads.com/lists/mythtv/dev/110113 */
     182
     183    int new_logfd = open(logfile_c, O_WRONLY|O_CREAT|O_APPEND|O_SYNC, 0664);
     184    if (new_logfd < 0)
     185    {
     186        // If we can't open the new logfile, send data to /dev/null
     187        if (report_error)
     188        {
     189            cerr << "cannot open logfile " << logfile_c << endl;
     190            return -1;
     191        }
     192        new_logfd = open("/dev/null", O_WRONLY);
     193        if (new_logfd < 0)
     194        {
     195            // There's not much we can do, so punt.
     196            return -1;
     197        }
     198    }
     199    while (dup2(new_logfd, 1) < 0 && errno == EINTR) ;
     200    while (dup2(new_logfd, 2) < 0 && errno == EINTR) ;
     201    while (close(new_logfd) < 0 && errno == EINTR) ;
     202    return 0;
     203}
     204
     205void log_rotate_handler(int)
     206{
     207    log_rotate(0);
     208}
     209
     210
    173211int main(int argc, char **argv)
    174212{
    175213    for(int i = 3; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
     
    392430        }
    393431    }
    394432
    395     int logfd = -1;
    396 
    397     if (logfile != "")
     433    if (logfile != "" && (logfile_c = strdup(logfile.ascii())) != NULL)
    398434    {
    399         logfd = open(logfile.ascii(), O_WRONLY|O_CREAT|O_APPEND|O_SYNC, 0664);
    400          
    401         if (logfd < 0)
    402         {
    403             perror(logfile.ascii());
    404             cerr << "Error opening logfile\n";
    405             return BACKEND_EXIT_OPENING_LOGFILE_ERROR;
    406         }
     435        if (log_rotate(1) < 0)
     436            cerr << "cannot open logfile; using stdout/stderr" << endl;
     437        else
     438            signal(SIGHUP, &log_rotate_handler);
    407439    }
    408440   
    409441    ofstream pidfs;
     
    435467        pidfs.close();
    436468    }
    437469
    438     if (logfd != -1)
    439     {
    440         // Send stdout and stderr to the logfile
    441         dup2(logfd, 1);
    442         dup2(logfd, 2);
    443 
    444         // Close the unduplicated logfd
    445         if (logfd != 1 && logfd != 2)
    446             close(logfd);
    447     }
    448 
    449470    gContext = NULL;
    450471    gContext = new MythContext(MYTH_BINARY_VERSION);
    451472    if (!gContext->Init(false))