OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // TODO(jamiewalch): Add unit tests for this. | |
6 | |
7 #include "remoting/host/sighup_listener_mac.h" | |
8 | |
9 #include <errno.h> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "base/eintr_wrapper.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/message_pump_libevent.h" | |
15 #include "base/threading/platform_thread.h" | |
16 | |
17 namespace { | |
18 | |
19 class SigHupListener : public base::MessagePumpLibevent::Watcher { | |
20 public: | |
21 SigHupListener(const base::Closure& callback); | |
22 | |
23 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
24 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} | |
25 | |
26 // WatchFileDescriptor needs a controller through which the operation can be | |
27 // canceled. We don't use it, but this is as good a place as any to store it. | |
28 base::MessagePumpLibevent::FileDescriptorWatcher controller; | |
29 | |
30 private: | |
31 base::Closure callback_; | |
32 }; | |
33 | |
34 SigHupListener::SigHupListener(const base::Closure& callback) | |
35 : callback_(callback) { | |
36 } | |
37 | |
38 void SigHupListener::OnFileCanReadWithoutBlocking(int fd) { | |
39 char buffer; | |
40 int result = HANDLE_EINTR(read(fd, &buffer, sizeof(buffer))); | |
41 if (result > 0) { | |
42 callback_.Run(); | |
43 } | |
44 } | |
45 | |
46 SigHupListener* g_config_updater = NULL; | |
47 int g_write_fd = 0; | |
48 | |
49 void HupSignalHandler(int signal) { | |
50 int r ALLOW_UNUSED = write(g_write_fd, "", 1); | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 namespace remoting { | |
56 | |
57 // RegisterHupSignalHandler registers a signal handler that writes a byte to a | |
58 // pipe each time SIGHUP is received. The read end of the pipe is registered | |
59 // with the current MessageLoop (which must be of type IO); whenever the pipe | |
60 // is readable, it invokes the specified callback. | |
61 // | |
62 // This arrangement is required because the set of system APIs that are safe to | |
63 // call from a singal handler is very limited (but does include write). | |
64 bool RegisterHupSignalHandler(const base::Closure& callback) { | |
65 DCHECK(!g_config_updater); | |
66 MessageLoopForIO* message_loop = MessageLoopForIO::current(); | |
67 int pipefd[2]; | |
68 int result = pipe(pipefd); | |
69 if (result < 0) { | |
70 LOG(ERROR) << "Could not create SIGHUP pipe: " << errno; | |
71 return false; | |
72 } | |
73 g_write_fd = pipefd[1]; | |
74 g_config_updater = new SigHupListener(callback); | |
75 result = message_loop->WatchFileDescriptor( | |
76 pipefd[0], true, MessageLoopForIO::WATCH_READ, | |
77 &g_config_updater->controller, g_config_updater); | |
78 if (!result) { | |
79 delete g_config_updater; | |
80 g_config_updater = NULL; | |
81 LOG(ERROR) << "Failed to create SIGHUP detector task."; | |
82 return false; | |
83 } | |
84 if (signal(SIGHUP, HupSignalHandler) == SIG_ERR) { | |
85 delete g_config_updater; | |
86 g_config_updater = NULL; | |
87 LOG(ERROR) << "signal() failed: " << errno; | |
88 return false; | |
89 } | |
90 return true; | |
91 } | |
92 | |
93 } // namespace remoting | |
OLD | NEW |