blob: 1041b82d0172fb6dd9e0ec1cc92e1b17992fa3fb [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
Colin Cross9c5366b2010-04-13 19:48:59 -070017#include <errno.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070018#include <fcntl.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <signal.h>
20#include <stdio.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070021#include <sys/socket.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022#include <sys/types.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070023#include <sys/wait.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070024#include <unistd.h>
25
Elliott Hughes4f713192015-12-04 22:00:26 -080026#include <android-base/stringprintf.h>
Dima Zavinda04c522011-09-01 17:09:44 -070027#include <cutils/list.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070028#include <cutils/sockets.h>
Colin Cross9c5366b2010-04-13 19:48:59 -070029
Tom Cherryfa0c21c2015-07-23 17:53:11 -070030#include "action.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070031#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070032#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070033#include "service.h"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080034#include "util.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070035
Elliott Hughes9042cae2015-04-24 17:43:21 -070036static int signal_write_fd = -1;
37static int signal_read_fd = -1;
Colin Cross9c5366b2010-04-13 19:48:59 -070038
Elliott Hughes929f4072015-04-24 21:13:44 -070039static void handle_signal() {
Elliott Hughes9042cae2015-04-24 17:43:21 -070040 // Clear outstanding requests.
41 char buf[32];
42 read(signal_read_fd, buf, sizeof(buf));
43
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080044 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
Elliott Hughes9042cae2015-04-24 17:43:21 -070045}
46
47static void SIGCHLD_handler(int) {
48 if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070049 PLOG(ERROR) << "write(signal_write_fd) failed";
Elliott Hughes8d82ea02015-02-06 20:15:18 -080050 }
Colin Cross9c5366b2010-04-13 19:48:59 -070051}
52
Elliott Hughes929f4072015-04-24 21:13:44 -070053void signal_handler_init() {
Elliott Hughes9042cae2015-04-24 17:43:21 -070054 // Create a signalling mechanism for SIGCHLD.
55 int s[2];
56 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070057 PLOG(ERROR) << "socketpair failed";
Elliott Hughes9042cae2015-04-24 17:43:21 -070058 exit(1);
59 }
60
61 signal_write_fd = s[0];
62 signal_read_fd = s[1];
63
64 // Write to signal_write_fd if we catch SIGCHLD.
Colin Cross12541c62010-04-16 20:28:11 -070065 struct sigaction act;
Chris Dearman6736eb12012-07-10 12:15:19 -070066 memset(&act, 0, sizeof(act));
Elliott Hughes9042cae2015-04-24 17:43:21 -070067 act.sa_handler = SIGCHLD_handler;
Colin Cross12541c62010-04-16 20:28:11 -070068 act.sa_flags = SA_NOCLDSTOP;
Colin Cross12541c62010-04-16 20:28:11 -070069 sigaction(SIGCHLD, &act, 0);
70
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080071 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
Colin Cross12541c62010-04-16 20:28:11 -070072
Elliott Hughes929f4072015-04-24 21:13:44 -070073 register_epoll_handler(signal_read_fd, handle_signal);
Colin Cross9c5366b2010-04-13 19:48:59 -070074}