blob: c428b96325638fe53e8650ce51aaa7ab289f2c99 [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>
21#include <unistd.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070022#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/wait.h>
25#include <cutils/sockets.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080026#include <cutils/android_reboot.h>
Dima Zavinda04c522011-09-01 17:09:44 -070027#include <cutils/list.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070028
29#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070030#include "log.h"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080031#include "util.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070032
33static int signal_fd = -1;
34static int signal_recv_fd = -1;
35
Elliott Hughes8d82ea02015-02-06 20:15:18 -080036static void sigchld_handler(int s) {
Colin Cross9c5366b2010-04-13 19:48:59 -070037 write(signal_fd, &s, 1);
38}
39
Colin Cross9c5366b2010-04-13 19:48:59 -070040#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
Elliott Hughes8d82ea02015-02-06 20:15:18 -080041#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */
Colin Cross9c5366b2010-04-13 19:48:59 -070042
Elliott Hughes8d82ea02015-02-06 20:15:18 -080043static int wait_for_one_process() {
Colin Cross9c5366b2010-04-13 19:48:59 -070044 int status;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080045 pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
46 if (pid <= 0) {
47 return -1;
48 }
Colin Cross9c5366b2010-04-13 19:48:59 -070049 INFO("waitpid returned pid %d, status = %08x\n", pid, status);
50
Elliott Hughes8d82ea02015-02-06 20:15:18 -080051 service* svc = service_find_by_pid(pid);
Colin Cross9c5366b2010-04-13 19:48:59 -070052 if (!svc) {
Elliott Hughes6c5ad5c2013-11-25 16:33:53 -080053 if (WIFEXITED(status)) {
54 ERROR("untracked pid %d exited with status %d\n", pid, WEXITSTATUS(status));
55 } else if (WIFSIGNALED(status)) {
56 ERROR("untracked pid %d killed by signal %d\n", pid, WTERMSIG(status));
57 } else if (WIFSTOPPED(status)) {
58 ERROR("untracked pid %d stopped by signal %d\n", pid, WSTOPSIG(status));
59 } else {
60 ERROR("untracked pid %d state changed\n", pid);
61 }
Colin Cross9c5366b2010-04-13 19:48:59 -070062 return 0;
63 }
64
Elliott Hughes8d82ea02015-02-06 20:15:18 -080065 // TODO: all the code from here down should be a member function on service.
66
Colin Cross9c5366b2010-04-13 19:48:59 -070067 NOTICE("process '%s', pid %d exited\n", svc->name, pid);
68
Mike Kasickb54f39f2012-01-25 23:48:46 -050069 if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -070070 NOTICE("process '%s' killing any children in process group\n", svc->name);
Elliott Hughes8d82ea02015-02-06 20:15:18 -080071 kill(-pid, SIGKILL);
Colin Cross9c5366b2010-04-13 19:48:59 -070072 }
73
Elliott Hughes8d82ea02015-02-06 20:15:18 -080074 // Remove any sockets we may have created.
75 for (socketinfo* si = svc->sockets; si; si = si->next) {
Colin Cross9c5366b2010-04-13 19:48:59 -070076 char tmp[128];
77 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name);
78 unlink(tmp);
79 }
80
Elliott Hughes8d82ea02015-02-06 20:15:18 -080081 if (svc->flags & SVC_EXEC) {
82 INFO("SVC_EXEC pid %d finished...\n", svc->pid);
83 waiting_for_exec = false;
84 list_remove(&svc->slist);
85 free(svc->name);
86 free(svc);
87 return 0;
88 }
89
Colin Cross9c5366b2010-04-13 19:48:59 -070090 svc->pid = 0;
91 svc->flags &= (~SVC_RUNNING);
92
Elliott Hughes8d82ea02015-02-06 20:15:18 -080093 // Oneshot processes go into the disabled state on exit,
94 // except when manually restarted.
Mike Kasickb54f39f2012-01-25 23:48:46 -050095 if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -070096 svc->flags |= SVC_DISABLED;
97 }
98
Elliott Hughes8d82ea02015-02-06 20:15:18 -080099 // Disabled and reset processes do not get restarted automatically.
100 if (svc->flags & (SVC_DISABLED | SVC_RESET)) {
101 svc->NotifyStateChange("stopped");
Colin Cross9c5366b2010-04-13 19:48:59 -0700102 return 0;
103 }
104
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800105 time_t now = gettime();
Mike Kasickb54f39f2012-01-25 23:48:46 -0500106 if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700107 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) {
108 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) {
109 ERROR("critical process '%s' exited %d times in %d minutes; "
110 "rebooting into recovery mode\n", svc->name,
111 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60);
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800112 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
Colin Cross9c5366b2010-04-13 19:48:59 -0700113 return 0;
114 }
115 } else {
116 svc->time_crashed = now;
117 svc->nr_crashed = 1;
118 }
119 }
120
Mike Kasickb54f39f2012-01-25 23:48:46 -0500121 svc->flags &= (~SVC_RESTART);
Colin Cross9c5366b2010-04-13 19:48:59 -0700122 svc->flags |= SVC_RESTARTING;
123
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800124 // Execute all onrestart commands for this service.
125 struct listnode* node;
Colin Cross9c5366b2010-04-13 19:48:59 -0700126 list_for_each(node, &svc->onrestart.commands) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800127 command* cmd = node_to_item(node, struct command, clist);
Colin Cross9c5366b2010-04-13 19:48:59 -0700128 cmd->func(cmd->nargs, cmd->args);
129 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800130 svc->NotifyStateChange("restarting");
Colin Cross9c5366b2010-04-13 19:48:59 -0700131 return 0;
132}
133
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800134void handle_signal() {
135 // We got a SIGCHLD - reap and restart as needed.
Colin Cross9c5366b2010-04-13 19:48:59 -0700136 char tmp[32];
Colin Cross9c5366b2010-04-13 19:48:59 -0700137 read(signal_recv_fd, tmp, sizeof(tmp));
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800138 while (!wait_for_one_process()) {
139 }
Colin Cross9c5366b2010-04-13 19:48:59 -0700140}
141
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800142void signal_init() {
Colin Cross12541c62010-04-16 20:28:11 -0700143 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -0700144 memset(&act, 0, sizeof(act));
Colin Cross12541c62010-04-16 20:28:11 -0700145 act.sa_handler = sigchld_handler;
146 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -0700147 sigaction(SIGCHLD, &act, 0);
148
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800149 // Create a signalling mechanism for the sigchld handler.
150 int s[2];
Nick Kralevich45a884f2015-02-02 14:37:22 -0800151 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == 0) {
Colin Cross12541c62010-04-16 20:28:11 -0700152 signal_fd = s[0];
153 signal_recv_fd = s[1];
Colin Cross12541c62010-04-16 20:28:11 -0700154 }
155
156 handle_signal();
157}
158
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800159int get_signal_fd() {
Colin Cross9c5366b2010-04-13 19:48:59 -0700160 return signal_recv_fd;
161}