James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 1 | #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 | |
| 11 | namespace aos::starter { |
| 12 | |
Austin Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 13 | // RAII class to become root and restore back to the original user and group |
| 14 | // afterwards. |
| 15 | class 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 | |
| 39 | MemoryCGroup::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) { |
Austin Schuh | 4d8a46e | 2022-10-07 19:20:20 -0700 | [diff] [blame] | 46 | PCHECK(rmdir(cgroup_.c_str()) == 0) |
Austin Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 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 | |
| 58 | void 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 | |
| 67 | void 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 | |
| 73 | MemoryCGroup::~MemoryCGroup() { |
| 74 | Sudo sudo; |
| 75 | PCHECK(rmdir(absl::StrCat(cgroup_).c_str()) == 0); |
| 76 | } |
| 77 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 78 | SignalListener::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 | |
| 84 | SignalListener::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 | |
| 100 | SignalListener::~SignalListener() { loop_->epoll()->DeleteFd(signalfd_.fd()); } |
| 101 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 102 | Application::Application(std::string_view name, |
| 103 | std::string_view executable_name, |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 104 | aos::EventLoop *event_loop, |
| 105 | std::function<void()> on_change) |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 106 | : name_(name), |
| 107 | path_(executable_name), |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 108 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 120 | 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 | |
| 133 | Application::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 Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 150 | |
| 151 | if (application->has_memory_limit() && application->memory_limit() > 0) { |
| 152 | SetMemoryLimit(application->memory_limit()); |
| 153 | } |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 154 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 155 | |
| 156 | void Application::DoStart() { |
| 157 | if (status_ != aos::starter::State::WAITING) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | start_timer_->Disable(); |
| 162 | restart_timer_->Disable(); |
| 163 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 164 | 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 Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 177 | |
| 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 196 | // 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 Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 200 | } |
| 201 | on_change_(); |
| 202 | return; |
| 203 | } |
| 204 | |
Austin Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 205 | if (memory_cgroup_) { |
| 206 | memory_cgroup_->AddTid(); |
| 207 | } |
| 208 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 209 | // 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 Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 217 | // 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 225 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 226 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 237 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 238 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 243 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 244 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 249 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 250 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 257 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 258 | 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 Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 263 | if (capture_stdout_) { |
| 264 | PCHECK(STDOUT_FILENO == dup2(stdout_pipes_.write->fd(), STDOUT_FILENO)); |
| 265 | stdout_pipes_.write.reset(); |
| 266 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 267 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 268 | if (capture_stderr_) { |
| 269 | PCHECK(STDERR_FILENO == dup2(stderr_pipes_.write->fd(), STDERR_FILENO)); |
| 270 | stderr_pipes_.write.reset(); |
| 271 | } |
| 272 | |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 273 | if (run_as_sudo_) { |
Sarah Newman | 6d1e53b | 2022-08-09 14:38:08 -0700 | [diff] [blame] | 274 | // For sudo we must supply the actual path |
| 275 | args_.insert(args_.begin(), path_); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 276 | args_.insert(args_.begin(), kSudo); |
Sarah Newman | 6d1e53b | 2022-08-09 14:38:08 -0700 | [diff] [blame] | 277 | } else { |
| 278 | // argv[0] should be the program name |
| 279 | args_.insert(args_.begin(), name_); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 280 | } |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 281 | |
| 282 | std::vector<char *> cargs = CArgs(); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 283 | const char* path = run_as_sudo_ ? kSudo : path_.c_str(); |
| 284 | execvp(path, cargs.data()); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 285 | |
| 286 | // If we got here, something went wrong |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 287 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 288 | static_cast<uint32_t>(aos::starter::LastStopReason::EXECV_ERR)); |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 289 | PLOG(WARNING) << "Could not execute " << name_ << " (" << path_ << ')'; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 290 | |
| 291 | _exit(EXIT_FAILURE); |
| 292 | } |
| 293 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 294 | void Application::FetchOutputs() { |
| 295 | if (capture_stdout_) { |
| 296 | stdout_pipes_.read->Read(&stdout_); |
| 297 | } |
| 298 | if (capture_stderr_) { |
| 299 | stderr_pipes_.read->Read(&stderr_); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | const std::string &Application::GetStdout() { |
| 304 | CHECK(capture_stdout_); |
| 305 | FetchOutputs(); |
| 306 | return stdout_; |
| 307 | } |
| 308 | |
| 309 | const std::string &Application::GetStderr() { |
| 310 | CHECK(capture_stderr_); |
| 311 | FetchOutputs(); |
| 312 | return stderr_; |
| 313 | } |
| 314 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 315 | void Application::DoStop(bool restart) { |
| 316 | // If stop or restart received, the old state of these is no longer applicable |
| 317 | // so cancel both. |
| 318 | restart_timer_->Disable(); |
| 319 | start_timer_->Disable(); |
| 320 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 321 | FetchOutputs(); |
| 322 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 323 | switch (status_) { |
| 324 | case aos::starter::State::STARTING: |
| 325 | case aos::starter::State::RUNNING: { |
| 326 | LOG(INFO) << "Stopping '" << name_ << "' pid: " << pid_ << " with signal " |
| 327 | << SIGINT; |
| 328 | status_ = aos::starter::State::STOPPING; |
| 329 | |
| 330 | kill(pid_, SIGINT); |
| 331 | |
| 332 | // Watchdog timer to SIGKILL application if it is still running 1 second |
| 333 | // after SIGINT |
| 334 | stop_timer_->Setup(event_loop_->monotonic_now() + |
| 335 | std::chrono::seconds(1)); |
| 336 | queue_restart_ = restart; |
| 337 | on_change_(); |
| 338 | break; |
| 339 | } |
| 340 | case aos::starter::State::WAITING: { |
| 341 | // If waiting to restart, and receives restart, skip the waiting period |
| 342 | // and restart immediately. If stop received, all we have to do is move |
| 343 | // to the STOPPED state. |
| 344 | if (restart) { |
| 345 | DoStart(); |
| 346 | } else { |
| 347 | status_ = aos::starter::State::STOPPED; |
| 348 | on_change_(); |
| 349 | } |
| 350 | break; |
| 351 | } |
| 352 | case aos::starter::State::STOPPING: { |
| 353 | // If the application is already stopping, then we just need to update the |
| 354 | // restart flag to the most recent status. |
| 355 | queue_restart_ = restart; |
| 356 | break; |
| 357 | } |
| 358 | case aos::starter::State::STOPPED: { |
| 359 | // Restart immediately if the application is already stopped |
| 360 | if (restart) { |
| 361 | status_ = aos::starter::State::WAITING; |
| 362 | DoStart(); |
| 363 | } |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void Application::QueueStart() { |
| 370 | status_ = aos::starter::State::WAITING; |
| 371 | |
| 372 | LOG(INFO) << "Restarting " << name_ << " in 3 seconds"; |
| 373 | restart_timer_->Setup(event_loop_->monotonic_now() + std::chrono::seconds(3)); |
| 374 | start_timer_->Disable(); |
| 375 | stop_timer_->Disable(); |
| 376 | on_change_(); |
| 377 | } |
| 378 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 379 | std::vector<char *> Application::CArgs() { |
| 380 | std::vector<char *> cargs; |
| 381 | std::transform(args_.begin(), args_.end(), std::back_inserter(cargs), |
| 382 | [](std::string &str) { return str.data(); }); |
| 383 | cargs.push_back(nullptr); |
| 384 | return cargs; |
| 385 | } |
| 386 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 387 | void Application::set_args( |
| 388 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v) { |
| 389 | args_.clear(); |
| 390 | std::transform(v.begin(), v.end(), std::back_inserter(args_), |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 391 | [](const flatbuffers::String *str) { return str->str(); }); |
| 392 | } |
| 393 | |
| 394 | void Application::set_args(std::vector<std::string> args) { |
| 395 | args_ = std::move(args); |
| 396 | } |
| 397 | |
| 398 | void Application::set_capture_stdout(bool capture) { |
| 399 | capture_stdout_ = capture; |
| 400 | } |
| 401 | |
| 402 | void Application::set_capture_stderr(bool capture) { |
| 403 | capture_stderr_ = capture; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | std::optional<uid_t> Application::FindUid(const char *name) { |
| 407 | // TODO(austin): Use the reentrant version. This should be safe. |
| 408 | struct passwd *user_data = getpwnam(name); |
| 409 | if (user_data != nullptr) { |
| 410 | return user_data->pw_uid; |
| 411 | } else { |
| 412 | LOG(FATAL) << "Could not find user " << name; |
| 413 | return std::nullopt; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | std::optional<gid_t> Application::FindPrimaryGidForUser(const char *name) { |
| 418 | // TODO(austin): Use the reentrant version. This should be safe. |
| 419 | struct passwd *user_data = getpwnam(name); |
| 420 | if (user_data != nullptr) { |
| 421 | return user_data->pw_gid; |
| 422 | } else { |
| 423 | LOG(FATAL) << "Could not find user " << name; |
| 424 | return std::nullopt; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | flatbuffers::Offset<aos::starter::ApplicationStatus> |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 429 | Application::PopulateStatus(flatbuffers::FlatBufferBuilder *builder, |
| 430 | util::Top *top) { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 431 | CHECK_NOTNULL(builder); |
| 432 | auto name_fbs = builder->CreateString(name_); |
| 433 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 434 | const bool valid_pid = pid_ > 0 && status_ != aos::starter::State::STOPPED; |
| 435 | const flatbuffers::Offset<util::ProcessInfo> process_info = |
| 436 | valid_pid ? top->InfoForProcess(builder, pid_) |
| 437 | : flatbuffers::Offset<util::ProcessInfo>(); |
| 438 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 439 | aos::starter::ApplicationStatus::Builder status_builder(*builder); |
| 440 | status_builder.add_name(name_fbs); |
| 441 | status_builder.add_state(status_); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 442 | if (exit_code_.has_value()) { |
| 443 | status_builder.add_last_exit_code(exit_code_.value()); |
| 444 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 445 | status_builder.add_last_stop_reason(stop_reason_); |
| 446 | if (pid_ != -1) { |
| 447 | status_builder.add_pid(pid_); |
| 448 | status_builder.add_id(id_); |
| 449 | } |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 450 | // Note that even if process_info is null, calling add_process_info is fine. |
| 451 | status_builder.add_process_info(process_info); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 452 | status_builder.add_last_start_time(start_time_.time_since_epoch().count()); |
| 453 | return status_builder.Finish(); |
| 454 | } |
| 455 | |
| 456 | void Application::Terminate() { |
| 457 | stop_reason_ = aos::starter::LastStopReason::TERMINATE; |
| 458 | DoStop(false); |
| 459 | terminating_ = true; |
| 460 | } |
| 461 | |
| 462 | void Application::HandleCommand(aos::starter::Command cmd) { |
| 463 | switch (cmd) { |
| 464 | case aos::starter::Command::START: { |
| 465 | switch (status_) { |
| 466 | case aos::starter::State::WAITING: { |
| 467 | restart_timer_->Disable(); |
| 468 | DoStart(); |
| 469 | break; |
| 470 | } |
| 471 | case aos::starter::State::STARTING: { |
| 472 | break; |
| 473 | } |
| 474 | case aos::starter::State::RUNNING: { |
| 475 | break; |
| 476 | } |
| 477 | case aos::starter::State::STOPPING: { |
| 478 | queue_restart_ = true; |
| 479 | break; |
| 480 | } |
| 481 | case aos::starter::State::STOPPED: { |
| 482 | status_ = aos::starter::State::WAITING; |
| 483 | DoStart(); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | case aos::starter::Command::STOP: { |
| 490 | stop_reason_ = aos::starter::LastStopReason::STOP_REQUESTED; |
| 491 | DoStop(false); |
| 492 | break; |
| 493 | } |
| 494 | case aos::starter::Command::RESTART: { |
| 495 | stop_reason_ = aos::starter::LastStopReason::RESTART_REQUESTED; |
| 496 | DoStop(true); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | bool Application::MaybeHandleSignal() { |
| 503 | int status; |
| 504 | |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 505 | if (status_ == aos::starter::State::WAITING || |
| 506 | status_ == aos::starter::State::STOPPED) { |
| 507 | // We can't possibly have received a signal meant for this process. |
| 508 | return false; |
| 509 | } |
| 510 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 511 | // Check if the status of this process has changed |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 512 | // The PID won't be -1 if this application has ever been run successfully |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 513 | if (pid_ == -1 || waitpid(pid_, &status, WNOHANG) != pid_) { |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // Check that the event was the process exiting |
| 518 | if (!WIFEXITED(status) && !WIFSIGNALED(status)) { |
| 519 | return false; |
| 520 | } |
| 521 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 522 | start_timer_->Disable(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 523 | exit_time_ = event_loop_->monotonic_now(); |
| 524 | exit_code_ = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status); |
| 525 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 526 | if (auto read_result = status_pipes_.read->Read()) { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 527 | stop_reason_ = static_cast<aos::starter::LastStopReason>(*read_result); |
| 528 | } |
| 529 | |
| 530 | switch (status_) { |
| 531 | case aos::starter::State::STARTING: { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 532 | if (exit_code_.value() == 0) { |
| 533 | LOG(INFO) << "Application '" << name_ << "' pid " << pid_ |
| 534 | << " exited with status " << exit_code_.value(); |
| 535 | } else { |
| 536 | LOG(WARNING) << "Failed to start '" << name_ << "' on pid " << pid_ |
| 537 | << " : Exited with status " << exit_code_.value(); |
| 538 | } |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 539 | if (autorestart()) { |
| 540 | QueueStart(); |
| 541 | } else { |
| 542 | status_ = aos::starter::State::STOPPED; |
| 543 | on_change_(); |
| 544 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | case aos::starter::State::RUNNING: { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 548 | if (exit_code_.value() == 0) { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 549 | LOG(INFO) << "Application '" << name_ << "' pid " << pid_ |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 550 | << " exited with status " << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 551 | } else { |
| 552 | LOG(WARNING) << "Application '" << name_ << "' pid " << pid_ |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 553 | << " exited unexpectedly with status " |
| 554 | << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 555 | } |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 556 | if (autorestart()) { |
| 557 | QueueStart(); |
| 558 | } else { |
| 559 | status_ = aos::starter::State::STOPPED; |
| 560 | on_change_(); |
| 561 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 562 | break; |
| 563 | } |
| 564 | case aos::starter::State::STOPPING: { |
| 565 | LOG(INFO) << "Successfully stopped '" << name_ << "' pid: " << pid_ |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 566 | << " with status " << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 567 | status_ = aos::starter::State::STOPPED; |
| 568 | |
| 569 | // Disable force stop timer since the process already died |
| 570 | stop_timer_->Disable(); |
| 571 | |
| 572 | on_change_(); |
| 573 | if (terminating_) { |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | if (queue_restart_) { |
| 578 | queue_restart_ = false; |
| 579 | status_ = aos::starter::State::WAITING; |
| 580 | DoStart(); |
| 581 | } |
| 582 | break; |
| 583 | } |
| 584 | case aos::starter::State::WAITING: |
| 585 | case aos::starter::State::STOPPED: { |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 586 | __builtin_unreachable(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | } // namespace aos::starter |