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) |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 80 | : SignalListener(loop->epoll(), std::move(callback)) {} |
| 81 | |
| 82 | SignalListener::SignalListener(aos::internal::EPoll *epoll, |
| 83 | std::function<void(signalfd_siginfo)> callback) |
| 84 | : SignalListener(epoll, callback, |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 85 | {SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGFPE, SIGSEGV, |
| 86 | SIGPIPE, SIGTERM, SIGBUS, SIGXCPU, SIGCHLD}) {} |
| 87 | |
| 88 | SignalListener::SignalListener(aos::ShmEventLoop *loop, |
| 89 | std::function<void(signalfd_siginfo)> callback, |
| 90 | std::initializer_list<unsigned int> signals) |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 91 | : SignalListener(loop->epoll(), std::move(callback), std::move(signals)) {} |
| 92 | |
| 93 | SignalListener::SignalListener(aos::internal::EPoll *epoll, |
| 94 | std::function<void(signalfd_siginfo)> callback, |
| 95 | std::initializer_list<unsigned int> signals) |
| 96 | : epoll_(epoll), callback_(std::move(callback)), signalfd_(signals) { |
| 97 | epoll_->OnReadable(signalfd_.fd(), [this] { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 98 | signalfd_siginfo info = signalfd_.Read(); |
| 99 | |
| 100 | if (info.ssi_signo == 0) { |
| 101 | LOG(WARNING) << "Could not read " << sizeof(signalfd_siginfo) << " bytes"; |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | callback_(info); |
| 106 | }); |
| 107 | } |
| 108 | |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 109 | SignalListener::~SignalListener() { epoll_->DeleteFd(signalfd_.fd()); } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 110 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 111 | Application::Application(std::string_view name, |
| 112 | std::string_view executable_name, |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 113 | aos::EventLoop *event_loop, |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 114 | std::function<void()> on_change, |
| 115 | QuietLogging quiet_flag) |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 116 | : name_(name), |
| 117 | path_(executable_name), |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 118 | event_loop_(event_loop), |
| 119 | start_timer_(event_loop_->AddTimer([this] { |
| 120 | status_ = aos::starter::State::RUNNING; |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 121 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 122 | << "Started '" << name_ << "' pid: " << pid_; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 123 | })), |
| 124 | restart_timer_(event_loop_->AddTimer([this] { DoStart(); })), |
| 125 | stop_timer_(event_loop_->AddTimer([this] { |
| 126 | if (kill(pid_, SIGKILL) == 0) { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 127 | LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo) |
| 128 | << "Failed to stop, sending SIGKILL to '" << name_ |
| 129 | << "' pid: " << pid_; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 130 | } |
| 131 | })), |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 132 | pipe_timer_(event_loop_->AddTimer([this]() { FetchOutputs(); })), |
| 133 | child_status_handler_( |
| 134 | event_loop_->AddTimer([this]() { MaybeHandleSignal(); })), |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 135 | on_change_({on_change}), |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 136 | quiet_flag_(quiet_flag) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 137 | event_loop_->OnRun([this]() { |
| 138 | // Every second poll to check if the child is dead. This is used as a |
| 139 | // default for the case where the user is not directly catching SIGCHLD and |
| 140 | // calling MaybeHandleSignal for us. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 141 | child_status_handler_->Schedule(event_loop_->monotonic_now(), |
| 142 | std::chrono::seconds(1)); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 143 | }); |
| 144 | } |
| 145 | |
| 146 | Application::Application(const aos::Application *application, |
| 147 | aos::EventLoop *event_loop, |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 148 | std::function<void()> on_change, |
| 149 | QuietLogging quiet_flag) |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 150 | : Application(application->name()->string_view(), |
| 151 | application->has_executable_name() |
| 152 | ? application->executable_name()->string_view() |
| 153 | : application->name()->string_view(), |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 154 | event_loop, on_change, quiet_flag) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 155 | user_name_ = application->has_user() ? application->user()->str() : ""; |
| 156 | user_ = application->has_user() ? FindUid(user_name_.c_str()) : std::nullopt; |
| 157 | group_ = application->has_user() ? FindPrimaryGidForUser(user_name_.c_str()) |
| 158 | : std::nullopt; |
| 159 | autostart_ = application->autostart(); |
| 160 | autorestart_ = application->autorestart(); |
| 161 | if (application->has_args()) { |
| 162 | set_args(*application->args()); |
| 163 | } |
Austin Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 164 | |
| 165 | if (application->has_memory_limit() && application->memory_limit() > 0) { |
| 166 | SetMemoryLimit(application->memory_limit()); |
| 167 | } |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 168 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 169 | |
| 170 | void Application::DoStart() { |
| 171 | if (status_ != aos::starter::State::WAITING) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | start_timer_->Disable(); |
| 176 | restart_timer_->Disable(); |
| 177 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 178 | status_pipes_ = util::ScopedPipe::MakePipe(); |
| 179 | |
| 180 | if (capture_stdout_) { |
| 181 | stdout_pipes_ = util::ScopedPipe::MakePipe(); |
| 182 | stdout_.clear(); |
| 183 | } |
| 184 | if (capture_stderr_) { |
| 185 | stderr_pipes_ = util::ScopedPipe::MakePipe(); |
| 186 | stderr_.clear(); |
| 187 | } |
| 188 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 189 | pipe_timer_->Schedule(event_loop_->monotonic_now(), |
| 190 | std::chrono::milliseconds(100)); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 191 | |
| 192 | const pid_t pid = fork(); |
| 193 | |
| 194 | if (pid != 0) { |
| 195 | if (pid == -1) { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 196 | PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo) |
| 197 | << "Failed to fork '" << name_ << "'"; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 198 | stop_reason_ = aos::starter::LastStopReason::FORK_ERR; |
| 199 | status_ = aos::starter::State::STOPPED; |
| 200 | } else { |
| 201 | pid_ = pid; |
| 202 | id_ = next_id_++; |
| 203 | start_time_ = event_loop_->monotonic_now(); |
| 204 | status_ = aos::starter::State::STARTING; |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 205 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 206 | << "Starting '" << name_ << "' pid " << pid_; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 207 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 208 | // Set up timer which moves application to RUNNING state if it is still |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 209 | // alive in 1 second. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 210 | start_timer_->Schedule(event_loop_->monotonic_now() + |
| 211 | std::chrono::seconds(1)); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 212 | // Since we are the parent process, clear our write-side of all the pipes. |
| 213 | status_pipes_.write.reset(); |
| 214 | stdout_pipes_.write.reset(); |
| 215 | stderr_pipes_.write.reset(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 216 | } |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 217 | OnChange(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
Austin Schuh | bbeb37e | 2022-08-17 16:19:27 -0700 | [diff] [blame] | 221 | if (memory_cgroup_) { |
| 222 | memory_cgroup_->AddTid(); |
| 223 | } |
| 224 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 225 | // Since we are the child process, clear our read-side of all the pipes. |
| 226 | status_pipes_.read.reset(); |
| 227 | stdout_pipes_.read.reset(); |
| 228 | stderr_pipes_.read.reset(); |
| 229 | |
| 230 | // The status pipe will not be needed if the execve succeeds. |
| 231 | status_pipes_.write->SetCloexec(); |
| 232 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 233 | // Clear out signal mask of parent so forked process receives all signals |
| 234 | // normally. |
| 235 | sigset_t empty_mask; |
| 236 | sigemptyset(&empty_mask); |
| 237 | sigprocmask(SIG_SETMASK, &empty_mask, nullptr); |
| 238 | |
| 239 | // Cleanup children if starter dies in a way that is not handled gracefully. |
| 240 | if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 241 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 242 | static_cast<uint32_t>(aos::starter::LastStopReason::SET_PRCTL_ERR)); |
| 243 | PLOG(FATAL) << "Could not set PR_SET_PDEATHSIG to SIGKILL"; |
| 244 | } |
| 245 | |
| 246 | if (group_) { |
| 247 | CHECK(!user_name_.empty()); |
| 248 | // The manpage for setgroups says we just need CAP_SETGID, but empirically |
| 249 | // we also need the effective UID to be 0 to make it work. user_ must also |
| 250 | // be set so we change this effective UID back later. |
| 251 | CHECK(user_); |
| 252 | if (seteuid(0) == -1) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 253 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 254 | static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR)); |
| 255 | PLOG(FATAL) << "Could not seteuid(0) for " << name_ |
| 256 | << " in preparation for setting groups"; |
| 257 | } |
| 258 | if (initgroups(user_name_.c_str(), *group_) == -1) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 259 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 260 | static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR)); |
| 261 | PLOG(FATAL) << "Could not initialize normal groups for " << name_ |
| 262 | << " as " << user_name_ << " with " << *group_; |
| 263 | } |
| 264 | if (setgid(*group_) == -1) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 265 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 266 | static_cast<uint32_t>(aos::starter::LastStopReason::SET_GRP_ERR)); |
| 267 | PLOG(FATAL) << "Could not set group for " << name_ << " to " << *group_; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (user_) { |
| 272 | if (setuid(*user_) == -1) { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 273 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 274 | static_cast<uint32_t>(aos::starter::LastStopReason::SET_USR_ERR)); |
| 275 | PLOG(FATAL) << "Could not set user for " << name_ << " to " << *user_; |
| 276 | } |
| 277 | } |
| 278 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 279 | if (capture_stdout_) { |
| 280 | PCHECK(STDOUT_FILENO == dup2(stdout_pipes_.write->fd(), STDOUT_FILENO)); |
| 281 | stdout_pipes_.write.reset(); |
| 282 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 283 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 284 | if (capture_stderr_) { |
| 285 | PCHECK(STDERR_FILENO == dup2(stderr_pipes_.write->fd(), STDERR_FILENO)); |
| 286 | stderr_pipes_.write.reset(); |
| 287 | } |
| 288 | |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 289 | if (run_as_sudo_) { |
Sarah Newman | 6d1e53b | 2022-08-09 14:38:08 -0700 | [diff] [blame] | 290 | // For sudo we must supply the actual path |
| 291 | args_.insert(args_.begin(), path_); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 292 | args_.insert(args_.begin(), kSudo); |
Sarah Newman | 6d1e53b | 2022-08-09 14:38:08 -0700 | [diff] [blame] | 293 | } else { |
| 294 | // argv[0] should be the program name |
| 295 | args_.insert(args_.begin(), name_); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 296 | } |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 297 | |
| 298 | std::vector<char *> cargs = CArgs(); |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 299 | const char *path = run_as_sudo_ ? kSudo : path_.c_str(); |
Sanjay Narayanan | 01a228f | 2022-04-26 14:19:30 -0700 | [diff] [blame] | 300 | execvp(path, cargs.data()); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 301 | |
| 302 | // If we got here, something went wrong |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 303 | status_pipes_.write->Write( |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 304 | static_cast<uint32_t>(aos::starter::LastStopReason::EXECV_ERR)); |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 305 | PLOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo) |
| 306 | << "Could not execute " << name_ << " (" << path_ << ')'; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 307 | |
| 308 | _exit(EXIT_FAILURE); |
| 309 | } |
| 310 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 311 | void Application::FetchOutputs() { |
| 312 | if (capture_stdout_) { |
| 313 | stdout_pipes_.read->Read(&stdout_); |
| 314 | } |
| 315 | if (capture_stderr_) { |
| 316 | stderr_pipes_.read->Read(&stderr_); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | const std::string &Application::GetStdout() { |
| 321 | CHECK(capture_stdout_); |
| 322 | FetchOutputs(); |
| 323 | return stdout_; |
| 324 | } |
| 325 | |
| 326 | const std::string &Application::GetStderr() { |
| 327 | CHECK(capture_stderr_); |
| 328 | FetchOutputs(); |
| 329 | return stderr_; |
| 330 | } |
| 331 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 332 | void Application::DoStop(bool restart) { |
| 333 | // If stop or restart received, the old state of these is no longer applicable |
| 334 | // so cancel both. |
| 335 | restart_timer_->Disable(); |
| 336 | start_timer_->Disable(); |
| 337 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 338 | FetchOutputs(); |
| 339 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 340 | switch (status_) { |
| 341 | case aos::starter::State::STARTING: |
| 342 | case aos::starter::State::RUNNING: { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 343 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 344 | << "Stopping '" << name_ << "' pid: " << pid_ << " with signal " |
| 345 | << SIGINT; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 346 | status_ = aos::starter::State::STOPPING; |
| 347 | |
| 348 | kill(pid_, SIGINT); |
| 349 | |
| 350 | // Watchdog timer to SIGKILL application if it is still running 1 second |
| 351 | // after SIGINT |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 352 | stop_timer_->Schedule(event_loop_->monotonic_now() + |
| 353 | std::chrono::seconds(1)); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 354 | queue_restart_ = restart; |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 355 | OnChange(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | case aos::starter::State::WAITING: { |
| 359 | // If waiting to restart, and receives restart, skip the waiting period |
| 360 | // and restart immediately. If stop received, all we have to do is move |
| 361 | // to the STOPPED state. |
| 362 | if (restart) { |
| 363 | DoStart(); |
| 364 | } else { |
| 365 | status_ = aos::starter::State::STOPPED; |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 366 | OnChange(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 367 | } |
| 368 | break; |
| 369 | } |
| 370 | case aos::starter::State::STOPPING: { |
| 371 | // If the application is already stopping, then we just need to update the |
| 372 | // restart flag to the most recent status. |
| 373 | queue_restart_ = restart; |
| 374 | break; |
| 375 | } |
| 376 | case aos::starter::State::STOPPED: { |
| 377 | // Restart immediately if the application is already stopped |
| 378 | if (restart) { |
| 379 | status_ = aos::starter::State::WAITING; |
| 380 | DoStart(); |
| 381 | } |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void Application::QueueStart() { |
| 388 | status_ = aos::starter::State::WAITING; |
| 389 | |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 390 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 391 | << "Restarting " << name_ << " in 3 seconds"; |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 392 | restart_timer_->Schedule(event_loop_->monotonic_now() + |
| 393 | std::chrono::seconds(3)); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 394 | start_timer_->Disable(); |
| 395 | stop_timer_->Disable(); |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 396 | OnChange(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 397 | } |
| 398 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 399 | std::vector<char *> Application::CArgs() { |
| 400 | std::vector<char *> cargs; |
| 401 | std::transform(args_.begin(), args_.end(), std::back_inserter(cargs), |
| 402 | [](std::string &str) { return str.data(); }); |
| 403 | cargs.push_back(nullptr); |
| 404 | return cargs; |
| 405 | } |
| 406 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 407 | void Application::set_args( |
| 408 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> &v) { |
| 409 | args_.clear(); |
| 410 | std::transform(v.begin(), v.end(), std::back_inserter(args_), |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 411 | [](const flatbuffers::String *str) { return str->str(); }); |
| 412 | } |
| 413 | |
| 414 | void Application::set_args(std::vector<std::string> args) { |
| 415 | args_ = std::move(args); |
| 416 | } |
| 417 | |
| 418 | void Application::set_capture_stdout(bool capture) { |
| 419 | capture_stdout_ = capture; |
| 420 | } |
| 421 | |
| 422 | void Application::set_capture_stderr(bool capture) { |
| 423 | capture_stderr_ = capture; |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | std::optional<uid_t> Application::FindUid(const char *name) { |
| 427 | // TODO(austin): Use the reentrant version. This should be safe. |
| 428 | struct passwd *user_data = getpwnam(name); |
| 429 | if (user_data != nullptr) { |
| 430 | return user_data->pw_uid; |
| 431 | } else { |
| 432 | LOG(FATAL) << "Could not find user " << name; |
| 433 | return std::nullopt; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | std::optional<gid_t> Application::FindPrimaryGidForUser(const char *name) { |
| 438 | // TODO(austin): Use the reentrant version. This should be safe. |
| 439 | struct passwd *user_data = getpwnam(name); |
| 440 | if (user_data != nullptr) { |
| 441 | return user_data->pw_gid; |
| 442 | } else { |
| 443 | LOG(FATAL) << "Could not find user " << name; |
| 444 | return std::nullopt; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | flatbuffers::Offset<aos::starter::ApplicationStatus> |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 449 | Application::PopulateStatus(flatbuffers::FlatBufferBuilder *builder, |
| 450 | util::Top *top) { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 451 | CHECK_NOTNULL(builder); |
| 452 | auto name_fbs = builder->CreateString(name_); |
| 453 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 454 | const bool valid_pid = pid_ > 0 && status_ != aos::starter::State::STOPPED; |
| 455 | const flatbuffers::Offset<util::ProcessInfo> process_info = |
| 456 | valid_pid ? top->InfoForProcess(builder, pid_) |
| 457 | : flatbuffers::Offset<util::ProcessInfo>(); |
| 458 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 459 | aos::starter::ApplicationStatus::Builder status_builder(*builder); |
| 460 | status_builder.add_name(name_fbs); |
| 461 | status_builder.add_state(status_); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 462 | if (exit_code_.has_value()) { |
| 463 | status_builder.add_last_exit_code(exit_code_.value()); |
| 464 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 465 | status_builder.add_last_stop_reason(stop_reason_); |
| 466 | if (pid_ != -1) { |
| 467 | status_builder.add_pid(pid_); |
| 468 | status_builder.add_id(id_); |
| 469 | } |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 470 | // Note that even if process_info is null, calling add_process_info is fine. |
| 471 | status_builder.add_process_info(process_info); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 472 | status_builder.add_last_start_time(start_time_.time_since_epoch().count()); |
| 473 | return status_builder.Finish(); |
| 474 | } |
| 475 | |
| 476 | void Application::Terminate() { |
| 477 | stop_reason_ = aos::starter::LastStopReason::TERMINATE; |
| 478 | DoStop(false); |
| 479 | terminating_ = true; |
| 480 | } |
| 481 | |
| 482 | void Application::HandleCommand(aos::starter::Command cmd) { |
| 483 | switch (cmd) { |
| 484 | case aos::starter::Command::START: { |
| 485 | switch (status_) { |
| 486 | case aos::starter::State::WAITING: { |
| 487 | restart_timer_->Disable(); |
| 488 | DoStart(); |
| 489 | break; |
| 490 | } |
| 491 | case aos::starter::State::STARTING: { |
| 492 | break; |
| 493 | } |
| 494 | case aos::starter::State::RUNNING: { |
| 495 | break; |
| 496 | } |
| 497 | case aos::starter::State::STOPPING: { |
| 498 | queue_restart_ = true; |
| 499 | break; |
| 500 | } |
| 501 | case aos::starter::State::STOPPED: { |
| 502 | status_ = aos::starter::State::WAITING; |
| 503 | DoStart(); |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | case aos::starter::Command::STOP: { |
| 510 | stop_reason_ = aos::starter::LastStopReason::STOP_REQUESTED; |
| 511 | DoStop(false); |
| 512 | break; |
| 513 | } |
| 514 | case aos::starter::Command::RESTART: { |
| 515 | stop_reason_ = aos::starter::LastStopReason::RESTART_REQUESTED; |
| 516 | DoStop(true); |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | bool Application::MaybeHandleSignal() { |
| 523 | int status; |
| 524 | |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 525 | if (status_ == aos::starter::State::WAITING || |
| 526 | status_ == aos::starter::State::STOPPED) { |
| 527 | // We can't possibly have received a signal meant for this process. |
| 528 | return false; |
| 529 | } |
| 530 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 531 | // Check if the status of this process has changed |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 532 | // 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] | 533 | if (pid_ == -1 || waitpid(pid_, &status, WNOHANG) != pid_) { |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | // Check that the event was the process exiting |
| 538 | if (!WIFEXITED(status) && !WIFSIGNALED(status)) { |
| 539 | return false; |
| 540 | } |
| 541 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 542 | start_timer_->Disable(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 543 | exit_time_ = event_loop_->monotonic_now(); |
| 544 | exit_code_ = WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status); |
| 545 | |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 546 | if (auto read_result = status_pipes_.read->Read()) { |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 547 | stop_reason_ = static_cast<aos::starter::LastStopReason>(*read_result); |
| 548 | } |
| 549 | |
| 550 | switch (status_) { |
| 551 | case aos::starter::State::STARTING: { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 552 | if (exit_code_.value() == 0) { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 553 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 554 | << "Application '" << name_ << "' pid " << pid_ |
| 555 | << " exited with status " << exit_code_.value(); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 556 | } else { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 557 | LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo) |
| 558 | << "Failed to start '" << name_ << "' on pid " << pid_ |
| 559 | << " : Exited with status " << exit_code_.value(); |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 560 | } |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 561 | if (autorestart()) { |
| 562 | QueueStart(); |
| 563 | } else { |
| 564 | status_ = aos::starter::State::STOPPED; |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 565 | OnChange(); |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 566 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | case aos::starter::State::RUNNING: { |
James Kuszmaul | d42edb4 | 2022-01-07 18:00:16 -0800 | [diff] [blame] | 570 | if (exit_code_.value() == 0) { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 571 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 572 | << "Application '" << name_ << "' pid " << pid_ |
| 573 | << " exited with status " << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 574 | } else { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 575 | LOG_IF(WARNING, quiet_flag_ == QuietLogging::kNo) |
| 576 | << "Application '" << name_ << "' pid " << pid_ |
| 577 | << " exited unexpectedly with status " << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 578 | } |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 579 | if (autorestart()) { |
| 580 | QueueStart(); |
| 581 | } else { |
| 582 | status_ = aos::starter::State::STOPPED; |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 583 | OnChange(); |
James Kuszmaul | 6f10b38 | 2022-03-11 22:31:38 -0800 | [diff] [blame] | 584 | } |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | case aos::starter::State::STOPPING: { |
payton.rehl | 2841b1c | 2023-05-25 17:23:55 -0700 | [diff] [blame] | 588 | LOG_IF(INFO, quiet_flag_ == QuietLogging::kNo) |
| 589 | << "Successfully stopped '" << name_ << "' pid: " << pid_ |
| 590 | << " with status " << exit_code_.value(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 591 | status_ = aos::starter::State::STOPPED; |
| 592 | |
| 593 | // Disable force stop timer since the process already died |
| 594 | stop_timer_->Disable(); |
| 595 | |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 596 | OnChange(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 597 | if (terminating_) { |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | if (queue_restart_) { |
| 602 | queue_restart_ = false; |
| 603 | status_ = aos::starter::State::WAITING; |
| 604 | DoStart(); |
| 605 | } |
| 606 | break; |
| 607 | } |
| 608 | case aos::starter::State::WAITING: |
| 609 | case aos::starter::State::STOPPED: { |
Sarah Newman | 21c5920 | 2022-06-16 12:36:33 -0700 | [diff] [blame] | 610 | __builtin_unreachable(); |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return false; |
| 616 | } |
| 617 | |
Austin Schuh | 1cea903 | 2023-07-10 11:56:40 -0700 | [diff] [blame^] | 618 | void Application::OnChange() { |
| 619 | for (auto &fn : on_change_) { |
| 620 | fn(); |
| 621 | } |
| 622 | } |
| 623 | |
James Kuszmaul | 3224b8e | 2022-01-07 19:00:39 -0800 | [diff] [blame] | 624 | } // namespace aos::starter |