Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #ifndef AOS_STARTER_STARTERD_LIB_H_ |
| 2 | #define AOS_STARTER_STARTERD_LIB_H_ |
| 3 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 4 | #include <sys/signalfd.h> |
| 5 | #include <sys/wait.h> |
| 6 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <csignal> |
| 8 | #include <cstdio> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <unordered_map> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "aos/configuration.h" |
| 14 | #include "aos/events/shm_event_loop.h" |
| 15 | #include "aos/ipc_lib/signalfd.h" |
| 16 | #include "aos/macros.h" |
| 17 | #include "aos/starter/starter_generated.h" |
| 18 | #include "aos/starter/starter_rpc_generated.h" |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 19 | #include "aos/starter/subprocess.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 20 | |
| 21 | namespace aos { |
| 22 | namespace starter { |
| 23 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 24 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 25 | const aos::Node *node); |
| 26 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 27 | const aos::Node *node); |
| 28 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 29 | class Starter { |
| 30 | public: |
| 31 | Starter(const aos::Configuration *event_loop_config); |
| 32 | |
| 33 | // Inserts a new application from config. Returns the inserted application if |
| 34 | // it was successful, otherwise nullptr if an application already exists |
| 35 | // with the given name. |
| 36 | Application *AddApplication(const aos::Application *application); |
| 37 | |
| 38 | // Runs the event loop and starts all applications |
| 39 | void Run(); |
| 40 | |
| 41 | void Cleanup(); |
| 42 | |
| 43 | private: |
| 44 | // Signals which indicate starter has died |
| 45 | static const inline std::vector<int> kStarterDeath = { |
| 46 | SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, |
| 47 | SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU}; |
| 48 | |
| 49 | void OnSignal(signalfd_siginfo signal); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 50 | void HandleStarterRpc(const StarterRpc &command); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 51 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 52 | // Sends the Status message if it wouldn't exceed the rate limit. |
| 53 | void MaybeSendStatus(); |
| 54 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 55 | void SendStatus(); |
| 56 | |
| 57 | const std::string config_path_; |
| 58 | const aos::Configuration *config_msg_; |
| 59 | |
| 60 | aos::ShmEventLoop event_loop_; |
| 61 | aos::Sender<aos::starter::Status> status_sender_; |
| 62 | aos::TimerHandler *status_timer_; |
| 63 | aos::TimerHandler *cleanup_timer_; |
| 64 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 65 | int status_count_ = 0; |
| 66 | const int max_status_count_; |
| 67 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 68 | std::unordered_map<std::string, Application> applications_; |
| 69 | |
| 70 | // Set to true on cleanup to block rpc commands and ensure cleanup only |
| 71 | // happens once. |
| 72 | bool exiting_ = false; |
| 73 | |
| 74 | SignalListener listener_; |
| 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(Starter); |
| 77 | }; |
| 78 | |
| 79 | } // namespace starter |
| 80 | } // namespace aos |
| 81 | |
| 82 | #endif // AOS_STARTER_STARTERD_LIB_H_ |