Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #include "starterd_lib.h" |
| 2 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <utility> |
| 5 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 6 | #include "absl/strings/str_format.h" |
| 7 | #include "aos/json_to_flatbuffer.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
| 9 | #include "glog/stl_logging.h" |
| 10 | |
| 11 | namespace aos { |
| 12 | namespace starter { |
| 13 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 14 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 15 | const aos::Node *node) { |
| 16 | return configuration::GetChannel<Status>(config, "/aos", "", node); |
| 17 | } |
| 18 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 19 | const aos::Node *node) { |
| 20 | return configuration::GetChannel<StarterRpc>(config, "/aos", "", node); |
| 21 | } |
| 22 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 23 | Starter::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 Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 27 | status_timer_(event_loop_.AddTimer([this] { |
| 28 | SendStatus(); |
| 29 | status_count_ = 0; |
| 30 | })), |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 31 | cleanup_timer_(event_loop_.AddTimer([this] { event_loop_.Exit(); })), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 32 | max_status_count_( |
| 33 | event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() - |
| 34 | 1), |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 35 | listener_(&event_loop_, |
| 36 | [this](signalfd_siginfo signal) { OnSignal(signal); }) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 37 | event_loop_.SkipAosLog(); |
| 38 | |
| 39 | event_loop_.OnRun([this] { |
| 40 | status_timer_->Setup(event_loop_.monotonic_now(), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 41 | std::chrono::milliseconds(1000)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 42 | }); |
| 43 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 44 | if (!aos::configuration::MultiNode(config_msg_)) { |
| 45 | event_loop_.MakeWatcher( |
| 46 | "/aos", |
| 47 | [this](const aos::starter::StarterRpc &cmd) { HandleStarterRpc(cmd); }); |
| 48 | } else { |
| 49 | for (const aos::Node *node : aos::configuration::GetNodes(config_msg_)) { |
| 50 | const Channel *channel = StarterRpcChannelForNode(config_msg_, node); |
| 51 | CHECK(channel != nullptr) << ": Failed to find channel /aos for " |
| 52 | << StarterRpc::GetFullyQualifiedName() << " on " |
| 53 | << node->name()->string_view(); |
| 54 | if (!aos::configuration::ChannelIsReadableOnNode(channel, |
| 55 | event_loop_.node())) { |
| 56 | LOG(INFO) << "StarterRpc channel " |
| 57 | << aos::configuration::StrippedChannelToString(channel) |
| 58 | << " is not readable on " |
| 59 | << event_loop_.node()->name()->string_view(); |
| 60 | } else { |
| 61 | event_loop_.MakeWatcher(channel->name()->string_view(), |
| 62 | [this](const aos::starter::StarterRpc &cmd) { |
| 63 | HandleStarterRpc(cmd); |
| 64 | }); |
| 65 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 66 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 67 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 68 | |
| 69 | if (config_msg_->has_applications()) { |
| 70 | const flatbuffers::Vector<flatbuffers::Offset<aos::Application>> |
| 71 | *applications = config_msg_->applications(); |
Ravago Jones | 7e2dd32 | 2020-11-21 15:58:58 -0800 | [diff] [blame] | 72 | |
| 73 | if (aos::configuration::MultiNode(config_msg_)) { |
| 74 | std::string_view current_node = event_loop_.node()->name()->string_view(); |
| 75 | for (const aos::Application *application : *applications) { |
| 76 | CHECK(application->has_nodes()); |
| 77 | for (const flatbuffers::String *node : *application->nodes()) { |
| 78 | if (node->string_view() == current_node) { |
| 79 | AddApplication(application); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | for (const aos::Application *application : *applications) { |
| 86 | AddApplication(application); |
| 87 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 92 | void Starter::HandleStarterRpc(const StarterRpc &command) { |
| 93 | if (!command.has_command() || !command.has_name() || exiting_) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | LOG(INFO) << "Received " << aos::FlatbufferToJson(&command); |
| 98 | |
| 99 | if (command.has_nodes()) { |
| 100 | CHECK(aos::configuration::MultiNode(config_msg_)); |
| 101 | bool relevant_to_this_node = false; |
| 102 | for (const flatbuffers::String *node : *command.nodes()) { |
| 103 | if (node->string_view() == event_loop_.node()->name()->string_view()) { |
| 104 | relevant_to_this_node = true; |
| 105 | } |
| 106 | } |
| 107 | if (!relevant_to_this_node) { |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | // If not populated, restart regardless of node. |
| 112 | |
| 113 | auto search = applications_.find(command.name()->str()); |
| 114 | if (search != applications_.end()) { |
| 115 | // If an applicatione exists by the given name, dispatch the command |
| 116 | search->second.HandleCommand(command.command()); |
| 117 | } |
| 118 | } |
| 119 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 120 | void Starter::MaybeSendStatus() { |
| 121 | if (status_count_ < max_status_count_) { |
| 122 | SendStatus(); |
| 123 | ++status_count_; |
| 124 | } else { |
| 125 | VLOG(1) << "That's enough " << status_count_ << " " << max_status_count_; |
| 126 | } |
| 127 | } |
| 128 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 129 | void Starter::Cleanup() { |
| 130 | if (exiting_) { |
| 131 | return; |
| 132 | } |
| 133 | exiting_ = true; |
| 134 | for (auto &application : applications_) { |
| 135 | application.second.Terminate(); |
| 136 | } |
| 137 | cleanup_timer_->Setup(event_loop_.monotonic_now() + |
| 138 | std::chrono::milliseconds(1500)); |
| 139 | } |
| 140 | |
| 141 | void Starter::OnSignal(signalfd_siginfo info) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 142 | if (info.ssi_signo == SIGCHLD) { |
| 143 | // SIGCHLD messages can be collapsed if multiple are received, so all |
| 144 | // applications must check their status. |
| 145 | for (auto iter = applications_.begin(); iter != applications_.end();) { |
| 146 | if (iter->second.MaybeHandleSignal()) { |
| 147 | iter = applications_.erase(iter); |
| 148 | } else { |
| 149 | ++iter; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (exiting_ && applications_.empty()) { |
| 154 | event_loop_.Exit(); |
| 155 | } |
Austin Schuh | 3204b33 | 2021-10-16 14:20:10 -0700 | [diff] [blame] | 156 | } else { |
| 157 | LOG(INFO) << "Received signal '" << strsignal(info.ssi_signo) << "'"; |
| 158 | |
| 159 | if (std::find(kStarterDeath.begin(), kStarterDeath.end(), info.ssi_signo) != |
| 160 | kStarterDeath.end()) { |
| 161 | LOG(WARNING) << "Starter shutting down"; |
| 162 | Cleanup(); |
| 163 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | Application *Starter::AddApplication(const aos::Application *application) { |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 168 | auto [iter, success] = |
| 169 | applications_.try_emplace(application->name()->str(), application, |
| 170 | &event_loop_, [this]() { MaybeSendStatus(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 171 | if (success) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame^] | 172 | // We should be catching and handling SIGCHLD correctly in the starter, so |
| 173 | // don't leave in the crutch for polling for the child process status (this |
| 174 | // is less about efficiency, and more about making sure bit rot doesn't |
| 175 | // result in the signal handling breaking). |
| 176 | iter->second.DisableChildDeathPolling(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 177 | return &(iter->second); |
| 178 | } |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | void Starter::Run() { |
Tyler Chatow | 03fdb2a | 2020-12-26 18:39:36 -0800 | [diff] [blame] | 183 | #ifdef AOS_ARCHITECTURE_arm_frc |
| 184 | PCHECK(setuid(0) == 0) << "Failed to change user to root"; |
| 185 | #endif |
| 186 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 187 | for (auto &application : applications_) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 188 | if (application.second.autostart()) { |
| 189 | application.second.Start(); |
| 190 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | event_loop_.Run(); |
| 194 | } |
| 195 | |
| 196 | void Starter::SendStatus() { |
| 197 | aos::Sender<aos::starter::Status>::Builder builder = |
| 198 | status_sender_.MakeBuilder(); |
| 199 | |
| 200 | std::vector<flatbuffers::Offset<aos::starter::ApplicationStatus>> statuses; |
| 201 | |
| 202 | for (auto &application : applications_) { |
| 203 | statuses.push_back(application.second.PopulateStatus(builder.fbb())); |
| 204 | } |
| 205 | |
| 206 | auto statuses_fbs = builder.fbb()->CreateVector(statuses); |
| 207 | |
| 208 | aos::starter::Status::Builder status_builder(*builder.fbb()); |
| 209 | status_builder.add_statuses(statuses_fbs); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 210 | builder.CheckOk(builder.Send(status_builder.Finish())); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } // namespace starter |
| 214 | } // namespace aos |