blob: 485d1f111faf0648ae7021f685c4945baf71e395 [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 })),
Tyler Chatowa79419d2020-08-12 20:12:11 -070038 cleanup_timer_(event_loop_.AddTimer([this] { event_loop_.Exit(); })),
Austin Schuhfc304942021-10-16 14:20:05 -070039 max_status_count_(
40 event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() -
41 1),
Austin Schuh4d275fc2022-09-16 15:42:45 -070042 shm_base_(FLAGS_shm_base),
Tyler Chatowa79419d2020-08-12 20:12:11 -070043 listener_(&event_loop_,
James Kuszmaul6295a642022-03-22 15:23:59 -070044 [this](signalfd_siginfo signal) { OnSignal(signal); }),
45 top_(&event_loop_) {
Tyler Chatowa79419d2020-08-12 20:12:11 -070046 event_loop_.SkipAosLog();
47
48 event_loop_.OnRun([this] {
49 status_timer_->Setup(event_loop_.monotonic_now(),
Austin Schuhfc304942021-10-16 14:20:05 -070050 std::chrono::milliseconds(1000));
Tyler Chatowa79419d2020-08-12 20:12:11 -070051 });
52
James Kuszmaul293b2172021-11-10 16:20:48 -080053 if (!aos::configuration::MultiNode(config_msg_)) {
54 event_loop_.MakeWatcher(
55 "/aos",
56 [this](const aos::starter::StarterRpc &cmd) { HandleStarterRpc(cmd); });
57 } else {
58 for (const aos::Node *node : aos::configuration::GetNodes(config_msg_)) {
59 const Channel *channel = StarterRpcChannelForNode(config_msg_, node);
60 CHECK(channel != nullptr) << ": Failed to find channel /aos for "
61 << StarterRpc::GetFullyQualifiedName() << " on "
62 << node->name()->string_view();
63 if (!aos::configuration::ChannelIsReadableOnNode(channel,
64 event_loop_.node())) {
65 LOG(INFO) << "StarterRpc channel "
66 << aos::configuration::StrippedChannelToString(channel)
67 << " is not readable on "
68 << event_loop_.node()->name()->string_view();
69 } else {
70 event_loop_.MakeWatcher(channel->name()->string_view(),
71 [this](const aos::starter::StarterRpc &cmd) {
72 HandleStarterRpc(cmd);
73 });
74 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070075 }
James Kuszmaul293b2172021-11-10 16:20:48 -080076 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070077
Austin Schuh4d275fc2022-09-16 15:42:45 -070078 // Catalogue all the applications for this node, so we can keep an eye on
79 // them.
Tyler Chatowa79419d2020-08-12 20:12:11 -070080 if (config_msg_->has_applications()) {
81 const flatbuffers::Vector<flatbuffers::Offset<aos::Application>>
82 *applications = config_msg_->applications();
Ravago Jones7e2dd322020-11-21 15:58:58 -080083
84 if (aos::configuration::MultiNode(config_msg_)) {
85 std::string_view current_node = event_loop_.node()->name()->string_view();
86 for (const aos::Application *application : *applications) {
87 CHECK(application->has_nodes());
88 for (const flatbuffers::String *node : *application->nodes()) {
89 if (node->string_view() == current_node) {
90 AddApplication(application);
91 break;
92 }
93 }
94 }
95 } else {
96 for (const aos::Application *application : *applications) {
97 AddApplication(application);
98 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070099 }
100 }
Austin Schuh4d275fc2022-09-16 15:42:45 -0700101
102 // Catalogue all the intranode channels for this node, and create
103 // MemoryMappedQueues for each one to allocate the shared memory before
104 // spawning any shasta process.
105 if (config_msg_->has_channels()) {
106 const aos::Node *this_node = event_loop_.node();
107 std::vector<const aos::Channel *> intranode_channels;
108 for (const aos::Channel *channel : *config_msg_->channels()) {
109 if (aos::configuration::ChannelIsReadableOnNode(channel, this_node)) {
110 AddChannel(channel);
111 }
112 }
113 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700114}
115
James Kuszmaul293b2172021-11-10 16:20:48 -0800116void Starter::HandleStarterRpc(const StarterRpc &command) {
117 if (!command.has_command() || !command.has_name() || exiting_) {
118 return;
119 }
120
121 LOG(INFO) << "Received " << aos::FlatbufferToJson(&command);
122
123 if (command.has_nodes()) {
124 CHECK(aos::configuration::MultiNode(config_msg_));
125 bool relevant_to_this_node = false;
126 for (const flatbuffers::String *node : *command.nodes()) {
127 if (node->string_view() == event_loop_.node()->name()->string_view()) {
128 relevant_to_this_node = true;
129 }
130 }
131 if (!relevant_to_this_node) {
132 return;
133 }
134 }
135 // If not populated, restart regardless of node.
136
137 auto search = applications_.find(command.name()->str());
138 if (search != applications_.end()) {
139 // If an applicatione exists by the given name, dispatch the command
140 search->second.HandleCommand(command.command());
141 }
142}
143
James Kuszmaul6295a642022-03-22 15:23:59 -0700144void Starter::HandleStateChange() {
145 std::set<pid_t> all_pids;
146 for (const auto &pair : applications_) {
147 if (pair.second.get_pid() > 0 &&
148 pair.second.status() != aos::starter::State::STOPPED) {
149 all_pids.insert(pair.second.get_pid());
150 }
151 }
152 top_.set_track_pids(all_pids);
153
Austin Schuhfc304942021-10-16 14:20:05 -0700154 if (status_count_ < max_status_count_) {
155 SendStatus();
156 ++status_count_;
157 } else {
158 VLOG(1) << "That's enough " << status_count_ << " " << max_status_count_;
159 }
160}
161
Tyler Chatowa79419d2020-08-12 20:12:11 -0700162void Starter::Cleanup() {
163 if (exiting_) {
164 return;
165 }
166 exiting_ = true;
167 for (auto &application : applications_) {
168 application.second.Terminate();
169 }
170 cleanup_timer_->Setup(event_loop_.monotonic_now() +
171 std::chrono::milliseconds(1500));
172}
173
174void Starter::OnSignal(signalfd_siginfo info) {
Tyler Chatowa79419d2020-08-12 20:12:11 -0700175 if (info.ssi_signo == SIGCHLD) {
176 // SIGCHLD messages can be collapsed if multiple are received, so all
177 // applications must check their status.
178 for (auto iter = applications_.begin(); iter != applications_.end();) {
179 if (iter->second.MaybeHandleSignal()) {
180 iter = applications_.erase(iter);
181 } else {
182 ++iter;
183 }
184 }
185
186 if (exiting_ && applications_.empty()) {
187 event_loop_.Exit();
188 }
Austin Schuh3204b332021-10-16 14:20:10 -0700189 } else {
190 LOG(INFO) << "Received signal '" << strsignal(info.ssi_signo) << "'";
191
192 if (std::find(kStarterDeath.begin(), kStarterDeath.end(), info.ssi_signo) !=
193 kStarterDeath.end()) {
194 LOG(WARNING) << "Starter shutting down";
195 Cleanup();
196 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700197 }
198}
199
200Application *Starter::AddApplication(const aos::Application *application) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700201 auto [iter, success] = applications_.try_emplace(
202 application->name()->str(), application, &event_loop_,
203 [this]() { HandleStateChange(); });
Tyler Chatowa79419d2020-08-12 20:12:11 -0700204 if (success) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800205 // We should be catching and handling SIGCHLD correctly in the starter, so
206 // don't leave in the crutch for polling for the child process status (this
207 // is less about efficiency, and more about making sure bit rot doesn't
208 // result in the signal handling breaking).
209 iter->second.DisableChildDeathPolling();
Tyler Chatowa79419d2020-08-12 20:12:11 -0700210 return &(iter->second);
211 }
212 return nullptr;
213}
214
215void Starter::Run() {
Tyler Chatow03fdb2a2020-12-26 18:39:36 -0800216#ifdef AOS_ARCHITECTURE_arm_frc
217 PCHECK(setuid(0) == 0) << "Failed to change user to root";
218#endif
219
Tyler Chatowa79419d2020-08-12 20:12:11 -0700220 for (auto &application : applications_) {
Austin Schuh5f79a5a2021-10-12 17:46:50 -0700221 if (application.second.autostart()) {
222 application.second.Start();
223 }
Tyler Chatowa79419d2020-08-12 20:12:11 -0700224 }
225
226 event_loop_.Run();
227}
228
229void Starter::SendStatus() {
230 aos::Sender<aos::starter::Status>::Builder builder =
231 status_sender_.MakeBuilder();
232
233 std::vector<flatbuffers::Offset<aos::starter::ApplicationStatus>> statuses;
234
235 for (auto &application : applications_) {
James Kuszmaul6295a642022-03-22 15:23:59 -0700236 statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700237 }
238
239 auto statuses_fbs = builder.fbb()->CreateVector(statuses);
240
241 aos::starter::Status::Builder status_builder(*builder.fbb());
242 status_builder.add_statuses(statuses_fbs);
milind1f1dca32021-07-03 13:50:07 -0700243 builder.CheckOk(builder.Send(status_builder.Finish()));
Tyler Chatowa79419d2020-08-12 20:12:11 -0700244}
245
Austin Schuh4d275fc2022-09-16 15:42:45 -0700246void Starter::AddChannel(const aos::Channel *channel) {
247 CHECK_NOTNULL(channel);
248 shm_queues_.emplace_back(std::make_unique<aos::ipc_lib::MemoryMappedQueue>(
249 shm_base_, FLAGS_permissions, event_loop_.configuration(), channel));
250 VLOG(1) << "Created MemoryMappedQueue for "
251 << aos::configuration::StrippedChannelToString(channel) << " under "
252 << shm_base_;
253}
254
Tyler Chatowa79419d2020-08-12 20:12:11 -0700255} // namespace starter
256} // namespace aos