• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/scopeguard.h>
30 #include <android-base/stringprintf.h>
31 
32 #include <thread>
33 
34 #include "init.h"
35 #include "service.h"
36 #include "service_list.h"
37 
38 using android::base::boot_clock;
39 using android::base::make_scope_guard;
40 using android::base::ReadFileToString;
41 using android::base::StringPrintf;
42 using android::base::Timer;
43 
44 namespace android {
45 namespace init {
46 
ReapOneProcess()47 static pid_t ReapOneProcess() {
48     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";
53         return 0;
54     }
55 
56     const pid_t pid = siginfo.si_pid;
57     if (pid == 0) {
58         DCHECK_EQ(siginfo.si_signo, 0);
59         return 0;
60     }
61 
62     DCHECK_EQ(siginfo.si_signo, SIGCHLD);
63 
64     // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid
65     // whenever the function returns from this point forward.
66     // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we
67     // want the pid to remain valid throughout that (and potentially future) usages.
68     auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); });
69 
70     std::string name;
71     std::string wait_string;
72     Service* service = nullptr;
73 
74     if (SubcontextChildReap(pid)) {
75         name = "Subcontext";
76     } else {
77         service = ServiceList::GetInstance().FindService(pid, &Service::pid);
78 
79         if (service) {
80             name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
81             if (service->flags() & SVC_EXEC) {
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).count();
85                 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
86             } else if (service->flags() & SVC_ONESHOT) {
87                 auto exec_duration = boot_clock::now() - service->time_started();
88                 auto exec_duration_ms =
89                         std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration)
90                                 .count();
91                 wait_string = StringPrintf(" oneshot service took %f seconds in background",
92                                            exec_duration_ms / 1000.0f);
93             }
94         } else {
95             name = StringPrintf("Untracked pid %d", pid);
96         }
97     }
98 
99     if (siginfo.si_code == CLD_EXITED) {
100         LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
101     } else {
102         LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
103     }
104 
105     if (!service) {
106         LOG(INFO) << name << " did not have an associated service entry and will not be reaped";
107         return pid;
108     }
109 
110     service->Reap(siginfo);
111 
112     if (service->flags() & SVC_TEMPORARY) {
113         ServiceList::GetInstance().RemoveService(*service);
114     }
115 
116     return pid;
117 }
118 
ReapAnyOutstandingChildren()119 void ReapAnyOutstandingChildren() {
120     while (ReapOneProcess() != 0) {
121     }
122 }
123 
WaitToBeReaped(const std::vector<pid_t> & pids,std::chrono::milliseconds timeout)124 void WaitToBeReaped(const std::vector<pid_t>& pids, std::chrono::milliseconds timeout) {
125     Timer t;
126     std::vector<pid_t> alive_pids(pids.begin(), pids.end());
127     while (!alive_pids.empty() && t.duration() < timeout) {
128         pid_t pid;
129         while ((pid = ReapOneProcess()) != 0) {
130             auto it = std::find(alive_pids.begin(), alive_pids.end(), pid);
131             if (it != alive_pids.end()) {
132                 alive_pids.erase(it);
133             }
134         }
135         if (alive_pids.empty()) {
136             break;
137         }
138         std::this_thread::sleep_for(50ms);
139     }
140     LOG(INFO) << "Waiting for " << pids.size() << " pids to be reaped took " << t << " with "
141               << alive_pids.size() << " of them still running";
142     for (pid_t pid : pids) {
143         std::string status = "(no-such-pid)";
144         ReadFileToString(StringPrintf("/proc/%d/status", pid), &status);
145         LOG(INFO) << "Still running: " << pid << ' ' << status;
146     }
147 }
148 
149 }  // namespace init
150 }  // namespace android
151