| 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 |  | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 17 | #include "signal_handler.h" | 
|  | 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 | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 32 | #include "property_service.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 33 | #include "service.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 |  | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 42 | static int signal_write_fd = -1; | 
|  | 43 | static int signal_read_fd = -1; | 
| Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 44 |  | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 45 | static bool ReapOneProcess() { | 
|  | 46 | 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"; | 
|  | 51 | return false; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | auto pid = siginfo.si_pid; | 
|  | 55 | if (pid == 0) return false; | 
|  | 56 |  | 
|  | 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 |  | 
|  | 63 | if (PropertyChildReap(pid)) return true; | 
|  | 64 |  | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 65 | Service* service = ServiceList::GetInstance().FindService(pid, &Service::pid); | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | std::string name; | 
|  | 68 | std::string wait_string; | 
|  | 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); | 
|  | 76 | } | 
|  | 77 | } else { | 
|  | 78 | name = StringPrintf("Untracked pid %d", pid); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | auto status = siginfo.si_status; | 
|  | 82 | if (WIFEXITED(status)) { | 
|  | 83 | LOG(INFO) << name << " exited with status " << WEXITSTATUS(status) << wait_string; | 
|  | 84 | } else if (WIFSIGNALED(status)) { | 
|  | 85 | LOG(INFO) << name << " killed by signal " << WTERMSIG(status) << wait_string; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | if (!service) return true; | 
|  | 89 |  | 
|  | 90 | service->Reap(); | 
|  | 91 |  | 
|  | 92 | if (service->flags() & SVC_TEMPORARY) { | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 93 | ServiceList::GetInstance().RemoveService(*service); | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
|  | 96 | return true; | 
|  | 97 | } | 
|  | 98 |  | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 99 | static void handle_signal() { | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 100 | // Clear outstanding requests. | 
|  | 101 | char buf[32]; | 
|  | 102 | read(signal_read_fd, buf, sizeof(buf)); | 
|  | 103 |  | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 104 | ReapAnyOutstandingChildren(); | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | static void SIGCHLD_handler(int) { | 
|  | 108 | if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 109 | PLOG(ERROR) << "write(signal_write_fd) failed"; | 
| Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 110 | } | 
| Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 113 | void ReapAnyOutstandingChildren() { | 
|  | 114 | while (ReapOneProcess()) { | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 118 | void signal_handler_init() { | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 119 | // Create a signalling mechanism for SIGCHLD. | 
|  | 120 | int s[2]; | 
|  | 121 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 122 | PLOG(ERROR) << "socketpair failed"; | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 123 | exit(1); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | signal_write_fd = s[0]; | 
|  | 127 | signal_read_fd = s[1]; | 
|  | 128 |  | 
|  | 129 | // Write to signal_write_fd if we catch SIGCHLD. | 
| Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 130 | struct sigaction act; | 
| Chris Dearman | 6736eb1 | 2012-07-10 12:15:19 -0700 | [diff] [blame] | 131 | memset(&act, 0, sizeof(act)); | 
| Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 132 | act.sa_handler = SIGCHLD_handler; | 
| Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 133 | act.sa_flags = SA_NOCLDSTOP; | 
| Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 134 | sigaction(SIGCHLD, &act, 0); | 
|  | 135 |  | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 136 | ReapAnyOutstandingChildren(); | 
| Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 137 |  | 
| Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 138 | register_epoll_handler(signal_read_fd, handle_signal); | 
| Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 139 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 140 |  | 
|  | 141 | }  // namespace init | 
|  | 142 | }  // namespace android |