1 /*
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
17 #include "sigchld_handler.h"
18
19 #include <signal.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include <android-base/chrono_utils.h>
27 #include <android-base/logging.h>
28 #include <android-base/scopeguard.h>
29 #include <android-base/stringprintf.h>
30
31 #include "init.h"
32 #include "property_service.h"
33 #include "service.h"
34
35 using android::base::StringPrintf;
36 using android::base::boot_clock;
37 using android::base::make_scope_guard;
38
39 namespace android {
40 namespace init {
41
ReapOneProcess()42 static bool ReapOneProcess() {
43 siginfo_t siginfo = {};
44 // This returns a zombie pid or informs us that there are no zombies left to be reaped.
45 // It does NOT reap the pid; that is done below.
46 if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) {
47 PLOG(ERROR) << "waitid failed";
48 return false;
49 }
50
51 auto pid = siginfo.si_pid;
52 if (pid == 0) return false;
53
54 // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
55 // whenever the function returns from this point forward.
56 // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
57 // want the pid to remain valid throughout that (and potentially future) usages.
58 auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
59
60 std::string name;
61 std::string wait_string;
62 Service* service = nullptr;
63
64 if (PropertyChildReap(pid)) {
65 name = "Async property child";
66 } else if (SubcontextChildReap(pid)) {
67 name = "Subcontext";
68 } else {
69 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
70
71 if (service) {
72 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
73 if (service->flags() & SVC_EXEC) {
74 auto exec_duration = boot_clock::now() - service->time_started();
75 auto exec_duration_ms =
76 std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
77 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
78 }
79 } else {
80 name = StringPrintf("Untracked pid %d", pid);
81 }
82 }
83
84 if (siginfo.si_code == CLD_EXITED) {
85 LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
86 } else {
87 LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
88 }
89
90 if (!service) return true;
91
92 service->Reap(siginfo);
93
94 if (service->flags() & SVC_TEMPORARY) {
95 ServiceList::GetInstance().RemoveService(*service);
96 }
97
98 return true;
99 }
100
ReapAnyOutstandingChildren()101 void ReapAnyOutstandingChildren() {
102 while (ReapOneProcess()) {
103 }
104 }
105
106 } // namespace init
107 } // namespace android
108