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

mendersoftware / mender / 1058883419

02 Nov 2023 12:38PM UTC coverage: 80.182% (+0.2%) from 79.944%
1058883419

push

gitlab-ci

kacf
fix: Make sure RebootAction::Automatic is handled in RollbackReboot.

Changelog: None
Ticket: None

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

17 of 17 new or added lines in 1 file covered. (100.0%)

6947 of 8664 relevant lines covered (80.18%)

9289.26 hits per line

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

70.0
/src/common/events.hpp
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
#ifndef MENDER_COMMON_EVENTS_HPP
16
#define MENDER_COMMON_EVENTS_HPP
17

18
#include <config.h>
19

20
#include <functional>
21
#include <system_error>
22
#include <vector>
23

24
#include <common/error.hpp>
25
#include <common/io.hpp>
26

27
#ifdef MENDER_USE_BOOST_ASIO
28
#include <boost/asio.hpp>
29
#endif // MENDER_USE_BOOST_ASIO
30

31
namespace mender {
32
namespace common {
33
namespace events {
34

35
using namespace std;
36

37
using EventHandler = function<void(mender::common::error::Error err)>;
38

39
#ifdef MENDER_USE_BOOST_ASIO
40
namespace asio = boost::asio;
41
#endif // MENDER_USE_BOOST_ASIO
42

43
namespace error = mender::common::error;
44
namespace mio = mender::common::io;
45

46
class EventLoop {
4✔
47
public:
48
        // Can be used recursively. Each invocation of `Run()` needs to be matched by an invocation
49
        // of `Stop()`.
50
        void Run();
51
        void Stop();
52

53
        // Runs the function on the event loop. Note that there is no way to cancel a registered
54
        // function before running it. If you need cancellation, make sure the function tests for a
55
        // cancellation condition before doing its work.
56
        //
57
        // Thread-safe.
58
        void Post(function<void()> func);
59

60
private:
61
#ifdef MENDER_USE_BOOST_ASIO
62
        asio::io_context ctx_;
63
#endif // MENDER_USE_BOOST_ASIO
64

65
        friend class EventLoopObject;
66
};
67

68
class EventLoopObject {
69
#ifdef MENDER_USE_BOOST_ASIO
70
protected:
71
        static asio::io_context &GetAsioIoContext(EventLoop &loop) {
72
                return loop.ctx_;
73
        }
74
#endif // MENDER_USE_BOOST_ASIO
75
};
76

77
class Timer : public EventLoopObject {
78
public:
79
        Timer(EventLoop &loop);
80
        ~Timer() {
96✔
81
                if (destroying_ == nullptr) {
48✔
82
                        // Can happen as a consequence of destruction after move.
83
                        return;
×
84
                }
85

86
                *destroying_ = true;
48✔
87
                Cancel();
48✔
88
        }
48✔
89

90
        Timer(Timer &&other) = default;
91

92
#ifdef MENDER_USE_BOOST_ASIO
93
        template <typename Duration>
94
        void Wait(Duration duration) {
95
                *active_ = true;
96
                timer_.expires_after(duration);
97
                timer_.wait();
98
                *active_ = false;
99
        }
100

101
        template <typename Duration>
102
        void AsyncWait(Duration duration, EventHandler handler) {
4✔
103
                *active_ = true;
4✔
104
                timer_.expires_after(duration);
4✔
105
                auto &destroying = destroying_;
106
                auto &active = active_;
107
                timer_.async_wait([destroying, handler, active](error_code ec) {
×
108
                        *active = false;
2✔
109
                        if (*destroying) {
2✔
110
                                return;
111
                        }
112

113
                        if (ec) {
2✔
114
                                auto err = ec.default_error_condition();
×
115
                                if (err == make_error_condition(boost::system::errc::operation_canceled)) {
×
116
                                        handler(error::Error(make_error_condition(errc::operation_canceled), ""));
×
117
                                } else {
118
                                        handler(error::Error(err, "Timer error"));
×
119
                                }
120
                        } else {
121
                                handler(error::NoError);
4✔
122
                        }
123
                });
124
        }
4✔
125
#endif // MENDER_USE_BOOST_ASIO
126

127
        void Cancel();
128

129
        bool GetActive() {
130
                return *active_;
131
        };
132

133
private:
134
#ifdef MENDER_USE_BOOST_ASIO
135
        asio::steady_timer timer_;
136
        shared_ptr<bool> destroying_;
137
        shared_ptr<bool> active_;
138
#endif // MENDER_USE_BOOST_ASIO
139
};
140

141
using SignalNumber = int;
142
using SignalSet = vector<SignalNumber>;
143
using SignalHandlerFn = function<void(SignalNumber)>;
144

145
class SignalHandler : public EventLoopObject, virtual public mio::Canceller {
146
public:
147
        SignalHandler(EventLoop &loop);
148
        ~SignalHandler() {
149
                Cancel();
150
        };
151
        error::Error RegisterHandler(const SignalSet &set, SignalHandlerFn handler_fn);
152
        void Cancel() override;
153

154
#ifdef MENDER_USE_BOOST_ASIO
155
private:
156
        asio::signal_set signal_set_;
157
#endif // MENDER_USE_BOOST_ASIO
158
};
159

160
} // namespace events
161
} // namespace common
162
} // namespace mender
163

164
#endif // MENDER_COMMON_EVENTS_HPP
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