jrg@chromium.org | 8b302f3 | 2012-01-26 08:53:06 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 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_posix.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stddef.h> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <sys/stat.h> |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 12 | #include <sys/types.h> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 13 | #include <sys/un.h> |
shenhan@google.com | 8a6e499 | 2012-06-05 10:54:46 +0900 | [diff] [blame] | 14 | #include <unistd.h> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 15 | |
mark@chromium.org | 61edb8b | 2011-10-19 02:46:22 +0900 | [diff] [blame] | 16 | #if defined(OS_OPENBSD) |
| 17 | #include <sys/uio.h> |
| 18 | #endif |
| 19 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 20 | #include <map> |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 21 | #include <string> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 22 | |
| 23 | #include "base/command_line.h" |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 24 | #include "base/file_util.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 26 | #include "base/location.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 27 | #include "base/logging.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 28 | #include "base/memory/scoped_ptr.h" |
| 29 | #include "base/memory/singleton.h" |
brettw@chromium.org | b1788fb | 2012-11-15 05:54:35 +0900 | [diff] [blame] | 30 | #include "base/posix/eintr_wrapper.h" |
brettw@chromium.org | ea3b3f2 | 2012-11-10 08:46:54 +0900 | [diff] [blame] | 31 | #include "base/posix/global_descriptors.h" |
rsesek@chromium.org | 1931971 | 2013-07-24 14:15:24 +0900 | [diff] [blame] | 32 | #include "base/process/process_handle.h" |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 33 | #include "base/rand_util.h" |
dilmah@chromium.org | dc4b970 | 2011-07-20 07:13:24 +0900 | [diff] [blame] | 34 | #include "base/stl_util.h" |
avi@chromium.org | e7eaf39 | 2013-06-11 15:32:18 +0900 | [diff] [blame] | 35 | #include "base/strings/string_util.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 36 | #include "base/synchronization/lock.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 37 | #include "ipc/file_descriptor_set_posix.h" |
tfarina@chromium.org | b6d8120 | 2012-11-16 07:22:17 +0900 | [diff] [blame] | 38 | #include "ipc/ipc_descriptors.h" |
| 39 | #include "ipc/ipc_listener.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 40 | #include "ipc/ipc_logging.h" |
| 41 | #include "ipc/ipc_message_utils.h" |
tfarina@chromium.org | b6d8120 | 2012-11-16 07:22:17 +0900 | [diff] [blame] | 42 | #include "ipc/ipc_switches.h" |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 43 | #include "ipc/unix_domain_socket_util.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 44 | |
| 45 | namespace IPC { |
| 46 | |
| 47 | // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 48 | // channel ids as the pipe names. Channels on POSIX use sockets as |
| 49 | // pipes These don't quite line up. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 50 | // |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 51 | // When creating a child subprocess we use a socket pair and the parent side of |
| 52 | // the fork arranges it such that the initial control channel ends up on the |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 53 | // magic file descriptor kPrimaryIPCChannel in the child. Future |
| 54 | // connections (file descriptors) can then be passed via that |
| 55 | // connection via sendmsg(). |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 56 | // |
| 57 | // A POSIX IPC channel can also be set up as a server for a bound UNIX domain |
| 58 | // socket, and will handle multiple connect and disconnect sequences. Currently |
| 59 | // it is limited to one connection at a time. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 60 | |
| 61 | //------------------------------------------------------------------------------ |
| 62 | namespace { |
| 63 | |
| 64 | // The PipeMap class works around this quirk related to unit tests: |
| 65 | // |
| 66 | // When running as a server, we install the client socket in a |
| 67 | // specific file descriptor number (@kPrimaryIPCChannel). However, we |
| 68 | // also have to support the case where we are running unittests in the |
| 69 | // same process. (We do not support forking without execing.) |
| 70 | // |
| 71 | // Case 1: normal running |
| 72 | // The IPC server object will install a mapping in PipeMap from the |
| 73 | // name which it was given to the client pipe. When forking the client, the |
| 74 | // GetClientFileDescriptorMapping will ensure that the socket is installed in |
| 75 | // the magic slot (@kPrimaryIPCChannel). The client will search for the |
| 76 | // mapping, but it won't find any since we are in a new process. Thus the |
| 77 | // magic fd number is returned. Once the client connects, the server will |
| 78 | // close its copy of the client socket and remove the mapping. |
| 79 | // |
| 80 | // Case 2: unittests - client and server in the same process |
| 81 | // The IPC server will install a mapping as before. The client will search |
| 82 | // for a mapping and find out. It duplicates the file descriptor and |
| 83 | // connects. Once the client connects, the server will close the original |
| 84 | // copy of the client socket and remove the mapping. Thus, when the client |
| 85 | // object closes, it will close the only remaining copy of the client socket |
| 86 | // in the fd table and the server will see EOF on its side. |
| 87 | // |
| 88 | // TODO(port): a client process cannot connect to multiple IPC channels with |
| 89 | // this scheme. |
| 90 | |
| 91 | class PipeMap { |
| 92 | public: |
satish@chromium.org | 7722283 | 2010-12-05 08:00:10 +0900 | [diff] [blame] | 93 | static PipeMap* GetInstance() { |
| 94 | return Singleton<PipeMap>::get(); |
| 95 | } |
| 96 | |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 97 | ~PipeMap() { |
| 98 | // Shouldn't have left over pipes. |
erg@google.com | aed4b38 | 2011-03-02 09:03:18 +0900 | [diff] [blame] | 99 | DCHECK(map_.empty()); |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 100 | } |
| 101 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 102 | // Lookup a given channel id. Return -1 if not found. |
| 103 | int Lookup(const std::string& channel_id) { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 104 | base::AutoLock locked(lock_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 105 | |
| 106 | ChannelToFDMap::const_iterator i = map_.find(channel_id); |
| 107 | if (i == map_.end()) |
| 108 | return -1; |
| 109 | return i->second; |
| 110 | } |
| 111 | |
| 112 | // Remove the mapping for the given channel id. No error is signaled if the |
| 113 | // channel_id doesn't exist |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 114 | void Remove(const std::string& channel_id) { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 115 | base::AutoLock locked(lock_); |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 116 | map_.erase(channel_id); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Insert a mapping from @channel_id to @fd. It's a fatal error to insert a |
| 120 | // mapping if one already exists for the given channel_id |
| 121 | void Insert(const std::string& channel_id, int fd) { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 122 | base::AutoLock locked(lock_); |
david.mike.futcher@gmail.com | 9eb2aa5 | 2011-04-19 05:07:08 +0900 | [diff] [blame] | 123 | DCHECK_NE(-1, fd); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 124 | |
| 125 | ChannelToFDMap::const_iterator i = map_.find(channel_id); |
| 126 | CHECK(i == map_.end()) << "Creating second IPC server (fd " << fd << ") " |
| 127 | << "for '" << channel_id << "' while first " |
| 128 | << "(fd " << i->second << ") still exists"; |
| 129 | map_[channel_id] = fd; |
| 130 | } |
| 131 | |
| 132 | private: |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 133 | base::Lock lock_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 134 | typedef std::map<std::string, int> ChannelToFDMap; |
| 135 | ChannelToFDMap map_; |
satish@chromium.org | 7722283 | 2010-12-05 08:00:10 +0900 | [diff] [blame] | 136 | |
| 137 | friend struct DefaultSingletonTraits<PipeMap>; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 140 | //------------------------------------------------------------------------------ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 141 | |
jeremy@chromium.org | 2a85b11 | 2009-12-08 23:48:08 +0900 | [diff] [blame] | 142 | bool SocketWriteErrorIsRecoverable() { |
| 143 | #if defined(OS_MACOSX) |
| 144 | // On OS X if sendmsg() is trying to send fds between processes and there |
| 145 | // isn't enough room in the output buffer to send the fd structure over |
| 146 | // atomically then EMSGSIZE is returned. |
| 147 | // |
| 148 | // EMSGSIZE presents a problem since the system APIs can only call us when |
| 149 | // there's room in the socket buffer and not when there is "enough" room. |
| 150 | // |
| 151 | // The current behavior is to return to the event loop when EMSGSIZE is |
| 152 | // received and hopefull service another FD. This is however still |
| 153 | // technically a busy wait since the event loop will call us right back until |
| 154 | // the receiver has read enough data to allow passing the FD over atomically. |
| 155 | return errno == EAGAIN || errno == EMSGSIZE; |
| 156 | #else |
| 157 | return errno == EAGAIN; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 158 | #endif // OS_MACOSX |
jeremy@chromium.org | 2a85b11 | 2009-12-08 23:48:08 +0900 | [diff] [blame] | 159 | } |
| 160 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 161 | } // namespace |
| 162 | //------------------------------------------------------------------------------ |
| 163 | |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 164 | #if defined(OS_LINUX) |
| 165 | int Channel::ChannelImpl::global_pid_ = 0; |
| 166 | #endif // OS_LINUX |
| 167 | |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 168 | Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, |
| 169 | Mode mode, Listener* listener) |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 170 | : ChannelReader(listener), |
| 171 | mode_(mode), |
jschuh@chromium.org | a5cd076 | 2012-04-05 11:38:34 +0900 | [diff] [blame] | 172 | peer_pid_(base::kNullProcessId), |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 173 | is_blocked_on_write_(false), |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 174 | waiting_connect_(true), |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 175 | message_send_bytes_written_(0), |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 176 | server_listen_pipe_(-1), |
| 177 | pipe_(-1), |
| 178 | client_pipe_(-1), |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 179 | #if defined(IPC_USES_READWRITE) |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 180 | fd_pipe_(-1), |
| 181 | remote_fd_pipe_(-1), |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 182 | #endif // IPC_USES_READWRITE |
| 183 | pipe_name_(channel_handle.name), |
satorux@chromium.org | ad12d3d | 2011-08-23 12:17:02 +0900 | [diff] [blame] | 184 | must_unlink_(false) { |
jhawkins@chromium.org | 8fa070a | 2011-06-22 07:48:29 +0900 | [diff] [blame] | 185 | memset(input_cmsg_buf_, 0, sizeof(input_cmsg_buf_)); |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 186 | if (!CreatePipe(channel_handle)) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 187 | // The pipe may have been closed already. |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 188 | const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client"; |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 189 | LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 190 | << "\" in " << modestr << " mode"; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
erg@google.com | a7331b6 | 2010-09-02 02:08:20 +0900 | [diff] [blame] | 194 | Channel::ChannelImpl::~ChannelImpl() { |
| 195 | Close(); |
| 196 | } |
| 197 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 198 | bool SocketPair(int* fd1, int* fd2) { |
| 199 | int pipe_fds[2]; |
| 200 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
tschmelcher@chromium.org | 90a3f8a | 2009-10-14 03:27:40 +0900 | [diff] [blame] | 201 | PLOG(ERROR) << "socketpair()"; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // Set both ends to be non-blocking. |
| 206 | if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| 207 | fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
tschmelcher@chromium.org | 90a3f8a | 2009-10-14 03:27:40 +0900 | [diff] [blame] | 208 | PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
thakis@chromium.org | 965db9a | 2010-06-23 09:37:46 +0900 | [diff] [blame] | 209 | if (HANDLE_EINTR(close(pipe_fds[0])) < 0) |
| 210 | PLOG(ERROR) << "close"; |
| 211 | if (HANDLE_EINTR(close(pipe_fds[1])) < 0) |
| 212 | PLOG(ERROR) << "close"; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | *fd1 = pipe_fds[0]; |
| 217 | *fd2 = pipe_fds[1]; |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 222 | bool Channel::ChannelImpl::CreatePipe( |
| 223 | const IPC::ChannelHandle& channel_handle) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 224 | DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 225 | |
| 226 | // Four possible cases: |
| 227 | // 1) It's a channel wrapping a pipe that is given to us. |
| 228 | // 2) It's for a named channel, so we create it. |
| 229 | // 3) It's for a client that we implement ourself. This is used |
| 230 | // in unittesting. |
| 231 | // 4) It's the initial IPC channel: |
| 232 | // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. |
| 233 | // 4b) Server side: create the pipe. |
| 234 | |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 235 | int local_pipe = -1; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 236 | if (channel_handle.socket.fd != -1) { |
| 237 | // Case 1 from comment above. |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 238 | local_pipe = channel_handle.socket.fd; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 239 | #if defined(IPC_USES_READWRITE) |
| 240 | // Test the socket passed into us to make sure it is nonblocking. |
| 241 | // We don't want to call read/write on a blocking socket. |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 242 | int value = fcntl(local_pipe, F_GETFL); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 243 | if (value == -1) { |
| 244 | PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; |
| 245 | return false; |
| 246 | } |
| 247 | if (!(value & O_NONBLOCK)) { |
| 248 | LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; |
| 249 | return false; |
| 250 | } |
| 251 | #endif // IPC_USES_READWRITE |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 252 | } else if (mode_ & MODE_NAMED_FLAG) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 253 | // Case 2 from comment above. |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 254 | if (mode_ & MODE_SERVER_FLAG) { |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 255 | if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_), |
| 256 | &local_pipe)) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 257 | return false; |
| 258 | } |
dmaclach@chromium.org | 0c55c56 | 2011-02-25 02:14:36 +0900 | [diff] [blame] | 259 | must_unlink_ = true; |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 260 | } else if (mode_ & MODE_CLIENT_FLAG) { |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 261 | if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_), |
| 262 | &local_pipe)) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 263 | return false; |
| 264 | } |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 265 | } else { |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 266 | LOG(ERROR) << "Bad mode: " << mode_; |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 267 | return false; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 268 | } |
| 269 | } else { |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 270 | local_pipe = PipeMap::GetInstance()->Lookup(pipe_name_); |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 271 | if (mode_ & MODE_CLIENT_FLAG) { |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 272 | if (local_pipe != -1) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 273 | // Case 3 from comment above. |
| 274 | // We only allow one connection. |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 275 | local_pipe = HANDLE_EINTR(dup(local_pipe)); |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 276 | PipeMap::GetInstance()->Remove(pipe_name_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 277 | } else { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 278 | // Case 4a from comment above. |
mark@chromium.org | ae34f8c | 2009-12-01 07:14:37 +0900 | [diff] [blame] | 279 | // Guard against inappropriate reuse of the initial IPC channel. If |
| 280 | // an IPC channel closes and someone attempts to reuse it by name, the |
| 281 | // initial channel must not be recycled here. http://crbug.com/26754. |
| 282 | static bool used_initial_channel = false; |
| 283 | if (used_initial_channel) { |
mark@chromium.org | c973c0f | 2010-03-17 05:31:10 +0900 | [diff] [blame] | 284 | LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " |
| 285 | << pipe_name_; |
mark@chromium.org | ae34f8c | 2009-12-01 07:14:37 +0900 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | used_initial_channel = true; |
| 289 | |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 290 | local_pipe = |
| 291 | base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 292 | } |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 293 | } else if (mode_ & MODE_SERVER_FLAG) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 294 | // Case 4b from comment above. |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 295 | if (local_pipe != -1) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 296 | LOG(ERROR) << "Server already exists for " << pipe_name_; |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 297 | return false; |
| 298 | } |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 299 | base::AutoLock lock(client_pipe_lock_); |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 300 | if (!SocketPair(&local_pipe, &client_pipe_)) |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 301 | return false; |
| 302 | PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_); |
| 303 | } else { |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 304 | LOG(ERROR) << "Bad mode: " << mode_; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 305 | return false; |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 306 | } |
| 307 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 308 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 309 | #if defined(IPC_USES_READWRITE) |
| 310 | // Create a dedicated socketpair() for exchanging file descriptors. |
| 311 | // See comments for IPC_USES_READWRITE for details. |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 312 | if (mode_ & MODE_CLIENT_FLAG) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 313 | if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | #endif // IPC_USES_READWRITE |
| 318 | |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 319 | if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) { |
| 320 | server_listen_pipe_ = local_pipe; |
| 321 | local_pipe = -1; |
| 322 | } |
| 323 | |
| 324 | pipe_ = local_pipe; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
| 328 | bool Channel::ChannelImpl::Connect() { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 329 | if (server_listen_pipe_ == -1 && pipe_ == -1) { |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 330 | DLOG(INFO) << "Channel creation failed: " << pipe_name_; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
| 334 | bool did_connect = true; |
| 335 | if (server_listen_pipe_ != -1) { |
| 336 | // Watch the pipe for connections, and turn any connections into |
| 337 | // active sockets. |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 338 | base::MessageLoopForIO::current()->WatchFileDescriptor( |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 339 | server_listen_pipe_, |
| 340 | true, |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 341 | base::MessageLoopForIO::WATCH_READ, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 342 | &server_listen_connection_watcher_, |
| 343 | this); |
| 344 | } else { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 345 | did_connect = AcceptConnection(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 346 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 347 | return did_connect; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 348 | } |
| 349 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 350 | bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
| 351 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 352 | // no connection? |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 353 | if (output_queue_.empty()) |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 354 | return true; |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 355 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 356 | if (pipe_ == -1) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 357 | return false; |
| 358 | |
| 359 | // Write out all the messages we can till the write blocks or there are no |
| 360 | // more outgoing messages. |
| 361 | while (!output_queue_.empty()) { |
| 362 | Message* msg = output_queue_.front(); |
| 363 | |
| 364 | size_t amt_to_write = msg->size() - message_send_bytes_written_; |
david.mike.futcher@gmail.com | 9eb2aa5 | 2011-04-19 05:07:08 +0900 | [diff] [blame] | 365 | DCHECK_NE(0U, amt_to_write); |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 366 | const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 367 | message_send_bytes_written_; |
| 368 | |
| 369 | struct msghdr msgh = {0}; |
| 370 | struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; |
| 371 | msgh.msg_iov = &iov; |
| 372 | msgh.msg_iovlen = 1; |
| 373 | char buf[CMSG_SPACE( |
pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 374 | sizeof(int) * FileDescriptorSet::kMaxDescriptorsPerMessage)]; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 375 | |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 376 | ssize_t bytes_written = 1; |
| 377 | int fd_written = -1; |
| 378 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 379 | if (message_send_bytes_written_ == 0 && |
| 380 | !msg->file_descriptor_set()->empty()) { |
| 381 | // This is the first chunk of a message which has descriptors to send |
| 382 | struct cmsghdr *cmsg; |
| 383 | const unsigned num_fds = msg->file_descriptor_set()->size(); |
| 384 | |
pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 385 | DCHECK(num_fds <= FileDescriptorSet::kMaxDescriptorsPerMessage); |
agl@chromium.org | c1e93ea | 2010-06-11 06:39:04 +0900 | [diff] [blame] | 386 | if (msg->file_descriptor_set()->ContainsDirectoryDescriptor()) { |
| 387 | LOG(FATAL) << "Panic: attempting to transport directory descriptor over" |
| 388 | " IPC. Aborting to maintain sandbox isolation."; |
| 389 | // If you have hit this then something tried to send a file descriptor |
| 390 | // to a directory over an IPC channel. Since IPC channels span |
| 391 | // sandboxes this is very bad: the receiving process can use openat |
| 392 | // with ".." elements in the path in order to reach the real |
| 393 | // filesystem. |
| 394 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 395 | |
| 396 | msgh.msg_control = buf; |
| 397 | msgh.msg_controllen = CMSG_SPACE(sizeof(int) * num_fds); |
| 398 | cmsg = CMSG_FIRSTHDR(&msgh); |
| 399 | cmsg->cmsg_level = SOL_SOCKET; |
| 400 | cmsg->cmsg_type = SCM_RIGHTS; |
| 401 | cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); |
| 402 | msg->file_descriptor_set()->GetDescriptors( |
| 403 | reinterpret_cast<int*>(CMSG_DATA(cmsg))); |
| 404 | msgh.msg_controllen = cmsg->cmsg_len; |
| 405 | |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 406 | // DCHECK_LE above already checks that |
pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 407 | // num_fds < kMaxDescriptorsPerMessage so no danger of overflow. |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 408 | msg->header()->num_fds = static_cast<uint16>(num_fds); |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 409 | |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 410 | #if defined(IPC_USES_READWRITE) |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 411 | if (!IsHelloMessage(*msg)) { |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 412 | // Only the Hello message sends the file descriptor with the message. |
| 413 | // Subsequently, we can send file descriptors on the dedicated |
| 414 | // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
| 415 | struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; |
| 416 | msgh.msg_iov = &fd_pipe_iov; |
| 417 | fd_written = fd_pipe_; |
| 418 | bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); |
| 419 | msgh.msg_iov = &iov; |
| 420 | msgh.msg_controllen = 0; |
| 421 | if (bytes_written > 0) { |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 422 | msg->file_descriptor_set()->CommitAll(); |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 423 | } |
| 424 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 425 | #endif // IPC_USES_READWRITE |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 426 | } |
| 427 | |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 428 | if (bytes_written == 1) { |
| 429 | fd_written = pipe_; |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 430 | #if defined(IPC_USES_READWRITE) |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 431 | if ((mode_ & MODE_CLIENT_FLAG) && IsHelloMessage(*msg)) { |
thomasvl@google.com | 9a24207 | 2010-07-23 23:18:59 +0900 | [diff] [blame] | 432 | DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 433 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 434 | if (!msgh.msg_controllen) { |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 435 | bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); |
| 436 | } else |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 437 | #endif // IPC_USES_READWRITE |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 438 | { |
| 439 | bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); |
| 440 | } |
| 441 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 442 | if (bytes_written > 0) |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 443 | msg->file_descriptor_set()->CommitAll(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 444 | |
jeremy@chromium.org | 2a85b11 | 2009-12-08 23:48:08 +0900 | [diff] [blame] | 445 | if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 446 | #if defined(OS_MACOSX) |
| 447 | // On OSX writing to a pipe with no listener returns EPERM. |
| 448 | if (errno == EPERM) { |
| 449 | Close(); |
| 450 | return false; |
| 451 | } |
| 452 | #endif // OS_MACOSX |
evan@chromium.org | 7d3eaa7 | 2009-10-23 10:48:21 +0900 | [diff] [blame] | 453 | if (errno == EPIPE) { |
| 454 | Close(); |
| 455 | return false; |
| 456 | } |
jeremy@chromium.org | 53a4fac | 2009-12-03 22:35:46 +0900 | [diff] [blame] | 457 | PLOG(ERROR) << "pipe error on " |
| 458 | << fd_written |
thestig@chromium.org | 8b56335 | 2011-02-11 17:43:52 +0900 | [diff] [blame] | 459 | << " Currently writing message of size: " |
jeremy@chromium.org | 2a85b11 | 2009-12-08 23:48:08 +0900 | [diff] [blame] | 460 | << msg->size(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
| 464 | if (static_cast<size_t>(bytes_written) != amt_to_write) { |
| 465 | if (bytes_written > 0) { |
| 466 | // If write() fails with EAGAIN then bytes_written will be -1. |
| 467 | message_send_bytes_written_ += bytes_written; |
| 468 | } |
| 469 | |
| 470 | // Tell libevent to call us back once things are unblocked. |
| 471 | is_blocked_on_write_ = true; |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 472 | base::MessageLoopForIO::current()->WatchFileDescriptor( |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 473 | pipe_, |
| 474 | false, // One shot |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 475 | base::MessageLoopForIO::WATCH_WRITE, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 476 | &write_watcher_, |
| 477 | this); |
| 478 | return true; |
| 479 | } else { |
| 480 | message_send_bytes_written_ = 0; |
| 481 | |
| 482 | // Message sent OK! |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 483 | DVLOG(2) << "sent message @" << msg << " on channel @" << this |
dmaclach@chromium.org | dec0e2b | 2010-12-09 10:13:12 +0900 | [diff] [blame] | 484 | << " with type " << msg->type() << " on fd " << pipe_; |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 485 | delete output_queue_.front(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 486 | output_queue_.pop(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | bool Channel::ChannelImpl::Send(Message* message) { |
pkasting@chromium.org | fcdd54b | 2010-10-20 08:50:00 +0900 | [diff] [blame] | 493 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 494 | << " with type " << message->type() |
| 495 | << " (" << output_queue_.size() << " in queue)"; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 496 | |
| 497 | #ifdef IPC_MESSAGE_LOG_ENABLED |
satish@chromium.org | aa87060 | 2010-12-13 17:18:55 +0900 | [diff] [blame] | 498 | Logging::GetInstance()->OnSendMessage(message, ""); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 499 | #endif // IPC_MESSAGE_LOG_ENABLED |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 500 | |
jbates@chromium.org | 7cc8033 | 2012-09-18 12:41:29 +0900 | [diff] [blame] | 501 | message->TraceMessageBegin(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 502 | output_queue_.push(message); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 503 | if (!is_blocked_on_write_ && !waiting_connect_) { |
| 504 | return ProcessOutgoingMessages(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 510 | int Channel::ChannelImpl::GetClientFileDescriptor() { |
| 511 | base::AutoLock lock(client_pipe_lock_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 512 | return client_pipe_; |
| 513 | } |
| 514 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 515 | int Channel::ChannelImpl::TakeClientFileDescriptor() { |
| 516 | base::AutoLock lock(client_pipe_lock_); |
| 517 | int fd = client_pipe_; |
| 518 | if (client_pipe_ != -1) { |
| 519 | PipeMap::GetInstance()->Remove(pipe_name_); |
| 520 | client_pipe_ = -1; |
| 521 | } |
| 522 | return fd; |
| 523 | } |
| 524 | |
| 525 | void Channel::ChannelImpl::CloseClientFileDescriptor() { |
| 526 | base::AutoLock lock(client_pipe_lock_); |
| 527 | if (client_pipe_ != -1) { |
| 528 | PipeMap::GetInstance()->Remove(pipe_name_); |
| 529 | if (HANDLE_EINTR(close(client_pipe_)) < 0) |
| 530 | PLOG(ERROR) << "close " << pipe_name_; |
| 531 | client_pipe_ = -1; |
| 532 | } |
| 533 | } |
| 534 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 535 | bool Channel::ChannelImpl::AcceptsConnections() const { |
| 536 | return server_listen_pipe_ != -1; |
| 537 | } |
dmaclach@chromium.org | 6a9b0c7 | 2010-12-20 15:19:07 +0900 | [diff] [blame] | 538 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 539 | bool Channel::ChannelImpl::HasAcceptedConnection() const { |
| 540 | return AcceptsConnections() && pipe_ != -1; |
| 541 | } |
dmaclach@chromium.org | 6a9b0c7 | 2010-12-20 15:19:07 +0900 | [diff] [blame] | 542 | |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 543 | bool Channel::ChannelImpl::GetPeerEuid(uid_t* peer_euid) const { |
| 544 | DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection()); |
| 545 | return IPC::GetPeerEuid(pipe_, peer_euid); |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 546 | } |
| 547 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 548 | void Channel::ChannelImpl::ResetToAcceptingConnectionState() { |
| 549 | // Unregister libevent for the unix domain socket and close it. |
| 550 | read_watcher_.StopWatchingFileDescriptor(); |
| 551 | write_watcher_.StopWatchingFileDescriptor(); |
| 552 | if (pipe_ != -1) { |
| 553 | if (HANDLE_EINTR(close(pipe_)) < 0) |
| 554 | PLOG(ERROR) << "close pipe_ " << pipe_name_; |
| 555 | pipe_ = -1; |
| 556 | } |
| 557 | #if defined(IPC_USES_READWRITE) |
| 558 | if (fd_pipe_ != -1) { |
| 559 | if (HANDLE_EINTR(close(fd_pipe_)) < 0) |
| 560 | PLOG(ERROR) << "close fd_pipe_ " << pipe_name_; |
| 561 | fd_pipe_ = -1; |
| 562 | } |
| 563 | if (remote_fd_pipe_ != -1) { |
| 564 | if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) |
| 565 | PLOG(ERROR) << "close remote_fd_pipe_ " << pipe_name_; |
| 566 | remote_fd_pipe_ = -1; |
| 567 | } |
| 568 | #endif // IPC_USES_READWRITE |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 569 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 570 | while (!output_queue_.empty()) { |
| 571 | Message* m = output_queue_.front(); |
| 572 | output_queue_.pop(); |
| 573 | delete m; |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 574 | } |
| 575 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 576 | // Close any outstanding, received file descriptors. |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 577 | ClearInputFDs(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 578 | } |
| 579 | |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 580 | // static |
| 581 | bool Channel::ChannelImpl::IsNamedServerInitialized( |
| 582 | const std::string& channel_id) { |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame] | 583 | return base::PathExists(base::FilePath(channel_id)); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 584 | } |
| 585 | |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 586 | #if defined(OS_LINUX) |
| 587 | // static |
| 588 | void Channel::ChannelImpl::SetGlobalPid(int pid) { |
| 589 | global_pid_ = pid; |
| 590 | } |
| 591 | #endif // OS_LINUX |
| 592 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 593 | // Called by libevent when we can read from the pipe without blocking. |
| 594 | void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 595 | bool send_server_hello_msg = false; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 596 | if (fd == server_listen_pipe_) { |
| 597 | int new_pipe = 0; |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 598 | if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe) || |
| 599 | new_pipe < 0) { |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 600 | Close(); |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 601 | listener()->OnChannelListenError(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | if (pipe_ != -1) { |
| 605 | // We already have a connection. We only handle one at a time. |
| 606 | // close our new descriptor. |
| 607 | if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 608 | DPLOG(ERROR) << "shutdown " << pipe_name_; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 609 | if (HANDLE_EINTR(close(new_pipe)) < 0) |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 610 | DPLOG(ERROR) << "close " << pipe_name_; |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 611 | listener()->OnChannelDenied(); |
dmaclach@chromium.org | f22df39 | 2010-12-20 15:39:44 +0900 | [diff] [blame] | 612 | return; |
dmaclach@chromium.org | 6a9b0c7 | 2010-12-20 15:19:07 +0900 | [diff] [blame] | 613 | } |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 614 | pipe_ = new_pipe; |
| 615 | |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 616 | if ((mode_ & MODE_OPEN_ACCESS_FLAG) == 0) { |
| 617 | // Verify that the IPC channel peer is running as the same user. |
| 618 | uid_t client_euid; |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 619 | if (!GetPeerEuid(&client_euid)) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 620 | DLOG(ERROR) << "Unable to query client euid"; |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 621 | ResetToAcceptingConnectionState(); |
| 622 | return; |
| 623 | } |
| 624 | if (client_euid != geteuid()) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 625 | DLOG(WARNING) << "Client euid is not authorised"; |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 626 | ResetToAcceptingConnectionState(); |
| 627 | return; |
| 628 | } |
| 629 | } |
| 630 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 631 | if (!AcceptConnection()) { |
| 632 | NOTREACHED() << "AcceptConnection should not fail on server"; |
| 633 | } |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 634 | send_server_hello_msg = true; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 635 | waiting_connect_ = false; |
| 636 | } else if (fd == pipe_) { |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 637 | if (waiting_connect_ && (mode_ & MODE_SERVER_FLAG)) { |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 638 | send_server_hello_msg = true; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 639 | waiting_connect_ = false; |
| 640 | } |
| 641 | if (!ProcessIncomingMessages()) { |
agl@chromium.org | 0d5ac66 | 2011-03-01 05:30:47 +0900 | [diff] [blame] | 642 | // ClosePipeOnError may delete this object, so we mustn't call |
| 643 | // ProcessOutgoingMessages. |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 644 | send_server_hello_msg = false; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 645 | ClosePipeOnError(); |
| 646 | } |
| 647 | } else { |
| 648 | NOTREACHED() << "Unknown pipe " << fd; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // If we're a server and handshaking, then we want to make sure that we |
| 652 | // only send our handshake message after we've processed the client's. |
| 653 | // This gives us a chance to kill the client if the incoming handshake |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 654 | // is invalid. |
| 655 | if (send_server_hello_msg) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 656 | ProcessOutgoingMessages(); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // Called by libevent when we can write to the pipe without blocking. |
| 661 | void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
david.mike.futcher@gmail.com | 9eb2aa5 | 2011-04-19 05:07:08 +0900 | [diff] [blame] | 662 | DCHECK_EQ(pipe_, fd); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 663 | is_blocked_on_write_ = false; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 664 | if (!ProcessOutgoingMessages()) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 665 | ClosePipeOnError(); |
dmaclach@chromium.org | 6a9b0c7 | 2010-12-20 15:19:07 +0900 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 669 | bool Channel::ChannelImpl::AcceptConnection() { |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 670 | base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 671 | pipe_, true, base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 672 | QueueHelloMessage(); |
| 673 | |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 674 | if (mode_ & MODE_CLIENT_FLAG) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 675 | // If we are a client we want to send a hello message out immediately. |
| 676 | // In server mode we will send a hello message when we receive one from a |
| 677 | // client. |
| 678 | waiting_connect_ = false; |
| 679 | return ProcessOutgoingMessages(); |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 680 | } else if (mode_ & MODE_SERVER_FLAG) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 681 | waiting_connect_ = true; |
| 682 | return true; |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 683 | } else { |
| 684 | NOTREACHED(); |
| 685 | return false; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | void Channel::ChannelImpl::ClosePipeOnError() { |
| 690 | if (HasAcceptedConnection()) { |
| 691 | ResetToAcceptingConnectionState(); |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 692 | listener()->OnChannelError(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 693 | } else { |
| 694 | Close(); |
| 695 | if (AcceptsConnections()) { |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 696 | listener()->OnChannelListenError(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 697 | } else { |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 698 | listener()->OnChannelError(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 703 | int Channel::ChannelImpl::GetHelloMessageProcId() { |
| 704 | int pid = base::GetCurrentProcId(); |
| 705 | #if defined(OS_LINUX) |
| 706 | // Our process may be in a sandbox with a separate PID namespace. |
| 707 | if (global_pid_) { |
| 708 | pid = global_pid_; |
| 709 | } |
| 710 | #endif |
| 711 | return pid; |
| 712 | } |
| 713 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 714 | void Channel::ChannelImpl::QueueHelloMessage() { |
| 715 | // Create the Hello message |
| 716 | scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
| 717 | HELLO_MESSAGE_TYPE, |
| 718 | IPC::Message::PRIORITY_NORMAL)); |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 719 | if (!msg->WriteInt(GetHelloMessageProcId())) { |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 720 | NOTREACHED() << "Unable to pickle hello message proc id"; |
| 721 | } |
| 722 | #if defined(IPC_USES_READWRITE) |
| 723 | scoped_ptr<Message> hello; |
| 724 | if (remote_fd_pipe_ != -1) { |
| 725 | if (!msg->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, |
| 726 | false))) { |
| 727 | NOTREACHED() << "Unable to pickle hello message file descriptors"; |
| 728 | } |
| 729 | DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
| 730 | } |
| 731 | #endif // IPC_USES_READWRITE |
| 732 | output_queue_.push(msg.release()); |
| 733 | } |
| 734 | |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 735 | Channel::ChannelImpl::ReadState Channel::ChannelImpl::ReadData( |
| 736 | char* buffer, |
| 737 | int buffer_len, |
| 738 | int* bytes_read) { |
| 739 | if (pipe_ == -1) |
| 740 | return READ_FAILED; |
| 741 | |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 742 | struct msghdr msg = {0}; |
| 743 | |
shenhan@google.com | 8a6e499 | 2012-06-05 10:54:46 +0900 | [diff] [blame] | 744 | struct iovec iov = {buffer, static_cast<size_t>(buffer_len)}; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 745 | msg.msg_iov = &iov; |
| 746 | msg.msg_iovlen = 1; |
| 747 | |
| 748 | msg.msg_control = input_cmsg_buf_; |
| 749 | |
| 750 | // recvmsg() returns 0 if the connection has closed or EAGAIN if no data |
| 751 | // is waiting on the pipe. |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 752 | #if defined(IPC_USES_READWRITE) |
| 753 | if (fd_pipe_ >= 0) { |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 754 | *bytes_read = HANDLE_EINTR(read(pipe_, buffer, buffer_len)); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 755 | msg.msg_controllen = 0; |
| 756 | } else |
| 757 | #endif // IPC_USES_READWRITE |
| 758 | { |
| 759 | msg.msg_controllen = sizeof(input_cmsg_buf_); |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 760 | *bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 761 | } |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 762 | if (*bytes_read < 0) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 763 | if (errno == EAGAIN) { |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 764 | return READ_PENDING; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 765 | #if defined(OS_MACOSX) |
| 766 | } else if (errno == EPERM) { |
| 767 | // On OSX, reading from a pipe with no listener returns EPERM |
| 768 | // treat this as a special case to prevent spurious error messages |
| 769 | // to the console. |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 770 | return READ_FAILED; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 771 | #endif // OS_MACOSX |
| 772 | } else if (errno == ECONNRESET || errno == EPIPE) { |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 773 | return READ_FAILED; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 774 | } else { |
| 775 | PLOG(ERROR) << "pipe error (" << pipe_ << ")"; |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 776 | return READ_FAILED; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 777 | } |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 778 | } else if (*bytes_read == 0) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 779 | // The pipe has closed... |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 780 | return READ_FAILED; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 781 | } |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 782 | DCHECK(*bytes_read); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 783 | |
| 784 | CloseClientFileDescriptor(); |
| 785 | |
| 786 | // Read any file descriptors from the message. |
| 787 | if (!ExtractFileDescriptorsFromMsghdr(&msg)) |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 788 | return READ_FAILED; |
| 789 | return READ_SUCCEEDED; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | #if defined(IPC_USES_READWRITE) |
| 793 | bool Channel::ChannelImpl::ReadFileDescriptorsFromFDPipe() { |
| 794 | char dummy; |
| 795 | struct iovec fd_pipe_iov = { &dummy, 1 }; |
| 796 | |
| 797 | struct msghdr msg = { 0 }; |
| 798 | msg.msg_iov = &fd_pipe_iov; |
| 799 | msg.msg_iovlen = 1; |
| 800 | msg.msg_control = input_cmsg_buf_; |
| 801 | msg.msg_controllen = sizeof(input_cmsg_buf_); |
| 802 | ssize_t bytes_received = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
| 803 | |
| 804 | if (bytes_received != 1) |
| 805 | return true; // No message waiting. |
| 806 | |
| 807 | if (!ExtractFileDescriptorsFromMsghdr(&msg)) |
| 808 | return false; |
| 809 | return true; |
| 810 | } |
| 811 | #endif |
| 812 | |
brettw@chromium.org | 0e9d0a1 | 2012-03-08 21:30:28 +0900 | [diff] [blame] | 813 | // On Posix, we need to fix up the file descriptors before the input message |
| 814 | // is dispatched. |
| 815 | // |
| 816 | // This will read from the input_fds_ (READWRITE mode only) and read more |
| 817 | // handles from the FD pipe if necessary. |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 818 | bool Channel::ChannelImpl::WillDispatchInputMessage(Message* msg) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 819 | uint16 header_fds = msg->header()->num_fds; |
| 820 | if (!header_fds) |
| 821 | return true; // Nothing to do. |
| 822 | |
| 823 | // The message has file descriptors. |
| 824 | const char* error = NULL; |
| 825 | if (header_fds > input_fds_.size()) { |
| 826 | // The message has been completely received, but we didn't get |
| 827 | // enough file descriptors. |
| 828 | #if defined(IPC_USES_READWRITE) |
| 829 | if (!ReadFileDescriptorsFromFDPipe()) |
| 830 | return false; |
| 831 | if (header_fds > input_fds_.size()) |
| 832 | #endif // IPC_USES_READWRITE |
| 833 | error = "Message needs unreceived descriptors"; |
| 834 | } |
| 835 | |
| 836 | if (header_fds > FileDescriptorSet::kMaxDescriptorsPerMessage) |
| 837 | error = "Message requires an excessive number of descriptors"; |
| 838 | |
| 839 | if (error) { |
| 840 | LOG(WARNING) << error |
| 841 | << " channel:" << this |
| 842 | << " message-type:" << msg->type() |
| 843 | << " header()->num_fds:" << header_fds; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 844 | // Abort the connection. |
| 845 | ClearInputFDs(); |
| 846 | return false; |
| 847 | } |
| 848 | |
fischman@chromium.org | 8b60dfa | 2012-04-10 06:40:44 +0900 | [diff] [blame] | 849 | // The shenaniganery below with &foo.front() requires input_fds_ to have |
| 850 | // contiguous underlying storage (such as a simple array or a std::vector). |
| 851 | // This is why the header warns not to make input_fds_ a deque<>. |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 852 | msg->file_descriptor_set()->SetDescriptors(&input_fds_.front(), |
| 853 | header_fds); |
| 854 | input_fds_.erase(input_fds_.begin(), input_fds_.begin() + header_fds); |
| 855 | return true; |
| 856 | } |
| 857 | |
brettw@chromium.org | 828e859 | 2012-03-02 06:41:47 +0900 | [diff] [blame] | 858 | bool Channel::ChannelImpl::DidEmptyInputBuffers() { |
| 859 | // When the input data buffer is empty, the fds should be too. If this is |
| 860 | // not the case, we probably have a rogue renderer which is trying to fill |
| 861 | // our descriptor table. |
| 862 | return input_fds_.empty(); |
| 863 | } |
| 864 | |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 865 | bool Channel::ChannelImpl::ExtractFileDescriptorsFromMsghdr(msghdr* msg) { |
| 866 | // Check that there are any control messages. On OSX, CMSG_FIRSTHDR will |
| 867 | // return an invalid non-NULL pointer in the case that controllen == 0. |
| 868 | if (msg->msg_controllen == 0) |
| 869 | return true; |
| 870 | |
| 871 | for (cmsghdr* cmsg = CMSG_FIRSTHDR(msg); |
| 872 | cmsg; |
| 873 | cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 874 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 875 | unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 876 | DCHECK_EQ(0U, payload_len % sizeof(int)); |
| 877 | const int* file_descriptors = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 878 | unsigned num_file_descriptors = payload_len / 4; |
| 879 | input_fds_.insert(input_fds_.end(), |
| 880 | file_descriptors, |
| 881 | file_descriptors + num_file_descriptors); |
| 882 | |
| 883 | // Check this after adding the FDs so we don't leak them. |
| 884 | if (msg->msg_flags & MSG_CTRUNC) { |
| 885 | ClearInputFDs(); |
| 886 | return false; |
| 887 | } |
| 888 | |
| 889 | return true; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // No file descriptors found, but that's OK. |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | void Channel::ChannelImpl::ClearInputFDs() { |
fischman@chromium.org | 8b60dfa | 2012-04-10 06:40:44 +0900 | [diff] [blame] | 898 | for (size_t i = 0; i < input_fds_.size(); ++i) { |
| 899 | if (HANDLE_EINTR(close(input_fds_[i])) < 0) |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 900 | PLOG(ERROR) << "close "; |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 901 | } |
fischman@chromium.org | 8b60dfa | 2012-04-10 06:40:44 +0900 | [diff] [blame] | 902 | input_fds_.clear(); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 903 | } |
| 904 | |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 905 | void Channel::ChannelImpl::HandleHelloMessage(const Message& msg) { |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 906 | // The Hello message contains only the process id. |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 907 | PickleIterator iter(msg); |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 908 | int pid; |
| 909 | if (!msg.ReadInt(&iter, &pid)) |
| 910 | NOTREACHED(); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 911 | |
| 912 | #if defined(IPC_USES_READWRITE) |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 913 | if (mode_ & MODE_SERVER_FLAG) { |
| 914 | // With IPC_USES_READWRITE, the Hello message from the client to the |
| 915 | // server also contains the fd_pipe_, which will be used for all |
| 916 | // subsequent file descriptor passing. |
| 917 | DCHECK_EQ(msg.file_descriptor_set()->size(), 1U); |
| 918 | base::FileDescriptor descriptor; |
| 919 | if (!msg.ReadFileDescriptor(&iter, &descriptor)) { |
| 920 | NOTREACHED(); |
| 921 | } |
| 922 | fd_pipe_ = descriptor.fd; |
| 923 | CHECK(descriptor.auto_close); |
hubbe@chromium.org | 1b503fc | 2013-10-11 06:12:14 +0900 | [diff] [blame] | 924 | } |
benwells@chromium.org | 6ecd712 | 2013-10-11 13:22:34 +0900 | [diff] [blame^] | 925 | #endif // IPC_USES_READWRITE |
| 926 | peer_pid_ = pid; |
| 927 | listener()->OnChannelConnected(pid); |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 928 | } |
| 929 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 930 | void Channel::ChannelImpl::Close() { |
| 931 | // Close can be called multiple time, so we need to make sure we're |
| 932 | // idempotent. |
| 933 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 934 | ResetToAcceptingConnectionState(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 935 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 936 | if (must_unlink_) { |
| 937 | unlink(pipe_name_.c_str()); |
| 938 | must_unlink_ = false; |
| 939 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 940 | if (server_listen_pipe_ != -1) { |
thakis@chromium.org | 965db9a | 2010-06-23 09:37:46 +0900 | [diff] [blame] | 941 | if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) |
brettw@chromium.org | 293988a | 2012-03-01 07:48:14 +0900 | [diff] [blame] | 942 | DPLOG(ERROR) << "close " << server_listen_pipe_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 943 | server_listen_pipe_ = -1; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 944 | // Unregister libevent for the listening socket and close it. |
| 945 | server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 946 | } |
| 947 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 948 | CloseClientFileDescriptor(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | //------------------------------------------------------------------------------ |
| 952 | // Channel's methods simply call through to ChannelImpl. |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 953 | Channel::Channel(const IPC::ChannelHandle& channel_handle, Mode mode, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 954 | Listener* listener) |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 955 | : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | Channel::~Channel() { |
| 959 | delete channel_impl_; |
| 960 | } |
| 961 | |
| 962 | bool Channel::Connect() { |
| 963 | return channel_impl_->Connect(); |
| 964 | } |
| 965 | |
| 966 | void Channel::Close() { |
dmichael@chromium.org | b9a99ce | 2012-05-17 03:58:32 +0900 | [diff] [blame] | 967 | if (channel_impl_) |
| 968 | channel_impl_->Close(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 969 | } |
| 970 | |
jschuh@chromium.org | a5cd076 | 2012-04-05 11:38:34 +0900 | [diff] [blame] | 971 | base::ProcessId Channel::peer_pid() const { |
| 972 | return channel_impl_->peer_pid(); |
| 973 | } |
| 974 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 975 | bool Channel::Send(Message* message) { |
| 976 | return channel_impl_->Send(message); |
| 977 | } |
| 978 | |
| 979 | int Channel::GetClientFileDescriptor() const { |
| 980 | return channel_impl_->GetClientFileDescriptor(); |
| 981 | } |
| 982 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 983 | int Channel::TakeClientFileDescriptor() { |
| 984 | return channel_impl_->TakeClientFileDescriptor(); |
| 985 | } |
| 986 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 987 | bool Channel::AcceptsConnections() const { |
| 988 | return channel_impl_->AcceptsConnections(); |
| 989 | } |
| 990 | |
| 991 | bool Channel::HasAcceptedConnection() const { |
| 992 | return channel_impl_->HasAcceptedConnection(); |
| 993 | } |
| 994 | |
jeremya@chromium.org | f14bfab | 2013-03-13 13:23:10 +0900 | [diff] [blame] | 995 | bool Channel::GetPeerEuid(uid_t* peer_euid) const { |
| 996 | return channel_impl_->GetPeerEuid(peer_euid); |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 997 | } |
| 998 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 999 | void Channel::ResetToAcceptingConnectionState() { |
| 1000 | channel_impl_->ResetToAcceptingConnectionState(); |
| 1001 | } |
| 1002 | |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 1003 | // static |
| 1004 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
| 1005 | return ChannelImpl::IsNamedServerInitialized(channel_id); |
| 1006 | } |
| 1007 | |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 1008 | // static |
| 1009 | std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
| 1010 | // A random name is sufficient validation on posix systems, so we don't need |
| 1011 | // an additional shared secret. |
| 1012 | |
| 1013 | std::string id = prefix; |
| 1014 | if (!id.empty()) |
| 1015 | id.append("."); |
| 1016 | |
| 1017 | return id.append(GenerateUniqueRandomChannelID()); |
| 1018 | } |
| 1019 | |
| 1020 | |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 1021 | #if defined(OS_LINUX) |
| 1022 | // static |
| 1023 | void Channel::SetGlobalPid(int pid) { |
| 1024 | ChannelImpl::SetGlobalPid(pid); |
| 1025 | } |
| 1026 | #endif // OS_LINUX |
| 1027 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1028 | } // namespace IPC |