signal_handler.c revision 12541c61311e0488e9873df754f8328cd12f99b4
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> 26#include <sys/reboot.h> 27 28#include "init.h" 29#include "util.h" 30 31static int signal_fd = -1; 32static int signal_recv_fd = -1; 33 34static void sigchld_handler(int s) 35{ 36 write(signal_fd, &s, 1); 37} 38 39#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */ 40#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery*/ 41 42static int wait_for_one_process(int block) 43{ 44 pid_t pid; 45 int status; 46 struct service *svc; 47 struct socketinfo *si; 48 time_t now; 49 struct listnode *node; 50 struct command *cmd; 51 52 while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR ); 53 if (pid <= 0) return -1; 54 INFO("waitpid returned pid %d, status = %08x\n", pid, status); 55 56 svc = service_find_by_pid(pid); 57 if (!svc) { 58 ERROR("untracked pid %d exited\n", pid); 59 return 0; 60 } 61 62 NOTICE("process '%s', pid %d exited\n", svc->name, pid); 63 64 if (!(svc->flags & SVC_ONESHOT)) { 65 kill(-pid, SIGKILL); 66 NOTICE("process '%s' killing any children in process group\n", svc->name); 67 } 68 69 /* remove any sockets we may have created */ 70 for (si = svc->sockets; si; si = si->next) { 71 char tmp[128]; 72 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name); 73 unlink(tmp); 74 } 75 76 svc->pid = 0; 77 svc->flags &= (~SVC_RUNNING); 78 79 /* oneshot processes go into the disabled state on exit */ 80 if (svc->flags & SVC_ONESHOT) { 81 svc->flags |= SVC_DISABLED; 82 } 83 84 /* disabled processes do not get restarted automatically */ 85 if (svc->flags & SVC_DISABLED) { 86 notify_service_state(svc->name, "stopped"); 87 return 0; 88 } 89 90 now = gettime(); 91 if (svc->flags & SVC_CRITICAL) { 92 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) { 93 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) { 94 ERROR("critical process '%s' exited %d times in %d minutes; " 95 "rebooting into recovery mode\n", svc->name, 96 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60); 97 sync(); 98 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, 99 LINUX_REBOOT_CMD_RESTART2, "recovery"); 100 return 0; 101 } 102 } else { 103 svc->time_crashed = now; 104 svc->nr_crashed = 1; 105 } 106 } 107 108 svc->flags |= SVC_RESTARTING; 109 110 /* Execute all onrestart commands for this service. */ 111 list_for_each(node, &svc->onrestart.commands) { 112 cmd = node_to_item(node, struct command, clist); 113 cmd->func(cmd->nargs, cmd->args); 114 } 115 notify_service_state(svc->name, "restarting"); 116 return 0; 117} 118 119void handle_signal(void) 120{ 121 char tmp[32]; 122 123 /* we got a SIGCHLD - reap and restart as needed */ 124 read(signal_recv_fd, tmp, sizeof(tmp)); 125 while (!wait_for_one_process(0)) 126 ; 127} 128 129void signal_init(void) 130{ 131 int s[2]; 132 133 struct sigaction act; 134 135 act.sa_handler = sigchld_handler; 136 act.sa_flags = SA_NOCLDSTOP; 137 act.sa_mask = 0; 138 act.sa_restorer = NULL; 139 sigaction(SIGCHLD, &act, 0); 140 141 /* create a signalling mechanism for the sigchld handler */ 142 if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0) { 143 signal_fd = s[0]; 144 signal_recv_fd = s[1]; 145 fcntl(s[0], F_SETFD, FD_CLOEXEC); 146 fcntl(s[0], F_SETFL, O_NONBLOCK); 147 fcntl(s[1], F_SETFD, FD_CLOEXEC); 148 fcntl(s[1], F_SETFL, O_NONBLOCK); 149 } 150 151 handle_signal(); 152} 153 154int get_signal_fd() 155{ 156 return signal_recv_fd; 157} 158