blob: 68931632fd788e4a244459f943ea13c70d221a43 [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
31#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070032#include "log.h"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080033#include "util.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070034
Elliott Hughesda40c002015-03-27 23:20:44 -070035#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
36#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */
37
Elliott Hughes9042cae2015-04-24 17:43:21 -070038static int signal_write_fd = -1;
39static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070040
Elliott Hughes9042cae2015-04-24 17:43:21 -070041static std::string DescribeStatus(int status) {
Elliott Hughesda40c002015-03-27 23:20:44 -070042 if (WIFEXITED(status)) {
43 return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status));
44 } else if (WIFSIGNALED(status)) {
45 return android::base::StringPrintf("killed by signal %d", WTERMSIG(status));
46 } else if (WIFSTOPPED(status)) {
47 return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status));
48 } else {
49 return "state changed";
50 }
51}
Colin Cross9c5366b2010-04-13 19:48:59 -070052
Elliott Hughes9042cae2015-04-24 17:43:21 -070053static bool wait_for_one_process() {
Colin Cross9c5366b2010-04-13 19:48:59 -070054 int status;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080055 pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
Elliott Hughes9042cae2015-04-24 17:43:21 -070056 if (pid == 0) {
57 return false;
58 } else if (pid == -1) {
59 ERROR("waitpid failed: %s\n", strerror(errno));
60 return false;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080061 }
Colin Cross9c5366b2010-04-13 19:48:59 -070062
Elliott Hughes8d82ea02015-02-06 20:15:18 -080063 service* svc = service_find_by_pid(pid);
Elliott Hughesda40c002015-03-27 23:20:44 -070064
65 std::string name;
66 if (svc) {
67 name = android::base::StringPrintf("Service '%s' (pid %d)", svc->name, pid);
68 } else {
69 name = android::base::StringPrintf("Untracked pid %d", pid);
70 }
71
72 NOTICE("%s %s\n", name.c_str(), DescribeStatus(status).c_str());
73
Colin Cross9c5366b2010-04-13 19:48:59 -070074 if (!svc) {
Elliott Hughes9042cae2015-04-24 17:43:21 -070075 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -070076 }
77
Elliott Hughes8d82ea02015-02-06 20:15:18 -080078 // TODO: all the code from here down should be a member function on service.
79
Mike Kasickb54f39f2012-01-25 23:48:46 -050080 if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) {
Elliott Hughesda40c002015-03-27 23:20:44 -070081 NOTICE("Service '%s' (pid %d) killing any children in process group\n", svc->name, pid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -080082 kill(-pid, SIGKILL);
Colin Cross9c5366b2010-04-13 19:48:59 -070083 }
84
Elliott Hughes8d82ea02015-02-06 20:15:18 -080085 // Remove any sockets we may have created.
86 for (socketinfo* si = svc->sockets; si; si = si->next) {
Colin Cross9c5366b2010-04-13 19:48:59 -070087 char tmp[128];
88 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name);
89 unlink(tmp);
90 }
91
Elliott Hughes8d82ea02015-02-06 20:15:18 -080092 if (svc->flags & SVC_EXEC) {
93 INFO("SVC_EXEC pid %d finished...\n", svc->pid);
94 waiting_for_exec = false;
95 list_remove(&svc->slist);
96 free(svc->name);
97 free(svc);
Elliott Hughes9042cae2015-04-24 17:43:21 -070098 return true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080099 }
100
Colin Cross9c5366b2010-04-13 19:48:59 -0700101 svc->pid = 0;
102 svc->flags &= (~SVC_RUNNING);
103
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800104 // Oneshot processes go into the disabled state on exit,
105 // except when manually restarted.
Mike Kasickb54f39f2012-01-25 23:48:46 -0500106 if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700107 svc->flags |= SVC_DISABLED;
108 }
109
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800110 // Disabled and reset processes do not get restarted automatically.
111 if (svc->flags & (SVC_DISABLED | SVC_RESET)) {
112 svc->NotifyStateChange("stopped");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700113 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -0700114 }
115
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800116 time_t now = gettime();
Mike Kasickb54f39f2012-01-25 23:48:46 -0500117 if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700118 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) {
119 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) {
120 ERROR("critical process '%s' exited %d times in %d minutes; "
121 "rebooting into recovery mode\n", svc->name,
122 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60);
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800123 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700124 return true;
Colin Cross9c5366b2010-04-13 19:48:59 -0700125 }
126 } else {
127 svc->time_crashed = now;
128 svc->nr_crashed = 1;
129 }
130 }
131
Mike Kasickb54f39f2012-01-25 23:48:46 -0500132 svc->flags &= (~SVC_RESTART);
Colin Cross9c5366b2010-04-13 19:48:59 -0700133 svc->flags |= SVC_RESTARTING;
134
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800135 // Execute all onrestart commands for this service.
136 struct listnode* node;
Colin Cross9c5366b2010-04-13 19:48:59 -0700137 list_for_each(node, &svc->onrestart.commands) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800138 command* cmd = node_to_item(node, struct command, clist);
Yabin Cuiee530062015-07-24 18:17:16 -0700139 std::vector<std::string> arg_strs;
140 if (expand_command_arguments(cmd->nargs, cmd->args, &arg_strs)) {
141 std::vector<char*> args;
142 for (auto& s : arg_strs) {
143 args.push_back(&s[0]);
144 }
145 cmd->func(args.size(), &args[0]);
146 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700147 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800148 svc->NotifyStateChange("restarting");
Elliott Hughes9042cae2015-04-24 17:43:21 -0700149 return true;
150}
151
152static void reap_any_outstanding_children() {
153 while (wait_for_one_process()) {
154 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700155}
156
Elliott Hughes929f4072015-04-24 21:13:44 -0700157static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700158 // Clear outstanding requests.
159 char buf[32];
160 read(signal_read_fd, buf, sizeof(buf));
161
162 reap_any_outstanding_children();
163}
164
165static void SIGCHLD_handler(int) {
166 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
167 ERROR("write(signal_write_fd) failed: %s\n", strerror(errno));
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800168 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700169}
170
Elliott Hughes929f4072015-04-24 21:13:44 -0700171void signal_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -0700172 // Create a signalling mechanism for SIGCHLD.
173 int s[2];
174 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
175 ERROR("socketpair failed: %s\n", strerror(errno));
176 exit(1);
177 }
178
179 signal_write_fd = s[0];
180 signal_read_fd = s[1];
181
182 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -0700183 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -0700184 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -0700185 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -0700186 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -0700187 sigaction(SIGCHLD, &act, 0);
188
Elliott Hughes9042cae2015-04-24 17:43:21 -0700189 reap_any_outstanding_children();
Colin Cross12541c62010-04-16 20:28:11 -0700190
Elliott Hughes929f4072015-04-24 21:13:44 -0700191 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -0700192}