Merge lp:~unity-api-team/hud/unix-signal-handler into lp:hud/14.04

Proposed by Pete Woods
Status: Merged
Approved by: Pete Woods
Approved revision: 392
Merged at revision: 385
Proposed branch: lp:~unity-api-team/hud/unix-signal-handler
Merge into: lp:hud/14.04
Prerequisite: lp:~unity-api-team/hud/test-failures
Diff against target: 233 lines (+164/-10)
5 files modified
service/CMakeLists.txt (+1/-0)
service/ItemStore.cpp (+1/-1)
service/SignalHandler.cpp (+99/-0)
service/SignalHandler.h (+60/-0)
service/main.cpp (+3/-9)
To merge this branch: bzr merge lp:~unity-api-team/hud/unix-signal-handler
Reviewer Review Type Date Requested Status
Marcus Tomlinson (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+212393@code.launchpad.net

Commit message

Call only "safe" write method from UNIX signal handler

See http://pubs.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01

Description of the change

* Is your branch in sync with latest trunk (e.g. bzr pull lp:trunk -> no changes)
  * Yes
 * Did you build your software in a clean sbuild/pbuilder chroot or ppa?
  * Yes
 * Did you build your software in a clean sbuild/pbuilder armhf chroot or ppa?
  * Yes
 * Has your component "TestPlan” been executed successfully on emulator, N4?
  * Yes
 * Has a 5 minute exploratory testing run been executed on N4?
  * Yes
 * If you changed the packaging (debian), did you subscribe a core-dev to this MP?
  * N/A
 * If you changed the UI, did you subscribe the design-reviewers to this MP?
  * No change
 * What components might get impacted by your changes?
  * Unity7
  * Unity8
 * Have you requested review by the teams of these owning components?
  * Yes

Check List:
https://wiki.ubuntu.com/Process/Merges/Checklists/hud

Test Plan:
https://wiki.ubuntu.com/Process/Merges/TestPlan/hud

Silo:
<waiting for silo>

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
390. By Pete Woods

Split whitespace / semicolon regular expression

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
391. By Pete Woods

Responsd to review comments

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

One more pedantic style fix ;)

90 + struct sigaction sigint, term;

should be:

90 + struct sigaction sigint, sigterm;

review: Needs Fixing
392. By Pete Woods

Rename term -> sigterm

Revision history for this message
Marcus Tomlinson (marcustomlinson) wrote :

Cools +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'service/CMakeLists.txt'
--- service/CMakeLists.txt 2014-02-15 10:23:45 +0000
+++ service/CMakeLists.txt 2014-03-25 09:43:25 +0000
@@ -29,6 +29,7 @@
29 QueryImpl.cpp29 QueryImpl.cpp
30 Result.cpp30 Result.cpp
31 SearchSettings.cpp31 SearchSettings.cpp
32 SignalHandler.cpp
32 SqliteUsageTracker.cpp33 SqliteUsageTracker.cpp
33 UsageTracker.cpp34 UsageTracker.cpp
34 Voice.cpp35 Voice.cpp
3536
=== modified file 'service/ItemStore.cpp'
--- service/ItemStore.cpp 2014-03-25 09:43:25 +0000
+++ service/ItemStore.cpp 2014-03-25 09:43:25 +0000
@@ -31,7 +31,7 @@
31static const QRegularExpression SINGLE_AMPERSAND("(?<![&])[&](?![&])");31static const QRegularExpression SINGLE_AMPERSAND("(?<![&])[&](?![&])");
32static const QRegularExpression BAD_CHARACTERS("\\.\\.\\.|…");32static const QRegularExpression BAD_CHARACTERS("\\.\\.\\.|…");
33static const QRegularExpression WHITESPACE("\\s+");33static const QRegularExpression WHITESPACE("\\s+");
34static const QRegularExpression WHITESPACE_OR_SEMICOLON("[;\\s+]");34static const QRegularExpression WHITESPACE_OR_SEMICOLON("[;\\s]+");
3535
36ItemStore::ItemStore(const QString &applicationId,36ItemStore::ItemStore(const QString &applicationId,
37 UsageTracker::Ptr usageTracker, SearchSettings::Ptr settings) :37 UsageTracker::Ptr usageTracker, SearchSettings::Ptr settings) :
3838
=== added file 'service/SignalHandler.cpp'
--- service/SignalHandler.cpp 1970-01-01 00:00:00 +0000
+++ service/SignalHandler.cpp 2014-03-25 09:43:25 +0000
@@ -0,0 +1,99 @@
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Pete Woods <pete.woods@canonical.com>
17 */
18
19#include <service/SignalHandler.h>
20
21#include <QCoreApplication>
22#include <QDebug>
23
24#include <csignal>
25#include <sys/socket.h>
26#include <unistd.h>
27
28using namespace hud::service;
29
30int SignalHandler::sigintFd[2];
31
32int SignalHandler::sigtermFd[2];
33
34SignalHandler::SignalHandler(QObject *parent) :
35 QObject(parent) {
36
37 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd)) {
38 qFatal("Couldn't create INT socketpair");
39 }
40 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd)) {
41 qFatal("Couldn't create TERM socketpair");
42 }
43
44 m_socketNotifierInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
45 connect(m_socketNotifierInt, &QSocketNotifier::activated, this, &SignalHandler::handleSigInt);
46 m_socketNotifierTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
47 connect(m_socketNotifierTerm, &QSocketNotifier::activated, this, &SignalHandler::handleSigTerm);
48}
49
50void SignalHandler::intSignalHandler(int) {
51 char a = 1;
52 ::write(sigintFd[0], &a, sizeof(a));
53}
54
55void SignalHandler::termSignalHandler(int) {
56 char a = 1;
57 ::write(sigtermFd[0], &a, sizeof(a));
58}
59
60int SignalHandler::setupUnixSignalHandlers() {
61 struct sigaction sigint, sigterm;
62
63 sigint.sa_handler = SignalHandler::intSignalHandler;
64 sigemptyset(&sigint.sa_mask);
65 sigint.sa_flags = 0;
66 sigint.sa_flags |= SA_RESTART;
67
68 if (sigaction(SIGINT, &sigint, 0) > 0)
69 return 1;
70
71 sigterm.sa_handler = SignalHandler::termSignalHandler;
72 sigemptyset(&sigterm.sa_mask);
73 sigterm.sa_flags |= SA_RESTART;
74
75 if (sigaction(SIGTERM, &sigterm, 0) > 0)
76 return 2;
77
78 return 0;
79}
80
81void SignalHandler::handleSigTerm() {
82 m_socketNotifierTerm->setEnabled(false);
83 char tmp;
84 ::read(sigtermFd[1], &tmp, sizeof(tmp));
85
86 QCoreApplication::exit(0);
87
88 m_socketNotifierTerm->setEnabled(true);
89}
90
91void SignalHandler::handleSigInt() {
92 m_socketNotifierInt->setEnabled(false);
93 char tmp;
94 ::read(sigintFd[1], &tmp, sizeof(tmp));
95
96 QCoreApplication::exit(0);
97
98 m_socketNotifierInt->setEnabled(true);
99}
0100
=== added file 'service/SignalHandler.h'
--- service/SignalHandler.h 1970-01-01 00:00:00 +0000
+++ service/SignalHandler.h 2014-03-25 09:43:25 +0000
@@ -0,0 +1,60 @@
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Pete Woods <pete.woods@canonical.com>
17 */
18
19#ifndef HUD_SERVICE_SIGNALHANDLER_H_
20#define HUD_SERVICE_SIGNALHANDLER_H_
21
22#include <QObject>
23#include <QSocketNotifier>
24
25namespace hud {
26namespace service {
27
28class SignalHandler: public QObject {
29Q_OBJECT
30
31public:
32 SignalHandler(QObject *parent = 0);
33
34 ~SignalHandler() = default;
35
36 static int setupUnixSignalHandlers();
37
38protected Q_SLOTS:
39 void handleSigInt();
40
41 void handleSigTerm();
42
43protected:
44 static void intSignalHandler(int unused);
45
46 static void termSignalHandler(int unused);
47
48 static int sigintFd[2];
49
50 static int sigtermFd[2];
51
52 QSocketNotifier *m_socketNotifierInt;
53
54 QSocketNotifier *m_socketNotifierTerm;
55};
56
57}
58}
59
60#endif /* HUD_SERVICE_SIGNALHANDLER_H_ */
061
=== modified file 'service/main.cpp'
--- service/main.cpp 2013-11-27 14:28:43 +0000
+++ service/main.cpp 2014-03-25 09:43:25 +0000
@@ -18,19 +18,14 @@
1818
19#include <common/Localisation.h>19#include <common/Localisation.h>
20#include <service/Factory.h>20#include <service/Factory.h>
21#include <service/SignalHandler.h>
2122
22#include <QDebug>23#include <QDebug>
23#include <QApplication>24#include <QApplication>
24#include <csignal>
2525
26using namespace std;26using namespace std;
27using namespace hud::service;27using namespace hud::service;
2828
29static void exitQt(int sig) {
30 Q_UNUSED(sig);
31 QApplication::exit(0);
32}
33
34int main(int argc, char *argv[]) {29int main(int argc, char *argv[]) {
35 qputenv("QT_QPA_PLATFORM", "minimal");30 qputenv("QT_QPA_PLATFORM", "minimal");
36 QApplication application(argc, argv);31 QApplication application(argc, argv);
@@ -39,12 +34,11 @@
39 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);34 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
40 textdomain(GETTEXT_PACKAGE);35 textdomain(GETTEXT_PACKAGE);
4136
42 signal(SIGINT, &exitQt);
43 signal(SIGTERM, &exitQt);
44
45 try {37 try {
46 Factory factory;38 Factory factory;
47 factory.singletonHudService();39 factory.singletonHudService();
40 SignalHandler handler;
41 handler.setupUnixSignalHandlers();
48 return application.exec();42 return application.exec();
49 } catch (std::exception &e) {43 } catch (std::exception &e) {
50 qWarning() << _("Hud Service:") << e.what();44 qWarning() << _("Hud Service:") << e.what();

Subscribers

People subscribed via source and target branches