blob: 072a0fb0b092645a7bb73c2e9dd80c87ec0c08c1 [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 Cherryeeee8312017-07-28 15:22:23 -070032#include "property_service.h"
Tom Cherrybac32992015-07-31 12:45:25 -070033#include "service.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070034
Tom Cherryeeee8312017-07-28 15:22:23 -070035using android::base::StringPrintf;
36using android::base::boot_clock;
37using android::base::make_scope_guard;
38
Tom Cherry81f5d3e2017-06-22 12:53:17 -070039namespace android {
40namespace init {
41
Elliott Hughes9042cae2015-04-24 17:43:21 -070042static int signal_write_fd = -1;
43static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070044
Tom Cherryeeee8312017-07-28 15:22:23 -070045static 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
Tom Cherryeeee8312017-07-28 15:22:23 -070063 std::string name;
64 std::string wait_string;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070065 Service* service = nullptr;
66
67 if (PropertyChildReap(pid)) {
68 name = "Async property child";
69 } else if (SubcontextChildReap(pid)) {
70 name = "Subcontext";
Tom Cherryeeee8312017-07-28 15:22:23 -070071 } else {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070072 service = ServiceList::GetInstance().FindService(pid, &Service::pid);
73
74 if (service) {
75 name = StringPrintf("Service '%s' (pid %d)", service->name().c_str(), pid);
76 if (service->flags() & SVC_EXEC) {
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).count();
80 wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
81 }
82 } else {
83 name = StringPrintf("Untracked pid %d", pid);
84 }
Tom Cherryeeee8312017-07-28 15:22:23 -070085 }
86
87 auto status = siginfo.si_status;
88 if (WIFEXITED(status)) {
89 LOG(INFO) << name << " exited with status " << WEXITSTATUS(status) << wait_string;
90 } else if (WIFSIGNALED(status)) {
91 LOG(INFO) << name << " killed by signal " << WTERMSIG(status) << wait_string;
92 }
93
94 if (!service) return true;
95
96 service->Reap();
97
98 if (service->flags() & SVC_TEMPORARY) {
Tom Cherry911b9b12017-07-27 16:20:58 -070099 ServiceList::GetInstance().RemoveService(*service);
Tom Cherryeeee8312017-07-28 15:22:23 -0700100 }
101
102 return true;
103}
104
Elliott Hughes929f4072015-04-24 21:13:44 -0700105static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700106 // Clear outstanding requests.
107 char buf[32];
108 read(signal_read_fd, buf, sizeof(buf));
109
Tom Cherryeeee8312017-07-28 15:22:23 -0700110 ReapAnyOutstandingChildren();
Elliott Hughes9042cae2015-04-24 17:43:21 -0700111}
112
113static void SIGCHLD_handler(int) {
114 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700115 PLOG(ERROR) << "write(signal_write_fd) failed";
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800116 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700117}
118
Tom Cherryeeee8312017-07-28 15:22:23 -0700119void ReapAnyOutstandingChildren() {
120 while (ReapOneProcess()) {
121 }
122}
123
Luis Hector Chavez9f97f472017-09-06 13:43:57 -0700124void sigchld_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700125 // Create a signalling mechanism for SIGCHLD.
126 int s[2];
127 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
Tom Cherry4a679452017-09-26 16:30:03 -0700128 PLOG(FATAL) << "socketpair failed in sigchld_handler_init";
Elliott Hughes9042cae2015-04-24 17:43:21 -0700129 }
130
131 signal_write_fd = s[0];
132 signal_read_fd = s[1];
133
134 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -0700135 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -0700136 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -0700137 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -0700138 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -0700139 sigaction(SIGCHLD, &act, 0);
140
Tom Cherryeeee8312017-07-28 15:22:23 -0700141 ReapAnyOutstandingChildren();
Colin Cross12541c62010-04-16 20:28:11 -0700142
Elliott Hughes929f4072015-04-24 21:13:44 -0700143 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -0700144}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700145
146} // namespace init
147} // namespace android