James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame^] | 1 | #ifndef AOS_STARTER_SUBPROCESS_H_ |
| 2 | #define AOS_STARTER_SUBPROCESS_H_ |
| 3 | |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | |
| 7 | #include "aos/events/event_loop.h" |
| 8 | #include "aos/events/shm_event_loop.h" |
| 9 | #include "aos/starter/starter_generated.h" |
| 10 | #include "aos/starter/starter_rpc_generated.h" |
| 11 | #include "aos/util/scoped_pipe.h" |
| 12 | |
| 13 | namespace aos::starter { |
| 14 | |
| 15 | // Registers a signalfd listener with the given event loop and calls callback |
| 16 | // whenever a signal is received. |
| 17 | class SignalListener { |
| 18 | public: |
| 19 | SignalListener(aos::ShmEventLoop *loop, |
| 20 | std::function<void(signalfd_siginfo)> callback); |
| 21 | SignalListener(aos::ShmEventLoop *loop, |
| 22 | std::function<void(signalfd_siginfo)> callback, |
| 23 | std::initializer_list<unsigned int> signals); |
| 24 | |
| 25 | ~SignalListener(); |
| 26 | |
| 27 | private: |
| 28 | aos::ShmEventLoop *loop_; |
| 29 | std::function<void(signalfd_siginfo)> callback_; |
| 30 | aos::ipc_lib::SignalFd signalfd_; |
| 31 | |
| 32 | DISALLOW_COPY_AND_ASSIGN(SignalListener); |
| 33 | }; |
| 34 | |
| 35 | // Manages a running process, allowing starting and stopping, and restarting |
| 36 | // automatically. |
| 37 | class Application { |
| 38 | public: |
| 39 | Application(const aos::Application *application, |
| 40 | aos::EventLoop *event_loop, std::function<void()> on_change); |
| 41 | |
| 42 | flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus( |
| 43 | flatbuffers::FlatBufferBuilder *builder); |
| 44 | |
| 45 | // Returns the last pid of this process. -1 if not started yet. |
| 46 | pid_t get_pid() const { return pid_; } |
| 47 | |
| 48 | // Handles a SIGCHLD signal received by the parent. Does nothing if this |
| 49 | // process was not the target. Returns true if this Application should be |
| 50 | // removed. |
| 51 | bool MaybeHandleSignal(); |
| 52 | |
| 53 | // Handles a command. May do nothing if application is already in the desired |
| 54 | // state. |
| 55 | void HandleCommand(aos::starter::Command cmd); |
| 56 | |
| 57 | void Start() { HandleCommand(aos::starter::Command::START); } |
| 58 | |
| 59 | void Stop() { HandleCommand(aos::starter::Command::STOP); } |
| 60 | |
| 61 | void Terminate(); |
| 62 | |
| 63 | void set_args( |
| 64 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> |
| 65 | &args); |
| 66 | |
| 67 | bool autostart() const { return autostart_; } |
| 68 | |
| 69 | bool autorestart() const { return autorestart_; } |
| 70 | |
| 71 | private: |
| 72 | void DoStart(); |
| 73 | |
| 74 | void DoStop(bool restart); |
| 75 | |
| 76 | void QueueStart(); |
| 77 | |
| 78 | // Copy flatbuffer vector of strings to vector of std::string. |
| 79 | static std::vector<std::string> FbsVectorToVector( |
| 80 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v); |
| 81 | |
| 82 | static std::optional<uid_t> FindUid(const char *name); |
| 83 | static std::optional<gid_t> FindPrimaryGidForUser(const char *name); |
| 84 | |
| 85 | // Next unique id for all applications |
| 86 | static inline uint64_t next_id_ = 0; |
| 87 | |
| 88 | std::string name_; |
| 89 | std::string path_; |
| 90 | std::vector<char *> args_; |
| 91 | std::string user_name_; |
| 92 | std::optional<uid_t> user_; |
| 93 | std::optional<gid_t> group_; |
| 94 | |
| 95 | pid_t pid_ = -1; |
| 96 | util::ScopedPipe::ScopedReadPipe read_pipe_; |
| 97 | util::ScopedPipe::ScopedWritePipe write_pipe_; |
| 98 | uint64_t id_; |
| 99 | int exit_code_ = 0; |
| 100 | aos::monotonic_clock::time_point start_time_, exit_time_; |
| 101 | bool queue_restart_ = false; |
| 102 | bool terminating_ = false; |
| 103 | bool autostart_ = true; |
| 104 | bool autorestart_ = true; |
| 105 | |
| 106 | aos::starter::State status_ = aos::starter::State::STOPPED; |
| 107 | aos::starter::LastStopReason stop_reason_ = |
| 108 | aos::starter::LastStopReason::STOP_REQUESTED; |
| 109 | |
| 110 | aos::EventLoop *event_loop_; |
| 111 | aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_; |
| 112 | |
| 113 | std::function<void()> on_change_; |
| 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(Application); |
| 116 | }; |
| 117 | |
| 118 | } // namespace aos::starter |
| 119 | #endif // AOS_STARTER_SUBPROCESS_H_ |