blob: 1efd56d550155a0dae5f1e155b6f41001f361022 [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:
46 MemoryCGroup(std::string_view name);
47 ~MemoryCGroup();
48
49 // Adds a thread ID to be managed by the cgroup.
50 void AddTid(pid_t pid = 0);
51
52 // Sets the provided limit to the provided value.
53 void SetLimit(std::string_view limit_name, uint64_t limit_value);
54
55 private:
56 std::string cgroup_;
57};
58
James Kuszmaul3224b8e2022-01-07 19:00:39 -080059// Manages a running process, allowing starting and stopping, and restarting
60// automatically.
61class Application {
62 public:
payton.rehl2841b1c2023-05-25 17:23:55 -070063 enum class QuietLogging { kYes, kNo };
James Kuszmauld42edb42022-01-07 18:00:16 -080064 Application(const aos::Application *application, aos::EventLoop *event_loop,
payton.rehl2841b1c2023-05-25 17:23:55 -070065 std::function<void()> on_change,
66 QuietLogging quiet_flag = QuietLogging::kNo);
James Kuszmauld42edb42022-01-07 18:00:16 -080067
Sarah Newman2c1b1212022-08-10 10:05:48 -070068 // executable_name is the actual executable path.
69 // When sudo is not used, name is used as argv[0] when exec'ing
70 // executable_name. When sudo is used it's not possible to pass in a
71 // distinct argv[0].
James Kuszmauld42edb42022-01-07 18:00:16 -080072 Application(std::string_view name, std::string_view executable_name,
payton.rehl2841b1c2023-05-25 17:23:55 -070073 aos::EventLoop *event_loop, std::function<void()> on_change,
74 QuietLogging quiet_flag = QuietLogging::kNo);
James Kuszmaul3224b8e2022-01-07 19:00:39 -080075
76 flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus(
James Kuszmaul6295a642022-03-22 15:23:59 -070077 flatbuffers::FlatBufferBuilder *builder, util::Top *top);
James Kuszmauld42edb42022-01-07 18:00:16 -080078 aos::starter::State status() const { return status_; };
James Kuszmaul3224b8e2022-01-07 19:00:39 -080079
80 // Returns the last pid of this process. -1 if not started yet.
81 pid_t get_pid() const { return pid_; }
82
83 // Handles a SIGCHLD signal received by the parent. Does nothing if this
84 // process was not the target. Returns true if this Application should be
85 // removed.
86 bool MaybeHandleSignal();
James Kuszmauld42edb42022-01-07 18:00:16 -080087 void DisableChildDeathPolling() { child_status_handler_->Disable(); }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080088
89 // Handles a command. May do nothing if application is already in the desired
90 // state.
91 void HandleCommand(aos::starter::Command cmd);
92
93 void Start() { HandleCommand(aos::starter::Command::START); }
94
95 void Stop() { HandleCommand(aos::starter::Command::STOP); }
96
97 void Terminate();
98
Austin Schuh1cea9032023-07-10 11:56:40 -070099 // Adds a callback which gets notified when the application changes state.
100 // This is in addition to any existing callbacks and doesn't replace any of
101 // them.
102 void AddOnChange(std::function<void()> fn) {
103 on_change_.emplace_back(std::move(fn));
104 }
105
James Kuszmauld42edb42022-01-07 18:00:16 -0800106 void set_args(std::vector<std::string> args);
107 void set_capture_stdout(bool capture);
108 void set_capture_stderr(bool capture);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700109 void set_run_as_sudo(bool value) { run_as_sudo_ = value; }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800110
111 bool autostart() const { return autostart_; }
112
113 bool autorestart() const { return autorestart_; }
114
James Kuszmauld42edb42022-01-07 18:00:16 -0800115 const std::string &GetStdout();
116 const std::string &GetStderr();
117 std::optional<int> exit_code() const { return exit_code_; }
118
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700119 // Sets the memory limit for the application to the provided limit.
120 void SetMemoryLimit(size_t limit) {
121 if (!memory_cgroup_) {
122 memory_cgroup_ = std::make_unique<MemoryCGroup>(name_);
123 }
124 memory_cgroup_->SetLimit("memory.limit_in_bytes", limit);
125 }
126
James Kuszmaul8544c492023-07-31 15:00:38 -0700127 // Observe a timing report message, and save it if it is relevant to us.
128 // It is the responsibility of the caller to manage this, because the lifetime
129 // of the Application itself is such that it cannot own Fetchers readily.
130 void ObserveTimingReport(const aos::monotonic_clock::time_point send_time,
131 const aos::timing::Report *msg);
132
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800133 private:
James Kuszmauld42edb42022-01-07 18:00:16 -0800134 typedef aos::util::ScopedPipe::PipePair PipePair;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700135
Philipp Schrader790cb542023-07-05 21:06:52 -0700136 static constexpr const char *const kSudo{"sudo"};
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700137
James Kuszmauld42edb42022-01-07 18:00:16 -0800138 void set_args(
139 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
140 &args);
141
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800142 void DoStart();
143
144 void DoStop(bool restart);
145
146 void QueueStart();
147
Austin Schuh1cea9032023-07-10 11:56:40 -0700148 void OnChange();
149
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800150 // Copy flatbuffer vector of strings to vector of std::string.
151 static std::vector<std::string> FbsVectorToVector(
152 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v);
153
154 static std::optional<uid_t> FindUid(const char *name);
155 static std::optional<gid_t> FindPrimaryGidForUser(const char *name);
156
James Kuszmauld42edb42022-01-07 18:00:16 -0800157 void FetchOutputs();
158
159 // Provides an std::vector of the args (such that CArgs().data() ends up being
160 // suitable to pass to execve()).
161 // The points are invalidated when args_ changes (e.g., due to a set_args
162 // call).
163 std::vector<char *> CArgs();
164
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800165 // Next unique id for all applications
166 static inline uint64_t next_id_ = 0;
167
168 std::string name_;
169 std::string path_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800170 std::vector<std::string> args_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800171 std::string user_name_;
172 std::optional<uid_t> user_;
173 std::optional<gid_t> group_;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700174 bool run_as_sudo_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800175
James Kuszmauld42edb42022-01-07 18:00:16 -0800176 bool capture_stdout_ = false;
177 PipePair stdout_pipes_;
178 std::string stdout_;
179 bool capture_stderr_ = false;
180 PipePair stderr_pipes_;
181 std::string stderr_;
182
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800183 pid_t pid_ = -1;
James Kuszmauld42edb42022-01-07 18:00:16 -0800184 PipePair status_pipes_;
185 uint64_t id_ = 0;
186 std::optional<int> exit_code_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800187 aos::monotonic_clock::time_point start_time_, exit_time_;
188 bool queue_restart_ = false;
189 bool terminating_ = false;
James Kuszmauld42edb42022-01-07 18:00:16 -0800190 bool autostart_ = false;
191 bool autorestart_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800192
193 aos::starter::State status_ = aos::starter::State::STOPPED;
194 aos::starter::LastStopReason stop_reason_ =
195 aos::starter::LastStopReason::STOP_REQUESTED;
196
197 aos::EventLoop *event_loop_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800198 aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_, *pipe_timer_,
199 *child_status_handler_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800200
James Kuszmaul8544c492023-07-31 15:00:38 -0700201 // Version string from the most recent valid timing report for this
202 // application. Cleared when the application restarts.
203 std::optional<std::string> latest_timing_report_version_;
204 aos::monotonic_clock::time_point last_timing_report_ =
205 aos::monotonic_clock::min_time;
206
Austin Schuh1cea9032023-07-10 11:56:40 -0700207 std::vector<std::function<void()>> on_change_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800208
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700209 std::unique_ptr<MemoryCGroup> memory_cgroup_;
210
payton.rehl2841b1c2023-05-25 17:23:55 -0700211 QuietLogging quiet_flag_ = QuietLogging::kNo;
212
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800213 DISALLOW_COPY_AND_ASSIGN(Application);
214};
215
216} // namespace aos::starter
James Kuszmauld42edb42022-01-07 18:00:16 -0800217#endif // AOS_STARTER_SUBPROCESS_H_