blob: 987b2f98c9606cd5289b08c1850acf9c515fc20e [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
31#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070032#include "service.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070033
Tom Cherryeeee8312017-07-28 15:22:23 -070034using android::base::StringPrintf;
35using android::base::boot_clock;
36using android::base::make_scope_guard;
37
Tom Cherry81f5d3e2017-06-22 12:53:17 -070038namespace android {
39namespace init {
40
Tom Cherryeeee8312017-07-28 15:22:23 -070041static bool ReapOneProcess() {
42 siginfo_t siginfo = {};
43 // This returns a zombie pid or informs us that there are no zombies left to be reaped.
44 // It does NOT reap the pid; that is done below.
45 if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) {
46 PLOG(ERROR) << "waitid failed";
47 return false;
48 }
49
50 auto pid = siginfo.si_pid;
51 if (pid == 0) return false;
52
53 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
54 // whenever the function returns from this point forward.
55 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
56 // want the pid to remain valid throughout that (and potentially future) usages.
57 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
58
Tom Cherryeeee8312017-07-28 15:22:23 -070059 std::string name;
60 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070061 Service* service = nullptr;
62
Tom Cherryfe815412019-04-23 15:11:07 -070063 if (SubcontextChildReap(pid)) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070064 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070065 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070066 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
67
68 if (service) {
69 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
70 if (service->flags() & SVC_EXEC) {
71 auto exec_duration = boot_clock::now() - service->time_started();
72 auto exec_duration_ms =
73 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
74 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
75 }
76 } else {
77 name = StringPrintf("Untracked pid %d", pid);
78 }
Tom Cherryeeee8312017-07-28 15:22:23 -070079 }
80
Paul Crowleyc73b2152018-04-13 17:38:57 +000081 if (siginfo.si_code == CLD_EXITED) {
82 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
83 } else {
84 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
Tom Cherryeeee8312017-07-28 15:22:23 -070085 }
86
87 if (!service) return true;
88
Paul Crowleyc73b2152018-04-13 17:38:57 +000089 service->Reap(siginfo);
Tom Cherryeeee8312017-07-28 15:22:23 -070090
91 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -070092 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -070093 }
94
95 return true;
96}
97
Tom Cherryeeee8312017-07-28 15:22:23 -070098void ReapAnyOutstandingChildren() {
99 while (ReapOneProcess()) {
100 }
101}
102
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700103} // namespace init
104} // namespace android