Skip to content

Commit

Permalink
Rework SignalHandler for more compact code
Browse files Browse the repository at this point in the history
Rather than have special cases all over, I have abstracted the code so it's
easier to deal with.

The pre-generated strings for the "last gasp" output for SEGV, etc has been
changed to include the entire string so it only uses one write() call.

Moved SIGHUP handling in the logserver to use the common SignalHandler class
code.

Added SIGBUS, SIGFPE, SIGILL to our standard supported signals, treating them
the same as SIGSEGV.
  • Loading branch information
Beirdo committed Jun 20, 2012
1 parent 7643393 commit 4afc54a
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 182 deletions.
67 changes: 13 additions & 54 deletions mythtv/libs/libmythbase/loggingserver.cpp
Expand Up @@ -95,12 +95,6 @@ static QAtomicInt msgsSinceHeartbeat;
#define TIMESTAMP_MAX 30
#define MAX_STRING_LENGTH (LOGLINE_MAX+120)

#ifndef _WIN32
static int sighupFd[2];
void logSighup(int signum);
#endif


/// \brief LoggerBase class constructor. Adds the new logger instance to the
/// loggerMap.
/// \param string a C-string of the handle for this instance (NULL if unused)
Expand Down Expand Up @@ -758,15 +752,18 @@ void DBLoggerThread::stop(void)


#ifndef _WIN32
/// \brief UNIX-side SIGHUP signal handler. Hand it off to the Qt-size via
/// a socketpair so Qt code can safely be used.
/// \brief Signal handler for SIGHUP. This passes it to the LogForwardThread
/// for processing.

void logSighup(int signum)
void logSigHup(void)
{
(void)signum;
char a = 1;
int ret = ::write(sighupFd[0], &a, sizeof(a));
(void)ret;
if (!logForwardThread)
return;

// This will be running in the thread that's used by SignalHandler
// Emit the signal which is connected to a slot that runs in the actual
// handling thread.
emit logForwardThread->incomingSigHup();
}
#endif

Expand Down Expand Up @@ -1064,7 +1061,7 @@ void DatabaseLogger::receivedMessage(const QList<QByteArray> &msg)
/// \brief LogForwardThread constructor.
LogForwardThread::LogForwardThread() :
MThread("LogForward"), m_aborted(false), m_zmqContext(NULL),
m_zmqPubSock(NULL), m_sighupNotifier(NULL), m_shutdownTimer(NULL)
m_zmqPubSock(NULL), m_shutdownTimer(NULL)
{
moveToThread(qthread());
}
Expand All @@ -1083,25 +1080,8 @@ void LogForwardThread::run(void)
{
RunProlog();

#ifndef _WIN32
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
{
LOG(VB_GENERAL, LOG_ERR, "Couldn't create HUP socketpair");
qApp->quit();
}
m_sighupNotifier = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read,
this);
connect(m_sighupNotifier, SIGNAL(activated(int)),
this, SLOT(handleSigHup()));

/* Setup SIGHUP */
LOG(VB_GENERAL, LOG_INFO, "Setup SIGHUP handler");
struct sigaction sa;
sa.sa_handler = logSighup;
sigemptyset( &sa.sa_mask );
sa.sa_flags = SA_RESTART;
sigaction( SIGHUP, &sa, NULL );
#endif
connect(this, SIGNAL(incomingSigHup(void)), this, SLOT(handleSigHup(void)),
Qt::QueuedConnection);

qRegisterMetaType<QList<QByteArray> >("QList<QByteArray>");

Expand Down Expand Up @@ -1151,19 +1131,6 @@ void LogForwardThread::run(void)
expireClients();
}

delete m_sighupNotifier;

#ifndef _WIN32
::close(sighupFd[0]);
::close(sighupFd[1]);

/* Tear down SIGHUP */
sa.sa_handler = SIG_DFL;
sigemptyset( &sa.sa_mask );
sa.sa_flags = SA_RESTART;
sigaction( SIGHUP, &sa, NULL );
#endif

m_zmqPubSock->setLinger(0);
m_zmqPubSock->close();

Expand Down Expand Up @@ -1258,12 +1225,6 @@ void LogForwardThread::expireClients(void)
void LogForwardThread::handleSigHup(void)
{
#ifndef _WIN32
m_sighupNotifier->setEnabled(false);

char tmp;
int ret = ::read(sighupFd[1], &tmp, sizeof(tmp));
(void)ret;

LOG(VB_GENERAL, LOG_INFO, "SIGHUP received, rolling log files.");

/* SIGHUP was sent. Close and reopen debug logfiles */
Expand All @@ -1273,8 +1234,6 @@ void LogForwardThread::handleSigHup(void)
{
it.value()->reopen();
}

m_sighupNotifier->setEnabled(true);
#endif
}

Expand Down
11 changes: 7 additions & 4 deletions mythtv/libs/libmythbase/loggingserver.h
Expand Up @@ -168,7 +168,7 @@ class LogForwardThread : public QObject, public MThread
{
Q_OBJECT

friend void logSighup(int signum);
friend void logSigHup(void);
public:
LogForwardThread();
~LogForwardThread();
Expand All @@ -180,14 +180,13 @@ class LogForwardThread : public QObject, public MThread
nzmqt::ZMQContext *m_zmqContext; ///< ZeroMQ context
nzmqt::ZMQSocket *m_zmqPubSock; ///< ZeroMQ publishing socket

QSocketNotifier *m_sighupNotifier; ///< Notifier to synchronize to UNIX
/// signal SIGHUP safely

MythSignalingTimer *m_shutdownTimer; ///< 5 min timer to shut down if no
/// clients

void forwardMessage(LogMessage *msg);
void expireClients(void);
signals:
void incomingSigHup(void);
protected slots:
void handleSigHup(void);
void shutdownTimerExpired(void);
Expand Down Expand Up @@ -243,6 +242,10 @@ class DBLoggerThread : public MThread

extern LogServerThread *logServerThread;

#ifndef _WIN32
MBASE_PUBLIC void logSigHup(void);
#endif

#endif

/*
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythversion.h
Expand Up @@ -12,7 +12,7 @@
/// Update this whenever the plug-in API changes.
/// Including changes in the libmythbase, libmyth, libmythtv, libmythav* and
/// libmythui class methods used by plug-ins.
#define MYTH_BINARY_VERSION "0.26.20120617-1"
#define MYTH_BINARY_VERSION "0.26.20120620-1"

/** \brief Increment this whenever the MythTV network protocol changes.
*
Expand Down

0 comments on commit 4afc54a

Please sign in to comment.