blob: 84e4d00c352444d364d8132b5668b8dad046918f [file] [log] [blame]
Tyler Chatowa79419d2020-08-12 20:12:11 -07001#include "starterd_lib.h"
2
Tyler Chatowa79419d2020-08-12 20:12:11 -07003#include <algorithm>
4#include <utility>
5
James Kuszmaul293b2172021-11-10 16:20:48 -08006#include "absl/strings/str_format.h"
7#include "aos/json_to_flatbuffer.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -07008#include "glog/logging.h"
9#include "glog/stl_logging.h"
10
11namespace aos {
12namespace starter {
13
James Kuszmaul293b2172021-11-10 16:20:48 -080014const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
15 const aos::Node *node) {
16 return configuration::GetChannel<Status>(config, "/aos", "", node);
17}
18const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
19 const aos::Node *node) {
20 return configuration::GetChannel<StarterRpc>(config, "/aos", "", node);
21}
22
Tyler Chatowa79419d2020-08-12 20:12:11 -070023Starter::Starter(const aos::Configuration *event_loop_config)
24 : config_msg_(event_loop_config),
25 event_loop_(event_loop_config),
26 status_sender_(event_loop_.MakeSender<aos::starter::Status>("/aos")),
Austin Schuhfc304942021-10-16 14:20:05 -070027 status_timer_(event_loop_.AddTimer([this] {
28 SendStatus();
29 status_count_ = 0;
30 })),
Tyler Chatowa79419d2020-08-12 20:12:11 -070031 cleanup_timer_(event_loop_.AddTimer([this] { event_loop_.Exit(); })),
Austin Schuhfc304942021-10-16 14:20:05 -070032 max_status_count_(
33 event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() -
34 1),
Tyler Chatowa79419d2020-08-12 20:12:11 -070035 listener_(&event_loop_,
James Kuszmaul6295a642022-03-22 15:23:59 -070036 [this](signalfd_siginfo signal) { OnSignal(signal); }),
37 top_(&event_loop_) {
Tyler Chatowa79419d2020-08-12 20:12:11 -070038 event_loop_.SkipAosLog();
39
40 event_loop_.OnRun([this] {
41 status_timer_->Setup(event_loop_.monotonic_now(),
Austin Schuhfc304942021-10-16 14:20:05 -070042 std::chrono::milliseconds(1000));
Tyler Chatowa79419d2020-08-12 20:12:11 -070043 });
44
James Kuszmaul293b2172021-11-10 16:20:48 -080045 if (!aos::configuration::MultiNode(config_msg_)) {
46 event_loop_.MakeWatcher(
47 "/aos",
48 [this](const aos::starter::StarterRpc &cmd) { HandleStarterRpc(cmd); });
49 } else {
50 for (const aos::Node *node : aos::configuration::GetNodes(config_msg_)) {
51 const Channel *channel = StarterRpcChannelForNode(config_msg_, node);
52 CHECK(channel != nullptr) << ": Failed to find channel /aos for "
53 << StarterRpc::GetFullyQualifiedName() << " on "
54 << node->name()->string_view();
55 if (!aos::configuration::ChannelIsReadableOnNode(channel,
56 event_loop_.node())) {
57 LOG(INFO) << "StarterRpc channel "
58 << aos::configuration::StrippedChannelToString(channel)
59 << " is not readable on "
60 << event_loop_.node()->name()->string_view();
61 } else {
62 event_loop_.MakeWatcher(channel->name()->string_view(),
63 [this](const aos::starter::StarterRpc &cmd) {
64 HandleStarterRpc(cmd);
65 });
66 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070067 }
James Kuszmaul293b2172021-11-10 16:20:48 -080068 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070069
70 if (config_msg_->has_applications()) {
71 const flatbuffers::Vector<flatbuffers::Offset<aos::Application>>
72 *applications = config_msg_->applications();
Ravago Jones7e2dd322020-11-21 15:58:58 -080073
74 if (aos::configuration::MultiNode(config_msg_)) {
75 std::string_view current_node = event_loop_.node()->name()->string_view();
76 for (const aos::Application *application : *applications) {
77 CHECK(application->has_nodes());
78 for (const flatbuffers::String *node : *application->nodes()) {
79 if (node->string_view() == current_node) {
80 AddApplication(application);
81 break;
82 }
83 }
84 }
85 } else {
86 for (const aos::Application *application : *applications) {
87 AddApplication(application);
88 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070089 }
90 }
91}
92
James Kuszmaul293b2172021-11-10 16:20:48 -080093void Starter::HandleStarterRpc(const StarterRpc &command) {
94 if (!command.has_command() || !command.has_name() || exiting_) {
95 return;
96 }
97
98 LOG(INFO) << "Received " << aos::FlatbufferToJson(&command);
99
100 if (command.has_nodes()) {
101 CHECK(aos::configuration::MultiNode(config_msg_));
102 bool relevant_to_this_node = false;
103 for (const flatbuffers::String *node : *command.nodes()) {
104 if (node->string_view() == event_loop_.node()->name()->string_view()) {
105 relevant_to_this_node = true;
106 }
107 }
108 if (!relevant_to_this_node) {
109 return;
110 }
111 }
112 // If not populated, restart regardless of node.
113
114 auto search = applications_.find(command.name()->str());
115 if (search != applications_.end()) {
116 // If an applicatione exists by the given name, dispatch the command
117 search->second.HandleCommand(command.command());
118 }
119}
120
James Kuszmaul6295a642022-03-22 15:23:59 -0700121void Starter::HandleStateChange() {
122 std::set<pid_t> all_pids;
123 for (const auto &pair : applications_) {
124 if (pair.second.get_pid() > 0 &&
125 pair.second.status() != aos::starter::State::STOPPED) {
126 all_pids.insert(pair.second.get_pid());
127 }
128 }
129 top_.set_track_pids(all_pids);
130
Austin Schuhfc304942021-10-16 14:20:05 -0700131 if (status_count_ < max_status_count_) {
132 SendStatus();
133 ++status_count_;
134 } else {
135 VLOG(1) << "That's enough " << status_count_ << " " << max_status_count_;
136 }
137}
138
Tyler Chatowa79419d2020-08-12 20:12:11 -0700139void Starter::Cleanup() {
140 if (exiting_) {
141 return;
142 }
143 exiting_ = true;
144 for (auto &application : applications_) {
145 application.second.Terminate();
146 }
147 cleanup_timer_->Setup(event_loop_.monotonic_now() +
148 std::chrono::milliseconds(1500));
149}
150
151void Starter::OnSignal(signalfd_siginfo info) {
Tyler Chatowa79419d2020-08-12 20:12:11 -0700152 if (info.ssi_signo == SIGCHLD) {
153 // SIGCHLD messages can be collapsed if multiple are received, so all
154 // applications must check their status.
155 for (auto iter = applications_.begin(); iter != applications_.end();) {
156 if (iter->second.MaybeHandleSignal()) {
157 iter = applications_.erase(iter);
158 } else {
159 ++iter;
160 }
161 }
162
163 if (exiting_ && applications_.empty()) {
164 event_loop_.Exit();
165 }
Austin Schuh3204b332021-10-16 14:20:10 -0700166 } else {
167 LOG(INFO) << "Received signal '" << strsignal(info.ssi_signo) << "'";
168
169 if (std::find(kStarterDeath.begin(), kStarterDeath.end(), info.ssi_signo) !=
170 kStarterDeath.end()) {
171 LOG(WARNING) << "Starter shutting down";
172 Cleanup();
173 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700174 }
175}
176
177Application *Starter::AddApplication(const aos::Application *application) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700178 auto [iter, success] = applications_.try_emplace(
179 application->name()->str(), application, &event_loop_,
180 [this]() { HandleStateChange(); });
Tyler Chatowa79419d2020-08-12 20:12:11 -0700181 if (success) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800182 // We should be catching and handling SIGCHLD correctly in the starter, so
183 // don't leave in the crutch for polling for the child process status (this
184 // is less about efficiency, and more about making sure bit rot doesn't
185 // result in the signal handling breaking).
186 iter->second.DisableChildDeathPolling();
Tyler Chatowa79419d2020-08-12 20:12:11 -0700187 return &(iter->second);
188 }
189 return nullptr;
190}
191
192void Starter::Run() {
Tyler Chatow03fdb2a2020-12-26 18:39:36 -0800193#ifdef AOS_ARCHITECTURE_arm_frc
194 PCHECK(setuid(0) == 0) << "Failed to change user to root";
195#endif
196
Tyler Chatowa79419d2020-08-12 20:12:11 -0700197 for (auto &application : applications_) {
Austin Schuh5f79a5a2021-10-12 17:46:50 -0700198 if (application.second.autostart()) {
199 application.second.Start();
200 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700201 }
202
203 event_loop_.Run();
204}
205
206void Starter::SendStatus() {
207 aos::Sender<aos::starter::Status>::Builder builder =
208 status_sender_.MakeBuilder();
209
210 std::vector<flatbuffers::Offset<aos::starter::ApplicationStatus>> statuses;
211
212 for (auto &application : applications_) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700213 statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700214 }
215
216 auto statuses_fbs = builder.fbb()->CreateVector(statuses);
217
218 aos::starter::Status::Builder status_builder(*builder.fbb());
219 status_builder.add_statuses(statuses_fbs);
milind1f1dca32021-07-03 13:50:07 -0700220 builder.CheckOk(builder.Send(status_builder.Finish()));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700221}
222
223} // namespace starter
224} // namespace aos