blob: 9b2c7d939ba0dabf102e08432e6bd1d8caa952ec [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>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070027#include <android-base/logging.h>
Tom Cherryeeee8312017-07-28 15:22:23 -070028#include <android-base/scopeguard.h>
29#include <android-base/stringprintf.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070030
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010031#include <thread>
32
Colin Cross9c5366b2010-04-13 19:48:59 -070033#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070034#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070035#include "service_list.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070036
Tom Cherryeeee8312017-07-28 15:22:23 -070037using android::base::boot_clock;
38using android::base::make_scope_guard;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010039using android::base::StringPrintf;
40using android::base::Timer;
Tom Cherryeeee8312017-07-28 15:22:23 -070041
Tom Cherry81f5d3e2017-06-22 12:53:17 -070042namespace android {
43namespace init {
44
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010045static pid_t ReapOneProcess() {
Tom Cherryeeee8312017-07-28 15:22:23 -070046 siginfo_t siginfo = {};
47 // This returns a zombie pid or informs us that there are no zombies left to be reaped.
48 // It does NOT reap the pid; that is done below.
49 if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) {
50 PLOG(ERROR) << "waitid failed";
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010051 return 0;
Tom Cherryeeee8312017-07-28 15:22:23 -070052 }
53
54 auto pid = siginfo.si_pid;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010055 if (pid == 0) return 0;
Tom Cherryeeee8312017-07-28 15:22:23 -070056
57 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
58 // whenever the function returns from this point forward.
59 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
60 // want the pid to remain valid throughout that (and potentially future) usages.
61 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
62
Tom Cherryeeee8312017-07-28 15:22:23 -070063 std::string name;
64 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070065 Service* service = nullptr;
66
Tom Cherryfe815412019-04-23 15:11:07 -070067 if (SubcontextChildReap(pid)) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070068 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070069 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070070 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
71
72 if (service) {
73 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
74 if (service->flags() & SVC_EXEC) {
75 auto exec_duration = boot_clock::now() - service->time_started();
76 auto exec_duration_ms =
77 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
78 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
Wei Wangf7c2bfe2019-07-31 11:35:18 -070079 } else if (service->flags() & SVC_ONESHOT) {
80 auto exec_duration = boot_clock::now() - service->time_started();
81 auto exec_duration_ms =
82 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration)
83 .count();
84 wait_string = StringPrintf(" oneshot service took %f seconds in background",
85 exec_duration_ms / 1000.0f);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070086 }
87 } else {
88 name = StringPrintf("Untracked pid %d", pid);
89 }
Tom Cherryeeee8312017-07-28 15:22:23 -070090 }
91
Paul Crowleyc73b2152018-04-13 17:38:57 +000092 if (siginfo.si_code == CLD_EXITED) {
93 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
94 } else {
95 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
Tom Cherryeeee8312017-07-28 15:22:23 -070096 }
97
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010098 if (!service) return pid;
Tom Cherryeeee8312017-07-28 15:22:23 -070099
Paul Crowleyc73b2152018-04-13 17:38:57 +0000100 service->Reap(siginfo);
Tom Cherryeeee8312017-07-28 15:22:23 -0700101
102 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700103 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -0700104 }
105
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100106 return pid;
Tom Cherryeeee8312017-07-28 15:22:23 -0700107}
108
Tom Cherryeeee8312017-07-28 15:22:23 -0700109void ReapAnyOutstandingChildren() {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100110 while (ReapOneProcess() != 0) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700111 }
112}
113
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100114void WaitToBeReaped(const std::vector<pid_t>& pids, std::chrono::milliseconds timeout) {
115 Timer t;
116 std::vector<pid_t> alive_pids(pids.begin(), pids.end());
117 while (!alive_pids.empty() && t.duration() < timeout) {
118 pid_t pid;
119 while ((pid = ReapOneProcess()) != 0) {
120 auto it = std::find(alive_pids.begin(), alive_pids.end(), pid);
121 if (it != alive_pids.end()) {
122 alive_pids.erase(it);
123 }
124 }
125 if (alive_pids.empty()) {
126 break;
127 }
128 std::this_thread::sleep_for(50ms);
129 }
130 LOG(INFO) << "Waiting for " << pids.size() << " pids to be reaped took " << t << " with "
131 << alive_pids.size() << " of them still running";
132}
133
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700134} // namespace init
135} // namespace android