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" |
| 19 | |
| 20 | namespace aos { |
| 21 | namespace starter { |
| 22 | |
| 23 | // RAII Pipe for sending individual ints between reader and writer. |
| 24 | class ScopedPipe { |
| 25 | public: |
| 26 | class ScopedReadPipe; |
| 27 | class ScopedWritePipe; |
| 28 | |
| 29 | static std::tuple<ScopedReadPipe, ScopedWritePipe> MakePipe(); |
| 30 | |
| 31 | virtual ~ScopedPipe(); |
| 32 | |
| 33 | int fd() const { return fd_; } |
| 34 | |
| 35 | private: |
| 36 | ScopedPipe(int fd = -1); |
| 37 | |
| 38 | int fd_; |
| 39 | |
| 40 | ScopedPipe(const ScopedPipe &) = delete; |
| 41 | ScopedPipe &operator=(const ScopedPipe &) = delete; |
| 42 | ScopedPipe(ScopedPipe &&); |
| 43 | ScopedPipe &operator=(ScopedPipe &&); |
| 44 | }; |
| 45 | |
| 46 | class ScopedPipe::ScopedReadPipe : public ScopedPipe { |
| 47 | public: |
| 48 | std::optional<uint32_t> Read(); |
| 49 | |
| 50 | private: |
| 51 | using ScopedPipe::ScopedPipe; |
| 52 | |
| 53 | friend class ScopedPipe; |
| 54 | }; |
| 55 | |
| 56 | class ScopedPipe::ScopedWritePipe : public ScopedPipe { |
| 57 | public: |
| 58 | void Write(uint32_t data); |
| 59 | |
| 60 | private: |
| 61 | using ScopedPipe::ScopedPipe; |
| 62 | |
| 63 | friend class ScopedPipe; |
| 64 | }; |
| 65 | |
| 66 | // Manages a running process, allowing starting and stopping, and restarting |
| 67 | // automatically. |
| 68 | class Application { |
| 69 | public: |
| 70 | Application(const aos::Application *application, |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame^] | 71 | aos::ShmEventLoop *event_loop, std::function<void()> on_change); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 72 | |
| 73 | flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus( |
| 74 | flatbuffers::FlatBufferBuilder *builder); |
| 75 | |
| 76 | // Returns the last pid of this process. -1 if not started yet. |
| 77 | pid_t get_pid() const { return pid_; } |
| 78 | |
| 79 | // Handles a SIGCHLD signal received by the parent. Does nothing if this |
| 80 | // process was not the target. Returns true if this Application should be |
| 81 | // removed. |
| 82 | bool MaybeHandleSignal(); |
| 83 | |
| 84 | // Handles a command. May do nothing if application is already in the desired |
| 85 | // state. |
| 86 | void HandleCommand(aos::starter::Command cmd); |
| 87 | |
| 88 | void Start() { HandleCommand(aos::starter::Command::START); } |
| 89 | |
| 90 | void Stop() { HandleCommand(aos::starter::Command::STOP); } |
| 91 | |
| 92 | void Terminate(); |
| 93 | |
| 94 | void set_args( |
| 95 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> |
| 96 | &args); |
| 97 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 98 | bool autostart() const { return autostart_; } |
| 99 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 100 | private: |
| 101 | void DoStart(); |
| 102 | |
| 103 | void DoStop(bool restart); |
| 104 | |
| 105 | void QueueStart(); |
| 106 | |
| 107 | // Copy flatbuffer vector of strings to vector of std::string. |
| 108 | static std::vector<std::string> FbsVectorToVector( |
| 109 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v); |
| 110 | |
| 111 | static std::optional<uid_t> FindUid(const char *name); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 112 | static std::optional<gid_t> FindPrimaryGidForUser(const char *name); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 113 | |
| 114 | // Next unique id for all applications |
| 115 | static inline uint64_t next_id_ = 0; |
| 116 | |
| 117 | std::string name_; |
| 118 | std::string path_; |
| 119 | std::vector<char *> args_; |
| 120 | std::optional<uid_t> user_; |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 121 | std::optional<gid_t> group_; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 122 | |
| 123 | pid_t pid_ = -1; |
| 124 | ScopedPipe::ScopedReadPipe read_pipe_; |
| 125 | ScopedPipe::ScopedWritePipe write_pipe_; |
| 126 | uint64_t id_; |
| 127 | int exit_code_ = 0; |
| 128 | aos::monotonic_clock::time_point start_time_, exit_time_; |
| 129 | bool queue_restart_ = false; |
| 130 | bool terminating_ = false; |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 131 | bool autostart_ = true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 132 | |
| 133 | aos::starter::State status_ = aos::starter::State::STOPPED; |
| 134 | aos::starter::LastStopReason stop_reason_ = |
| 135 | aos::starter::LastStopReason::STOP_REQUESTED; |
| 136 | |
| 137 | aos::ShmEventLoop *event_loop_; |
| 138 | aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_; |
| 139 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame^] | 140 | std::function<void()> on_change_; |
| 141 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(Application); |
| 143 | }; |
| 144 | |
| 145 | // Registers a signalfd listener with the given event loop and calls callback |
| 146 | // whenever a signal is received. |
| 147 | class SignalListener { |
| 148 | public: |
| 149 | SignalListener(aos::ShmEventLoop *loop, |
| 150 | std::function<void(signalfd_siginfo)> callback); |
| 151 | |
| 152 | ~SignalListener(); |
| 153 | |
| 154 | private: |
| 155 | aos::ShmEventLoop *loop_; |
| 156 | std::function<void(signalfd_siginfo)> callback_; |
| 157 | aos::ipc_lib::SignalFd signalfd_; |
| 158 | |
| 159 | DISALLOW_COPY_AND_ASSIGN(SignalListener); |
| 160 | }; |
| 161 | |
| 162 | class Starter { |
| 163 | public: |
| 164 | Starter(const aos::Configuration *event_loop_config); |
| 165 | |
| 166 | // Inserts a new application from config. Returns the inserted application if |
| 167 | // it was successful, otherwise nullptr if an application already exists |
| 168 | // with the given name. |
| 169 | Application *AddApplication(const aos::Application *application); |
| 170 | |
| 171 | // Runs the event loop and starts all applications |
| 172 | void Run(); |
| 173 | |
| 174 | void Cleanup(); |
| 175 | |
| 176 | private: |
| 177 | // Signals which indicate starter has died |
| 178 | static const inline std::vector<int> kStarterDeath = { |
| 179 | SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, |
| 180 | SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU}; |
| 181 | |
| 182 | void OnSignal(signalfd_siginfo signal); |
| 183 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame^] | 184 | // Sends the Status message if it wouldn't exceed the rate limit. |
| 185 | void MaybeSendStatus(); |
| 186 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 187 | void SendStatus(); |
| 188 | |
| 189 | const std::string config_path_; |
| 190 | const aos::Configuration *config_msg_; |
| 191 | |
| 192 | aos::ShmEventLoop event_loop_; |
| 193 | aos::Sender<aos::starter::Status> status_sender_; |
| 194 | aos::TimerHandler *status_timer_; |
| 195 | aos::TimerHandler *cleanup_timer_; |
| 196 | |
Austin Schuh | fc30494 | 2021-10-16 14:20:05 -0700 | [diff] [blame^] | 197 | int status_count_ = 0; |
| 198 | const int max_status_count_; |
| 199 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 200 | std::unordered_map<std::string, Application> applications_; |
| 201 | |
| 202 | // Set to true on cleanup to block rpc commands and ensure cleanup only |
| 203 | // happens once. |
| 204 | bool exiting_ = false; |
| 205 | |
| 206 | SignalListener listener_; |
| 207 | |
| 208 | DISALLOW_COPY_AND_ASSIGN(Starter); |
| 209 | }; |
| 210 | |
| 211 | } // namespace starter |
| 212 | } // namespace aos |
| 213 | |
| 214 | #endif // AOS_STARTER_STARTERD_LIB_H_ |