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 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 17 | #include <signal.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 18 | #include <string.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 19 | #include <sys/socket.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 24 | |
| 25 | #include "init.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 26 | #include "service.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 27 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace init { |
| 30 | |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 31 | static int signal_write_fd = -1; |
| 32 | static int signal_read_fd = -1; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 34 | static void handle_signal() { |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 35 | // Clear outstanding requests. |
| 36 | char buf[32]; |
| 37 | read(signal_read_fd, buf, sizeof(buf)); |
| 38 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 39 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void SIGCHLD_handler(int) { |
| 43 | if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 44 | PLOG(ERROR) << "write(signal_write_fd) failed"; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 45 | } |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 48 | void signal_handler_init() { |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 49 | // Create a signalling mechanism for SIGCHLD. |
| 50 | int s[2]; |
| 51 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 52 | PLOG(ERROR) << "socketpair failed"; |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 53 | exit(1); |
| 54 | } |
| 55 | |
| 56 | signal_write_fd = s[0]; |
| 57 | signal_read_fd = s[1]; |
| 58 | |
| 59 | // Write to signal_write_fd if we catch SIGCHLD. |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 60 | struct sigaction act; |
Chris Dearman | 6736eb1 | 2012-07-10 12:15:19 -0700 | [diff] [blame] | 61 | memset(&act, 0, sizeof(act)); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 62 | act.sa_handler = SIGCHLD_handler; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 63 | act.sa_flags = SA_NOCLDSTOP; |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 64 | sigaction(SIGCHLD, &act, 0); |
| 65 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 66 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
Colin Cross | 12541c6 | 2010-04-16 20:28:11 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 68 | register_epoll_handler(signal_read_fd, handle_signal); |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 69 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 70 | |
| 71 | } // namespace init |
| 72 | } // namespace android |