Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 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 | |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 17 | #include "sigchld_handler.h" |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 19 | #include <signal.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 20 | #include <string.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 23 | #include <sys/wait.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 26 | #include <android-base/chrono_utils.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 28 | #include <android-base/scopeguard.h> |
| 29 | #include <android-base/stringprintf.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 30 | |
| 31 | #include "init.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 32 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 33 | #include "service_list.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 34 | |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | using android::base::boot_clock; |
| 37 | using android::base::make_scope_guard; |
| 38 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace init { |
| 41 | |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 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 | |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 60 | std::string name; |
| 61 | std::string wait_string; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 62 | Service* service = nullptr; |
| 63 | |
Tom Cherry | fe81541 | 2019-04-23 15:11:07 -0700 | [diff] [blame] | 64 | if (SubcontextChildReap(pid)) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 65 | name = "Subcontext"; |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 66 | } else { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 67 | service = ServiceList::GetInstance().FindService(pid, &Service::pid); |
| 68 | |
| 69 | if (service) { |
| 70 | name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid); |
| 71 | if (service->flags() & SVC_EXEC) { |
| 72 | auto exec_duration = boot_clock::now() - service->time_started(); |
| 73 | auto exec_duration_ms = |
| 74 | std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count(); |
| 75 | wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f); |
Wei Wang | f7c2bfe | 2019-07-31 11:35:18 -0700 | [diff] [blame^] | 76 | } else if (service->flags() & SVC_ONESHOT) { |
| 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) |
| 80 | .count(); |
| 81 | wait_string = StringPrintf(" oneshot service took %f seconds in background", |
| 82 | exec_duration_ms / 1000.0f); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 83 | } |
| 84 | } else { |
| 85 | name = StringPrintf("Untracked pid %d", pid); |
| 86 | } |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 89 | if (siginfo.si_code == CLD_EXITED) { |
| 90 | LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string; |
| 91 | } else { |
| 92 | LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string; |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (!service) return true; |
| 96 | |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 97 | service->Reap(siginfo); |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 98 | |
| 99 | if (service->flags() & SVC_TEMPORARY) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 100 | ServiceList::GetInstance().RemoveService(*service); |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 106 | void ReapAnyOutstandingChildren() { |
| 107 | while (ReapOneProcess()) { |
| 108 | } |
| 109 | } |
| 110 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 111 | } // namespace init |
| 112 | } // namespace android |