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 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 11 | // FLAGS_shm_base is defined elsewhere, declare it here so it can be used |
| 12 | // to override the shared memory folder for unit testing. |
| 13 | DECLARE_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. |
| 16 | DECLARE_uint32(permissions); |
| 17 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 18 | namespace aos { |
| 19 | namespace starter { |
| 20 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 21 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 22 | const aos::Node *node) { |
| 23 | return configuration::GetChannel<Status>(config, "/aos", "", node); |
| 24 | } |
| 25 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 26 | const aos::Node *node) { |
| 27 | return configuration::GetChannel<StarterRpc>(config, "/aos", "", node); |
| 28 | } |
| 29 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 30 | Starter::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 Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 34 | status_timer_(event_loop_.AddTimer([this] { |
| 35 | SendStatus(); |
| 36 | status_count_ = 0; |
| 37 | })), |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 38 | cleanup_timer_(event_loop_.AddTimer([this] { event_loop_.Exit(); })), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 39 | max_status_count_( |
| 40 | event_loop_.GetChannel<aos::starter::Status>("/aos")->frequency() - |
| 41 | 1), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 42 | shm_base_(FLAGS_shm_base), |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 43 | listener_(&event_loop_, |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 44 | [this](signalfd_siginfo signal) { OnSignal(signal); }), |
| 45 | top_(&event_loop_) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 46 | event_loop_.SkipAosLog(); |
| 47 | |
| 48 | event_loop_.OnRun([this] { |
| 49 | status_timer_->Setup(event_loop_.monotonic_now(), |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 50 | std::chrono::milliseconds(1000)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 51 | }); |
| 52 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 53 | 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 75 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 76 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 77 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 78 | // Catalogue all the applications for this node, so we can keep an eye on |
| 79 | // them. |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 80 | if (config_msg_->has_applications()) { |
| 81 | const flatbuffers::Vector<flatbuffers::Offset<aos::Application>> |
| 82 | *applications = config_msg_->applications(); |
Ravago Jones | 7e2dd32 | 2020-11-21 15:58:58 -0800 | [diff] [blame] | 83 | |
| 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 101 | |
| 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 116 | void 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 Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 144 | void 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 Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 154 | 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 162 | void 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 | |
| 174 | void Starter::OnSignal(signalfd_siginfo info) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 175 | 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 Schuh | 3204b33 | 2021-10-16 14:20:10 -0700 | [diff] [blame] | 189 | } 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | Application *Starter::AddApplication(const aos::Application *application) { |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 201 | auto [iter, success] = applications_.try_emplace( |
| 202 | application->name()->str(), application, &event_loop_, |
| 203 | [this]() { HandleStateChange(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 204 | if (success) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 205 | // 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 210 | return &(iter->second); |
| 211 | } |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | void Starter::Run() { |
Tyler Chatow | 03fdb2a | 2020-12-26 18:39:36 -0800 | [diff] [blame] | 216 | #ifdef AOS_ARCHITECTURE_arm_frc |
| 217 | PCHECK(setuid(0) == 0) << "Failed to change user to root"; |
| 218 | #endif |
| 219 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 220 | for (auto &application : applications_) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 221 | if (application.second.autostart()) { |
| 222 | application.second.Start(); |
| 223 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | event_loop_.Run(); |
| 227 | } |
| 228 | |
| 229 | void 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 Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 236 | statuses.push_back(application.second.PopulateStatus(builder.fbb(), &top_)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 237 | } |
| 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); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 243 | builder.CheckOk(builder.Send(status_builder.Finish())); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 246 | void 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 Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 255 | } // namespace starter |
| 256 | } // namespace aos |