blob: 5a875bb47f51f2f6ad5cfc79b9a2241af1b52183 [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
26#include <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"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080034#include "util.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070035
Elliott Hughesda40c002015-03-27 23:20:44 -070036#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
37#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */
38
Elliott Hughes9042cae2015-04-24 17:43:21 -070039static int signal_write_fd = -1;
40static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070041
Elliott Hughes9042cae2015-04-24 17:43:21 -070042static std::string DescribeStatus(int status) {
Elliott Hughesda40c002015-03-27 23:20:44 -070043 if (WIFEXITED(status)) {
44 return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status));
45 } else if (WIFSIGNALED(status)) {
46 return android::base::StringPrintf("killed by signal %d", WTERMSIG(status));
47 } else if (WIFSTOPPED(status)) {
48 return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status));
49 } else {
50 return "state changed";
51 }
52}
Colin Cross9c5366b2010-04-13 19:48:59 -070053
Elliott Hughes9042cae2015-04-24 17:43:21 -070054static bool wait_for_one_process() {
Colin Cross9c5366b2010-04-13 19:48:59 -070055 int status;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080056 pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
Elliott Hughes9042cae2015-04-24 17:43:21 -070057 if (pid == 0) {
58 return false;
59 } else if (pid == -1) {
60 ERROR("waitpid failed: %s\n", strerror(errno));
61 return false;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080062 }
Colin Cross9c5366b2010-04-13 19:48:59 -070063
Elliott Hughes8d82ea02015-02-06 20:15:18 -080064 service* svc = service_find_by_pid(pid);
Elliott Hughesda40c002015-03-27 23:20:44 -070065
66 std::string name;
67 if (svc) {
68 name = android::base::StringPrintf("Service '%s' (pid %d)", svc->name, pid);
69 } else {
70 name = android::base::StringPrintf("Untracked pid %d", pid);
71 }
72
73 NOTICE("%s %s\n", name.c_str(), DescribeStatus(status).c_str());
74
Colin Cross9c5366b2010-04-13 19:48:59 -070075 if (!svc) {
Elliott Hughes9042cae2015-04-24 17:43:21 -070076 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -070077 }
78
Elliott Hughes8d82ea02015-02-06 20:15:18 -080079 // TODO: all the code from here down should be a member function on service.
80
Mike Kasickb54f39f2012-01-25 23:48:46 -050081 if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) {
Elliott Hughesda40c002015-03-27 23:20:44 -070082 NOTICE("Service '%s' (pid %d) killing any children in process group\n", svc->name, pid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -080083 kill(-pid, SIGKILL);
Colin Cross9c5366b2010-04-13 19:48:59 -070084 }
85
Elliott Hughes8d82ea02015-02-06 20:15:18 -080086 // Remove any sockets we may have created.
87 for (socketinfo* si = svc->sockets; si; si = si->next) {
Colin Cross9c5366b2010-04-13 19:48:59 -070088 char tmp[128];
89 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name);
90 unlink(tmp);
91 }
92
Elliott Hughes8d82ea02015-02-06 20:15:18 -080093 if (svc->flags & SVC_EXEC) {
94 INFO("SVC_EXEC pid %d finished...\n", svc->pid);
95 waiting_for_exec = false;
96 list_remove(&svc->slist);
97 free(svc->name);
98 free(svc);
Elliott Hughes9042cae2015-04-24 17:43:21 -070099 return true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800100 }
101
Colin Cross9c5366b2010-04-13 19:48:59 -0700102 svc->pid = 0;
103 svc->flags &= (~SVC_RUNNING);
104
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800105 // Oneshot processes go into the disabled state on exit,
106 // except when manually restarted.
Mike Kasickb54f39f2012-01-25 23:48:46 -0500107 if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700108 svc->flags |= SVC_DISABLED;
109 }
110
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800111 // Disabled and reset processes do not get restarted automatically.
112 if (svc->flags & (SVC_DISABLED | SVC_RESET)) {
113 svc->NotifyStateChange("stopped");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700114 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -0700115 }
116
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800117 time_t now = gettime();
Mike Kasickb54f39f2012-01-25 23:48:46 -0500118 if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700119 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) {
120 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) {
121 ERROR("critical process '%s' exited %d times in %d minutes; "
122 "rebooting into recovery mode\n", svc->name,
123 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60);
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800124 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700125 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -0700126 }
127 } else {
128 svc->time_crashed = now;
129 svc->nr_crashed = 1;
130 }
131 }
132
Mike Kasickb54f39f2012-01-25 23:48:46 -0500133 svc->flags &= (~SVC_RESTART);
Colin Cross9c5366b2010-04-13 19:48:59 -0700134 svc->flags |= SVC_RESTARTING;
135
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800136 // Execute all onrestart commands for this service.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700137 svc->onrestart->ExecuteAllCommands();
138
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800139 svc->NotifyStateChange("restarting");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700140 return true;
141}
142
143static void reap_any_outstanding_children() {
144 while (wait_for_one_process()) {
145 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700146}
147
Elliott Hughes929f4072015-04-24 21:13:44 -0700148static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700149 // Clear outstanding requests.
150 char buf[32];
151 read(signal_read_fd, buf, sizeof(buf));
152
153 reap_any_outstanding_children();
154}
155
156static void SIGCHLD_handler(int) {
157 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
158 ERROR("write(signal_write_fd) failed: %s\n", strerror(errno));
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800159 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700160}
161
Elliott Hughes929f4072015-04-24 21:13:44 -0700162void signal_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700163 // Create a signalling mechanism for SIGCHLD.
164 int s[2];
165 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
166 ERROR("socketpair failed: %s\n", strerror(errno));
167 exit(1);
168 }
169
170 signal_write_fd = s[0];
171 signal_read_fd = s[1];
172
173 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -0700174 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -0700175 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -0700176 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -0700177 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -0700178 sigaction(SIGCHLD, &act, 0);
179
Elliott Hughes9042cae2015-04-24 17:43:21 -0700180 reap_any_outstanding_children();
Colin Cross12541c62010-04-16 20:28:11 -0700181
Elliott Hughes929f4072015-04-24 21:13:44 -0700182 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -0700183}