blob: 30e0887af8e664e62d54188965db97406ec211ed [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
Austin Schuh4d275fc2022-09-16 15:42:45 -070011// FLAGS_shm_base is defined elsewhere, declare it here so it can be used
12// to override the shared memory folder for unit testing.
13DECLARE_string(shm_base);
14// FLAGS_permissions is defined elsewhere, declare it here so it can be used
15// to set the file permissions on the shared memory block.
16DECLARE_uint32(permissions);
17
Tyler Chatowa79419d2020-08-12 20:12:11 -070018namespace aos {
19namespace starter {
20
James Kuszmaul293b2172021-11-10 16:20:48 -080021const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
22 const aos::Node *node) {
23 return configuration::GetChannel<Status>(config, "/aos", "", node);
24}
25const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
26 const aos::Node *node) {
27 return configuration::GetChannel<StarterRpc>(config, "/aos", "", node);
28}
29
Tyler Chatowa79419d2020-08-12 20:12:11 -070030Starter::Starter(const aos::Configuration *event_loop_config)
31 : config_msg_(event_loop_config),
32 event_loop_(event_loop_config),
33 status_sender_(event_loop_.MakeSender<aos::starter::Status>("/aos")),
Austin Schuhfc304942021-10-16 14:20:05 -070034 status_timer_(event_loop_.AddTimer([this] {
35 SendStatus();
36 status_count_ = 0;
37 })),
Austin Schuh59398d32023-05-03 08:10:55 -070038 cleanup_timer_(event_loop_.AddTimer([this] {
39 event_loop_.Exit();
40 LOG(INFO) << "Starter event loop exit finished.";
41 })),
Austin Schuhfc304942021-10-16 14:20:05 -070042 max_status_count_(
43 event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() -
44 1),
Austin Schuh4d275fc2022-09-16 15:42:45 -070045 shm_base_(FLAGS_shm_base),
Tyler Chatowa79419d2020-08-12 20:12:11 -070046 listener_(&event_loop_,
James Kuszmaul6295a642022-03-22 15:23:59 -070047 [this](signalfd_siginfo signal) { OnSignal(signal); }),
48 top_(&event_loop_) {
Tyler Chatowa79419d2020-08-12 20:12:11 -070049 event_loop_.SkipAosLog();
50
51 event_loop_.OnRun([this] {
52 status_timer_->Setup(event_loop_.monotonic_now(),
Austin Schuhfc304942021-10-16 14:20:05 -070053 std::chrono::milliseconds(1000));
Tyler Chatowa79419d2020-08-12 20:12:11 -070054 });
55
James Kuszmaul293b2172021-11-10 16:20:48 -080056 if (!aos::configuration::MultiNode(config_msg_)) {
57 event_loop_.MakeWatcher(
58 "/aos",
59 [this](const aos::starter::StarterRpc &cmd) { HandleStarterRpc(cmd); });
60 } else {
61 for (const aos::Node *node : aos::configuration::GetNodes(config_msg_)) {
62 const Channel *channel = StarterRpcChannelForNode(config_msg_, node);
63 CHECK(channel != nullptr) << ": Failed to find channel /aos for "
64 << StarterRpc::GetFullyQualifiedName() << " on "
65 << node->name()->string_view();
66 if (!aos::configuration::ChannelIsReadableOnNode(channel,
67 event_loop_.node())) {
68 LOG(INFO) << "StarterRpc channel "
69 << aos::configuration::StrippedChannelToString(channel)
70 << " is not readable on "
71 << event_loop_.node()->name()->string_view();
72 } else {
73 event_loop_.MakeWatcher(channel->name()->string_view(),
74 [this](const aos::starter::StarterRpc &cmd) {
75 HandleStarterRpc(cmd);
76 });
77 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070078 }
James Kuszmaul293b2172021-11-10 16:20:48 -080079 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070080
Austin Schuh4d275fc2022-09-16 15:42:45 -070081 // Catalogue all the applications for this node, so we can keep an eye on
82 // them.
Tyler Chatowa79419d2020-08-12 20:12:11 -070083 if (config_msg_->has_applications()) {
84 const flatbuffers::Vector<flatbuffers::Offset<aos::Application>>
85 *applications = config_msg_->applications();
Ravago Jones7e2dd322020-11-21 15:58:58 -080086
87 if (aos::configuration::MultiNode(config_msg_)) {
88 std::string_view current_node = event_loop_.node()->name()->string_view();
89 for (const aos::Application *application : *applications) {
Austin Schuh228609b2023-03-21 15:43:11 -070090 CHECK(application->has_nodes())
91 << ": Missing nodes on " << aos::FlatbufferToJson(application);
Ravago Jones7e2dd322020-11-21 15:58:58 -080092 for (const flatbuffers::String *node : *application->nodes()) {
93 if (node->string_view() == current_node) {
94 AddApplication(application);
95 break;
96 }
97 }
98 }
99 } else {
100 for (const aos::Application *application : *applications) {
101 AddApplication(application);
102 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700103 }
104 }
Austin Schuh4d275fc2022-09-16 15:42:45 -0700105
106 // Catalogue all the intranode channels for this node, and create
107 // MemoryMappedQueues for each one to allocate the shared memory before
108 // spawning any shasta process.
109 if (config_msg_->has_channels()) {
110 const aos::Node *this_node = event_loop_.node();
111 std::vector<const aos::Channel *> intranode_channels;
112 for (const aos::Channel *channel : *config_msg_->channels()) {
113 if (aos::configuration::ChannelIsReadableOnNode(channel, this_node)) {
114 AddChannel(channel);
115 }
116 }
117 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700118}
119
James Kuszmaul293b2172021-11-10 16:20:48 -0800120void Starter::HandleStarterRpc(const StarterRpc &command) {
121 if (!command.has_command() || !command.has_name() || exiting_) {
122 return;
123 }
124
125 LOG(INFO) << "Received " << aos::FlatbufferToJson(&command);
126
127 if (command.has_nodes()) {
128 CHECK(aos::configuration::MultiNode(config_msg_));
129 bool relevant_to_this_node = false;
130 for (const flatbuffers::String *node : *command.nodes()) {
131 if (node->string_view() == event_loop_.node()->name()->string_view()) {
132 relevant_to_this_node = true;
133 }
134 }
135 if (!relevant_to_this_node) {
136 return;
137 }
138 }
139 // If not populated, restart regardless of node.
140
141 auto search = applications_.find(command.name()->str());
142 if (search != applications_.end()) {
143 // If an applicatione exists by the given name, dispatch the command
144 search->second.HandleCommand(command.command());
145 }
146}
147
James Kuszmaul6295a642022-03-22 15:23:59 -0700148void Starter::HandleStateChange() {
149 std::set<pid_t> all_pids;
150 for (const auto &pair : applications_) {
151 if (pair.second.get_pid() > 0 &&
152 pair.second.status() != aos::starter::State::STOPPED) {
153 all_pids.insert(pair.second.get_pid());
154 }
155 }
156 top_.set_track_pids(all_pids);
157
Austin Schuhfc304942021-10-16 14:20:05 -0700158 if (status_count_ < max_status_count_) {
159 SendStatus();
160 ++status_count_;
161 } else {
162 VLOG(1) << "That's enough " << status_count_ << " " << max_status_count_;
163 }
164}
165
Tyler Chatowa79419d2020-08-12 20:12:11 -0700166void Starter::Cleanup() {
167 if (exiting_) {
168 return;
169 }
170 exiting_ = true;
171 for (auto &application : applications_) {
172 application.second.Terminate();
173 }
174 cleanup_timer_->Setup(event_loop_.monotonic_now() +
175 std::chrono::milliseconds(1500));
176}
177
178void Starter::OnSignal(signalfd_siginfo info) {
Tyler Chatowa79419d2020-08-12 20:12:11 -0700179 if (info.ssi_signo == SIGCHLD) {
180 // SIGCHLD messages can be collapsed if multiple are received, so all
181 // applications must check their status.
182 for (auto iter = applications_.begin(); iter != applications_.end();) {
183 if (iter->second.MaybeHandleSignal()) {
184 iter = applications_.erase(iter);
185 } else {
186 ++iter;
187 }
188 }
189
190 if (exiting_ && applications_.empty()) {
191 event_loop_.Exit();
192 }
Austin Schuh3204b332021-10-16 14:20:10 -0700193 } else {
194 LOG(INFO) << "Received signal '" << strsignal(info.ssi_signo) << "'";
195
196 if (std::find(kStarterDeath.begin(), kStarterDeath.end(), info.ssi_signo) !=
197 kStarterDeath.end()) {
198 LOG(WARNING) << "Starter shutting down";
199 Cleanup();
200 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700201 }
202}
203
204Application *Starter::AddApplication(const aos::Application *application) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700205 auto [iter, success] = applications_.try_emplace(
206 application->name()->str(), application, &event_loop_,
207 [this]() { HandleStateChange(); });
Tyler Chatowa79419d2020-08-12 20:12:11 -0700208 if (success) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800209 // We should be catching and handling SIGCHLD correctly in the starter, so
210 // don't leave in the crutch for polling for the child process status (this
211 // is less about efficiency, and more about making sure bit rot doesn't
212 // result in the signal handling breaking).
213 iter->second.DisableChildDeathPolling();
Tyler Chatowa79419d2020-08-12 20:12:11 -0700214 return &(iter->second);
215 }
216 return nullptr;
217}
218
219void Starter::Run() {
Tyler Chatow03fdb2a2020-12-26 18:39:36 -0800220#ifdef AOS_ARCHITECTURE_arm_frc
221 PCHECK(setuid(0) == 0) << "Failed to change user to root";
222#endif
223
Tyler Chatowa79419d2020-08-12 20:12:11 -0700224 for (auto &application : applications_) {
Austin Schuh5f79a5a2021-10-12 17:46:50 -0700225 if (application.second.autostart()) {
226 application.second.Start();
227 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700228 }
229
230 event_loop_.Run();
231}
232
233void Starter::SendStatus() {
234 aos::Sender<aos::starter::Status>::Builder builder =
235 status_sender_.MakeBuilder();
236
237 std::vector<flatbuffers::Offset<aos::starter::ApplicationStatus>> statuses;
238
239 for (auto &application : applications_) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700240 statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700241 }
242
243 auto statuses_fbs = builder.fbb()->CreateVector(statuses);
244
245 aos::starter::Status::Builder status_builder(*builder.fbb());
246 status_builder.add_statuses(statuses_fbs);
milind1f1dca32021-07-03 13:50:07 -0700247 builder.CheckOk(builder.Send(status_builder.Finish()));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700248}
249
Austin Schuh4d275fc2022-09-16 15:42:45 -0700250void Starter::AddChannel(const aos::Channel *channel) {
251 CHECK_NOTNULL(channel);
252 shm_queues_.emplace_back(std::make_unique<aos::ipc_lib::MemoryMappedQueue>(
253 shm_base_, FLAGS_permissions, event_loop_.configuration(), channel));
254 VLOG(1) << "Created MemoryMappedQueue for "
255 << aos::configuration::StrippedChannelToString(channel) << " under "
256 << shm_base_;
257}
258
Tyler Chatowa79419d2020-08-12 20:12:11 -0700259} // namespace starter
260} // namespace aos