OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "ipc/ipc_channel_factory.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <stddef.h> |
| 10 #include <sys/socket.h> |
| 11 #include <sys/stat.h> |
| 12 #include <sys/types.h> |
| 13 #include <sys/un.h> |
| 14 #include <unistd.h> |
| 15 |
| 16 #include "base/file_util.h" |
| 17 #include "base/logging.h" |
| 18 #include "ipc/ipc_channel_posix.h" |
| 19 #include "ipc/unix_domain_socket_util.h" |
| 20 |
| 21 namespace IPC { |
| 22 |
| 23 ChannelFactory::ChannelFactory(const std::string& path, Delegate* delegate) |
| 24 : path_(path), delegate_(delegate), listen_pipe_(-1) { |
| 25 if (!CreatePipe()) { |
| 26 // The pipe may have been closed already. |
| 27 LOG(WARNING) << "Unable to create pipe named \"" << path << "\""; |
| 28 } |
| 29 } |
| 30 |
| 31 bool ChannelFactory::CreatePipe() { |
| 32 DCHECK(listen_pipe_ == -1); |
| 33 |
| 34 int local_pipe = -1; |
| 35 |
| 36 // Create the socket. |
| 37 if (!CreateServerUnixDomainSocket(path_, &local_pipe)) |
| 38 return false; |
| 39 |
| 40 listen_pipe_ = local_pipe; |
| 41 |
| 42 return true; |
| 43 } |
| 44 |
| 45 bool ChannelFactory::Listen() { |
| 46 if (listen_pipe_ == -1) { |
| 47 DLOG(INFO) << "Factory creation failed: " << path_.value(); |
| 48 return false; |
| 49 } |
| 50 // Watch the pipe for connections, and turn any connections into |
| 51 // active sockets. |
| 52 MessageLoopForIO::current()->WatchFileDescriptor( |
| 53 listen_pipe_, |
| 54 true, |
| 55 MessageLoopForIO::WATCH_READ, |
| 56 &server_listen_connection_watcher_, |
| 57 this); |
| 58 return true; |
| 59 } |
| 60 |
| 61 // Called by libevent when we can read from the pipe without blocking. |
| 62 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { |
| 63 DCHECK(fd == listen_pipe_); |
| 64 int new_pipe = 0; |
| 65 if (!ServerAcceptConnection(listen_pipe_, &new_pipe)) { |
| 66 /*Close(); |
| 67 listener()->OnChannelListenError();*/ |
| 68 return; |
| 69 } |
| 70 |
| 71 // Verify that the IPC channel peer is running as the same user. |
| 72 uid_t client_euid; |
| 73 if (!GetPeerEuid(new_pipe, &client_euid)) { |
| 74 DLOG(ERROR) << "Unable to query client euid"; |
| 75 // TODO close new pipe |
| 76 return; |
| 77 } |
| 78 if (client_euid != geteuid()) { |
| 79 DLOG(WARNING) << "Client euid is not authorised"; |
| 80 // TODO close new pipe |
| 81 return; |
| 82 } |
| 83 |
| 84 ChannelHandle handle("", base::FileDescriptor(new_pipe, true)); |
| 85 delegate_->OnClientConnected(handle); |
| 86 } |
| 87 |
| 88 } |
OLD | NEW |