File: SyncSigHandler.cpp

package info (click to toggle)
buteo-syncfw 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,704 kB
  • sloc: cpp: 18,406; xml: 396; sh: 34; makefile: 20
file content (101 lines) | stat: -rw-r--r-- 2,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * This file is part of buteo-syncfw package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <QCoreApplication>


#include "SyncSigHandler.h"
#include "LogMacros.h"

int SyncSigHandler::iSigHupFd[2];
int SyncSigHandler::iSigTermFd[2];

SyncSigHandler::SyncSigHandler(QObject *aParent, const char */*aName*/)
    : QObject(aParent)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    //Adding signal hanlder for unix Signals
    signal(SIGTERM, termSignalHandler);
    signal(SIGINT, termSignalHandler);
    signal(SIGHUP, hupSignalHandler);

    //Adding socketpair to monitor those fd's.
    if (::socketpair(AF_UNIX, SOCK_STREAM, 0, iSigHupFd)) {
        qCCritical(lcButeoMsyncd) << "Couldn't create HUP socketpair";
    }

    if (::socketpair(AF_UNIX, SOCK_STREAM, 0, iSigTermFd)) {
        qCCritical(lcButeoMsyncd) << "Couldn't create TERM socketpair";
    }

    //SocketNotifier for read those fd's.
    iSigHup = new QSocketNotifier(iSigHupFd[1], QSocketNotifier::Read, this);
    connect(iSigHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));

    iSigTerm = new QSocketNotifier(iSigTermFd[1], QSocketNotifier::Read, this);
    connect(iSigTerm, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));
}

SyncSigHandler::~SyncSigHandler()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    delete iSigHup;
    iSigHup = 0;
    delete iSigTerm;
    iSigTerm = 0;
}

// Linux signal handler.
//This application is shutdown by sending a SIGTERM to it.
void SyncSigHandler::termSignalHandler(int /*signal*/)
{
    char a = 1;
    ::write(iSigTermFd[0], &a, sizeof(a));
}

void SyncSigHandler::hupSignalHandler(int /*signal*/)
{
    // Do nothing
}

//Qt Slot will eventually get called corresponding to Unix signal.
void SyncSigHandler::handleSigTerm()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    iSigTerm->setEnabled(false);
    char tmp;
    ::read(iSigTermFd[1], &tmp, sizeof(tmp));

    // Doing Qt stuff.Exiting application
    QCoreApplication::exit(0);
}

void SyncSigHandler::handleSigHup()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}