blob: 7b5fce6d9b3515b3c7273d81053962d15d78c592 [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_;
Sarah Newman9687e062023-09-08 12:22:27 -0700147 } else {
148 PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
149 << "Failed to send SIGKILL to '" << name_ << "' pid: " << pid_;
150 stop_timer_->Schedule(event_loop_->monotonic_now() +
151 std::chrono::seconds(1));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800152 }
153 })),
James Kuszmauld42edb42022-01-07 18:00:16 -0800154 pipe_timer_(event_loop_->AddTimer([this]() { FetchOutputs(); })),
155 child_status_handler_(
156 event_loop_->AddTimer([this]() { MaybeHandleSignal(); })),
Austin Schuh1cea9032023-07-10 11:56:40 -0700157 on_change_({on_change}),
payton.rehl2841b1c2023-05-25 17:23:55 -0700158 quiet_flag_(quiet_flag) {
Sanjay Narayanan92fdc3d2023-08-25 14:42:56 -0700159 // Every second poll to check if the child is dead. This is used as a
160 // default for the case where the user is not directly catching SIGCHLD and
161 // calling MaybeHandleSignal for us.
162 child_status_handler_->Schedule(event_loop_->monotonic_now(),
163 std::chrono::seconds(1));
James Kuszmauld42edb42022-01-07 18:00:16 -0800164}
165
166Application::Application(const aos::Application *application,
167 aos::EventLoop *event_loop,
payton.rehl2841b1c2023-05-25 17:23:55 -0700168 std::function<void()> on_change,
169 QuietLogging quiet_flag)
James Kuszmauld42edb42022-01-07 18:00:16 -0800170 : Application(application->name()->string_view(),
171 application->has_executable_name()
172 ? application->executable_name()->string_view()
173 : application->name()->string_view(),
payton.rehl2841b1c2023-05-25 17:23:55 -0700174 event_loop, on_change, quiet_flag) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800175 user_name_ = application->has_user() ? application->user()->str() : "";
176 user_ = application->has_user() ? FindUid(user_name_.c_str()) : std::nullopt;
177 group_ = application->has_user() ? FindPrimaryGidForUser(user_name_.c_str())
178 : std::nullopt;
179 autostart_ = application->autostart();
180 autorestart_ = application->autorestart();
181 if (application->has_args()) {
182 set_args(*application->args());
183 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700184
185 if (application->has_memory_limit() && application->memory_limit() > 0) {
186 SetMemoryLimit(application->memory_limit());
187 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800188}
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800189
190void Application::DoStart() {
191 if (status_ != aos::starter::State::WAITING) {
192 return;
193 }
194
195 start_timer_->Disable();
196 restart_timer_->Disable();
197
James Kuszmauld42edb42022-01-07 18:00:16 -0800198 status_pipes_ = util::ScopedPipe::MakePipe();
199
200 if (capture_stdout_) {
201 stdout_pipes_ = util::ScopedPipe::MakePipe();
202 stdout_.clear();
203 }
204 if (capture_stderr_) {
205 stderr_pipes_ = util::ScopedPipe::MakePipe();
206 stderr_.clear();
207 }
208
Philipp Schradera6712522023-07-05 20:25:11 -0700209 pipe_timer_->Schedule(event_loop_->monotonic_now(),
210 std::chrono::milliseconds(100));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800211
212 const pid_t pid = fork();
213
214 if (pid != 0) {
215 if (pid == -1) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700216 PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
217 << "Failed to fork '" << name_ << "'";
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800218 stop_reason_ = aos::starter::LastStopReason::FORK_ERR;
219 status_ = aos::starter::State::STOPPED;
220 } else {
221 pid_ = pid;
222 id_ = next_id_++;
223 start_time_ = event_loop_->monotonic_now();
224 status_ = aos::starter::State::STARTING;
James Kuszmaul8544c492023-07-31 15:00:38 -0700225 latest_timing_report_version_.reset();
payton.rehl2841b1c2023-05-25 17:23:55 -0700226 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
227 << "Starting '" << name_ << "' pid " << pid_;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800228
Philipp Schradera6712522023-07-05 20:25:11 -0700229 // Set up timer which moves application to RUNNING state if it is still
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800230 // alive in 1 second.
Philipp Schradera6712522023-07-05 20:25:11 -0700231 start_timer_->Schedule(event_loop_->monotonic_now() +
232 std::chrono::seconds(1));
James Kuszmauld42edb42022-01-07 18:00:16 -0800233 // Since we are the parent process, clear our write-side of all the pipes.
234 status_pipes_.write.reset();
235 stdout_pipes_.write.reset();
236 stderr_pipes_.write.reset();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800237 }
Austin Schuh1cea9032023-07-10 11:56:40 -0700238 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800239 return;
240 }
241
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700242 if (memory_cgroup_) {
243 memory_cgroup_->AddTid();
244 }
245
James Kuszmauld42edb42022-01-07 18:00:16 -0800246 // Since we are the child process, clear our read-side of all the pipes.
247 status_pipes_.read.reset();
248 stdout_pipes_.read.reset();
249 stderr_pipes_.read.reset();
250
251 // The status pipe will not be needed if the execve succeeds.
252 status_pipes_.write->SetCloexec();
253
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800254 // Clear out signal mask of parent so forked process receives all signals
255 // normally.
256 sigset_t empty_mask;
257 sigemptyset(&empty_mask);
258 sigprocmask(SIG_SETMASK, &empty_mask, nullptr);
259
260 // Cleanup children if starter dies in a way that is not handled gracefully.
261 if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800262 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800263 static_cast<uint32_t>(aos::starter::LastStopReason::SET_PRCTL_ERR));
264 PLOG(FATAL) << "Could not set PR_SET_PDEATHSIG to SIGKILL";
265 }
266
267 if (group_) {
268 CHECK(!user_name_.empty());
269 // The manpage for setgroups says we just need CAP_SETGID, but empirically
270 // we also need the effective UID to be 0 to make it work. user_ must also
271 // be set so we change this effective UID back later.
272 CHECK(user_);
273 if (seteuid(0) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800274 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800275 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
276 PLOG(FATAL) << "Could not seteuid(0) for " << name_
277 << " in preparation for setting groups";
278 }
279 if (initgroups(user_name_.c_str(), *group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800280 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800281 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
282 PLOG(FATAL) << "Could not initialize normal groups for " << name_
283 << " as " << user_name_ << " with " << *group_;
284 }
285 if (setgid(*group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800286 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800287 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
288 PLOG(FATAL) << "Could not set group for " << name_ << " to " << *group_;
289 }
290 }
291
292 if (user_) {
293 if (setuid(*user_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800294 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800295 static_cast<uint32_t>(aos::starter::LastStopReason::SET_USR_ERR));
296 PLOG(FATAL) << "Could not set user for " << name_ << " to " << *user_;
297 }
298 }
299
James Kuszmauld42edb42022-01-07 18:00:16 -0800300 if (capture_stdout_) {
301 PCHECK(STDOUT_FILENO == dup2(stdout_pipes_.write->fd(), STDOUT_FILENO));
302 stdout_pipes_.write.reset();
303 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800304
James Kuszmauld42edb42022-01-07 18:00:16 -0800305 if (capture_stderr_) {
306 PCHECK(STDERR_FILENO == dup2(stderr_pipes_.write->fd(), STDERR_FILENO));
307 stderr_pipes_.write.reset();
308 }
309
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700310 if (run_as_sudo_) {
Sarah Newman6d1e53b2022-08-09 14:38:08 -0700311 // For sudo we must supply the actual path
312 args_.insert(args_.begin(), path_);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700313 args_.insert(args_.begin(), kSudo);
Sarah Newman6d1e53b2022-08-09 14:38:08 -0700314 } else {
315 // argv[0] should be the program name
316 args_.insert(args_.begin(), name_);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700317 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800318
319 std::vector<char *> cargs = CArgs();
Philipp Schrader790cb542023-07-05 21:06:52 -0700320 const char *path = run_as_sudo_ ? kSudo : path_.c_str();
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700321 execvp(path, cargs.data());
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800322
323 // If we got here, something went wrong
James Kuszmauld42edb42022-01-07 18:00:16 -0800324 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800325 static_cast<uint32_t>(aos::starter::LastStopReason::EXECV_ERR));
payton.rehl2841b1c2023-05-25 17:23:55 -0700326 PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
327 << "Could not execute " << name_ << " (" << path_ << ')';
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800328
329 _exit(EXIT_FAILURE);
330}
331
James Kuszmaul8544c492023-07-31 15:00:38 -0700332void Application::ObserveTimingReport(
333 const aos::monotonic_clock::time_point send_time,
334 const aos::timing::Report *msg) {
335 if (msg->name()->string_view() == name_ && msg->pid() == pid_ &&
336 msg->has_version()) {
337 latest_timing_report_version_ = msg->version()->str();
338 last_timing_report_ = send_time;
339 }
340}
341
James Kuszmauld42edb42022-01-07 18:00:16 -0800342void Application::FetchOutputs() {
343 if (capture_stdout_) {
344 stdout_pipes_.read->Read(&stdout_);
345 }
346 if (capture_stderr_) {
347 stderr_pipes_.read->Read(&stderr_);
348 }
349}
350
351const std::string &Application::GetStdout() {
352 CHECK(capture_stdout_);
353 FetchOutputs();
354 return stdout_;
355}
356
357const std::string &Application::GetStderr() {
358 CHECK(capture_stderr_);
359 FetchOutputs();
360 return stderr_;
361}
362
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800363void Application::DoStop(bool restart) {
364 // If stop or restart received, the old state of these is no longer applicable
365 // so cancel both.
366 restart_timer_->Disable();
367 start_timer_->Disable();
368
James Kuszmauld42edb42022-01-07 18:00:16 -0800369 FetchOutputs();
370
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800371 switch (status_) {
372 case aos::starter::State::STARTING:
373 case aos::starter::State::RUNNING: {
payton.rehl2841b1c2023-05-25 17:23:55 -0700374 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
375 << "Stopping '" << name_ << "' pid: " << pid_ << " with signal "
376 << SIGINT;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800377 status_ = aos::starter::State::STOPPING;
378
379 kill(pid_, SIGINT);
380
381 // Watchdog timer to SIGKILL application if it is still running 1 second
382 // after SIGINT
Philipp Schradera6712522023-07-05 20:25:11 -0700383 stop_timer_->Schedule(event_loop_->monotonic_now() +
384 std::chrono::seconds(1));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800385 queue_restart_ = restart;
Austin Schuh1cea9032023-07-10 11:56:40 -0700386 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800387 break;
388 }
389 case aos::starter::State::WAITING: {
390 // If waiting to restart, and receives restart, skip the waiting period
391 // and restart immediately. If stop received, all we have to do is move
392 // to the STOPPED state.
393 if (restart) {
394 DoStart();
395 } else {
396 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700397 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800398 }
399 break;
400 }
401 case aos::starter::State::STOPPING: {
402 // If the application is already stopping, then we just need to update the
403 // restart flag to the most recent status.
404 queue_restart_ = restart;
405 break;
406 }
407 case aos::starter::State::STOPPED: {
408 // Restart immediately if the application is already stopped
409 if (restart) {
410 status_ = aos::starter::State::WAITING;
411 DoStart();
412 }
413 break;
414 }
415 }
416}
417
418void Application::QueueStart() {
419 status_ = aos::starter::State::WAITING;
420
payton.rehl2841b1c2023-05-25 17:23:55 -0700421 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
422 << "Restarting " << name_ << " in 3 seconds";
Philipp Schradera6712522023-07-05 20:25:11 -0700423 restart_timer_->Schedule(event_loop_->monotonic_now() +
424 std::chrono::seconds(3));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800425 start_timer_->Disable();
426 stop_timer_->Disable();
Austin Schuh1cea9032023-07-10 11:56:40 -0700427 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800428}
429
James Kuszmauld42edb42022-01-07 18:00:16 -0800430std::vector<char *> Application::CArgs() {
431 std::vector<char *> cargs;
432 std::transform(args_.begin(), args_.end(), std::back_inserter(cargs),
433 [](std::string &str) { return str.data(); });
434 cargs.push_back(nullptr);
435 return cargs;
436}
437
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800438void Application::set_args(
439 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v) {
440 args_.clear();
441 std::transform(v.begin(), v.end(), std::back_inserter(args_),
James Kuszmauld42edb42022-01-07 18:00:16 -0800442 [](const flatbuffers::String *str) { return str->str(); });
443}
444
445void Application::set_args(std::vector<std::string> args) {
446 args_ = std::move(args);
447}
448
449void Application::set_capture_stdout(bool capture) {
450 capture_stdout_ = capture;
451}
452
453void Application::set_capture_stderr(bool capture) {
454 capture_stderr_ = capture;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800455}
456
457std::optional<uid_t> Application::FindUid(const char *name) {
458 // TODO(austin): Use the reentrant version. This should be safe.
459 struct passwd *user_data = getpwnam(name);
460 if (user_data != nullptr) {
461 return user_data->pw_uid;
462 } else {
463 LOG(FATAL) << "Could not find user " << name;
464 return std::nullopt;
465 }
466}
467
468std::optional<gid_t> Application::FindPrimaryGidForUser(const char *name) {
469 // TODO(austin): Use the reentrant version. This should be safe.
470 struct passwd *user_data = getpwnam(name);
471 if (user_data != nullptr) {
472 return user_data->pw_gid;
473 } else {
474 LOG(FATAL) << "Could not find user " << name;
475 return std::nullopt;
476 }
477}
478
479flatbuffers::Offset<aos::starter::ApplicationStatus>
James Kuszmaul6295a642022-03-22 15:23:59 -0700480Application::PopulateStatus(flatbuffers::FlatBufferBuilder *builder,
481 util::Top *top) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800482 CHECK_NOTNULL(builder);
483 auto name_fbs = builder->CreateString(name_);
484
James Kuszmaul6295a642022-03-22 15:23:59 -0700485 const bool valid_pid = pid_ > 0 && status_ != aos::starter::State::STOPPED;
486 const flatbuffers::Offset<util::ProcessInfo> process_info =
487 valid_pid ? top->InfoForProcess(builder, pid_)
488 : flatbuffers::Offset<util::ProcessInfo>();
489
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800490 aos::starter::ApplicationStatus::Builder status_builder(*builder);
491 status_builder.add_name(name_fbs);
492 status_builder.add_state(status_);
James Kuszmauld42edb42022-01-07 18:00:16 -0800493 if (exit_code_.has_value()) {
494 status_builder.add_last_exit_code(exit_code_.value());
495 }
James Kuszmaul8544c492023-07-31 15:00:38 -0700496 status_builder.add_has_active_timing_report(
497 last_timing_report_ +
498 // Leave a bit of margin on the timing report receipt time, to allow
499 // for timing errors.
500 3 * std::chrono::milliseconds(FLAGS_timing_report_ms) >
501 event_loop_->monotonic_now());
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800502 status_builder.add_last_stop_reason(stop_reason_);
503 if (pid_ != -1) {
504 status_builder.add_pid(pid_);
505 status_builder.add_id(id_);
506 }
James Kuszmaul6295a642022-03-22 15:23:59 -0700507 // Note that even if process_info is null, calling add_process_info is fine.
508 status_builder.add_process_info(process_info);
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800509 status_builder.add_last_start_time(start_time_.time_since_epoch().count());
510 return status_builder.Finish();
511}
512
513void Application::Terminate() {
514 stop_reason_ = aos::starter::LastStopReason::TERMINATE;
515 DoStop(false);
516 terminating_ = true;
517}
518
519void Application::HandleCommand(aos::starter::Command cmd) {
520 switch (cmd) {
521 case aos::starter::Command::START: {
522 switch (status_) {
523 case aos::starter::State::WAITING: {
524 restart_timer_->Disable();
525 DoStart();
526 break;
527 }
528 case aos::starter::State::STARTING: {
529 break;
530 }
531 case aos::starter::State::RUNNING: {
532 break;
533 }
534 case aos::starter::State::STOPPING: {
535 queue_restart_ = true;
536 break;
537 }
538 case aos::starter::State::STOPPED: {
539 status_ = aos::starter::State::WAITING;
540 DoStart();
541 break;
542 }
543 }
544 break;
545 }
546 case aos::starter::Command::STOP: {
547 stop_reason_ = aos::starter::LastStopReason::STOP_REQUESTED;
548 DoStop(false);
549 break;
550 }
551 case aos::starter::Command::RESTART: {
552 stop_reason_ = aos::starter::LastStopReason::RESTART_REQUESTED;
553 DoStop(true);
554 break;
555 }
556 }
557}
558
559bool Application::MaybeHandleSignal() {
560 int status;
561
Sarah Newman21c59202022-06-16 12:36:33 -0700562 if (status_ == aos::starter::State::WAITING ||
563 status_ == aos::starter::State::STOPPED) {
564 // We can't possibly have received a signal meant for this process.
565 return false;
566 }
567
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800568 // Check if the status of this process has changed
Sarah Newman21c59202022-06-16 12:36:33 -0700569 // The PID won't be -1 if this application has ever been run successfully
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800570 if (pid_ == -1 || waitpid(pid_, &status, WNOHANG) != pid_) {
571 return false;
572 }
573
574 // Check that the event was the process exiting
575 if (!WIFEXITED(status) && !WIFSIGNALED(status)) {
576 return false;
577 }
578
James Kuszmauld42edb42022-01-07 18:00:16 -0800579 start_timer_->Disable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800580 exit_time_ = event_loop_->monotonic_now();
581 exit_code_ = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status);
582
James Kuszmauld42edb42022-01-07 18:00:16 -0800583 if (auto read_result = status_pipes_.read->Read()) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800584 stop_reason_ = static_cast<aos::starter::LastStopReason>(*read_result);
585 }
586
587 switch (status_) {
588 case aos::starter::State::STARTING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800589 if (exit_code_.value() == 0) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700590 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
591 << "Application '" << name_ << "' pid " << pid_
592 << " exited with status " << exit_code_.value();
James Kuszmauld42edb42022-01-07 18:00:16 -0800593 } else {
payton.rehl2841b1c2023-05-25 17:23:55 -0700594 LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
595 << "Failed to start '" << name_ << "' on pid " << pid_
596 << " : Exited with status " << exit_code_.value();
James Kuszmauld42edb42022-01-07 18:00:16 -0800597 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800598 if (autorestart()) {
599 QueueStart();
600 } else {
601 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700602 OnChange();
James Kuszmaul6f10b382022-03-11 22:31:38 -0800603 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800604 break;
605 }
606 case aos::starter::State::RUNNING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800607 if (exit_code_.value() == 0) {
payton.rehl2841b1c2023-05-25 17:23:55 -0700608 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
609 << "Application '" << name_ << "' pid " << pid_
610 << " exited with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800611 } else {
James Kuszmaul8544c492023-07-31 15:00:38 -0700612 if (quiet_flag_ == QuietLogging::kNo) {
613 std::string version_string =
614 latest_timing_report_version_.has_value()
615 ? absl::StrCat("'", latest_timing_report_version_.value(),
616 "'")
617 : "unknown";
618 LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo)
619 << "Application '" << name_ << "' pid " << pid_ << " version "
620 << version_string << " exited unexpectedly with status "
621 << exit_code_.value();
622 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800623 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800624 if (autorestart()) {
625 QueueStart();
626 } else {
627 status_ = aos::starter::State::STOPPED;
Austin Schuh1cea9032023-07-10 11:56:40 -0700628 OnChange();
James Kuszmaul6f10b382022-03-11 22:31:38 -0800629 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800630 break;
631 }
632 case aos::starter::State::STOPPING: {
payton.rehl2841b1c2023-05-25 17:23:55 -0700633 LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo)
634 << "Successfully stopped '" << name_ << "' pid: " << pid_
635 << " with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800636 status_ = aos::starter::State::STOPPED;
637
638 // Disable force stop timer since the process already died
639 stop_timer_->Disable();
640
Austin Schuh1cea9032023-07-10 11:56:40 -0700641 OnChange();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800642 if (terminating_) {
643 return true;
644 }
645
646 if (queue_restart_) {
647 queue_restart_ = false;
648 status_ = aos::starter::State::WAITING;
649 DoStart();
650 }
651 break;
652 }
653 case aos::starter::State::WAITING:
654 case aos::starter::State::STOPPED: {
Sarah Newman21c59202022-06-16 12:36:33 -0700655 __builtin_unreachable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800656 break;
657 }
658 }
659
660 return false;
661}
662
Austin Schuh1cea9032023-07-10 11:56:40 -0700663void Application::OnChange() {
664 for (auto &fn : on_change_) {
665 fn();
666 }
667}
668
Adam Snaider70deaf22023-08-11 13:58:34 -0700669Application::~Application() {
670 start_timer_->Disable();
671 restart_timer_->Disable();
672 stop_timer_->Disable();
673 pipe_timer_->Disable();
674 child_status_handler_->Disable();
675}
676
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800677} // namespace aos::starter