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 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 26 | #include <android-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 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 31 | #include "action.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 32 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 33 | #include "log.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 34 | #include "service.h" |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 35 | #include "util.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 37 | static int signal_write_fd = -1; |
| 38 | static int signal_read_fd = -1; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 40 | static std::string DescribeStatus(int status) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 41 | if (WIFEXITED(status)) { |
| 42 | return android::base::StringPrintf("exited with status %d", WEXITSTATUS(status)); |
| 43 | } else if (WIFSIGNALED(status)) { |
| 44 | return android::base::StringPrintf("killed by signal %d", WTERMSIG(status)); |
| 45 | } else if (WIFSTOPPED(status)) { |
| 46 | return android::base::StringPrintf("stopped by signal %d", WSTOPSIG(status)); |
| 47 | } else { |
| 48 | return "state changed"; |
| 49 | } |
| 50 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 52 | static bool wait_for_one_process() { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 53 | int status; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 54 | pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG)); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 55 | if (pid == 0) { |
| 56 | return false; |
| 57 | } else if (pid == -1) { |
| 58 | ERROR("waitpid failed: %s\n", strerror(errno)); |
| 59 | return false; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 60 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 61 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 62 | Service* svc = ServiceManager::GetInstance().FindServiceByPid(pid); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 63 | |
| 64 | std::string name; |
| 65 | if (svc) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 66 | name = android::base::StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 67 | } else { |
| 68 | name = android::base::StringPrintf("Untracked pid %d", pid); |
| 69 | } |
| 70 | |
| 71 | NOTICE("%s %s\n", name.c_str(), DescribeStatus(status).c_str()); |
| 72 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 73 | if (!svc) { |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 74 | return true; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 77 | if (svc->Reap()) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 78 | waiting_for_exec = false; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 79 | ServiceManager::GetInstance().RemoveService(*svc); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | static void reap_any_outstanding_children() { |
| 86 | while (wait_for_one_process()) { |
| 87 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 90 | static void handle_signal() { |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 91 | // Clear outstanding requests. |
| 92 | char buf[32]; |
| 93 | read(signal_read_fd, buf, sizeof(buf)); |
| 94 | |
| 95 | reap_any_outstanding_children(); |
| 96 | } |
| 97 | |
| 98 | static void SIGCHLD_handler(int) { |
| 99 | if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) { |
| 100 | ERROR("write(signal_write_fd) failed: %s\n", strerror(errno)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 101 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 104 | void signal_handler_init() { |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 105 | // Create a signalling mechanism for SIGCHLD. |
| 106 | int s[2]; |
| 107 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) { |
| 108 | ERROR("socketpair failed: %s\n", strerror(errno)); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | signal_write_fd = s[0]; |
| 113 | signal_read_fd = s[1]; |
| 114 | |
| 115 | // Write to signal_write_fd if we catch SIGCHLD. |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 116 | struct sigaction act; |
Chris Dearman | 6736eb1 | 2012-07-10 12:15:19 -0700 | [diff] [blame] | 117 | memset(&act, 0, sizeof(act)); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 118 | act.sa_handler = SIGCHLD_handler; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 119 | act.sa_flags = SA_NOCLDSTOP; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 120 | sigaction(SIGCHLD, &act, 0); |
| 121 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 122 | reap_any_outstanding_children(); |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 123 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 124 | register_epoll_handler(signal_read_fd, handle_signal); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 125 | } |