blob: e7d42cb9bb079a16ed76bbfb02d8275bff15418d [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
Colin Cross9c5366b2010-04-13 19:48:59 -070017#include <errno.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070018#include <fcntl.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <signal.h>
20#include <stdio.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>
Colin Cross9c5366b2010-04-13 19:48:59 -070023#include <sys/wait.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070024#include <unistd.h>
25
Elliott Hughes4f713192015-12-04 22:00:26 -080026#include <android-base/stringprintf.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080027#include <cutils/android_reboot.h>
Dima Zavinda04c522011-09-01 17:09:44 -070028#include <cutils/list.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070029#include <cutils/sockets.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070030
Tom Cherryfa0c21c2015-07-23 17:53:11 -070031#include "action.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070032#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070033#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070034#include "service.h"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080035#include "util.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070036
Elliott Hughes9042cae2015-04-24 17:43:21 -070037static int signal_write_fd = -1;
38static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070039
Elliott Hughes9042cae2015-04-24 17:43:21 -070040static std::string DescribeStatus(int status) {
Elliott Hughesda40c002015-03-27 23:20:44 -070041 if (WIFEXITED(status)) {
42 return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status));
43 } else if (WIFSIGNALED(status)) {
44 return android::base::StringPrintf("killed by signal %d", WTERMSIG(status));
45 } else if (WIFSTOPPED(status)) {
46 return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status));
47 } else {
48 return "state changed";
49 }
50}
Colin Cross9c5366b2010-04-13 19:48:59 -070051
Elliott Hughes9042cae2015-04-24 17:43:21 -070052static bool wait_for_one_process() {
Colin Cross9c5366b2010-04-13 19:48:59 -070053 int status;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080054 pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
Elliott Hughes9042cae2015-04-24 17:43:21 -070055 if (pid == 0) {
56 return false;
57 } else if (pid == -1) {
58 ERROR("waitpid failed: %s\n", strerror(errno));
59 return false;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080060 }
Colin Cross9c5366b2010-04-13 19:48:59 -070061
Tom Cherrybac32992015-07-31 12:45:25 -070062 Service* svc = ServiceManager::GetInstance().FindServiceByPid(pid);
Elliott Hughesda40c002015-03-27 23:20:44 -070063
64 std::string name;
65 if (svc) {
Tom Cherrybac32992015-07-31 12:45:25 -070066 name = android::base::StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid);
Elliott Hughesda40c002015-03-27 23:20:44 -070067 } else {
68 name = android::base::StringPrintf("Untracked pid %d", pid);
69 }
70
71 NOTICE("%s %s\n", name.c_str(), DescribeStatus(status).c_str());
72
Colin Cross9c5366b2010-04-13 19:48:59 -070073 if (!svc) {
Elliott Hughes9042cae2015-04-24 17:43:21 -070074 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -070075 }
76
Tom Cherrybac32992015-07-31 12:45:25 -070077 if (svc->Reap()) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -080078 waiting_for_exec = false;
Tom Cherrybac32992015-07-31 12:45:25 -070079 ServiceManager::GetInstance().RemoveService(*svc);
Elliott Hughes8d82ea02015-02-06 20:15:18 -080080 }
81
Elliott Hughes9042cae2015-04-24 17:43:21 -070082 return true;
83}
84
85static void reap_any_outstanding_children() {
86 while (wait_for_one_process()) {
87 }
Colin Cross9c5366b2010-04-13 19:48:59 -070088}
89
Elliott Hughes929f4072015-04-24 21:13:44 -070090static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -070091 // Clear outstanding requests.
92 char buf[32];
93 read(signal_read_fd, buf, sizeof(buf));
94
95 reap_any_outstanding_children();
96}
97
98static void SIGCHLD_handler(int) {
99 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
100 ERROR("write(signal_write_fd) failed: %s\n", strerror(errno));
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800101 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700102}
103
Elliott Hughes929f4072015-04-24 21:13:44 -0700104void signal_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700105 // Create a signalling mechanism for SIGCHLD.
106 int s[2];
107 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
108 ERROR("socketpair failed: %s\n", strerror(errno));
109 exit(1);
110 }
111
112 signal_write_fd = s[0];
113 signal_read_fd = s[1];
114
115 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -0700116 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -0700117 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -0700118 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -0700119 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -0700120 sigaction(SIGCHLD, &act, 0);
121
Elliott Hughes9042cae2015-04-24 17:43:21 -0700122 reap_any_outstanding_children();
Colin Cross12541c62010-04-16 20:28:11 -0700123
Elliott Hughes929f4072015-04-24 21:13:44 -0700124 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -0700125}