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_, |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame^] | 36 | [this](signalfd_siginfo signal) { OnSignal(signal); }), |
| 37 | top_(&event_loop_) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 38 | event_loop_.SkipAosLog(); |
| 39 | |
| 40 | event_loop_.OnRun([this] { |
| 41 | status_timer_->Setup(event_loop_.monotonic_now(), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 42 | std::chrono::milliseconds(1000)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 43 | }); |
| 44 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 45 | 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 67 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 68 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 69 | |
| 70 | if (config_msg_->has_applications()) { |
| 71 | const flatbuffers::Vector<flatbuffers::Offset<aos::Application>> |
| 72 | *applications = config_msg_->applications(); |
Ravago Jones | 7e2dd32 | 2020-11-21 15:58:58 -0800 | [diff] [blame] | 73 | |
| 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 93 | void 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 Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame^] | 121 | void 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 Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 131 | 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 139 | void 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 | |
| 151 | void Starter::OnSignal(signalfd_siginfo info) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 152 | 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 Schuh | 3204b33 | 2021-10-16 14:20:10 -0700 | [diff] [blame] | 166 | } 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | Application *Starter::AddApplication(const aos::Application *application) { |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame^] | 178 | auto [iter, success] = applications_.try_emplace( |
| 179 | application->name()->str(), application, &event_loop_, |
| 180 | [this]() { HandleStateChange(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 181 | if (success) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 182 | // 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 187 | return &(iter->second); |
| 188 | } |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
| 192 | void Starter::Run() { |
Tyler Chatow | 03fdb2a | 2020-12-26 18:39:36 -0800 | [diff] [blame] | 193 | #ifdef AOS_ARCHITECTURE_arm_frc |
| 194 | PCHECK(setuid(0) == 0) << "Failed to change user to root"; |
| 195 | #endif |
| 196 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 197 | for (auto &application : applications_) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 198 | if (application.second.autostart()) { |
| 199 | application.second.Start(); |
| 200 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | event_loop_.Run(); |
| 204 | } |
| 205 | |
| 206 | void 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 Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame^] | 213 | statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 214 | } |
| 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 220 | builder.CheckOk(builder.Send(status_builder.Finish())); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } // namespace starter |
| 224 | } // namespace aos |