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