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