Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include <stdio.h> |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <cutils/sockets.h> |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 26 | #include <cutils/android_reboot.h> |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 27 | #include <cutils/list.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 28 | |
| 29 | #include "init.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 30 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 31 | #include "log.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 32 | |
| 33 | static int signal_fd = -1; |
| 34 | static int signal_recv_fd = -1; |
| 35 | |
| 36 | static void sigchld_handler(int s) |
| 37 | { |
| 38 | write(signal_fd, &s, 1); |
| 39 | } |
| 40 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 41 | #define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */ |
| 42 | #define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery*/ |
| 43 | |
| 44 | static int wait_for_one_process(int block) |
| 45 | { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 46 | int status; |
| 47 | struct service *svc; |
| 48 | struct socketinfo *si; |
| 49 | time_t now; |
| 50 | struct listnode *node; |
| 51 | struct command *cmd; |
| 52 | |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 53 | pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, block ? 0 : WNOHANG)); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 54 | if (pid <= 0) return -1; |
| 55 | INFO("waitpid returned pid %d, status = %08x\n", pid, status); |
| 56 | |
| 57 | svc = service_find_by_pid(pid); |
| 58 | if (!svc) { |
Elliott Hughes | 6c5ad5c | 2013-11-25 16:33:53 -0800 | [diff] [blame] | 59 | if (WIFEXITED(status)) { |
| 60 | ERROR("untracked pid %d exited with status %d\n", pid, WEXITSTATUS(status)); |
| 61 | } else if (WIFSIGNALED(status)) { |
| 62 | ERROR("untracked pid %d killed by signal %d\n", pid, WTERMSIG(status)); |
| 63 | } else if (WIFSTOPPED(status)) { |
| 64 | ERROR("untracked pid %d stopped by signal %d\n", pid, WSTOPSIG(status)); |
| 65 | } else { |
| 66 | ERROR("untracked pid %d state changed\n", pid); |
| 67 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | NOTICE("process '%s', pid %d exited\n", svc->name, pid); |
| 72 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 73 | if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 74 | kill(-pid, SIGKILL); |
| 75 | NOTICE("process '%s' killing any children in process group\n", svc->name); |
| 76 | } |
| 77 | |
| 78 | /* remove any sockets we may have created */ |
| 79 | for (si = svc->sockets; si; si = si->next) { |
| 80 | char tmp[128]; |
| 81 | snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name); |
| 82 | unlink(tmp); |
| 83 | } |
| 84 | |
| 85 | svc->pid = 0; |
| 86 | svc->flags &= (~SVC_RUNNING); |
| 87 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 88 | /* oneshot processes go into the disabled state on exit, |
| 89 | * except when manually restarted. */ |
| 90 | if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 91 | svc->flags |= SVC_DISABLED; |
| 92 | } |
| 93 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 94 | /* disabled and reset processes do not get restarted automatically */ |
| 95 | if (svc->flags & (SVC_DISABLED | SVC_RESET) ) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 96 | notify_service_state(svc->name, "stopped"); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | now = gettime(); |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 101 | if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 102 | if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) { |
| 103 | if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) { |
| 104 | ERROR("critical process '%s' exited %d times in %d minutes; " |
| 105 | "rebooting into recovery mode\n", svc->name, |
| 106 | CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60); |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 107 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | } else { |
| 111 | svc->time_crashed = now; |
| 112 | svc->nr_crashed = 1; |
| 113 | } |
| 114 | } |
| 115 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 116 | svc->flags &= (~SVC_RESTART); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 117 | svc->flags |= SVC_RESTARTING; |
| 118 | |
| 119 | /* Execute all onrestart commands for this service. */ |
| 120 | list_for_each(node, &svc->onrestart.commands) { |
| 121 | cmd = node_to_item(node, struct command, clist); |
| 122 | cmd->func(cmd->nargs, cmd->args); |
| 123 | } |
| 124 | notify_service_state(svc->name, "restarting"); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void handle_signal(void) |
| 129 | { |
| 130 | char tmp[32]; |
| 131 | |
| 132 | /* we got a SIGCHLD - reap and restart as needed */ |
| 133 | read(signal_recv_fd, tmp, sizeof(tmp)); |
| 134 | while (!wait_for_one_process(0)) |
| 135 | ; |
| 136 | } |
| 137 | |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 138 | void signal_init(void) |
| 139 | { |
| 140 | int s[2]; |
| 141 | |
| 142 | struct sigaction act; |
Chris Dearman | 6736eb1 | 2012-07-10 12:15:19 -0700 | [diff] [blame] | 143 | memset(&act, 0, sizeof(act)); |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 144 | act.sa_handler = sigchld_handler; |
| 145 | act.sa_flags = SA_NOCLDSTOP; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 146 | sigaction(SIGCHLD, &act, 0); |
| 147 | |
| 148 | /* create a signalling mechanism for the sigchld handler */ |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 149 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == 0) { |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 150 | signal_fd = s[0]; |
| 151 | signal_recv_fd = s[1]; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | handle_signal(); |
| 155 | } |
| 156 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 157 | int get_signal_fd() |
| 158 | { |
| 159 | return signal_recv_fd; |
| 160 | } |