blob: f8c501f8a832fefbfbd6822004ca4c760111a4cd [file] [log] [blame]
Colin Cross9c5366b2010-04-13 19:48:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070017#include "sigchld_handler.h"
Tom Cherryeeee8312017-07-28 15:22:23 -070018
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <signal.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <string.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070021#include <sys/socket.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022#include <sys/types.h>
Tom Cherryeeee8312017-07-28 15:22:23 -070023#include <sys/wait.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070024#include <unistd.h>
25
Tom Cherryeeee8312017-07-28 15:22:23 -070026#include <android-base/chrono_utils.h>
Bart Van Asscheea595ba2022-10-21 12:44:02 -070027#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070028#include <android-base/logging.h>
Tom Cherryeeee8312017-07-28 15:22:23 -070029#include <android-base/scopeguard.h>
30#include <android-base/stringprintf.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070031
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010032#include <thread>
33
Colin Cross9c5366b2010-04-13 19:48:59 -070034#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070035#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070036#include "service_list.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070037
Tom Cherryeeee8312017-07-28 15:22:23 -070038using android::base::boot_clock;
39using android::base::make_scope_guard;
Bart Van Asscheea595ba2022-10-21 12:44:02 -070040using android::base::ReadFileToString;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010041using android::base::StringPrintf;
42using android::base::Timer;
Tom Cherryeeee8312017-07-28 15:22:23 -070043
Tom Cherry81f5d3e2017-06-22 12:53:17 -070044namespace android {
45namespace init {
46
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010047static pid_t ReapOneProcess() {
Tom Cherryeeee8312017-07-28 15:22:23 -070048 siginfo_t siginfo = {};
49 // This returns a zombie pid or informs us that there are no zombies left to be reaped.
50 // It does NOT reap the pid; that is done below.
51 if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) {
52 PLOG(ERROR) << "waitid failed";
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010053 return 0;
Tom Cherryeeee8312017-07-28 15:22:23 -070054 }
55
Bart Van Asschec7d7ed02022-10-19 10:52:23 -070056 const pid_t pid = siginfo.si_pid;
57 if (pid == 0) {
58 DCHECK_EQ(siginfo.si_signo, 0);
59 return 0;
60 }
61
62 DCHECK_EQ(siginfo.si_signo, SIGCHLD);
Tom Cherryeeee8312017-07-28 15:22:23 -070063
64 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
65 // whenever the function returns from this point forward.
66 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
67 // want the pid to remain valid throughout that (and potentially future) usages.
68 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
69
Tom Cherryeeee8312017-07-28 15:22:23 -070070 std::string name;
71 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070072 Service* service = nullptr;
73
Tom Cherryfe815412019-04-23 15:11:07 -070074 if (SubcontextChildReap(pid)) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070075 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070076 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070077 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
78
79 if (service) {
80 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
81 if (service->flags() & SVC_EXEC) {
82 auto exec_duration = boot_clock::now() - service->time_started();
83 auto exec_duration_ms =
84 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
85 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
Wei Wangf7c2bfe2019-07-31 11:35:18 -070086 } else if (service->flags() & SVC_ONESHOT) {
87 auto exec_duration = boot_clock::now() - service->time_started();
88 auto exec_duration_ms =
89 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration)
90 .count();
91 wait_string = StringPrintf(" oneshot service took %f seconds in background",
92 exec_duration_ms / 1000.0f);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070093 }
94 } else {
95 name = StringPrintf("Untracked pid %d", pid);
96 }
Tom Cherryeeee8312017-07-28 15:22:23 -070097 }
98
Paul Crowleyc73b2152018-04-13 17:38:57 +000099 if (siginfo.si_code == CLD_EXITED) {
100 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
101 } else {
102 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
Tom Cherryeeee8312017-07-28 15:22:23 -0700103 }
104
David Anderson0fa7c402022-03-07 19:10:57 -0800105 if (!service) {
106 LOG(INFO) << name << " did not have an associated service entry and will not be reaped";
107 return pid;
108 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700109
Paul Crowleyc73b2152018-04-13 17:38:57 +0000110 service->Reap(siginfo);
Tom Cherryeeee8312017-07-28 15:22:23 -0700111
112 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700113 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -0700114 }
115
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100116 return pid;
Tom Cherryeeee8312017-07-28 15:22:23 -0700117}
118
Tom Cherryeeee8312017-07-28 15:22:23 -0700119void ReapAnyOutstandingChildren() {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100120 while (ReapOneProcess() != 0) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700121 }
122}
123
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100124void WaitToBeReaped(const std::vector<pid_t>& pids, std::chrono::milliseconds timeout) {
125 Timer t;
126 std::vector<pid_t> alive_pids(pids.begin(), pids.end());
127 while (!alive_pids.empty() && t.duration() < timeout) {
128 pid_t pid;
129 while ((pid = ReapOneProcess()) != 0) {
130 auto it = std::find(alive_pids.begin(), alive_pids.end(), pid);
131 if (it != alive_pids.end()) {
132 alive_pids.erase(it);
133 }
134 }
135 if (alive_pids.empty()) {
136 break;
137 }
138 std::this_thread::sleep_for(50ms);
139 }
140 LOG(INFO) << "Waiting for " << pids.size() << " pids to be reaped took " << t << " with "
141 << alive_pids.size() << " of them still running";
Bart Van Asscheea595ba2022-10-21 12:44:02 -0700142 for (pid_t pid : pids) {
143 std::string status = "(no-such-pid)";
144 ReadFileToString(StringPrintf("/proc/%d/status", pid), &status);
145 LOG(INFO) << "Still running: " << pid << ' ' << status;
146 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100147}
148
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700149} // namespace init
150} // namespace android