blob: 784c544d4e61f906dbae1bf93b5ec172632c068e [file] [log] [blame]
James Kuszmaul3224b8e2022-01-07 19:00:39 -08001#ifndef AOS_STARTER_SUBPROCESS_H_
2#define AOS_STARTER_SUBPROCESS_H_
3
James Kuszmauld42edb42022-01-07 18:00:16 -08004#include <memory>
James Kuszmaul3224b8e2022-01-07 19:00:39 -08005#include <string>
James Kuszmauld42edb42022-01-07 18:00:16 -08006#include <tuple>
James Kuszmaul3224b8e2022-01-07 19:00:39 -08007#include <vector>
8
9#include "aos/events/event_loop.h"
10#include "aos/events/shm_event_loop.h"
11#include "aos/starter/starter_generated.h"
12#include "aos/starter/starter_rpc_generated.h"
13#include "aos/util/scoped_pipe.h"
James Kuszmaul6295a642022-03-22 15:23:59 -070014#include "aos/util/top.h"
James Kuszmaul3224b8e2022-01-07 19:00:39 -080015
16namespace aos::starter {
17
18// Registers a signalfd listener with the given event loop and calls callback
19// whenever a signal is received.
20class SignalListener {
21 public:
22 SignalListener(aos::ShmEventLoop *loop,
23 std::function<void(signalfd_siginfo)> callback);
Austin Schuh1cea9032023-07-10 11:56:40 -070024 SignalListener(aos::internal::EPoll *epoll,
25 std::function<void(signalfd_siginfo)> callback);
James Kuszmaul3224b8e2022-01-07 19:00:39 -080026 SignalListener(aos::ShmEventLoop *loop,
27 std::function<void(signalfd_siginfo)> callback,
28 std::initializer_list<unsigned int> signals);
Austin Schuh1cea9032023-07-10 11:56:40 -070029 SignalListener(aos::internal::EPoll *epoll,
30 std::function<void(signalfd_siginfo)> callback,
31 std::initializer_list<unsigned int> signals);
James Kuszmaul3224b8e2022-01-07 19:00:39 -080032
33 ~SignalListener();
34
35 private:
Austin Schuh1cea9032023-07-10 11:56:40 -070036 aos::internal::EPoll *epoll_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -080037 std::function<void(signalfd_siginfo)> callback_;
38 aos::ipc_lib::SignalFd signalfd_;
39
40 DISALLOW_COPY_AND_ASSIGN(SignalListener);
41};
42
Austin Schuhbbeb37e2022-08-17 16:19:27 -070043// Class to use the V1 cgroup API to limit memory usage.
44class MemoryCGroup {
45 public:
Austin Schuh77e20a32023-08-01 12:25:03 -070046 // Enum to control if MemoryCGroup should create the cgroup and remove it on
47 // its own, or if it should assume it already exists and just use it.
48 enum class Create {
49 kDoCreate,
50 kDoNotCreate,
51 };
52
53 MemoryCGroup(std::string_view name, Create should_create = Create::kDoCreate);
Austin Schuhbbeb37e2022-08-17 16:19:27 -070054 ~MemoryCGroup();
55
56 // Adds a thread ID to be managed by the cgroup.
57 void AddTid(pid_t pid = 0);
58
59 // Sets the provided limit to the provided value.
60 void SetLimit(std::string_view limit_name, uint64_t limit_value);
61
62 private:
63 std::string cgroup_;
Austin Schuh77e20a32023-08-01 12:25:03 -070064 Create should_create_;
Austin Schuhbbeb37e2022-08-17 16:19:27 -070065};
66
James Kuszmaul3224b8e2022-01-07 19:00:39 -080067// Manages a running process, allowing starting and stopping, and restarting
68// automatically.
69class Application {
70 public:
payton.rehl2841b1c2023-05-25 17:23:55 -070071 enum class QuietLogging { kYes, kNo };
James Kuszmauld42edb42022-01-07 18:00:16 -080072 Application(const aos::Application *application, aos::EventLoop *event_loop,
payton.rehl2841b1c2023-05-25 17:23:55 -070073 std::function<void()> on_change,
74 QuietLogging quiet_flag = QuietLogging::kNo);
James Kuszmauld42edb42022-01-07 18:00:16 -080075
Sarah Newman2c1b1212022-08-10 10:05:48 -070076 // executable_name is the actual executable path.
77 // When sudo is not used, name is used as argv[0] when exec'ing
78 // executable_name. When sudo is used it's not possible to pass in a
79 // distinct argv[0].
James Kuszmauld42edb42022-01-07 18:00:16 -080080 Application(std::string_view name, std::string_view executable_name,
payton.rehl2841b1c2023-05-25 17:23:55 -070081 aos::EventLoop *event_loop, std::function<void()> on_change,
82 QuietLogging quiet_flag = QuietLogging::kNo);
James Kuszmaul3224b8e2022-01-07 19:00:39 -080083
Adam Snaider70deaf22023-08-11 13:58:34 -070084 ~Application();
85
James Kuszmaul3224b8e2022-01-07 19:00:39 -080086 flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus(
James Kuszmaul6295a642022-03-22 15:23:59 -070087 flatbuffers::FlatBufferBuilder *builder, util::Top *top);
James Kuszmauld42edb42022-01-07 18:00:16 -080088 aos::starter::State status() const { return status_; };
James Kuszmaul3224b8e2022-01-07 19:00:39 -080089
90 // Returns the last pid of this process. -1 if not started yet.
91 pid_t get_pid() const { return pid_; }
92
93 // Handles a SIGCHLD signal received by the parent. Does nothing if this
94 // process was not the target. Returns true if this Application should be
95 // removed.
96 bool MaybeHandleSignal();
James Kuszmauld42edb42022-01-07 18:00:16 -080097 void DisableChildDeathPolling() { child_status_handler_->Disable(); }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080098
99 // Handles a command. May do nothing if application is already in the desired
100 // state.
101 void HandleCommand(aos::starter::Command cmd);
102
103 void Start() { HandleCommand(aos::starter::Command::START); }
104
Sanjay Narayanan92fdc3d2023-08-25 14:42:56 -0700105 // Stops the command by sending a SIGINT first, followed by a SIGKILL if it's
106 // still alive in 1s.
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800107 void Stop() { HandleCommand(aos::starter::Command::STOP); }
108
Sanjay Narayanan92fdc3d2023-08-25 14:42:56 -0700109 // Stops the command the same way as Stop() does, but updates internal state
110 // to reflect that the application was terminated.
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800111 void Terminate();
112
Austin Schuh1cea9032023-07-10 11:56:40 -0700113 // Adds a callback which gets notified when the application changes state.
114 // This is in addition to any existing callbacks and doesn't replace any of
115 // them.
116 void AddOnChange(std::function<void()> fn) {
117 on_change_.emplace_back(std::move(fn));
118 }
119
James Kuszmauld42edb42022-01-07 18:00:16 -0800120 void set_args(std::vector<std::string> args);
121 void set_capture_stdout(bool capture);
122 void set_capture_stderr(bool capture);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700123 void set_run_as_sudo(bool value) { run_as_sudo_ = value; }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800124
125 bool autostart() const { return autostart_; }
126
127 bool autorestart() const { return autorestart_; }
Adam Snaider70deaf22023-08-11 13:58:34 -0700128 void set_autorestart(bool autorestart) { autorestart_ = autorestart; }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800129
James Kuszmauld42edb42022-01-07 18:00:16 -0800130 const std::string &GetStdout();
131 const std::string &GetStderr();
132 std::optional<int> exit_code() const { return exit_code_; }
133
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700134 // Sets the memory limit for the application to the provided limit.
135 void SetMemoryLimit(size_t limit) {
136 if (!memory_cgroup_) {
137 memory_cgroup_ = std::make_unique<MemoryCGroup>(name_);
138 }
139 memory_cgroup_->SetLimit("memory.limit_in_bytes", limit);
140 }
141
Austin Schuh77e20a32023-08-01 12:25:03 -0700142 // Sets the cgroup and memory limit to a pre-existing cgroup which is
143 // externally managed. This lets us configure the cgroup of an application
144 // without root access.
145 void SetExistingCgroupMemoryLimit(std::string_view name, size_t limit) {
146 if (!memory_cgroup_) {
147 memory_cgroup_ = std::make_unique<MemoryCGroup>(
148 name, MemoryCGroup::Create::kDoNotCreate);
149 }
150 memory_cgroup_->SetLimit("memory.limit_in_bytes", limit);
151 }
152
James Kuszmaul8544c492023-07-31 15:00:38 -0700153 // Observe a timing report message, and save it if it is relevant to us.
154 // It is the responsibility of the caller to manage this, because the lifetime
155 // of the Application itself is such that it cannot own Fetchers readily.
156 void ObserveTimingReport(const aos::monotonic_clock::time_point send_time,
157 const aos::timing::Report *msg);
158
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800159 private:
James Kuszmauld42edb42022-01-07 18:00:16 -0800160 typedef aos::util::ScopedPipe::PipePair PipePair;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700161
Philipp Schrader790cb542023-07-05 21:06:52 -0700162 static constexpr const char *const kSudo{"sudo"};
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700163
James Kuszmauld42edb42022-01-07 18:00:16 -0800164 void set_args(
165 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
166 &args);
167
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800168 void DoStart();
169
170 void DoStop(bool restart);
171
172 void QueueStart();
173
Austin Schuh1cea9032023-07-10 11:56:40 -0700174 void OnChange();
175
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800176 // Copy flatbuffer vector of strings to vector of std::string.
177 static std::vector<std::string> FbsVectorToVector(
178 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v);
179
180 static std::optional<uid_t> FindUid(const char *name);
181 static std::optional<gid_t> FindPrimaryGidForUser(const char *name);
182
James Kuszmauld42edb42022-01-07 18:00:16 -0800183 void FetchOutputs();
184
185 // Provides an std::vector of the args (such that CArgs().data() ends up being
186 // suitable to pass to execve()).
187 // The points are invalidated when args_ changes (e.g., due to a set_args
188 // call).
189 std::vector<char *> CArgs();
190
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800191 // Next unique id for all applications
192 static inline uint64_t next_id_ = 0;
193
194 std::string name_;
195 std::string path_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800196 std::vector<std::string> args_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800197 std::string user_name_;
198 std::optional<uid_t> user_;
199 std::optional<gid_t> group_;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700200 bool run_as_sudo_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800201
James Kuszmauld42edb42022-01-07 18:00:16 -0800202 bool capture_stdout_ = false;
203 PipePair stdout_pipes_;
204 std::string stdout_;
205 bool capture_stderr_ = false;
206 PipePair stderr_pipes_;
207 std::string stderr_;
208
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800209 pid_t pid_ = -1;
James Kuszmauld42edb42022-01-07 18:00:16 -0800210 PipePair status_pipes_;
211 uint64_t id_ = 0;
212 std::optional<int> exit_code_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800213 aos::monotonic_clock::time_point start_time_, exit_time_;
214 bool queue_restart_ = false;
215 bool terminating_ = false;
James Kuszmauld42edb42022-01-07 18:00:16 -0800216 bool autostart_ = false;
217 bool autorestart_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800218
219 aos::starter::State status_ = aos::starter::State::STOPPED;
220 aos::starter::LastStopReason stop_reason_ =
221 aos::starter::LastStopReason::STOP_REQUESTED;
222
223 aos::EventLoop *event_loop_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800224 aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_, *pipe_timer_,
225 *child_status_handler_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800226
James Kuszmaul8544c492023-07-31 15:00:38 -0700227 // Version string from the most recent valid timing report for this
228 // application. Cleared when the application restarts.
229 std::optional<std::string> latest_timing_report_version_;
230 aos::monotonic_clock::time_point last_timing_report_ =
231 aos::monotonic_clock::min_time;
232
Austin Schuh1cea9032023-07-10 11:56:40 -0700233 std::vector<std::function<void()>> on_change_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800234
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700235 std::unique_ptr<MemoryCGroup> memory_cgroup_;
236
payton.rehl2841b1c2023-05-25 17:23:55 -0700237 QuietLogging quiet_flag_ = QuietLogging::kNo;
238
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800239 DISALLOW_COPY_AND_ASSIGN(Application);
240};
241
242} // namespace aos::starter
James Kuszmauld42edb42022-01-07 18:00:16 -0800243#endif // AOS_STARTER_SUBPROCESS_H_