| 1 | #pragma once |
| 2 | |
| 3 | #include "unit.hpp" |
| 4 | |
| 5 | #include <atomic> |
| 6 | |
| 7 | enum class StateChangeNotifyType |
| 8 | { |
| 9 | Immediate, ///< Service state change is applied immediately |
| 10 | Notify, ///< Service executable is capable of telling us that it has started |
| 11 | }; |
| 12 | |
| 13 | struct ServiceOptions |
| 14 | { |
| 15 | explicit ServiceOptions(toml::node_view<toml::node> table); |
| 16 | |
| 17 | StateChangeNotifyType stateChangeNotifyType = StateChangeNotifyType::Immediate; |
| 18 | }; |
| 19 | |
| 20 | struct Service : public Unit |
| 21 | { |
| 22 | explicit Service(const std::string &id, toml::table &table, std::shared_ptr<const Template> template_ = nullptr, const ArgumentMap &args = {}); |
| 23 | std::vector<std::string> exec; |
| 24 | |
| 25 | void OnExited(int status); |
| 26 | |
| 27 | std::string GetToken() const |
| 28 | { |
| 29 | return token; |
| 30 | } |
| 31 | |
| 32 | pid_t GetMainPid() const |
| 33 | { |
| 34 | return main_pid.load(); |
| 35 | } |
| 36 | |
| 37 | void ChangeState(const UnitStatus &status); |
| 38 | |
| 39 | private: |
| 40 | UnitType GetType() const override |
| 41 | { |
| 42 | return UnitType::Service; |
| 43 | } |
| 44 | |
| 45 | bool Start() override; |
| 46 | bool Stop() override; |
| 47 | void onPrint(std::ostream &os) const override; |
| 48 | |
| 49 | private: |
| 50 | std::atomic<pid_t> main_pid = -1; |
| 51 | int exit_status = -1; |
| 52 | std::string token; |
| 53 | ServiceOptions service_options; |
| 54 | }; |
| 55 | |