2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/ipc/ipc_handler_linux.h"
19 #include <sys/socket.h>
22 #include <base/bind.h>
24 #include "osi/include/socket_utils/sockets.h"
25 #include "service/daemon.h"
26 #include "service/ipc/linux_ipc_host.h"
27 #include "service/settings.h"
31 IPCHandlerLinux::IPCHandlerLinux(bluetooth::Adapter* adapter,
32 IPCManager::Delegate* delegate)
33 : IPCHandler(adapter, delegate),
35 thread_("IPCHandlerLinux"),
39 IPCHandlerLinux::~IPCHandlerLinux() {
40 // This will only be set if the Settings::create_ipc_socket_path() was
41 // originally provided.
42 if (!socket_path_.empty())
43 unlink(socket_path_.value().c_str());
46 bool IPCHandlerLinux::Run() {
49 const std::string& android_suffix =
50 bluetooth::Daemon::Get()->GetSettings()->android_ipc_socket_suffix();
51 const base::FilePath& path =
52 bluetooth::Daemon::Get()->GetSettings()->create_ipc_socket_path();
54 // Both flags cannot be set at the same time.
55 CHECK(android_suffix.empty() || path.empty());
56 if (android_suffix.empty() && path.empty()) {
57 LOG(ERROR) << "No domain socket path provided";
61 CHECK(base::MessageLoop::current()); // An origin event loop is required.
62 origin_task_runner_ = base::MessageLoop::current()->task_runner();
64 if (!android_suffix.empty()) {
65 int server_fd = osi_android_get_control_socket(android_suffix.c_str());
66 if (server_fd == -1) {
67 LOG(ERROR) << "Unable to get Android socket from: " << android_suffix;
70 LOG(INFO) << "Binding to Android server socket:" << android_suffix;
71 socket_.reset(server_fd);
73 LOG(INFO) << "Creating a Unix domain socket:" << path.value();
75 // TODO(armansito): This is opens the door to potentially unlinking files in
76 // the current directory that we're not supposed to. For now we will have an
77 // assumption that the daemon runs in a sandbox but we should generally do
79 unlink(path.value().c_str());
81 base::ScopedFD server_socket(socket(PF_UNIX, SOCK_SEQPACKET, 0));
82 if (!server_socket.is_valid()) {
83 LOG(ERROR) << "Failed to open domain socket for IPC";
87 struct sockaddr_un address;
88 memset(&address, 0, sizeof(address));
89 address.sun_family = AF_UNIX;
90 strncpy(address.sun_path, path.value().c_str(),
91 sizeof(address.sun_path) - 1);
92 if (bind(server_socket.get(), (struct sockaddr*)&address, sizeof(address)) <
94 LOG(ERROR) << "Failed to bind IPC socket to address: " << strerror(errno);
98 socket_.swap(server_socket);
102 CHECK(socket_.is_valid());
104 running_ = true; // Set this here before launching the thread.
106 // Start an IO thread and post the listening task.
107 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
108 if (!thread_.StartWithOptions(options)) {
109 LOG(ERROR) << "Failed to start IPCHandlerLinux thread";
114 thread_.task_runner()->PostTask(
116 base::Bind(&IPCHandlerLinux::StartListeningOnThread, this));
121 void IPCHandlerLinux::Stop() {
122 keep_running_ = false;
124 // At this moment the listening thread might be blocking on the accept
125 // syscall. Shutdown and close the server socket before joining the thread to
126 // interrupt accept so that the main thread doesn't keep blocking.
127 shutdown(socket_.get(), SHUT_RDWR);
130 // Join and clean up the thread.
133 // Thread exited. Notify the delegate. Post this on the event loop so that the
134 // callback isn't reentrant.
135 NotifyStoppedOnOriginThread();
138 void IPCHandlerLinux::StartListeningOnThread() {
139 CHECK(socket_.is_valid());
143 LOG(INFO) << "Listening to incoming connections";
145 int status = listen(socket_.get(), SOMAXCONN);
147 LOG(ERROR) << "Failed to listen on domain socket: " << strerror(errno);
148 origin_task_runner_->PostTask(
150 base::Bind(&IPCHandlerLinux::ShutDownOnOriginThread, this));
154 NotifyStartedOnOriginThread();
156 // TODO(armansito): The code below can cause the daemon to run indefinitely if
157 // the thread is joined while it's in the middle of the EventLoop() call. The
158 // EventLoop() won't exit until a client terminates the connection, however
159 // this can be fixed by using the |thread_|'s MessageLoopForIO instead (since
160 // it gets stopped along with the thread).
161 // TODO(icoolidge): accept simultaneous clients
162 while (keep_running_.load()) {
163 int client_socket = accept4(socket_.get(), nullptr, nullptr, SOCK_NONBLOCK);
164 if (client_socket < 0) {
165 LOG(ERROR) << "Failed to accept client connection: " << strerror(errno);
169 LOG(INFO) << "Established client connection: fd=" << client_socket;
171 LinuxIPCHost ipc_host(client_socket, adapter());
173 // TODO(armansito): Use |thread_|'s MessageLoopForIO instead of using a
174 // custom event loop to poll from the socket.
175 ipc_host.EventLoop();
179 void IPCHandlerLinux::ShutDownOnOriginThread() {
180 LOG(INFO) << "Shutting down IPCHandlerLinux thread";
184 NotifyStoppedOnCurrentThread();
187 void IPCHandlerLinux::NotifyStartedOnOriginThread() {
191 origin_task_runner_->PostTask(
193 base::Bind(&IPCHandlerLinux::NotifyStartedOnCurrentThread, this));
196 void IPCHandlerLinux::NotifyStartedOnCurrentThread() {
198 delegate()->OnIPCHandlerStarted(IPCManager::TYPE_LINUX);
201 void IPCHandlerLinux::NotifyStoppedOnOriginThread() {
205 origin_task_runner_->PostTask(
207 base::Bind(&IPCHandlerLinux::NotifyStoppedOnCurrentThread, this));
210 void IPCHandlerLinux::NotifyStoppedOnCurrentThread() {
212 delegate()->OnIPCHandlerStopped(IPCManager::TYPE_LINUX);