blob: f5563e68c011d8252a8cffc6f0594ce77a6c7c44 [file] [log] [blame]
James Kuszmaul3224b8e2022-01-07 19:00:39 -08001#include "aos/starter/subprocess.h"
2
3#include <grp.h>
4#include <pwd.h>
5#include <sys/prctl.h>
6#include <sys/types.h>
7#include <sys/wait.h>
8
9#include "glog/logging.h"
10
James Kuszmaul8544c492023-07-31 15:00:38 -070011#include "aos/flatbuffer_merge.h"
12
James Kuszmaul3224b8e2022-01-07 19:00:39 -080013namespace aos::starter {
14
Austin Schuhbbeb37e2022-08-17 16:19:27 -070015// RAII class to become root and restore back to the original user and group
16// afterwards.
17class Sudo {
18 public:
19 Sudo() {
20 // Save what we were.
21 PCHECK(getresuid(&ruid_, &euid_, &suid_) == 0);
22 PCHECK(getresgid(&rgid_, &egid_, &sgid_) == 0);
23
24 // Become root.
25 PCHECK(setresuid(/* ruid */ 0 /* root */, /* euid */ 0, /* suid */ 0) == 0)
26 << ": Failed to become root";
27 PCHECK(setresgid(/* ruid */ 0 /* root */, /* euid */ 0, /* suid */ 0) == 0)
28 << ": Failed to become root";
29 }
30
31 ~Sudo() {
32 // And recover.
33 PCHECK(setresgid(rgid_, egid_, sgid_) == 0);
34 PCHECK(setresuid(ruid_, euid_, suid_) == 0);
35 }
36
37 uid_t ruid_, euid_, suid_;
38 gid_t rgid_, egid_, sgid_;
39};
40
Austin Schuh77e20a32023-08-01 12:25:03 -070041MemoryCGroup::MemoryCGroup(std::string_view name, Create should_create)
42 : cgroup_(absl::StrCat("/sys/fs/cgroup/memory/aos_", name)),
43 should_create_(should_create) {
44 if (should_create_ == Create::kDoCreate) {
45 Sudo sudo;
46 int ret = mkdir(cgroup_.c_str(), 0755);
Austin Schuhbbeb37e2022-08-17 16:19:27 -070047
Austin Schuh77e20a32023-08-01 12:25:03 -070048 if (ret != 0) {
49 if (errno == EEXIST) {
50 PCHECK(rmdir(cgroup_.c_str()) == 0)
51 << ": Failed to remove previous cgroup " << cgroup_;
52 ret = mkdir(cgroup_.c_str(), 0755);
53 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070054 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070055
Austin Schuh77e20a32023-08-01 12:25:03 -070056 if (ret != 0) {
57 PLOG(FATAL) << ": Failed to create cgroup aos_" << cgroup_
58 << ", do you have permission?";
59 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070060 }
61}
62
63void MemoryCGroup::AddTid(pid_t pid) {
64 if (pid == 0) {
65 pid = getpid();
66 }
Austin Schuh77e20a32023-08-01 12:25:03 -070067 if (should_create_ == Create::kDoCreate) {
68 Sudo sudo;
69 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/tasks").c_str(),
70 std::to_string(pid));
71 } else {
72 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/tasks").c_str(),
73 std::to_string(pid));
74 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070075}
76
77void MemoryCGroup::SetLimit(std::string_view limit_name, uint64_t limit_value) {
Austin Schuh77e20a32023-08-01 12:25:03 -070078 if (should_create_ == Create::kDoCreate) {
79 Sudo sudo;
80 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/", limit_name).c_str(),
81 std::to_string(limit_value));
82 } else {
83 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/", limit_name).c_str(),
84 std::to_string(limit_value));
85 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070086}
87
88MemoryCGroup::~MemoryCGroup() {
Austin Schuh77e20a32023-08-01 12:25:03 -070089 if (should_create_ == Create::kDoCreate) {
90 Sudo sudo;
91 PCHECK(rmdir(absl::StrCat(cgroup_).c_str()) == 0);
92 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -070093}
94
James Kuszmaul3224b8e2022-01-07 19:00:39 -080095SignalListener::SignalListener(aos::ShmEventLoop *loop,
96 std::function<void(signalfd_siginfo)> callback)
Austin Schuh1cea9032023-07-10 11:56:40 -070097 : SignalListener(loop->epoll(), std::move(callback)) {}
98
99SignalListener::SignalListener(aos::internal::EPoll *epoll,
100 std::function<void(signalfd_siginfo)> callback)
101 : SignalListener(epoll, callback,
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800102 {SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGFPE, SIGSEGV,
103 SIGPIPE, SIGTERM, SIGBUS, SIGXCPU, SIGCHLD}) {}
104
105SignalListener::SignalListener(aos::ShmEventLoop *loop,
106 std::function<void(signalfd_siginfo)> callback,
107 std::initializer_list<unsigned int> signals)
Austin Schuh1cea9032023-07-10 11:56:40 -0700108 : SignalListener(loop->epoll(), std::move(callback), std::move(signals)) {}
109
110SignalListener::SignalListener(aos::internal::EPoll *epoll,
111 std::function<void(signalfd_siginfo)> callback,
112 std::initializer_list<unsigned int> signals)
113 : epoll_(epoll), callback_(std::move(callback)), signalfd_(signals) {
114 epoll_->OnReadable(signalfd_.fd(), [this] {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800115 signalfd_siginfo info = signalfd_.Read();
116
117 if (info.ssi_signo == 0) {
118 LOG(WARNING) << "Could not read " << sizeof(signalfd_siginfo) << " bytes";
119 return;
120 }
121
122 callback_(info);
123 });
124}
125
Austin Schuh1cea9032023-07-10 11:56:40 -0700126SignalListener::~SignalListener() { epoll_->DeleteFd(signalfd_.fd()); }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800127
James Kuszmauld42edb42022-01-07 18:00:16 -0800128Application::Application(std::string_view name,
129 std::string_view executable_name,
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800130 aos::EventLoop *event_loop,
payton.rehl2841b1c2023-05-25 17:23:55 -0700131 std::function<void()> on_change,
132 QuietLogging quiet_flag)
James Kuszmauld42edb42022-01-07 18:00:16 -0800133 : name_(name),
134 path_(executable_name),
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800135 event_loop_(event_loop),
136 start_timer_(event_loop_->AddTimer([this] {
137 status_ = aos::starter::State::RUNNING;
payton.rehl2841b1c2023-05-25 17:23:55 -0700138 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
139 << "Started '" << name_ << "' pid: " << pid_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800140 })),
141 restart_timer_(event_loop_->AddTimer([this] { DoStart(); })),
142 stop_timer_(event_loop_->AddTimer([this] {
143 if (kill(pid_, SIGKILL) == 0) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700144 LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
145 << "Failed to stop, sending SIGKILL to '" << name_
146 << "' pid: " << pid_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800147 }
148 })),
James Kuszmauld42edb42022-01-07 18:00:16 -0800149 pipe_timer_(event_loop_->AddTimer([this]() { FetchOutputs(); })),
150 child_status_handler_(
151 event_loop_->AddTimer([this]() { MaybeHandleSignal(); })),
Austin Schuh1cea9032023-07-10 11:56:40 -0700152 on_change_({on_change}),
payton.rehl2841b1c2023-05-25 17:23:55 -0700153 quiet_flag_(quiet_flag) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800154 event_loop_->OnRun([this]() {
155 // Every second poll to check if the child is dead. This is used as a
156 // default for the case where the user is not directly catching SIGCHLD and
157 // calling MaybeHandleSignal for us.
Philipp Schradera6712522023-07-05 20:25:11 -0700158 child_status_handler_->Schedule(event_loop_->monotonic_now(),
159 std::chrono::seconds(1));
James Kuszmauld42edb42022-01-07 18:00:16 -0800160 });
161}
162
163Application::Application(const aos::Application *application,
164 aos::EventLoop *event_loop,
payton.rehl2841b1c2023-05-25 17:23:55 -0700165 std::function<void()> on_change,
166 QuietLogging quiet_flag)
James Kuszmauld42edb42022-01-07 18:00:16 -0800167 : Application(application->name()->string_view(),
168 application->has_executable_name()
169 ? application->executable_name()->string_view()
170 : application->name()->string_view(),
payton.rehl2841b1c2023-05-25 17:23:55 -0700171 event_loop, on_change, quiet_flag) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800172 user_name_ = application->has_user() ? application->user()->str() : "";
173 user_ = application->has_user() ? FindUid(user_name_.c_str()) : std::nullopt;
174 group_ = application->has_user() ? FindPrimaryGidForUser(user_name_.c_str())
175 : std::nullopt;
176 autostart_ = application->autostart();
177 autorestart_ = application->autorestart();
178 if (application->has_args()) {
179 set_args(*application->args());
180 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700181
182 if (application->has_memory_limit() && application->memory_limit() > 0) {
183 SetMemoryLimit(application->memory_limit());
184 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800185}
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800186
187void Application::DoStart() {
188 if (status_ != aos::starter::State::WAITING) {
189 return;
190 }
191
192 start_timer_->Disable();
193 restart_timer_->Disable();
194
James Kuszmauld42edb42022-01-07 18:00:16 -0800195 status_pipes_ = util::ScopedPipe::MakePipe();
196
197 if (capture_stdout_) {
198 stdout_pipes_ = util::ScopedPipe::MakePipe();
199 stdout_.clear();
200 }
201 if (capture_stderr_) {
202 stderr_pipes_ = util::ScopedPipe::MakePipe();
203 stderr_.clear();
204 }
205
Philipp Schradera6712522023-07-05 20:25:11 -0700206 pipe_timer_->Schedule(event_loop_->monotonic_now(),
207 std::chrono::milliseconds(100));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800208
209 const pid_t pid = fork();
210
211 if (pid != 0) {
212 if (pid == -1) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700213 PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
214 << "Failed to fork '" << name_ << "'";
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800215 stop_reason_ = aos::starter::LastStopReason::FORK_ERR;
216 status_ = aos::starter::State::STOPPED;
217 } else {
218 pid_ = pid;
219 id_ = next_id_++;
220 start_time_ = event_loop_->monotonic_now();
221 status_ = aos::starter::State::STARTING;
James Kuszmaul8544c492023-07-31 15:00:38 -0700222 latest_timing_report_version_.reset();
payton.rehl2841b1c2023-05-25 17:23:55 -0700223 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
224 << "Starting '" << name_ << "' pid " << pid_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800225
Philipp Schradera6712522023-07-05 20:25:11 -0700226 // Set up timer which moves application to RUNNING state if it is still
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800227 // alive in 1 second.
Philipp Schradera6712522023-07-05 20:25:11 -0700228 start_timer_->Schedule(event_loop_->monotonic_now() +
229 std::chrono::seconds(1));
James Kuszmauld42edb42022-01-07 18:00:16 -0800230 // Since we are the parent process, clear our write-side of all the pipes.
231 status_pipes_.write.reset();
232 stdout_pipes_.write.reset();
233 stderr_pipes_.write.reset();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800234 }
Austin Schuh1cea9032023-07-10 11:56:40 -0700235 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800236 return;
237 }
238
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700239 if (memory_cgroup_) {
240 memory_cgroup_->AddTid();
241 }
242
James Kuszmauld42edb42022-01-07 18:00:16 -0800243 // Since we are the child process, clear our read-side of all the pipes.
244 status_pipes_.read.reset();
245 stdout_pipes_.read.reset();
246 stderr_pipes_.read.reset();
247
248 // The status pipe will not be needed if the execve succeeds.
249 status_pipes_.write->SetCloexec();
250
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800251 // Clear out signal mask of parent so forked process receives all signals
252 // normally.
253 sigset_t empty_mask;
254 sigemptyset(&empty_mask);
255 sigprocmask(SIG_SETMASK, &empty_mask, nullptr);
256
257 // Cleanup children if starter dies in a way that is not handled gracefully.
258 if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800259 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800260 static_cast<uint32_t>(aos::starter::LastStopReason::SET_PRCTL_ERR));
261 PLOG(FATAL) << "Could not set PR_SET_PDEATHSIG to SIGKILL";
262 }
263
264 if (group_) {
265 CHECK(!user_name_.empty());
266 // The manpage for setgroups says we just need CAP_SETGID, but empirically
267 // we also need the effective UID to be 0 to make it work. user_ must also
268 // be set so we change this effective UID back later.
269 CHECK(user_);
270 if (seteuid(0) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800271 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800272 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
273 PLOG(FATAL) << "Could not seteuid(0) for " << name_
274 << " in preparation for setting groups";
275 }
276 if (initgroups(user_name_.c_str(), *group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800277 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800278 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
279 PLOG(FATAL) << "Could not initialize normal groups for " << name_
280 << " as " << user_name_ << " with " << *group_;
281 }
282 if (setgid(*group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800283 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800284 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
285 PLOG(FATAL) << "Could not set group for " << name_ << " to " << *group_;
286 }
287 }
288
289 if (user_) {
290 if (setuid(*user_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800291 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800292 static_cast<uint32_t>(aos::starter::LastStopReason::SET_USR_ERR));
293 PLOG(FATAL) << "Could not set user for " << name_ << " to " << *user_;
294 }
295 }
296
James Kuszmauld42edb42022-01-07 18:00:16 -0800297 if (capture_stdout_) {
298 PCHECK(STDOUT_FILENO == dup2(stdout_pipes_.write->fd(), STDOUT_FILENO));
299 stdout_pipes_.write.reset();
300 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800301
James Kuszmauld42edb42022-01-07 18:00:16 -0800302 if (capture_stderr_) {
303 PCHECK(STDERR_FILENO == dup2(stderr_pipes_.write->fd(), STDERR_FILENO));
304 stderr_pipes_.write.reset();
305 }
306
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700307 if (run_as_sudo_) {
Sarah Newman6d1e53b2022-08-09 14:38:08 -0700308 // For sudo we must supply the actual path
309 args_.insert(args_.begin(), path_);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700310 args_.insert(args_.begin(), kSudo);
Sarah Newman6d1e53b2022-08-09 14:38:08 -0700311 } else {
312 // argv[0] should be the program name
313 args_.insert(args_.begin(), name_);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700314 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800315
316 std::vector<char *> cargs = CArgs();
Philipp Schrader790cb542023-07-05 21:06:52 -0700317 const char *path = run_as_sudo_ ? kSudo : path_.c_str();
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700318 execvp(path, cargs.data());
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800319
320 // If we got here, something went wrong
James Kuszmauld42edb42022-01-07 18:00:16 -0800321 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800322 static_cast<uint32_t>(aos::starter::LastStopReason::EXECV_ERR));
payton.rehl2841b1c2023-05-25 17:23:55 -0700323 PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
324 << "Could not execute " << name_ << " (" << path_ << ')';
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800325
326 _exit(EXIT_FAILURE);
327}
328
James Kuszmaul8544c492023-07-31 15:00:38 -0700329void Application::ObserveTimingReport(
330 const aos::monotonic_clock::time_point send_time,
331 const aos::timing::Report *msg) {
332 if (msg->name()->string_view() == name_ && msg->pid() == pid_ &&
333 msg->has_version()) {
334 latest_timing_report_version_ = msg->version()->str();
335 last_timing_report_ = send_time;
336 }
337}
338
James Kuszmauld42edb42022-01-07 18:00:16 -0800339void Application::FetchOutputs() {
340 if (capture_stdout_) {
341 stdout_pipes_.read->Read(&stdout_);
342 }
343 if (capture_stderr_) {
344 stderr_pipes_.read->Read(&stderr_);
345 }
346}
347
348const std::string &Application::GetStdout() {
349 CHECK(capture_stdout_);
350 FetchOutputs();
351 return stdout_;
352}
353
354const std::string &Application::GetStderr() {
355 CHECK(capture_stderr_);
356 FetchOutputs();
357 return stderr_;
358}
359
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800360void Application::DoStop(bool restart) {
361 // If stop or restart received, the old state of these is no longer applicable
362 // so cancel both.
363 restart_timer_->Disable();
364 start_timer_->Disable();
365
James Kuszmauld42edb42022-01-07 18:00:16 -0800366 FetchOutputs();
367
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800368 switch (status_) {
369 case aos::starter::State::STARTING:
370 case aos::starter::State::RUNNING: {
payton.rehl2841b1c2023-05-25 17:23:55 -0700371 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
372 << "Stopping '" << name_ << "' pid: " << pid_ << " with signal "
373 << SIGINT;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800374 status_ = aos::starter::State::STOPPING;
375
376 kill(pid_, SIGINT);
377
378 // Watchdog timer to SIGKILL application if it is still running 1 second
379 // after SIGINT
Philipp Schradera6712522023-07-05 20:25:11 -0700380 stop_timer_->Schedule(event_loop_->monotonic_now() +
381 std::chrono::seconds(1));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800382 queue_restart_ = restart;
Austin Schuh1cea9032023-07-10 11:56:40 -0700383 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800384 break;
385 }
386 case aos::starter::State::WAITING: {
387 // If waiting to restart, and receives restart, skip the waiting period
388 // and restart immediately. If stop received, all we have to do is move
389 // to the STOPPED state.
390 if (restart) {
391 DoStart();
392 } else {
393 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700394 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800395 }
396 break;
397 }
398 case aos::starter::State::STOPPING: {
399 // If the application is already stopping, then we just need to update the
400 // restart flag to the most recent status.
401 queue_restart_ = restart;
402 break;
403 }
404 case aos::starter::State::STOPPED: {
405 // Restart immediately if the application is already stopped
406 if (restart) {
407 status_ = aos::starter::State::WAITING;
408 DoStart();
409 }
410 break;
411 }
412 }
413}
414
415void Application::QueueStart() {
416 status_ = aos::starter::State::WAITING;
417
payton.rehl2841b1c2023-05-25 17:23:55 -0700418 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
419 << "Restarting " << name_ << " in 3 seconds";
Philipp Schradera6712522023-07-05 20:25:11 -0700420 restart_timer_->Schedule(event_loop_->monotonic_now() +
421 std::chrono::seconds(3));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800422 start_timer_->Disable();
423 stop_timer_->Disable();
Austin Schuh1cea9032023-07-10 11:56:40 -0700424 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800425}
426
James Kuszmauld42edb42022-01-07 18:00:16 -0800427std::vector<char *> Application::CArgs() {
428 std::vector<char *> cargs;
429 std::transform(args_.begin(), args_.end(), std::back_inserter(cargs),
430 [](std::string &str) { return str.data(); });
431 cargs.push_back(nullptr);
432 return cargs;
433}
434
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800435void Application::set_args(
436 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v) {
437 args_.clear();
438 std::transform(v.begin(), v.end(), std::back_inserter(args_),
James Kuszmauld42edb42022-01-07 18:00:16 -0800439 [](const flatbuffers::String *str) { return str->str(); });
440}
441
442void Application::set_args(std::vector<std::string> args) {
443 args_ = std::move(args);
444}
445
446void Application::set_capture_stdout(bool capture) {
447 capture_stdout_ = capture;
448}
449
450void Application::set_capture_stderr(bool capture) {
451 capture_stderr_ = capture;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800452}
453
454std::optional<uid_t> Application::FindUid(const char *name) {
455 // TODO(austin): Use the reentrant version. This should be safe.
456 struct passwd *user_data = getpwnam(name);
457 if (user_data != nullptr) {
458 return user_data->pw_uid;
459 } else {
460 LOG(FATAL) << "Could not find user " << name;
461 return std::nullopt;
462 }
463}
464
465std::optional<gid_t> Application::FindPrimaryGidForUser(const char *name) {
466 // TODO(austin): Use the reentrant version. This should be safe.
467 struct passwd *user_data = getpwnam(name);
468 if (user_data != nullptr) {
469 return user_data->pw_gid;
470 } else {
471 LOG(FATAL) << "Could not find user " << name;
472 return std::nullopt;
473 }
474}
475
476flatbuffers::Offset<aos::starter::ApplicationStatus>
James Kuszmaul6295a642022-03-22 15:23:59 -0700477Application::PopulateStatus(flatbuffers::FlatBufferBuilder *builder,
478 util::Top *top) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800479 CHECK_NOTNULL(builder);
480 auto name_fbs = builder->CreateString(name_);
481
James Kuszmaul6295a642022-03-22 15:23:59 -0700482 const bool valid_pid = pid_ > 0 && status_ != aos::starter::State::STOPPED;
483 const flatbuffers::Offset<util::ProcessInfo> process_info =
484 valid_pid ? top->InfoForProcess(builder, pid_)
485 : flatbuffers::Offset<util::ProcessInfo>();
486
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800487 aos::starter::ApplicationStatus::Builder status_builder(*builder);
488 status_builder.add_name(name_fbs);
489 status_builder.add_state(status_);
James Kuszmauld42edb42022-01-07 18:00:16 -0800490 if (exit_code_.has_value()) {
491 status_builder.add_last_exit_code(exit_code_.value());
492 }
James Kuszmaul8544c492023-07-31 15:00:38 -0700493 status_builder.add_has_active_timing_report(
494 last_timing_report_ +
495 // Leave a bit of margin on the timing report receipt time, to allow
496 // for timing errors.
497 3 * std::chrono::milliseconds(FLAGS_timing_report_ms) >
498 event_loop_->monotonic_now());
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800499 status_builder.add_last_stop_reason(stop_reason_);
500 if (pid_ != -1) {
501 status_builder.add_pid(pid_);
502 status_builder.add_id(id_);
503 }
James Kuszmaul6295a642022-03-22 15:23:59 -0700504 // Note that even if process_info is null, calling add_process_info is fine.
505 status_builder.add_process_info(process_info);
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800506 status_builder.add_last_start_time(start_time_.time_since_epoch().count());
507 return status_builder.Finish();
508}
509
510void Application::Terminate() {
511 stop_reason_ = aos::starter::LastStopReason::TERMINATE;
512 DoStop(false);
513 terminating_ = true;
514}
515
516void Application::HandleCommand(aos::starter::Command cmd) {
517 switch (cmd) {
518 case aos::starter::Command::START: {
519 switch (status_) {
520 case aos::starter::State::WAITING: {
521 restart_timer_->Disable();
522 DoStart();
523 break;
524 }
525 case aos::starter::State::STARTING: {
526 break;
527 }
528 case aos::starter::State::RUNNING: {
529 break;
530 }
531 case aos::starter::State::STOPPING: {
532 queue_restart_ = true;
533 break;
534 }
535 case aos::starter::State::STOPPED: {
536 status_ = aos::starter::State::WAITING;
537 DoStart();
538 break;
539 }
540 }
541 break;
542 }
543 case aos::starter::Command::STOP: {
544 stop_reason_ = aos::starter::LastStopReason::STOP_REQUESTED;
545 DoStop(false);
546 break;
547 }
548 case aos::starter::Command::RESTART: {
549 stop_reason_ = aos::starter::LastStopReason::RESTART_REQUESTED;
550 DoStop(true);
551 break;
552 }
553 }
554}
555
556bool Application::MaybeHandleSignal() {
557 int status;
558
Sarah Newman21c59202022-06-16 12:36:33 -0700559 if (status_ == aos::starter::State::WAITING ||
560 status_ == aos::starter::State::STOPPED) {
561 // We can't possibly have received a signal meant for this process.
562 return false;
563 }
564
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800565 // Check if the status of this process has changed
Sarah Newman21c59202022-06-16 12:36:33 -0700566 // The PID won't be -1 if this application has ever been run successfully
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800567 if (pid_ == -1 || waitpid(pid_, &status, WNOHANG) != pid_) {
568 return false;
569 }
570
571 // Check that the event was the process exiting
572 if (!WIFEXITED(status) && !WIFSIGNALED(status)) {
573 return false;
574 }
575
James Kuszmauld42edb42022-01-07 18:00:16 -0800576 start_timer_->Disable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800577 exit_time_ = event_loop_->monotonic_now();
578 exit_code_ = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status);
579
James Kuszmauld42edb42022-01-07 18:00:16 -0800580 if (auto read_result = status_pipes_.read->Read()) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800581 stop_reason_ = static_cast<aos::starter::LastStopReason>(*read_result);
582 }
583
584 switch (status_) {
585 case aos::starter::State::STARTING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800586 if (exit_code_.value() == 0) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700587 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
588 << "Application '" << name_ << "' pid " << pid_
589 << " exited with status " << exit_code_.value();
James Kuszmauld42edb42022-01-07 18:00:16 -0800590 } else {
payton.rehl2841b1c2023-05-25 17:23:55 -0700591 LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
592 << "Failed to start '" << name_ << "' on pid " << pid_
593 << " : Exited with status " << exit_code_.value();
James Kuszmauld42edb42022-01-07 18:00:16 -0800594 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800595 if (autorestart()) {
596 QueueStart();
597 } else {
598 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700599 OnChange();
James Kuszmaul6f10b382022-03-11 22:31:38 -0800600 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800601 break;
602 }
603 case aos::starter::State::RUNNING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800604 if (exit_code_.value() == 0) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700605 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
606 << "Application '" << name_ << "' pid " << pid_
607 << " exited with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800608 } else {
James Kuszmaul8544c492023-07-31 15:00:38 -0700609 if (quiet_flag_ == QuietLogging::kNo) {
610 std::string version_string =
611 latest_timing_report_version_.has_value()
612 ? absl::StrCat("'", latest_timing_report_version_.value(),
613 "'")
614 : "unknown";
615 LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
616 << "Application '" << name_ << "' pid " << pid_ << " version "
617 << version_string << " exited unexpectedly with status "
618 << exit_code_.value();
619 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800620 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800621 if (autorestart()) {
622 QueueStart();
623 } else {
624 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700625 OnChange();
James Kuszmaul6f10b382022-03-11 22:31:38 -0800626 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800627 break;
628 }
629 case aos::starter::State::STOPPING: {
payton.rehl2841b1c2023-05-25 17:23:55 -0700630 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
631 << "Successfully stopped '" << name_ << "' pid: " << pid_
632 << " with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800633 status_ = aos::starter::State::STOPPED;
634
635 // Disable force stop timer since the process already died
636 stop_timer_->Disable();
637
Austin Schuh1cea9032023-07-10 11:56:40 -0700638 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800639 if (terminating_) {
640 return true;
641 }
642
643 if (queue_restart_) {
644 queue_restart_ = false;
645 status_ = aos::starter::State::WAITING;
646 DoStart();
647 }
648 break;
649 }
650 case aos::starter::State::WAITING:
651 case aos::starter::State::STOPPED: {
Sarah Newman21c59202022-06-16 12:36:33 -0700652 __builtin_unreachable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800653 break;
654 }
655 }
656
657 return false;
658}
659
Austin Schuh1cea9032023-07-10 11:56:40 -0700660void Application::OnChange() {
661 for (auto &fn : on_change_) {
662 fn();
663 }
664}
665
Adam Snaider70deaf22023-08-11 13:58:34 -0700666Application::~Application() {
667 start_timer_->Disable();
668 restart_timer_->Disable();
669 stop_timer_->Disable();
670 pipe_timer_->Disable();
671 child_status_handler_->Disable();
672}
673
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800674} // namespace aos::starter