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

mendersoftware / mender / 974575668

21 Aug 2023 12:04PM UTC coverage: 78.829% (-0.05%) from 78.877%
974575668

push

gitlab-ci

kacf
chore: Implement pushing of logs to the server.

Ticket: MEN-6581

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

18 of 18 new or added lines in 2 files covered. (100.0%)

5492 of 6967 relevant lines covered (78.83%)

238.75 hits per line

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

69.74
/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
namespace processes = mender::common::processes;
37

38

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

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

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

66
        processes::OutputHandler stderr_handler {"Update Module output (stderr): "};
842✔
67

68
        error::Error processStart;
842✔
69
        if (procOut) {
421✔
70
                // Provide string to put content in.
71
                output.emplace(string());
124✔
72
                processStart = proc.Start(
124✔
73
                        [this](const char *data, size_t size) {
258✔
74
                                // At the moment, no state that queries output accepts more than one line,
75
                                // so reject multiple lines here. This would have been rejected anyway due
76
                                // to matching, but by doing it here, we also prevent using excessive memory
77
                                // if the process dumps a large log on us.
78
                                if (!first_line_captured) {
86✔
79
                                        auto lines = mender::common::SplitString(string(data, size), "\n");
340✔
80
                                        if (lines.size() >= 1) {
85✔
81
                                                *output = lines[0];
85✔
82
                                                first_line_captured = true;
85✔
83
                                        }
84
                                        if (lines.size() > 2 || (lines.size() == 2 && lines[1] != "")) {
85✔
85
                                                too_many_lines = true;
1✔
86
                                        }
87
                                } else {
88
                                        too_many_lines = true;
1✔
89
                                }
90
                        },
86✔
91
                        stderr_handler);
248✔
92
        } else {
93
                processStart = proc.Start(
297✔
94
                        processes::OutputHandler {"Update Module output (stdout): "}, stderr_handler);
594✔
95
        }
96
        if (processStart != error::NoError) {
421✔
97
                return GetProcessError(processStart).WithContext(state_string);
×
98
        }
99

100
        error::Error err;
842✔
101
        err = proc.AsyncWait(
421✔
102
                loop,
103
                [this, state, handler](error::Error process_err) {
1,686✔
104
                        if (process_err.code == make_error_condition(errc::timed_out)) {
421✔
105
                                proc.EnsureTerminated();
2✔
106
                        }
107

108
                        auto err = process_err.WithContext(StateToString(state));
421✔
109
                        ProcessFinishedHandler(state, err);
421✔
110
                },
421✔
111
                timeout_seconds);
842✔
112

113
        return err;
421✔
114
}
115

116
void UpdateModule::StateRunner::ProcessFinishedHandler(State state, error::Error err) {
421✔
117
        if (state == State::Cleanup) {
421✔
118
                std::error_code ec;
66✔
119
                // False is returned if the directory doesn't exist, and `ec` is only set to an
120
                // error if it's not this type of error, which is what we want.
121
                if (!fs::remove_all(module_work_path, ec) && ec) {
66✔
122
                        err = err.FollowedBy(error::Error(
×
123
                                ec.default_error_condition(),
×
124
                                StateToString(state) + ": Error removing directory: " + module_work_path));
×
125
                }
126
        }
127

128
        if (err == error::NoError && too_many_lines) {
421✔
129
                err = error::Error(
2✔
130
                        make_error_condition(errc::protocol_error),
2✔
131
                        "Too many lines when querying " + StateToString(state));
6✔
132
        }
133

134
        if (err != error::NoError) {
421✔
135
                handler(expected::unexpected(err));
46✔
136
        } else {
137
                handler(output);
375✔
138
        }
139
}
421✔
140

141
error::Error UpdateModule::AsyncSystemReboot(
×
142
        events::EventLoop &event_loop, StateFinishedHandler handler) {
143
        system_reboot_.reset(new SystemRebootRunner {vector<string> {"reboot"}, event_loop});
×
144

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

147
        auto err = system_reboot_->proc.AsyncWait(event_loop, [](error::Error err) {
×
148
                // Even if it returns, give the reboot ten minutes to kill us. `handler` will only
149
                // be called from the timeout handler.
150
                if (err != error::NoError) {
×
151
                        log::Warning("`reboot` command returned error: " + err.String());
×
152
                }
153
        });
×
154
        if (err != error::NoError) {
×
155
                return err.WithContext("Unable to call system reboot command");
×
156
        }
157

158
        system_reboot_->timeout.AsyncWait(chrono::minutes(10), [handler](error::Error err) {
×
159
                if (err != error::NoError) {
×
160
                        handler(err.WithContext("UpdateModule::AsyncSystemReboot"));
×
161
                }
162

163
                handler(error::Error(
×
164
                        make_error_condition(errc::timed_out),
×
165
                        "`reboot` command did not kill us; rebooting failed"));
×
166
        });
×
167

168
        return error::NoError;
×
169
}
170

171
} // namespace v3
172
} // namespace update_module
173
} // namespace update
174
} // 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