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