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

mendersoftware / mender / 1001210836

12 Sep 2023 01:20PM UTC coverage: 79.201% (+0.09%) from 79.112%
1001210836

push

gitlab-ci

vpodzime
feat: Add support for signals delivering two strings

Ticket: MEN-6651
Changelog: none
Signed-off-by: Vratislav Podzimek <v.podzimek@mykolab.com>

5731 of 7236 relevant lines covered (79.2%)

256.26 hits per line

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

88.37
/common/events/platform/boost/events.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <common/events.hpp>
16

17
#include <boost/asio.hpp>
18

19
#include <common/error.hpp>
20
#include <common/log.hpp>
21

22
namespace mender {
23
namespace common {
24
namespace events {
25

26
namespace asio = boost::asio;
27
namespace error = mender::common::error;
28
namespace log = mender::common::log;
29

30
void EventLoop::Run() {
692✔
31
        bool stopped = ctx_.stopped();
692✔
32
        if (stopped) {
692✔
33
                ctx_.restart();
168✔
34
        }
35
        ctx_.run();
692✔
36
        if (!stopped) {
692✔
37
                // For recursive invocations. If we were originally running, but we stopped and
38
                // exited this level, then keep the running state of the previous recursive level.
39
                ctx_.restart();
524✔
40
        }
41
}
692✔
42

43
void EventLoop::Stop() {
689✔
44
        ctx_.stop();
689✔
45
}
689✔
46

47
void EventLoop::Post(std::function<void()> func) {
3,443✔
48
        ctx_.post(func);
3,443✔
49
}
3,443✔
50

51
Timer::Timer(EventLoop &loop) :
979✔
52
        timer_(GetAsioIoContext(loop)) {
979✔
53
}
979✔
54

55
void Timer::Cancel() {
1,077✔
56
        timer_.cancel();
1,077✔
57
}
1,077✔
58

59
SignalHandler::SignalHandler(EventLoop &loop) :
×
60
        signal_set_ {GetAsioIoContext(loop)} {};
×
61

62
void SignalHandler::Cancel() {
146✔
63
        signal_set_.cancel();
146✔
64
}
146✔
65

66
error::Error SignalHandler::RegisterHandler(const SignalSet &set, SignalHandlerFn handler_fn) {
146✔
67
        signal_set_.clear();
146✔
68
        for (auto sig_num : set) {
390✔
69
                boost::system::error_code ec;
244✔
70
                signal_set_.add(sig_num, ec);
244✔
71
                if (ec) {
244✔
72
                        return error::Error(
73
                                ec.default_error_condition(),
×
74
                                "Could not add signal " + std::to_string(sig_num) + " to signal set");
×
75
                }
76
        }
77

78
        class SignalHandlerFunctor {
79
        public:
80
                asio::signal_set &sig_set;
81
                SignalHandlerFn handler_fn;
82

83
                void operator()(const boost::system::error_code &ec, int signal_number) {
8✔
84
                        if (ec) {
8✔
85
                                if (ec == boost::asio::error::operation_aborted) {
4✔
86
                                        // All handlers are called with this error when the signal set
87
                                        // is cancelled.
88
                                        return;
4✔
89
                                }
90
                                log::Error("Failure in signal handler: " + ec.message());
×
91
                        } else {
92
                                handler_fn(signal_number);
4✔
93
                        }
94

95
                        // in either case, register ourselves again because
96
                        // asio::signal_set::async_wait() is a one-off thing
97
                        sig_set.async_wait(*this);
4✔
98
                }
99
        };
100

101
        SignalHandlerFunctor fctor {signal_set_, handler_fn};
292✔
102
        signal_set_.async_wait(fctor);
146✔
103

104
        // Unfortunately, asio::signal_set::async_wait() doesn't return anything so
105
        // we have no chance to propagate any error here (wonder what they do if
106
        // they get an error from sigaction() or whatever they use
107
        // internally). Other implementations may have better options, though.
108
        return error::NoError;
146✔
109
}
110

111
} // namespace events
112
} // namespace common
113
} // namespace mender
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