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

mendersoftware / mender / 1054591953

30 Oct 2023 10:02AM UTC coverage: 80.194% (+0.06%) from 80.137%
1054591953

push

gitlab-ci

kacf
fix: Make sure `device_type` is submitted with inventory data.

Changelog: None
Ticket: None

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

6871 of 8568 relevant lines covered (80.19%)

9388.93 hits per line

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

68.0
/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(
841✔
40
        events::EventLoop &loop,
41
        State state,
42
        const string &module_path,
43
        const string &module_work_path) :
841✔
44
        loop(loop),
45
        module_work_path(module_work_path),
46
        proc({module_path, StateToString(state), module_work_path}) {
3,364✔
47
        proc.SetWorkDir(module_work_path);
48
}
841✔
49

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

54
        string state_string = StateToString(state);
841✔
55
        if (!fs::is_directory(module_work_path)) {
841✔
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),
2✔
62
                                state_string + ": File tree does not exist: " + module_work_path);
2✔
63
                }
64
        }
65

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

68
        error::Error processStart;
840✔
69
        if (procOut) {
840✔
70
                // Provide string to put content in.
71
                output.emplace(string());
300✔
72
                processStart = proc.Start(
300✔
73
                        [this](const char *data, size_t size) {
199✔
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) {
200✔
79
                                        auto lines = mender::common::SplitString(string(data, size), "\n");
597✔
80
                                        if (lines.size() >= 1) {
199✔
81
                                                *output = lines[0];
82
                                                first_line_captured = true;
199✔
83
                                        }
84
                                        if (lines.size() > 2 || (lines.size() == 2 && lines[1] != "")) {
199✔
85
                                                too_many_lines = true;
2✔
86
                                        }
87
                                } else {
88
                                        too_many_lines = true;
1✔
89
                                }
90
                        },
200✔
91
                        stderr_handler);
600✔
92
        } else {
93
                processStart = proc.Start(
540✔
94
                        processes::OutputHandler {"Update Module output (stdout): "}, stderr_handler);
1,080✔
95
        }
96
        if (processStart != error::NoError) {
840✔
97
                return GetProcessError(processStart).WithContext(state_string);
×
98
        }
99

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

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

113
        return err;
840✔
114
}
115

116
void UpdateModule::StateRunner::ProcessFinishedHandler(State state, error::Error err) {
840✔
117
        if (state == State::Cleanup) {
840✔
118
                std::error_code ec;
122✔
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) {
122✔
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) {
840✔
129
                err = error::Error(
3✔
130
                        make_error_condition(errc::protocol_error),
6✔
131
                        "Too many lines when querying " + StateToString(state));
9✔
132
        }
133

134
        if (err != error::NoError) {
840✔
135
                handler(expected::unexpected(err));
126✔
136
        } else {
137
                handler(output);
1,554✔
138
        }
139
}
840✔
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
        system_reboot_->proc.Start();
×
147

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

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

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

169
        return error::NoError;
×
170
}
171

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