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

mendersoftware / mender / 969241802

16 Aug 2023 08:29AM UTC coverage: 79.019% (+0.2%) from 78.825%
969241802

push

gitlab-ci

oleorhagen
chore(crypto): Make the PrivateKey constructor public

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

5314 of 6725 relevant lines covered (79.02%)

200.29 hits per line

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

70.59
/mender-update/update_module/v3/platform/c++17/update_module_call.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 <mender-update/update_module/v3/update_module.hpp>
16

17
#include <iostream>
18
#include <sstream>
19

20
#include <filesystem>
21

22
#include <common/common.hpp>
23
#include <common/events.hpp>
24
#include <common/log.hpp>
25
#include <common/processes.hpp>
26

27
namespace mender {
28
namespace update {
29
namespace update_module {
30
namespace v3 {
31

32
namespace error = mender::common::error;
33
namespace events = mender::common::events;
34
namespace log = mender::common::log;
35
namespace fs = std::filesystem;
36

37
UpdateModule::StateRunner::StateRunner(
422✔
38
        events::EventLoop &loop,
39
        State state,
40
        const string &module_path,
41
        const string &module_work_path) :
422✔
42
        loop(loop),
43
        module_work_path(module_work_path),
44
        proc({module_path, StateToString(state), module_work_path}) {
1,688✔
45
        proc.SetWorkDir(module_work_path);
422✔
46
}
422✔
47

48
error::Error UpdateModule::StateRunner::AsyncCallState(
422✔
49
        State state, bool procOut, chrono::seconds timeout_seconds, HandlerFunction handler) {
50
        this->handler = handler;
422✔
51

52
        string state_string = StateToString(state);
844✔
53
        if (!fs::is_directory(module_work_path)) {
422✔
54
                if (state == State::Cleanup) {
1✔
55
                        loop.Post([this, state]() { ProcessFinishedHandler(state, error::NoError); });
×
56
                        return error::NoError;
×
57
                } else {
58
                        return error::Error(
59
                                make_error_condition(errc::no_such_file_or_directory),
1✔
60
                                state_string + ": File tree does not exist: " + module_work_path);
3✔
61
                }
62
        }
63

64
        class OutputHandler {
65
        public:
66
                void operator()(const char *data, size_t size) {
9✔
67
                        if (size == 0) {
9✔
68
                                return;
×
69
                        }
70
                        // Get rid of exactly one trailing newline, if there is one. This is because
71
                        // we unconditionally print one at the end of every log line. If the string
72
                        // does not contain a trailing newline, add a "{...}" instead, since we
73
                        // cannot avoid breaking the line apart then.
74
                        string content(data, size);
18✔
75
                        if (content.back() == '\n') {
9✔
76
                                content.pop_back();
9✔
77
                        } else {
78
                                content.append("{...}");
×
79
                        }
80
                        auto lines = mender::common::SplitString(content, "\n");
27✔
81
                        for (auto line : lines) {
18✔
82
                                log::Info(prefix + line);
9✔
83
                        }
84
                }
85
                string prefix;
86
        } stderr_handler {"Update Module output (stderr): "};
842✔
87

88
        error::Error processStart;
842✔
89
        if (procOut) {
421✔
90
                // Provide string to put content in.
91
                output.emplace(string());
124✔
92
                processStart = proc.Start(
124✔
93
                        [this](const char *data, size_t size) {
258✔
94
                                // At the moment, no state that queries output accepts more than one line,
95
                                // so reject multiple lines here. This would have been rejected anyway due
96
                                // to matching, but by doing it here, we also prevent using excessive memory
97
                                // if the process dumps a large log on us.
98
                                if (!first_line_captured) {
86✔
99
                                        auto lines = mender::common::SplitString(string(data, size), "\n");
340✔
100
                                        if (lines.size() >= 1) {
85✔
101
                                                *output = lines[0];
85✔
102
                                                first_line_captured = true;
85✔
103
                                        }
104
                                        if (lines.size() > 2 || (lines.size() == 2 && lines[1] != "")) {
85✔
105
                                                too_many_lines = true;
1✔
106
                                        }
107
                                } else {
108
                                        too_many_lines = true;
1✔
109
                                }
110
                        },
86✔
111
                        stderr_handler);
248✔
112
        } else {
113
                processStart =
114
                        proc.Start(OutputHandler {"Update Module output (stdout): "}, stderr_handler);
297✔
115
        }
116
        if (processStart != error::NoError) {
421✔
117
                return GetProcessError(processStart).WithContext(state_string);
×
118
        }
119

120
        error::Error err;
842✔
121
        err = proc.AsyncWait(
421✔
122
                loop,
123
                [this, state, handler](error::Error process_err) {
1,686✔
124
                        if (process_err.code == make_error_condition(errc::timed_out)) {
421✔
125
                                proc.EnsureTerminated();
2✔
126
                        }
127

128
                        auto err = process_err.WithContext(StateToString(state));
421✔
129
                        ProcessFinishedHandler(state, err);
421✔
130
                },
421✔
131
                timeout_seconds);
842✔
132

133
        return err;
421✔
134
}
135

136
void UpdateModule::StateRunner::ProcessFinishedHandler(State state, error::Error err) {
421✔
137
        if (state == State::Cleanup) {
421✔
138
                std::error_code ec;
66✔
139
                // False is returned if the directory doesn't exist, and `ec` is only set to an
140
                // error if it's not this type of error, which is what we want.
141
                if (!fs::remove_all(module_work_path, ec) && ec) {
66✔
142
                        err = err.FollowedBy(error::Error(
×
143
                                ec.default_error_condition(),
×
144
                                StateToString(state) + ": Error removing directory: " + module_work_path));
×
145
                }
146
        }
147

148
        if (err == error::NoError && too_many_lines) {
421✔
149
                err = error::Error(
2✔
150
                        make_error_condition(errc::protocol_error),
2✔
151
                        "Too many lines when querying " + StateToString(state));
6✔
152
        }
153

154
        if (err != error::NoError) {
421✔
155
                handler(expected::unexpected(err));
46✔
156
        } else {
157
                handler(output);
375✔
158
        }
159
}
421✔
160

161
error::Error UpdateModule::AsyncSystemReboot(
×
162
        events::EventLoop &event_loop, StateFinishedHandler handler) {
163
        system_reboot_.reset(new SystemRebootRunner {vector<string> {"reboot"}, event_loop});
×
164

165
        log::Info("Calling `reboot` command and waiting for system to restart.");
×
166

167
        auto err = system_reboot_->proc.AsyncWait(event_loop, [](error::Error err) {
×
168
                // Even if it returns, give the reboot ten minutes to kill us. `handler` will only
169
                // be called from the timeout handler.
170
                if (err != error::NoError) {
×
171
                        log::Warning("`reboot` command returned error: " + err.String());
×
172
                }
173
        });
×
174
        if (err != error::NoError) {
×
175
                return err.WithContext("Unable to call system reboot command");
×
176
        }
177

178
        system_reboot_->timeout.AsyncWait(chrono::minutes(10), [handler](error::Error err) {
×
179
                if (err != error::NoError) {
×
180
                        handler(err.WithContext("UpdateModule::AsyncSystemReboot"));
×
181
                }
182

183
                handler(error::Error(
×
184
                        make_error_condition(errc::timed_out),
×
185
                        "`reboot` command did not kill us; rebooting failed"));
×
186
        });
×
187

188
        return error::NoError;
×
189
}
190

191
} // namespace v3
192
} // namespace update_module
193
} // namespace update
194
} // 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