blob: 8e9e713c4840043a3421a692603288948fe5823b [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>
Bart Van Asschea75f2102023-11-03 10:33:17 -070021#include <sys/signalfd.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070022#include <sys/socket.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070023#include <sys/types.h>
Tom Cherryeeee8312017-07-28 15:22:23 -070024#include <sys/wait.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070025#include <unistd.h>
26
Tom Cherryeeee8312017-07-28 15:22:23 -070027#include <android-base/chrono_utils.h>
Bart Van Asscheea595ba2022-10-21 12:44:02 -070028#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070029#include <android-base/logging.h>
Tom Cherryeeee8312017-07-28 15:22:23 -070030#include <android-base/scopeguard.h>
31#include <android-base/stringprintf.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070032
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010033#include <thread>
34
Bart Van Asschea75f2102023-11-03 10:33:17 -070035#include "epoll.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070036#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070037#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070038#include "service_list.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070039
Tom Cherryeeee8312017-07-28 15:22:23 -070040using android::base::boot_clock;
41using android::base::make_scope_guard;
Bart Van Asscheea595ba2022-10-21 12:44:02 -070042using android::base::ReadFileToString;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010043using android::base::StringPrintf;
44using android::base::Timer;
Tom Cherryeeee8312017-07-28 15:22:23 -070045
Tom Cherry81f5d3e2017-06-22 12:53:17 -070046namespace android {
47namespace init {
48
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010049static pid_t ReapOneProcess() {
Tom Cherryeeee8312017-07-28 15:22:23 -070050 siginfo_t siginfo = {};
51 // This returns a zombie pid or informs us that there are no zombies left to be reaped.
52 // It does NOT reap the pid; that is done below.
53 if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) {
54 PLOG(ERROR) << "waitid failed";
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010055 return 0;
Tom Cherryeeee8312017-07-28 15:22:23 -070056 }
57
Bart Van Asschec7d7ed02022-10-19 10:52:23 -070058 const pid_t pid = siginfo.si_pid;
59 if (pid == 0) {
60 DCHECK_EQ(siginfo.si_signo, 0);
61 return 0;
62 }
63
64 DCHECK_EQ(siginfo.si_signo, SIGCHLD);
Tom Cherryeeee8312017-07-28 15:22:23 -070065
66 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
67 // whenever the function returns from this point forward.
68 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
69 // want the pid to remain valid throughout that (and potentially future) usages.
70 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
71
Tom Cherryeeee8312017-07-28 15:22:23 -070072 std::string name;
73 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070074 Service* service = nullptr;
75
Tom Cherryfe815412019-04-23 15:11:07 -070076 if (SubcontextChildReap(pid)) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070077 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070078 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070079 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
80
81 if (service) {
82 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
83 if (service->flags() & SVC_EXEC) {
84 auto exec_duration = boot_clock::now() - service->time_started();
85 auto exec_duration_ms =
86 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
87 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
Wei Wangf7c2bfe2019-07-31 11:35:18 -070088 } else if (service->flags() & SVC_ONESHOT) {
89 auto exec_duration = boot_clock::now() - service->time_started();
90 auto exec_duration_ms =
91 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration)
92 .count();
93 wait_string = StringPrintf(" oneshot service took %f seconds in background",
94 exec_duration_ms / 1000.0f);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070095 }
96 } else {
97 name = StringPrintf("Untracked pid %d", pid);
98 }
Tom Cherryeeee8312017-07-28 15:22:23 -070099 }
100
Paul Crowleyc73b2152018-04-13 17:38:57 +0000101 if (siginfo.si_code == CLD_EXITED) {
102 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
103 } else {
104 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
Tom Cherryeeee8312017-07-28 15:22:23 -0700105 }
106
David Anderson0fa7c402022-03-07 19:10:57 -0800107 if (!service) {
108 LOG(INFO) << name << " did not have an associated service entry and will not be reaped";
109 return pid;
110 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700111
Paul Crowleyc73b2152018-04-13 17:38:57 +0000112 service->Reap(siginfo);
Tom Cherryeeee8312017-07-28 15:22:23 -0700113
114 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700115 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -0700116 }
117
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100118 return pid;
Tom Cherryeeee8312017-07-28 15:22:23 -0700119}
120
Bart Van Assche9c6b7232023-11-20 14:38:55 -0800121std::set<pid_t> ReapAnyOutstandingChildren() {
122 std::set<pid_t> reaped_pids;
123 for (;;) {
124 const pid_t pid = ReapOneProcess();
125 if (pid <= 0) {
126 return reaped_pids;
127 }
128 reaped_pids.emplace(pid);
129 }
130}
131
132static void ReapAndRemove(std::vector<pid_t>& alive_pids) {
133 for (auto pid : ReapAnyOutstandingChildren()) {
134 const auto it = std::find(alive_pids.begin(), alive_pids.end(), pid);
135 if (it != alive_pids.end()) {
136 alive_pids.erase(it);
137 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700138 }
139}
140
Bart Van Assche1daf88d2023-11-16 11:19:08 -0800141static void HandleSignal(int signal_fd) {
Bart Van Asschea75f2102023-11-03 10:33:17 -0700142 signalfd_siginfo siginfo;
143 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(signal_fd, &siginfo, sizeof(siginfo)));
144 if (bytes_read != sizeof(siginfo)) {
145 LOG(WARNING) << "Unexpected: " << __func__ << " read " << bytes_read << " bytes instead of "
146 << sizeof(siginfo);
147 }
148}
149
150void WaitToBeReaped(int sigchld_fd, const std::vector<pid_t>& pids,
151 std::chrono::milliseconds timeout) {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100152 Timer t;
Bart Van Asschea75f2102023-11-03 10:33:17 -0700153 Epoll epoll;
Bart Van Asschea75f2102023-11-03 10:33:17 -0700154 if (sigchld_fd >= 0) {
Bart Van Assche1daf88d2023-11-16 11:19:08 -0800155 if (auto result = epoll.Open(); result.ok()) {
156 result =
157 epoll.RegisterHandler(sigchld_fd, [sigchld_fd]() { HandleSignal(sigchld_fd); });
158 if (!result.ok()) {
159 LOG(WARNING) << __func__
160 << " RegisterHandler() failed. Falling back to sleep_for(): "
161 << result.error();
162 sigchld_fd = -1;
163 }
Bart Van Asschea75f2102023-11-03 10:33:17 -0700164 } else {
Bart Van Assche1daf88d2023-11-16 11:19:08 -0800165 LOG(WARNING) << __func__ << " Epoll::Open() failed. Falling back to sleep_for(): "
166 << result.error();
167 sigchld_fd = -1;
Bart Van Asschea75f2102023-11-03 10:33:17 -0700168 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100169 }
Bart Van Assche1daf88d2023-11-16 11:19:08 -0800170 std::vector<pid_t> alive_pids(pids);
171 ReapAndRemove(alive_pids);
172 while (!alive_pids.empty() && t.duration() < timeout) {
173 if (sigchld_fd >= 0) {
174 auto result = epoll.Wait(std::max(timeout - t.duration(), 0ms));
175 if (result.ok()) {
176 ReapAndRemove(alive_pids);
177 continue;
178 } else {
179 LOG(WARNING) << "Epoll::Wait() failed " << result.error();
180 }
181 }
182 std::this_thread::sleep_for(50ms);
183 ReapAndRemove(alive_pids);
184 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100185 LOG(INFO) << "Waiting for " << pids.size() << " pids to be reaped took " << t << " with "
186 << alive_pids.size() << " of them still running";
Bart Van Assche7ce64532023-11-01 14:29:07 -0700187 for (pid_t pid : alive_pids) {
Bart Van Asscheea595ba2022-10-21 12:44:02 -0700188 std::string status = "(no-such-pid)";
189 ReadFileToString(StringPrintf("/proc/%d/status", pid), &status);
Bart Van Assche7ce64532023-11-01 14:29:07 -0700190 LOG(INFO) << "Still running: " << pid << '\n' << status;
Bart Van Asscheea595ba2022-10-21 12:44:02 -0700191 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100192}
193
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700194} // namespace init
195} // namespace android