blob: 1f16168f62c242bcb34581ceefc531f28e78e57d [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);
24 SignalListener(aos::ShmEventLoop *loop,
25 std::function<void(signalfd_siginfo)> callback,
26 std::initializer_list<unsigned int> signals);
27
28 ~SignalListener();
29
30 private:
31 aos::ShmEventLoop *loop_;
32 std::function<void(signalfd_siginfo)> callback_;
33 aos::ipc_lib::SignalFd signalfd_;
34
35 DISALLOW_COPY_AND_ASSIGN(SignalListener);
36};
37
Austin Schuhbbeb37e2022-08-17 16:19:27 -070038// Class to use the V1 cgroup API to limit memory usage.
39class MemoryCGroup {
40 public:
41 MemoryCGroup(std::string_view name);
42 ~MemoryCGroup();
43
44 // Adds a thread ID to be managed by the cgroup.
45 void AddTid(pid_t pid = 0);
46
47 // Sets the provided limit to the provided value.
48 void SetLimit(std::string_view limit_name, uint64_t limit_value);
49
50 private:
51 std::string cgroup_;
52};
53
James Kuszmaul3224b8e2022-01-07 19:00:39 -080054// Manages a running process, allowing starting and stopping, and restarting
55// automatically.
56class Application {
57 public:
James Kuszmauld42edb42022-01-07 18:00:16 -080058 Application(const aos::Application *application, aos::EventLoop *event_loop,
59 std::function<void()> on_change);
60
Sarah Newman2c1b1212022-08-10 10:05:48 -070061 // executable_name is the actual executable path.
62 // When sudo is not used, name is used as argv[0] when exec'ing
63 // executable_name. When sudo is used it's not possible to pass in a
64 // distinct argv[0].
James Kuszmauld42edb42022-01-07 18:00:16 -080065 Application(std::string_view name, std::string_view executable_name,
James Kuszmaul3224b8e2022-01-07 19:00:39 -080066 aos::EventLoop *event_loop, std::function<void()> on_change);
67
68 flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus(
James Kuszmaul6295a642022-03-22 15:23:59 -070069 flatbuffers::FlatBufferBuilder *builder, util::Top *top);
James Kuszmauld42edb42022-01-07 18:00:16 -080070 aos::starter::State status() const { return status_; };
James Kuszmaul3224b8e2022-01-07 19:00:39 -080071
72 // Returns the last pid of this process. -1 if not started yet.
73 pid_t get_pid() const { return pid_; }
74
75 // Handles a SIGCHLD signal received by the parent. Does nothing if this
76 // process was not the target. Returns true if this Application should be
77 // removed.
78 bool MaybeHandleSignal();
James Kuszmauld42edb42022-01-07 18:00:16 -080079 void DisableChildDeathPolling() { child_status_handler_->Disable(); }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080080
81 // Handles a command. May do nothing if application is already in the desired
82 // state.
83 void HandleCommand(aos::starter::Command cmd);
84
85 void Start() { HandleCommand(aos::starter::Command::START); }
86
87 void Stop() { HandleCommand(aos::starter::Command::STOP); }
88
89 void Terminate();
90
James Kuszmauld42edb42022-01-07 18:00:16 -080091 void set_args(std::vector<std::string> args);
92 void set_capture_stdout(bool capture);
93 void set_capture_stderr(bool capture);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -070094 void set_run_as_sudo(bool value) { run_as_sudo_ = value; }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080095
96 bool autostart() const { return autostart_; }
97
98 bool autorestart() const { return autorestart_; }
99
James Kuszmauld42edb42022-01-07 18:00:16 -0800100 const std::string &GetStdout();
101 const std::string &GetStderr();
102 std::optional<int> exit_code() const { return exit_code_; }
103
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700104 // Sets the memory limit for the application to the provided limit.
105 void SetMemoryLimit(size_t limit) {
106 if (!memory_cgroup_) {
107 memory_cgroup_ = std::make_unique<MemoryCGroup>(name_);
108 }
109 memory_cgroup_->SetLimit("memory.limit_in_bytes", limit);
110 }
111
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800112 private:
James Kuszmauld42edb42022-01-07 18:00:16 -0800113 typedef aos::util::ScopedPipe::PipePair PipePair;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700114
Philipp Schrader790cb542023-07-05 21:06:52 -0700115 static constexpr const char *const kSudo{"sudo"};
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700116
James Kuszmauld42edb42022-01-07 18:00:16 -0800117 void set_args(
118 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
119 &args);
120
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800121 void DoStart();
122
123 void DoStop(bool restart);
124
125 void QueueStart();
126
127 // Copy flatbuffer vector of strings to vector of std::string.
128 static std::vector<std::string> FbsVectorToVector(
129 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v);
130
131 static std::optional<uid_t> FindUid(const char *name);
132 static std::optional<gid_t> FindPrimaryGidForUser(const char *name);
133
James Kuszmauld42edb42022-01-07 18:00:16 -0800134 void FetchOutputs();
135
136 // Provides an std::vector of the args (such that CArgs().data() ends up being
137 // suitable to pass to execve()).
138 // The points are invalidated when args_ changes (e.g., due to a set_args
139 // call).
140 std::vector<char *> CArgs();
141
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800142 // Next unique id for all applications
143 static inline uint64_t next_id_ = 0;
144
145 std::string name_;
146 std::string path_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800147 std::vector<std::string> args_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800148 std::string user_name_;
149 std::optional<uid_t> user_;
150 std::optional<gid_t> group_;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700151 bool run_as_sudo_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800152
James Kuszmauld42edb42022-01-07 18:00:16 -0800153 bool capture_stdout_ = false;
154 PipePair stdout_pipes_;
155 std::string stdout_;
156 bool capture_stderr_ = false;
157 PipePair stderr_pipes_;
158 std::string stderr_;
159
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800160 pid_t pid_ = -1;
James Kuszmauld42edb42022-01-07 18:00:16 -0800161 PipePair status_pipes_;
162 uint64_t id_ = 0;
163 std::optional<int> exit_code_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800164 aos::monotonic_clock::time_point start_time_, exit_time_;
165 bool queue_restart_ = false;
166 bool terminating_ = false;
James Kuszmauld42edb42022-01-07 18:00:16 -0800167 bool autostart_ = false;
168 bool autorestart_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800169
170 aos::starter::State status_ = aos::starter::State::STOPPED;
171 aos::starter::LastStopReason stop_reason_ =
172 aos::starter::LastStopReason::STOP_REQUESTED;
173
174 aos::EventLoop *event_loop_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800175 aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_, *pipe_timer_,
176 *child_status_handler_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800177
178 std::function<void()> on_change_;
179
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700180 std::unique_ptr<MemoryCGroup> memory_cgroup_;
181
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800182 DISALLOW_COPY_AND_ASSIGN(Application);
183};
184
185} // namespace aos::starter
James Kuszmauld42edb42022-01-07 18:00:16 -0800186#endif // AOS_STARTER_SUBPROCESS_H_