Ticket #3622: mythfrontend-logrotate.patch

File mythfrontend-logrotate.patch, 2.4 KB (added by drhuettl@…, 13 years ago)
  • programs/mythfrontend/main.cpp

    diff -urN mythtv/programs/mythfrontend/main.cpp mythtv.new/programs/mythfrontend/main.cpp
    old new  
    99#include <qwidget.h>
    1010#include <fcntl.h>
    1111#include <signal.h>
     12#include <cerrno>
    1213#include <pthread.h>
    1314
    1415#include <iostream>
     
    5354
    5455ThemedMenu *menu;
    5556XBox *xbox = NULL;
     57QString logfile = "";
    5658
    5759void startGuide(void)
    5860{
     
    735737    query.exec();
    736738}
    737739
     740int log_rotate(int report_error)
     741{
     742    int new_logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0664);
     743
     744    if (new_logfd < 0) {
     745        /* If we can't open the new logfile, send data to /dev/null */
     746        if (report_error) {
     747            cerr << "cannot open logfile " << logfile << endl;
     748            return -1;
     749        }
     750
     751        new_logfd = open("/dev/null", O_WRONLY);
     752
     753        if (new_logfd < 0) {
     754            /* There's not much we can do, so punt. */
     755            return -1;
     756        }
     757    }
     758
     759    while (dup2(new_logfd, 1) < 0 && errno == EINTR);
     760    while (dup2(new_logfd, 2) < 0 && errno == EINTR);
     761    while (close(new_logfd) < 0   && errno == EINTR);
     762
     763    return 0;
     764}
     765
     766void log_rotate_handler(int)
     767{
     768    log_rotate(0);
     769}
     770
    738771int main(int argc, char **argv)
    739772{
    740773    QString geometry = "";
     
    752787#endif
    753788    QApplication a(argc, argv);
    754789
    755     QString logfile = "";
    756 
    757790    QString pluginname = "";
    758791
    759792    QFileInfo finfo(a.argv()[0]);
     
    871904        }
    872905    }
    873906
    874     int logfd = -1;
    875 
    876907    if (logfile != "")
    877908    {
    878         logfd = open(logfile.ascii(), O_WRONLY|O_CREAT|O_APPEND, 0664);
    879  
    880         if (logfd < 0)
    881         {
    882             perror("open(logfile)");
    883             return FRONTEND_EXIT_OPENING_LOGFILE_ERROR;
    884         }
    885     }
    886 
    887     if (logfd != -1)
    888     {
    889         // Send stdout and stderr to the logfile
    890         dup2(logfd, 1);
    891         dup2(logfd, 2);
    892 
    893         // Close the unduplicated logfd
    894         if (logfd != 1 && logfd != 2)
    895             close(logfd);
     909        if (log_rotate(1) < 0)
     910            cerr << "cannot open logfile; using stdout/stderr" << endl;
     911        else
     912            signal(SIGHUP, &log_rotate_handler);
    896913    }
    897914
    898915    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
     
    10811098        pthread_join(priv_thread, &value);
    10821099    }
    10831100
     1101    signal(SIGHUP, SIG_DFL);
     1102
    10841103    if (networkControl)
    10851104        delete networkControl;