Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: components/arc/standalone/service_helper.cc

Issue 2391743002: Use FileDescriptorWatcher in ServiceHelper. (Closed)
Patch Set: self-review Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/standalone/service_helper.h" 5 #include "components/arc/standalone/service_helper.h"
6 6
7 #include "base/bind.h"
7 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/posix/eintr_wrapper.h" 10 #include "base/posix/eintr_wrapper.h"
11 11
12 namespace arc { 12 namespace arc {
13 13
14 // static 14 // static
15 ServiceHelper* ServiceHelper::self_ = nullptr; 15 ServiceHelper* ServiceHelper::self_ = nullptr;
16 16
17 ServiceHelper::ServiceHelper() { 17 ServiceHelper::ServiceHelper() {
18 } 18 }
19 19
20 ServiceHelper::~ServiceHelper() { 20 ServiceHelper::~ServiceHelper() {
21 // Best efforts clean up, ignore the return values. 21 // Best efforts clean up, ignore the return values.
22 sigaction(SIGINT, &old_sigint_, NULL); 22 sigaction(SIGINT, &old_sigint_, NULL);
23 sigaction(SIGTERM, &old_sigterm_, NULL); 23 sigaction(SIGTERM, &old_sigterm_, NULL);
24 } 24 }
25 25
26 void ServiceHelper::Init(const base::Closure& closure) { 26 void ServiceHelper::Init(const base::Closure& closure) {
27 CHECK(!ServiceHelper::self_); 27 CHECK(!ServiceHelper::self_);
28 ServiceHelper::self_ = this; 28 ServiceHelper::self_ = this;
29 closure_ = closure; 29 closure_ = closure;
30 30
31 // Initialize pipe 31 // Initialize pipe
32 int pipe_fd[2]; 32 int pipe_fd[2];
33 CHECK_EQ(0, pipe(pipe_fd)); 33 CHECK_EQ(0, pipe(pipe_fd));
34 read_fd_.reset(pipe_fd[0]); 34 read_fd_.reset(pipe_fd[0]);
35 write_fd_.reset(pipe_fd[1]); 35 write_fd_.reset(pipe_fd[1]);
36 CHECK(base::SetNonBlocking(write_fd_.get())); 36 CHECK(base::SetNonBlocking(write_fd_.get()));
37 CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor( 37 watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
38 read_fd_.get(), 38 read_fd_.get(), base::Bind(&ServiceHelper::OnFileCanReadWithoutBlocking,
39 true, /* persistent */ 39 base::Unretained(this)));
40 base::MessageLoopForIO::WATCH_READ,
41 &watcher_,
42 this));
43 40
44 struct sigaction action = {}; 41 struct sigaction action = {};
45 CHECK_EQ(0, sigemptyset(&action.sa_mask)); 42 CHECK_EQ(0, sigemptyset(&action.sa_mask));
46 action.sa_handler = ServiceHelper::TerminationHandler; 43 action.sa_handler = ServiceHelper::TerminationHandler;
47 action.sa_flags = 0; 44 action.sa_flags = 0;
48 45
49 CHECK_EQ(0, sigaction(SIGINT, &action, &old_sigint_)); 46 CHECK_EQ(0, sigaction(SIGINT, &action, &old_sigint_));
50 CHECK_EQ(0, sigaction(SIGTERM, &action, &old_sigterm_)); 47 CHECK_EQ(0, sigaction(SIGTERM, &action, &old_sigterm_));
51 } 48 }
52 49
53 // static 50 // static
54 void ServiceHelper::TerminationHandler(int /* signum */) { 51 void ServiceHelper::TerminationHandler(int /* signum */) {
55 if (HANDLE_EINTR(write(self_->write_fd_.get(), "1", 1)) < 0) { 52 if (HANDLE_EINTR(write(self_->write_fd_.get(), "1", 1)) < 0) {
56 _exit(2); // We still need to exit the program, but in a brute force way. 53 _exit(2); // We still need to exit the program, but in a brute force way.
57 } 54 }
58 } 55 }
59 56
60 void ServiceHelper::OnFileCanReadWithoutBlocking(int fd) { 57 void ServiceHelper::OnFileCanReadWithoutBlocking() {
61 CHECK_EQ(read_fd_.get(), fd);
62
63 char c; 58 char c;
64 // We don't really care about the return value, since it indicates closing. 59 // We don't really care about the return value, since it indicates closing.
65 HANDLE_EINTR(read(read_fd_.get(), &c, 1)); 60 HANDLE_EINTR(read(read_fd_.get(), &c, 1));
66 closure_.Run(); 61 closure_.Run();
67 } 62 }
68 63
69 void ServiceHelper::OnFileCanWriteWithoutBlocking(int fd) {
70 NOTREACHED();
71 }
72
73 } // namespace arc 64 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/standalone/service_helper.h ('k') | components/arc/standalone/service_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698