blob: 4d56d847293dcbf3556db562c9e7f1d6ac473e8d [file] [log] [blame]
Colin Cross9c5366b2010-04-13 19:48:59 -07001/*
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 Hughes8d82ea02015-02-06 20:15:18 -080017#include <signal.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070018#include <string.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070019#include <sys/socket.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <sys/types.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070021#include <unistd.h>
22
Tom Cherry3f5eaae52017-04-06 16:30:22 -070023#include <android-base/logging.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080024#include <android-base/stringprintf.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070025
26#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070027#include "service.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070028
Elliott Hughes9042cae2015-04-24 17:43:21 -070029static int signal_write_fd = -1;
30static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070031
Elliott Hughes929f4072015-04-24 21:13:44 -070032static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -070033 // Clear outstanding requests.
34 char buf[32];
35 read(signal_read_fd, buf, sizeof(buf));
36
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080037 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
Elliott Hughes9042cae2015-04-24 17:43:21 -070038}
39
40static void SIGCHLD_handler(int) {
41 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070042 PLOG(ERROR) << "write(signal_write_fd) failed";
Elliott Hughes8d82ea02015-02-06 20:15:18 -080043 }
Colin Cross9c5366b2010-04-13 19:48:59 -070044}
45
Elliott Hughes929f4072015-04-24 21:13:44 -070046void signal_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -070047 // Create a signalling mechanism for SIGCHLD.
48 int s[2];
49 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070050 PLOG(ERROR) << "socketpair failed";
Elliott Hughes9042cae2015-04-24 17:43:21 -070051 exit(1);
52 }
53
54 signal_write_fd = s[0];
55 signal_read_fd = s[1];
56
57 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -070058 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -070059 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -070060 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -070061 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -070062 sigaction(SIGCHLD, &act, 0);
63
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080064 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
Colin Cross12541c62010-04-16 20:28:11 -070065
Elliott Hughes929f4072015-04-24 21:13:44 -070066 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -070067}