| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/arc/standalone/service_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/posix/eintr_wrapper.h" | |
| 11 | |
| 12 namespace arc { | |
| 13 | |
| 14 // static | |
| 15 ServiceHelper* ServiceHelper::self_ = nullptr; | |
| 16 | |
| 17 ServiceHelper::ServiceHelper() { | |
| 18 } | |
| 19 | |
| 20 ServiceHelper::~ServiceHelper() { | |
| 21 // Best efforts clean up, ignore the return values. | |
| 22 sigaction(SIGINT, &old_sigint_, NULL); | |
| 23 sigaction(SIGTERM, &old_sigterm_, NULL); | |
| 24 } | |
| 25 | |
| 26 void ServiceHelper::Init(const base::Closure& closure) { | |
| 27 CHECK(!ServiceHelper::self_); | |
| 28 ServiceHelper::self_ = this; | |
| 29 closure_ = closure; | |
| 30 | |
| 31 // Initialize pipe | |
| 32 int pipe_fd[2]; | |
| 33 CHECK_EQ(0, pipe(pipe_fd)); | |
| 34 read_fd_.reset(pipe_fd[0]); | |
| 35 write_fd_.reset(pipe_fd[1]); | |
| 36 CHECK(base::SetNonBlocking(write_fd_.get())); | |
| 37 watch_controller_ = base::FileDescriptorWatcher::WatchReadable( | |
| 38 read_fd_.get(), base::Bind(&ServiceHelper::OnFileCanReadWithoutBlocking, | |
| 39 base::Unretained(this))); | |
| 40 | |
| 41 struct sigaction action = {}; | |
| 42 CHECK_EQ(0, sigemptyset(&action.sa_mask)); | |
| 43 action.sa_handler = ServiceHelper::TerminationHandler; | |
| 44 action.sa_flags = 0; | |
| 45 | |
| 46 CHECK_EQ(0, sigaction(SIGINT, &action, &old_sigint_)); | |
| 47 CHECK_EQ(0, sigaction(SIGTERM, &action, &old_sigterm_)); | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 void ServiceHelper::TerminationHandler(int /* signum */) { | |
| 52 if (HANDLE_EINTR(write(self_->write_fd_.get(), "1", 1)) < 0) { | |
| 53 _exit(2); // We still need to exit the program, but in a brute force way. | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void ServiceHelper::OnFileCanReadWithoutBlocking() { | |
| 58 char c; | |
| 59 // We don't really care about the return value, since it indicates closing. | |
| 60 HANDLE_EINTR(read(read_fd_.get(), &c, 1)); | |
| 61 closure_.Run(); | |
| 62 } | |
| 63 | |
| 64 } // namespace arc | |
| OLD | NEW |