• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hluk / CopyQ / #4015

17 Oct 2023 06:08AM CUT coverage: 74.296% (-0.1%) from 74.4%
#4015

push

travis-ci

web-flow
itemfakevim: fix build with qt 6.6.0 (#2508)

Reference: https://github.com/qt-creator/qt-creator/commit/e56e3b6f3

18429 of 24805 relevant lines covered (74.3%)

24531.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.88
/src/platform/unix/unixsignalhandler.cpp
1
// SPDX-License-Identifier: GPL-3.0-or-later
2

3
#include "unixsignalhandler.h"
4

5
#include "common/log.h"
6

7
#include <QCoreApplication>
8
#include <QSocketNotifier>
9

10
#include <csignal>
11
#include <sys/socket.h>
12
#include <unistd.h>
13

14
namespace {
15

16
namespace SignalAction {
17
enum SignalAction { Write, Read, Count };
18
}
19

20
int signalFd[SignalAction::Count];
21
QSocketNotifier *signalFdNotifier = nullptr;
22

23
/**
24
 * Catch Unix signal.
25
 *
26
 * Since this can be called at any time, Qt code cannot be handled here. For example,
27
 * this can be called from QString constructor which can easily deadlock the application on
28
 * a mutex when trying to create new QString from this handler. Also note that creating
29
 * QSettings recursively can result in resetting application settings.
30
 */
31
void exitSignalHandler(int)
105✔
32
{
33
    const qint64 pid = QCoreApplication::applicationPid();
105✔
34
    const auto written = ::write(signalFd[SignalAction::Write], &pid, sizeof(pid));
105✔
35
    if (written == -1)
105✔
36
        log("Failed to handle signal!", LogError);
×
37
}
105✔
38

39
void handleSignal()
×
40
{
41
    signalFdNotifier->setEnabled(false);
×
42

43
    qint64 pid;
44
    if ( ::read(signalFd[SignalAction::Read], &pid, sizeof(pid)) != sizeof(pid) ) {
×
45
        COPYQ_LOG("Incorrect number of bytes read from Unix signal socket!");
×
46
        signalFdNotifier->setEnabled(true);
×
47
    } else if (pid != QCoreApplication::applicationPid()) {
×
48
        COPYQ_LOG("Wrong PID written to Unix signal socket!");
×
49
        signalFdNotifier->setEnabled(true);
×
50
    } else {
51
        COPYQ_LOG("Terminating application on signal.");
×
52
        QCoreApplication::exit();
×
53
    }
54
}
55

56
} // namespace
57

58
bool initUnixSignalHandler()
3,655✔
59
{
60
    // Safely quit application on TERM and HUP signals.
61
    struct sigaction sigact{};
3,655✔
62

63
    sigact.sa_handler = exitSignalHandler;
3,655✔
64
    sigemptyset(&sigact.sa_mask);
3,655✔
65
    sigact.sa_flags = 0;
3,655✔
66
    sigact.sa_flags |= SA_RESTART;
3,655✔
67

68
    if ( sigaction(SIGINT, &sigact, nullptr) > 0
3,655✔
69
         || sigaction(SIGTERM, &sigact, nullptr) > 0 )
3,655✔
70
    {
71
        log("sigaction() failed!", LogError);
×
72
        return false;
×
73
    }
74

75
    if (::socketpair(AF_UNIX, SOCK_STREAM, 0, signalFd)) {
3,655✔
76
        log("socketpair() failed!", LogError);
×
77
        return false;
×
78
    }
79

80
    return true;
3,655✔
81
}
82

83
void startUnixSignalHandler()
3,652✔
84
{
85
    signalFdNotifier = new QSocketNotifier(signalFd[SignalAction::Read], QSocketNotifier::Read);
3,652✔
86
    QObject::connect(signalFdNotifier, &QSocketNotifier::activated, handleSignal);
3,652✔
87
}
3,652✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc