blob: 87479e8071001cd024eecc8dcca6ae36a0e74c42 [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
61 Application(std::string_view name, std::string_view executable_name,
James Kuszmaul3224b8e2022-01-07 19:00:39 -080062 aos::EventLoop *event_loop, std::function<void()> on_change);
63
64 flatbuffers::Offset<aos::starter::ApplicationStatus> PopulateStatus(
James Kuszmaul6295a642022-03-22 15:23:59 -070065 flatbuffers::FlatBufferBuilder *builder, util::Top *top);
James Kuszmauld42edb42022-01-07 18:00:16 -080066 aos::starter::State status() const { return status_; };
James Kuszmaul3224b8e2022-01-07 19:00:39 -080067
68 // Returns the last pid of this process. -1 if not started yet.
69 pid_t get_pid() const { return pid_; }
70
71 // Handles a SIGCHLD signal received by the parent. Does nothing if this
72 // process was not the target. Returns true if this Application should be
73 // removed.
74 bool MaybeHandleSignal();
James Kuszmauld42edb42022-01-07 18:00:16 -080075 void DisableChildDeathPolling() { child_status_handler_->Disable(); }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080076
77 // Handles a command. May do nothing if application is already in the desired
78 // state.
79 void HandleCommand(aos::starter::Command cmd);
80
81 void Start() { HandleCommand(aos::starter::Command::START); }
82
83 void Stop() { HandleCommand(aos::starter::Command::STOP); }
84
85 void Terminate();
86
James Kuszmauld42edb42022-01-07 18:00:16 -080087 void set_args(std::vector<std::string> args);
88 void set_capture_stdout(bool capture);
89 void set_capture_stderr(bool capture);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -070090 void set_run_as_sudo(bool value) { run_as_sudo_ = value; }
James Kuszmaul3224b8e2022-01-07 19:00:39 -080091
92 bool autostart() const { return autostart_; }
93
94 bool autorestart() const { return autorestart_; }
95
James Kuszmauld42edb42022-01-07 18:00:16 -080096 const std::string &GetStdout();
97 const std::string &GetStderr();
98 std::optional<int> exit_code() const { return exit_code_; }
99
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700100 // Sets the memory limit for the application to the provided limit.
101 void SetMemoryLimit(size_t limit) {
102 if (!memory_cgroup_) {
103 memory_cgroup_ = std::make_unique<MemoryCGroup>(name_);
104 }
105 memory_cgroup_->SetLimit("memory.limit_in_bytes", limit);
106 }
107
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800108 private:
James Kuszmauld42edb42022-01-07 18:00:16 -0800109 typedef aos::util::ScopedPipe::PipePair PipePair;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700110
111 static constexpr const char* const kSudo{"sudo"};
112
James Kuszmauld42edb42022-01-07 18:00:16 -0800113 void set_args(
114 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
115 &args);
116
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800117 void DoStart();
118
119 void DoStop(bool restart);
120
121 void QueueStart();
122
123 // Copy flatbuffer vector of strings to vector of std::string.
124 static std::vector<std::string> FbsVectorToVector(
125 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v);
126
127 static std::optional<uid_t> FindUid(const char *name);
128 static std::optional<gid_t> FindPrimaryGidForUser(const char *name);
129
James Kuszmauld42edb42022-01-07 18:00:16 -0800130 void FetchOutputs();
131
132 // Provides an std::vector of the args (such that CArgs().data() ends up being
133 // suitable to pass to execve()).
134 // The points are invalidated when args_ changes (e.g., due to a set_args
135 // call).
136 std::vector<char *> CArgs();
137
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800138 // Next unique id for all applications
139 static inline uint64_t next_id_ = 0;
140
141 std::string name_;
142 std::string path_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800143 std::vector<std::string> args_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800144 std::string user_name_;
145 std::optional<uid_t> user_;
146 std::optional<gid_t> group_;
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700147 bool run_as_sudo_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800148
James Kuszmauld42edb42022-01-07 18:00:16 -0800149 bool capture_stdout_ = false;
150 PipePair stdout_pipes_;
151 std::string stdout_;
152 bool capture_stderr_ = false;
153 PipePair stderr_pipes_;
154 std::string stderr_;
155
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800156 pid_t pid_ = -1;
James Kuszmauld42edb42022-01-07 18:00:16 -0800157 PipePair status_pipes_;
158 uint64_t id_ = 0;
159 std::optional<int> exit_code_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800160 aos::monotonic_clock::time_point start_time_, exit_time_;
161 bool queue_restart_ = false;
162 bool terminating_ = false;
James Kuszmauld42edb42022-01-07 18:00:16 -0800163 bool autostart_ = false;
164 bool autorestart_ = false;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800165
166 aos::starter::State status_ = aos::starter::State::STOPPED;
167 aos::starter::LastStopReason stop_reason_ =
168 aos::starter::LastStopReason::STOP_REQUESTED;
169
170 aos::EventLoop *event_loop_;
James Kuszmauld42edb42022-01-07 18:00:16 -0800171 aos::TimerHandler *start_timer_, *restart_timer_, *stop_timer_, *pipe_timer_,
172 *child_status_handler_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800173
174 std::function<void()> on_change_;
175
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700176 std::unique_ptr<MemoryCGroup> memory_cgroup_;
177
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800178 DISALLOW_COPY_AND_ASSIGN(Application);
179};
180
181} // namespace aos::starter
James Kuszmauld42edb42022-01-07 18:00:16 -0800182#endif // AOS_STARTER_SUBPROCESS_H_