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> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 22 | #include <sys/types.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 23 | #include <sys/wait.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <base/stringprintf.h> |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 27 | #include <cutils/android_reboot.h> |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 28 | #include <cutils/list.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 29 | #include <cutils/sockets.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 30 | |
| 31 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 32 | #include "log.h" |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 33 | #include "util.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 35 | #define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */ |
| 36 | #define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery */ |
| 37 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 38 | static int signal_fd = -1; |
| 39 | static int signal_recv_fd = -1; |
| 40 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 41 | static void sigchld_handler(int s) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 42 | write(signal_fd, &s, 1); |
| 43 | } |
| 44 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 45 | std::string DescribeStatus(int status) { |
| 46 | if (WIFEXITED(status)) { |
| 47 | return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status)); |
| 48 | } else if (WIFSIGNALED(status)) { |
| 49 | return android::base::StringPrintf("killed by signal %d", WTERMSIG(status)); |
| 50 | } else if (WIFSTOPPED(status)) { |
| 51 | return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status)); |
| 52 | } else { |
| 53 | return "state changed"; |
| 54 | } |
| 55 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 57 | static int wait_for_one_process() { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 58 | int status; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 59 | pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG)); |
| 60 | if (pid <= 0) { |
| 61 | return -1; |
| 62 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 64 | service* svc = service_find_by_pid(pid); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 65 | |
| 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 Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 75 | if (!svc) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 79 | // TODO: all the code from here down should be a member function on service. |
| 80 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 81 | if (!(svc->flags & SVC_ONESHOT) || (svc->flags & SVC_RESTART)) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame^] | 82 | NOTICE("Service '%s' (pid %d) killing any children in process group\n", svc->name, pid); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 83 | kill(-pid, SIGKILL); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 86 | // Remove any sockets we may have created. |
| 87 | for (socketinfo* si = svc->sockets; si; si = si->next) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 88 | char tmp[128]; |
| 89 | snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name); |
| 90 | unlink(tmp); |
| 91 | } |
| 92 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 93 | 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); |
| 99 | return 0; |
| 100 | } |
| 101 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 102 | svc->pid = 0; |
| 103 | svc->flags &= (~SVC_RUNNING); |
| 104 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 105 | // Oneshot processes go into the disabled state on exit, |
| 106 | // except when manually restarted. |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 107 | if ((svc->flags & SVC_ONESHOT) && !(svc->flags & SVC_RESTART)) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 108 | svc->flags |= SVC_DISABLED; |
| 109 | } |
| 110 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 111 | // Disabled and reset processes do not get restarted automatically. |
| 112 | if (svc->flags & (SVC_DISABLED | SVC_RESET)) { |
| 113 | svc->NotifyStateChange("stopped"); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 117 | time_t now = gettime(); |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 118 | if ((svc->flags & SVC_CRITICAL) && !(svc->flags & SVC_RESTART)) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 119 | 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 Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 124 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | } else { |
| 128 | svc->time_crashed = now; |
| 129 | svc->nr_crashed = 1; |
| 130 | } |
| 131 | } |
| 132 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 133 | svc->flags &= (~SVC_RESTART); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 134 | svc->flags |= SVC_RESTARTING; |
| 135 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 136 | // Execute all onrestart commands for this service. |
| 137 | struct listnode* node; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 138 | list_for_each(node, &svc->onrestart.commands) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 139 | command* cmd = node_to_item(node, struct command, clist); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 140 | cmd->func(cmd->nargs, cmd->args); |
| 141 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 142 | svc->NotifyStateChange("restarting"); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 146 | void handle_signal() { |
| 147 | // We got a SIGCHLD - reap and restart as needed. |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 148 | char tmp[32]; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 149 | read(signal_recv_fd, tmp, sizeof(tmp)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 150 | while (!wait_for_one_process()) { |
| 151 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 154 | void signal_init() { |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 155 | struct sigaction act; |
Chris Dearman | 6736eb1 | 2012-07-10 12:15:19 -0700 | [diff] [blame] | 156 | memset(&act, 0, sizeof(act)); |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 157 | act.sa_handler = sigchld_handler; |
| 158 | act.sa_flags = SA_NOCLDSTOP; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 159 | sigaction(SIGCHLD, &act, 0); |
| 160 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 161 | // Create a signalling mechanism for the sigchld handler. |
| 162 | int s[2]; |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 163 | 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] | 164 | signal_fd = s[0]; |
| 165 | signal_recv_fd = s[1]; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | handle_signal(); |
| 169 | } |
| 170 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 171 | int get_signal_fd() { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 172 | return signal_recv_fd; |
| 173 | } |