blob: 23705cc41635c01eabdc2992089267da884be4d4 [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
11namespace aos::starter {
12
Austin Schuhbbeb37e2022-08-17 16:19:27 -070013// RAII class to become root and restore back to the original user and group
14// afterwards.
15class Sudo {
16 public:
17 Sudo() {
18 // Save what we were.
19 PCHECK(getresuid(&ruid_, &euid_, &suid_) == 0);
20 PCHECK(getresgid(&rgid_, &egid_, &sgid_) == 0);
21
22 // Become root.
23 PCHECK(setresuid(/* ruid */ 0 /* root */, /* euid */ 0, /* suid */ 0) == 0)
24 << ": Failed to become root";
25 PCHECK(setresgid(/* ruid */ 0 /* root */, /* euid */ 0, /* suid */ 0) == 0)
26 << ": Failed to become root";
27 }
28
29 ~Sudo() {
30 // And recover.
31 PCHECK(setresgid(rgid_, egid_, sgid_) == 0);
32 PCHECK(setresuid(ruid_, euid_, suid_) == 0);
33 }
34
35 uid_t ruid_, euid_, suid_;
36 gid_t rgid_, egid_, sgid_;
37};
38
39MemoryCGroup::MemoryCGroup(std::string_view name)
40 : cgroup_(absl::StrCat("/sys/fs/cgroup/memory/aos_", name)) {
41 Sudo sudo;
42 int ret = mkdir(cgroup_.c_str(), 0755);
43
44 if (ret != 0) {
45 if (errno == EEXIST) {
46 PCHECK(remove(cgroup_.c_str()) == 0)
47 << ": Failed to remove previous cgroup " << cgroup_;
48 ret = mkdir(cgroup_.c_str(), 0755);
49 }
50 }
51
52 if (ret != 0) {
53 PLOG(FATAL) << ": Failed to create cgroup aos_" << cgroup_
54 << ", do you have permission?";
55 }
56}
57
58void MemoryCGroup::AddTid(pid_t pid) {
59 if (pid == 0) {
60 pid = getpid();
61 }
62 Sudo sudo;
63 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/tasks").c_str(),
64 std::to_string(pid));
65}
66
67void MemoryCGroup::SetLimit(std::string_view limit_name, uint64_t limit_value) {
68 Sudo sudo;
69 util::WriteStringToFileOrDie(absl::StrCat(cgroup_, "/", limit_name).c_str(),
70 std::to_string(limit_value));
71}
72
73MemoryCGroup::~MemoryCGroup() {
74 Sudo sudo;
75 PCHECK(rmdir(absl::StrCat(cgroup_).c_str()) == 0);
76}
77
James Kuszmaul3224b8e2022-01-07 19:00:39 -080078SignalListener::SignalListener(aos::ShmEventLoop *loop,
79 std::function<void(signalfd_siginfo)> callback)
80 : SignalListener(loop, callback,
81 {SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGFPE, SIGSEGV,
82 SIGPIPE, SIGTERM, SIGBUS, SIGXCPU, SIGCHLD}) {}
83
84SignalListener::SignalListener(aos::ShmEventLoop *loop,
85 std::function<void(signalfd_siginfo)> callback,
86 std::initializer_list<unsigned int> signals)
87 : loop_(loop), callback_(std::move(callback)), signalfd_(signals) {
88 loop->epoll()->OnReadable(signalfd_.fd(), [this] {
89 signalfd_siginfo info = signalfd_.Read();
90
91 if (info.ssi_signo == 0) {
92 LOG(WARNING) << "Could not read " << sizeof(signalfd_siginfo) << " bytes";
93 return;
94 }
95
96 callback_(info);
97 });
98}
99
100SignalListener::~SignalListener() { loop_->epoll()->DeleteFd(signalfd_.fd()); }
101
James Kuszmauld42edb42022-01-07 18:00:16 -0800102Application::Application(std::string_view name,
103 std::string_view executable_name,
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800104 aos::EventLoop *event_loop,
105 std::function<void()> on_change)
James Kuszmauld42edb42022-01-07 18:00:16 -0800106 : name_(name),
107 path_(executable_name),
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800108 event_loop_(event_loop),
109 start_timer_(event_loop_->AddTimer([this] {
110 status_ = aos::starter::State::RUNNING;
111 LOG(INFO) << "Started '" << name_ << "' pid: " << pid_;
112 })),
113 restart_timer_(event_loop_->AddTimer([this] { DoStart(); })),
114 stop_timer_(event_loop_->AddTimer([this] {
115 if (kill(pid_, SIGKILL) == 0) {
116 LOG(WARNING) << "Failed to stop, sending SIGKILL to '" << name_
117 << "' pid: " << pid_;
118 }
119 })),
James Kuszmauld42edb42022-01-07 18:00:16 -0800120 pipe_timer_(event_loop_->AddTimer([this]() { FetchOutputs(); })),
121 child_status_handler_(
122 event_loop_->AddTimer([this]() { MaybeHandleSignal(); })),
123 on_change_(on_change) {
124 event_loop_->OnRun([this]() {
125 // Every second poll to check if the child is dead. This is used as a
126 // default for the case where the user is not directly catching SIGCHLD and
127 // calling MaybeHandleSignal for us.
128 child_status_handler_->Setup(event_loop_->monotonic_now(),
129 std::chrono::seconds(1));
130 });
131}
132
133Application::Application(const aos::Application *application,
134 aos::EventLoop *event_loop,
135 std::function<void()> on_change)
136 : Application(application->name()->string_view(),
137 application->has_executable_name()
138 ? application->executable_name()->string_view()
139 : application->name()->string_view(),
140 event_loop, on_change) {
141 user_name_ = application->has_user() ? application->user()->str() : "";
142 user_ = application->has_user() ? FindUid(user_name_.c_str()) : std::nullopt;
143 group_ = application->has_user() ? FindPrimaryGidForUser(user_name_.c_str())
144 : std::nullopt;
145 autostart_ = application->autostart();
146 autorestart_ = application->autorestart();
147 if (application->has_args()) {
148 set_args(*application->args());
149 }
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700150
151 if (application->has_memory_limit() && application->memory_limit() > 0) {
152 SetMemoryLimit(application->memory_limit());
153 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800154}
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800155
156void Application::DoStart() {
157 if (status_ != aos::starter::State::WAITING) {
158 return;
159 }
160
161 start_timer_->Disable();
162 restart_timer_->Disable();
163
James Kuszmauld42edb42022-01-07 18:00:16 -0800164 status_pipes_ = util::ScopedPipe::MakePipe();
165
166 if (capture_stdout_) {
167 stdout_pipes_ = util::ScopedPipe::MakePipe();
168 stdout_.clear();
169 }
170 if (capture_stderr_) {
171 stderr_pipes_ = util::ScopedPipe::MakePipe();
172 stderr_.clear();
173 }
174
175 pipe_timer_->Setup(event_loop_->monotonic_now(),
176 std::chrono::milliseconds(100));
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800177
178 const pid_t pid = fork();
179
180 if (pid != 0) {
181 if (pid == -1) {
182 PLOG(WARNING) << "Failed to fork '" << name_ << "'";
183 stop_reason_ = aos::starter::LastStopReason::FORK_ERR;
184 status_ = aos::starter::State::STOPPED;
185 } else {
186 pid_ = pid;
187 id_ = next_id_++;
188 start_time_ = event_loop_->monotonic_now();
189 status_ = aos::starter::State::STARTING;
190 LOG(INFO) << "Starting '" << name_ << "' pid " << pid_;
191
192 // Setup timer which moves application to RUNNING state if it is still
193 // alive in 1 second.
194 start_timer_->Setup(event_loop_->monotonic_now() +
195 std::chrono::seconds(1));
James Kuszmauld42edb42022-01-07 18:00:16 -0800196 // Since we are the parent process, clear our write-side of all the pipes.
197 status_pipes_.write.reset();
198 stdout_pipes_.write.reset();
199 stderr_pipes_.write.reset();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800200 }
201 on_change_();
202 return;
203 }
204
Austin Schuhbbeb37e2022-08-17 16:19:27 -0700205 if (memory_cgroup_) {
206 memory_cgroup_->AddTid();
207 }
208
James Kuszmauld42edb42022-01-07 18:00:16 -0800209 // Since we are the child process, clear our read-side of all the pipes.
210 status_pipes_.read.reset();
211 stdout_pipes_.read.reset();
212 stderr_pipes_.read.reset();
213
214 // The status pipe will not be needed if the execve succeeds.
215 status_pipes_.write->SetCloexec();
216
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800217 // Clear out signal mask of parent so forked process receives all signals
218 // normally.
219 sigset_t empty_mask;
220 sigemptyset(&empty_mask);
221 sigprocmask(SIG_SETMASK, &empty_mask, nullptr);
222
223 // Cleanup children if starter dies in a way that is not handled gracefully.
224 if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800225 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800226 static_cast<uint32_t>(aos::starter::LastStopReason::SET_PRCTL_ERR));
227 PLOG(FATAL) << "Could not set PR_SET_PDEATHSIG to SIGKILL";
228 }
229
230 if (group_) {
231 CHECK(!user_name_.empty());
232 // The manpage for setgroups says we just need CAP_SETGID, but empirically
233 // we also need the effective UID to be 0 to make it work. user_ must also
234 // be set so we change this effective UID back later.
235 CHECK(user_);
236 if (seteuid(0) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800237 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800238 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
239 PLOG(FATAL) << "Could not seteuid(0) for " << name_
240 << " in preparation for setting groups";
241 }
242 if (initgroups(user_name_.c_str(), *group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800243 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800244 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
245 PLOG(FATAL) << "Could not initialize normal groups for " << name_
246 << " as " << user_name_ << " with " << *group_;
247 }
248 if (setgid(*group_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800249 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800250 static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR));
251 PLOG(FATAL) << "Could not set group for " << name_ << " to " << *group_;
252 }
253 }
254
255 if (user_) {
256 if (setuid(*user_) == -1) {
James Kuszmauld42edb42022-01-07 18:00:16 -0800257 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800258 static_cast<uint32_t>(aos::starter::LastStopReason::SET_USR_ERR));
259 PLOG(FATAL) << "Could not set user for " << name_ << " to " << *user_;
260 }
261 }
262
James Kuszmauld42edb42022-01-07 18:00:16 -0800263 if (capture_stdout_) {
264 PCHECK(STDOUT_FILENO == dup2(stdout_pipes_.write->fd(), STDOUT_FILENO));
265 stdout_pipes_.write.reset();
266 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800267
James Kuszmauld42edb42022-01-07 18:00:16 -0800268 if (capture_stderr_) {
269 PCHECK(STDERR_FILENO == dup2(stderr_pipes_.write->fd(), STDERR_FILENO));
270 stderr_pipes_.write.reset();
271 }
272
273 // argv[0] should be the program name
James Kuszmaul6f10b382022-03-11 22:31:38 -0800274 args_.insert(args_.begin(), path_);
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700275 if (run_as_sudo_) {
276 args_.insert(args_.begin(), kSudo);
277 }
James Kuszmauld42edb42022-01-07 18:00:16 -0800278
279 std::vector<char *> cargs = CArgs();
Sanjay Narayanan01a228f2022-04-26 14:19:30 -0700280 const char* path = run_as_sudo_ ? kSudo : path_.c_str();
281 execvp(path, cargs.data());
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800282
283 // If we got here, something went wrong
James Kuszmauld42edb42022-01-07 18:00:16 -0800284 status_pipes_.write->Write(
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800285 static_cast<uint32_t>(aos::starter::LastStopReason::EXECV_ERR));
James Kuszmaul6f10b382022-03-11 22:31:38 -0800286 PLOG(WARNING) << "Could not execute " << name_ << " (" << path_ << ')';
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800287
288 _exit(EXIT_FAILURE);
289}
290
James Kuszmauld42edb42022-01-07 18:00:16 -0800291void Application::FetchOutputs() {
292 if (capture_stdout_) {
293 stdout_pipes_.read->Read(&stdout_);
294 }
295 if (capture_stderr_) {
296 stderr_pipes_.read->Read(&stderr_);
297 }
298}
299
300const std::string &Application::GetStdout() {
301 CHECK(capture_stdout_);
302 FetchOutputs();
303 return stdout_;
304}
305
306const std::string &Application::GetStderr() {
307 CHECK(capture_stderr_);
308 FetchOutputs();
309 return stderr_;
310}
311
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800312void Application::DoStop(bool restart) {
313 // If stop or restart received, the old state of these is no longer applicable
314 // so cancel both.
315 restart_timer_->Disable();
316 start_timer_->Disable();
317
James Kuszmauld42edb42022-01-07 18:00:16 -0800318 FetchOutputs();
319
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800320 switch (status_) {
321 case aos::starter::State::STARTING:
322 case aos::starter::State::RUNNING: {
323 LOG(INFO) << "Stopping '" << name_ << "' pid: " << pid_ << " with signal "
324 << SIGINT;
325 status_ = aos::starter::State::STOPPING;
326
327 kill(pid_, SIGINT);
328
329 // Watchdog timer to SIGKILL application if it is still running 1 second
330 // after SIGINT
331 stop_timer_->Setup(event_loop_->monotonic_now() +
332 std::chrono::seconds(1));
333 queue_restart_ = restart;
334 on_change_();
335 break;
336 }
337 case aos::starter::State::WAITING: {
338 // If waiting to restart, and receives restart, skip the waiting period
339 // and restart immediately. If stop received, all we have to do is move
340 // to the STOPPED state.
341 if (restart) {
342 DoStart();
343 } else {
344 status_ = aos::starter::State::STOPPED;
345 on_change_();
346 }
347 break;
348 }
349 case aos::starter::State::STOPPING: {
350 // If the application is already stopping, then we just need to update the
351 // restart flag to the most recent status.
352 queue_restart_ = restart;
353 break;
354 }
355 case aos::starter::State::STOPPED: {
356 // Restart immediately if the application is already stopped
357 if (restart) {
358 status_ = aos::starter::State::WAITING;
359 DoStart();
360 }
361 break;
362 }
363 }
364}
365
366void Application::QueueStart() {
367 status_ = aos::starter::State::WAITING;
368
369 LOG(INFO) << "Restarting " << name_ << " in 3 seconds";
370 restart_timer_->Setup(event_loop_->monotonic_now() + std::chrono::seconds(3));
371 start_timer_->Disable();
372 stop_timer_->Disable();
373 on_change_();
374}
375
James Kuszmauld42edb42022-01-07 18:00:16 -0800376std::vector<char *> Application::CArgs() {
377 std::vector<char *> cargs;
378 std::transform(args_.begin(), args_.end(), std::back_inserter(cargs),
379 [](std::string &str) { return str.data(); });
380 cargs.push_back(nullptr);
381 return cargs;
382}
383
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800384void Application::set_args(
385 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v) {
386 args_.clear();
387 std::transform(v.begin(), v.end(), std::back_inserter(args_),
James Kuszmauld42edb42022-01-07 18:00:16 -0800388 [](const flatbuffers::String *str) { return str->str(); });
389}
390
391void Application::set_args(std::vector<std::string> args) {
392 args_ = std::move(args);
393}
394
395void Application::set_capture_stdout(bool capture) {
396 capture_stdout_ = capture;
397}
398
399void Application::set_capture_stderr(bool capture) {
400 capture_stderr_ = capture;
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800401}
402
403std::optional<uid_t> Application::FindUid(const char *name) {
404 // TODO(austin): Use the reentrant version. This should be safe.
405 struct passwd *user_data = getpwnam(name);
406 if (user_data != nullptr) {
407 return user_data->pw_uid;
408 } else {
409 LOG(FATAL) << "Could not find user " << name;
410 return std::nullopt;
411 }
412}
413
414std::optional<gid_t> Application::FindPrimaryGidForUser(const char *name) {
415 // TODO(austin): Use the reentrant version. This should be safe.
416 struct passwd *user_data = getpwnam(name);
417 if (user_data != nullptr) {
418 return user_data->pw_gid;
419 } else {
420 LOG(FATAL) << "Could not find user " << name;
421 return std::nullopt;
422 }
423}
424
425flatbuffers::Offset<aos::starter::ApplicationStatus>
James Kuszmaul6295a642022-03-22 15:23:59 -0700426Application::PopulateStatus(flatbuffers::FlatBufferBuilder *builder,
427 util::Top *top) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800428 CHECK_NOTNULL(builder);
429 auto name_fbs = builder->CreateString(name_);
430
James Kuszmaul6295a642022-03-22 15:23:59 -0700431 const bool valid_pid = pid_ > 0 && status_ != aos::starter::State::STOPPED;
432 const flatbuffers::Offset<util::ProcessInfo> process_info =
433 valid_pid ? top->InfoForProcess(builder, pid_)
434 : flatbuffers::Offset<util::ProcessInfo>();
435
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800436 aos::starter::ApplicationStatus::Builder status_builder(*builder);
437 status_builder.add_name(name_fbs);
438 status_builder.add_state(status_);
James Kuszmauld42edb42022-01-07 18:00:16 -0800439 if (exit_code_.has_value()) {
440 status_builder.add_last_exit_code(exit_code_.value());
441 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800442 status_builder.add_last_stop_reason(stop_reason_);
443 if (pid_ != -1) {
444 status_builder.add_pid(pid_);
445 status_builder.add_id(id_);
446 }
James Kuszmaul6295a642022-03-22 15:23:59 -0700447 // Note that even if process_info is null, calling add_process_info is fine.
448 status_builder.add_process_info(process_info);
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800449 status_builder.add_last_start_time(start_time_.time_since_epoch().count());
450 return status_builder.Finish();
451}
452
453void Application::Terminate() {
454 stop_reason_ = aos::starter::LastStopReason::TERMINATE;
455 DoStop(false);
456 terminating_ = true;
457}
458
459void Application::HandleCommand(aos::starter::Command cmd) {
460 switch (cmd) {
461 case aos::starter::Command::START: {
462 switch (status_) {
463 case aos::starter::State::WAITING: {
464 restart_timer_->Disable();
465 DoStart();
466 break;
467 }
468 case aos::starter::State::STARTING: {
469 break;
470 }
471 case aos::starter::State::RUNNING: {
472 break;
473 }
474 case aos::starter::State::STOPPING: {
475 queue_restart_ = true;
476 break;
477 }
478 case aos::starter::State::STOPPED: {
479 status_ = aos::starter::State::WAITING;
480 DoStart();
481 break;
482 }
483 }
484 break;
485 }
486 case aos::starter::Command::STOP: {
487 stop_reason_ = aos::starter::LastStopReason::STOP_REQUESTED;
488 DoStop(false);
489 break;
490 }
491 case aos::starter::Command::RESTART: {
492 stop_reason_ = aos::starter::LastStopReason::RESTART_REQUESTED;
493 DoStop(true);
494 break;
495 }
496 }
497}
498
499bool Application::MaybeHandleSignal() {
500 int status;
501
Sarah Newman21c59202022-06-16 12:36:33 -0700502 if (status_ == aos::starter::State::WAITING ||
503 status_ == aos::starter::State::STOPPED) {
504 // We can't possibly have received a signal meant for this process.
505 return false;
506 }
507
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800508 // Check if the status of this process has changed
Sarah Newman21c59202022-06-16 12:36:33 -0700509 // The PID won't be -1 if this application has ever been run successfully
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800510 if (pid_ == -1 || waitpid(pid_, &status, WNOHANG) != pid_) {
511 return false;
512 }
513
514 // Check that the event was the process exiting
515 if (!WIFEXITED(status) && !WIFSIGNALED(status)) {
516 return false;
517 }
518
James Kuszmauld42edb42022-01-07 18:00:16 -0800519 start_timer_->Disable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800520 exit_time_ = event_loop_->monotonic_now();
521 exit_code_ = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status);
522
James Kuszmauld42edb42022-01-07 18:00:16 -0800523 if (auto read_result = status_pipes_.read->Read()) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800524 stop_reason_ = static_cast<aos::starter::LastStopReason>(*read_result);
525 }
526
527 switch (status_) {
528 case aos::starter::State::STARTING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800529 if (exit_code_.value() == 0) {
530 LOG(INFO) << "Application '" << name_ << "' pid " << pid_
531 << " exited with status " << exit_code_.value();
532 } else {
533 LOG(WARNING) << "Failed to start '" << name_ << "' on pid " << pid_
534 << " : Exited with status " << exit_code_.value();
535 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800536 if (autorestart()) {
537 QueueStart();
538 } else {
539 status_ = aos::starter::State::STOPPED;
540 on_change_();
541 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800542 break;
543 }
544 case aos::starter::State::RUNNING: {
James Kuszmauld42edb42022-01-07 18:00:16 -0800545 if (exit_code_.value() == 0) {
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800546 LOG(INFO) << "Application '" << name_ << "' pid " << pid_
James Kuszmauld42edb42022-01-07 18:00:16 -0800547 << " exited with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800548 } else {
549 LOG(WARNING) << "Application '" << name_ << "' pid " << pid_
James Kuszmauld42edb42022-01-07 18:00:16 -0800550 << " exited unexpectedly with status "
551 << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800552 }
James Kuszmaul6f10b382022-03-11 22:31:38 -0800553 if (autorestart()) {
554 QueueStart();
555 } else {
556 status_ = aos::starter::State::STOPPED;
557 on_change_();
558 }
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800559 break;
560 }
561 case aos::starter::State::STOPPING: {
562 LOG(INFO) << "Successfully stopped '" << name_ << "' pid: " << pid_
James Kuszmauld42edb42022-01-07 18:00:16 -0800563 << " with status " << exit_code_.value();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800564 status_ = aos::starter::State::STOPPED;
565
566 // Disable force stop timer since the process already died
567 stop_timer_->Disable();
568
569 on_change_();
570 if (terminating_) {
571 return true;
572 }
573
574 if (queue_restart_) {
575 queue_restart_ = false;
576 status_ = aos::starter::State::WAITING;
577 DoStart();
578 }
579 break;
580 }
581 case aos::starter::State::WAITING:
582 case aos::starter::State::STOPPED: {
Sarah Newman21c59202022-06-16 12:36:33 -0700583 __builtin_unreachable();
James Kuszmaul3224b8e2022-01-07 19:00:39 -0800584 break;
585 }
586 }
587
588 return false;
589}
590
591} // namespace aos::starter