blob: 6a6050b2aab5cbc5a073dd89afb327286d46e19e [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
56 auto pid = siginfo.si_pid;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010057 if (pid == 0) return 0;
Tom Cherryeeee8312017-07-28 15:22:23 -070058
59 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
60 // whenever the function returns from this point forward.
61 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
62 // want the pid to remain valid throughout that (and potentially future) usages.
63 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
64
Tom Cherryeeee8312017-07-28 15:22:23 -070065 std::string name;
66 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070067 Service* service = nullptr;
68
Tom Cherryfe815412019-04-23 15:11:07 -070069 if (SubcontextChildReap(pid)) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070070 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070071 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070072 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
73
74 if (service) {
75 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
76 if (service->flags() & SVC_EXEC) {
77 auto exec_duration = boot_clock::now() - service->time_started();
78 auto exec_duration_ms =
79 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
80 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
Wei Wangf7c2bfe2019-07-31 11:35:18 -070081 } else if (service->flags() & SVC_ONESHOT) {
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)
85 .count();
86 wait_string = StringPrintf(" oneshot service took %f seconds in background",
87 exec_duration_ms / 1000.0f);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070088 }
89 } else {
90 name = StringPrintf("Untracked pid %d", pid);
91 }
Tom Cherryeeee8312017-07-28 15:22:23 -070092 }
93
Paul Crowleyc73b2152018-04-13 17:38:57 +000094 if (siginfo.si_code == CLD_EXITED) {
95 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
96 } else {
97 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
Tom Cherryeeee8312017-07-28 15:22:23 -070098 }
99
David Anderson0fa7c402022-03-07 19:10:57 -0800100 if (!service) {
101 LOG(INFO) << name << " did not have an associated service entry and will not be reaped";
102 return pid;
103 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700104
Paul Crowleyc73b2152018-04-13 17:38:57 +0000105 service->Reap(siginfo);
Tom Cherryeeee8312017-07-28 15:22:23 -0700106
107 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700108 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -0700109 }
110
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100111 return pid;
Tom Cherryeeee8312017-07-28 15:22:23 -0700112}
113
Tom Cherryeeee8312017-07-28 15:22:23 -0700114void ReapAnyOutstandingChildren() {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100115 while (ReapOneProcess() != 0) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700116 }
117}
118
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100119void WaitToBeReaped(const std::vector<pid_t>& pids, std::chrono::milliseconds timeout) {
120 Timer t;
121 std::vector<pid_t> alive_pids(pids.begin(), pids.end());
122 while (!alive_pids.empty() && t.duration() < timeout) {
123 pid_t pid;
124 while ((pid = ReapOneProcess()) != 0) {
125 auto it = std::find(alive_pids.begin(), alive_pids.end(), pid);
126 if (it != alive_pids.end()) {
127 alive_pids.erase(it);
128 }
129 }
130 if (alive_pids.empty()) {
131 break;
132 }
133 std::this_thread::sleep_for(50ms);
134 }
135 LOG(INFO) << "Waiting for " << pids.size() << " pids to be reaped took " << t << " with "
136 << alive_pids.size() << " of them still running";
Bart Van Asscheea595ba2022-10-21 12:44:02 -0700137 for (pid_t pid : pids) {
138 std::string status = "(no-such-pid)";
139 ReadFileToString(StringPrintf("/proc/%d/status", pid), &status);
140 LOG(INFO) << "Still running: " << pid << ' ' << status;
141 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100142}
143
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700144} // namespace init
145} // namespace android