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" |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 14 | #include "aos/ipc_lib/memory_mapped_queue.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 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" |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 20 | #include "aos/util/top.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 21 | |
| 22 | namespace aos { |
| 23 | namespace starter { |
| 24 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 25 | const aos::Channel *StatusChannelForNode(const aos::Configuration *config, |
| 26 | const aos::Node *node); |
| 27 | const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config, |
| 28 | const aos::Node *node); |
| 29 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 30 | class Starter { |
| 31 | public: |
| 32 | Starter(const aos::Configuration *event_loop_config); |
| 33 | |
| 34 | // Inserts a new application from config. Returns the inserted application if |
| 35 | // it was successful, otherwise nullptr if an application already exists |
| 36 | // with the given name. |
| 37 | Application *AddApplication(const aos::Application *application); |
| 38 | |
| 39 | // Runs the event loop and starts all applications |
| 40 | void Run(); |
| 41 | |
| 42 | void Cleanup(); |
| 43 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 44 | // EventLoop that we use for running the code. Mostly exposed for testing |
| 45 | // purposes. |
| 46 | EventLoop *event_loop() { return &event_loop_; } |
| 47 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 48 | private: |
| 49 | // Signals which indicate starter has died |
| 50 | static const inline std::vector<int> kStarterDeath = { |
| 51 | SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, |
| 52 | SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU}; |
| 53 | |
| 54 | void OnSignal(signalfd_siginfo signal); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 55 | void HandleStarterRpc(const StarterRpc &command); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 56 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 57 | // Handles any potential state change in the child applications. |
| 58 | // In particular, sends the Status message if it wouldn't exceed the rate |
| 59 | // limit. |
| 60 | void HandleStateChange(); |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 61 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 62 | void SendStatus(); |
| 63 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 64 | // Creates a MemoryMappedQueue for the given channel, to pre-allocate shared |
| 65 | // memory to give this process credit for the memory instead of any other |
| 66 | // process that accesses it. |
| 67 | void AddChannel(const aos::Channel *channel); |
| 68 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 69 | const std::string config_path_; |
| 70 | const aos::Configuration *config_msg_; |
| 71 | |
| 72 | aos::ShmEventLoop event_loop_; |
| 73 | aos::Sender<aos::starter::Status> status_sender_; |
| 74 | aos::TimerHandler *status_timer_; |
| 75 | aos::TimerHandler *cleanup_timer_; |
| 76 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame] | 77 | int status_count_ = 0; |
| 78 | const int max_status_count_; |
| 79 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 80 | std::unordered_map<std::string, Application> applications_; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame^] | 81 | std::vector<std::unique_ptr<aos::ipc_lib::MemoryMappedQueue>> shm_queues_; |
| 82 | |
| 83 | // Capture the --shm_base flag at construction time. This makes it much |
| 84 | // easier to make different shared memory regions for doing things like |
| 85 | // multi-node tests. |
| 86 | std::string shm_base_; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 87 | |
| 88 | // Set to true on cleanup to block rpc commands and ensure cleanup only |
| 89 | // happens once. |
| 90 | bool exiting_ = false; |
| 91 | |
| 92 | SignalListener listener_; |
| 93 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 94 | util::Top top_; |
| 95 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 96 | DISALLOW_COPY_AND_ASSIGN(Starter); |
| 97 | }; |
| 98 | |
| 99 | } // namespace starter |
| 100 | } // namespace aos |
| 101 | |
| 102 | #endif // AOS_STARTER_STARTERD_LIB_H_ |