Gracefully handling signals
Signed-off-by: Dušan Poizl <nou.spiro@gmail.com>
This commit is contained in:
@@ -11,6 +11,11 @@
|
||||
#include <QDebug>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/btrfs.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
int MainWindow::socketPair[2] = {0, 0};
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
loading(false),
|
||||
@@ -50,6 +55,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
m_database = new Database(this);
|
||||
if(!m_database->init())
|
||||
QMessageBox::critical(this, tr("Can't open DB"), tr("Can't open SQLITE database"));
|
||||
|
||||
setupSigterm();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@@ -87,6 +94,39 @@ void MainWindow::keyReleaseEvent(QKeyEvent *event)
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void MainWindow::setupSigterm()
|
||||
{
|
||||
struct sigaction signal;
|
||||
|
||||
signal.sa_handler = MainWindow::signalHandler;
|
||||
sigemptyset(&signal.sa_mask);
|
||||
signal.sa_flags = 0;
|
||||
signal.sa_flags |= SA_RESTART;
|
||||
|
||||
sigaction(SIGHUP, &signal, 0);
|
||||
sigaction(SIGTERM, &signal, 0);
|
||||
sigaction(SIGINT, &signal, 0);
|
||||
|
||||
::socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair);
|
||||
socketNotifier = new QSocketNotifier(socketPair[1], QSocketNotifier::Read, this);
|
||||
connect(socketNotifier, SIGNAL(activated(int)), this, SLOT(socketNotify()));
|
||||
}
|
||||
|
||||
void MainWindow::signalHandler(int)
|
||||
{
|
||||
char a = 1;
|
||||
::write(socketPair[0], &a, sizeof(a));
|
||||
}
|
||||
|
||||
void MainWindow::socketNotify()
|
||||
{
|
||||
socketNotifier->setEnabled(false);
|
||||
char tmp;
|
||||
read(socketPair[1], &tmp, sizeof(tmp));
|
||||
close();
|
||||
socketNotifier->setEnabled(true);
|
||||
}
|
||||
|
||||
void MainWindow::pixmapLoaded(QPixmap pix)
|
||||
{
|
||||
m_image->setImage(pix);
|
||||
|
||||
Reference in New Issue
Block a user