brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_CHANNEL_H_ |
| 6 | #define IPC_IPC_CHANNEL_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 8 | #include <string> |
| 9 | |
shenhan@google.com | 3fcbd4b | 2012-06-05 01:54:46 | [diff] [blame] | 10 | #if defined(OS_POSIX) |
| 11 | #include <sys/types.h> |
| 12 | #endif |
| 13 | |
evan@chromium.org | 39703fb | 2010-10-19 19:11:15 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 15 | #include "base/files/scoped_file.h" |
rsesek@chromium.org | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 16 | #include "base/process/process.h" |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 17 | #include "ipc/ipc_channel_handle.h" |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 18 | #include "ipc/ipc_message.h" |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 19 | #include "ipc/ipc_sender.h" |
| 20 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | namespace IPC { |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 22 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 23 | class AttachmentBroker; |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 24 | class Listener; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | |
| 26 | //------------------------------------------------------------------------------ |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 27 | // See |
| 28 | // http://www.chromium.org/developers/design-documents/inter-process-communication |
| 29 | // for overview of IPC in Chromium. |
| 30 | |
| 31 | // Channels are implemented using named pipes on Windows, and |
| 32 | // socket pairs (or in some special cases unix domain sockets) on POSIX. |
| 33 | // On Windows we access pipes in various processes by name. |
| 34 | // On POSIX we pass file descriptors to child processes and assign names to them |
| 35 | // in a lookup table. |
| 36 | // In general on POSIX we do not use unix domain sockets due to security |
| 37 | // concerns and the fact that they can leave garbage around the file system |
| 38 | // (MacOS does not support abstract named unix domain sockets). |
| 39 | // You can use unix domain sockets if you like on POSIX by constructing the |
| 40 | // the channel with the mode set to one of the NAMED modes. NAMED modes are |
| 41 | // currently used by automation and service processes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 43 | class IPC_EXPORT Channel : public Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | // Security tests need access to the pipe handle. |
| 45 | friend class ChannelTest; |
| 46 | |
| 47 | public: |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 48 | // Flags to test modes |
| 49 | enum ModeFlags { |
| 50 | MODE_NO_FLAG = 0x0, |
| 51 | MODE_SERVER_FLAG = 0x1, |
| 52 | MODE_CLIENT_FLAG = 0x2, |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 53 | MODE_NAMED_FLAG = 0x4, |
| 54 | #if defined(OS_POSIX) |
| 55 | MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID. |
| 56 | #endif |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Some Standard Modes |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 60 | // TODO(morrita): These are under deprecation work. You should use Create*() |
| 61 | // functions instead. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | enum Mode { |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 63 | MODE_NONE = MODE_NO_FLAG, |
| 64 | MODE_SERVER = MODE_SERVER_FLAG, |
| 65 | MODE_CLIENT = MODE_CLIENT_FLAG, |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 66 | MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG, |
| 67 | MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG, |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 68 | #if defined(OS_POSIX) |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 69 | MODE_OPEN_NAMED_SERVER = MODE_OPEN_ACCESS_FLAG | MODE_SERVER_FLAG | |
| 70 | MODE_NAMED_FLAG |
| 71 | #endif |
brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 72 | }; |
| 73 | |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 74 | // Messages internal to the IPC implementation are defined here. |
| 75 | // Uses Maximum value of message type (uint16), to avoid conflicting |
| 76 | // with normal message types, which are enumeration constants starting from 0. |
brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 77 | enum { |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 78 | // The Hello message is sent by the peer when the channel is connected. |
| 79 | // The message contains just the process id (pid). |
| 80 | // The message has a special routing_id (MSG_ROUTING_NONE) |
| 81 | // and type (HELLO_MESSAGE_TYPE). |
| 82 | HELLO_MESSAGE_TYPE = kuint16max, |
| 83 | // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to |
| 84 | // work around a bug in sendmsg() on Mac. When an FD is sent |
| 85 | // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2. |
| 86 | // The client will return the message with hops = 1, *after* it |
| 87 | // has received the message that contains the FD. When we |
| 88 | // receive it again on the sender side, we close the FD. |
| 89 | CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | }; |
| 91 | |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 92 | // The maximum message size in bytes. Attempting to receive a message of this |
| 93 | // size or bigger results in a channel error. |
| 94 | static const size_t kMaximumMessageSize = 128 * 1024 * 1024; |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 95 | |
viettrungluu@chromium.org | 4c4c0dc | 2013-01-05 02:13:04 | [diff] [blame] | 96 | // Amount of data to read at once from the pipe. |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 97 | static const size_t kReadBufferSize = 4 * 1024; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
| 99 | // Initialize a Channel. |
| 100 | // |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 101 | // |channel_handle| identifies the communication Channel. For POSIX, if |
| 102 | // the file descriptor in the channel handle is != -1, the channel takes |
| 103 | // ownership of the file descriptor and will close it appropriately, otherwise |
| 104 | // it will create a new descriptor internally. |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 105 | // |listener| receives a callback on the current thread for each newly |
| 106 | // received message. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | // |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 108 | // There are four type of modes how channels operate: |
| 109 | // |
| 110 | // - Server and named server: In these modes, the Channel is |
| 111 | // responsible for settingb up the IPC object |
| 112 | // - An "open" named server: It accepts connections from ANY client. |
| 113 | // The caller must then implement their own access-control based on the |
| 114 | // client process' user Id. |
| 115 | // - Client and named client: In these mode, the Channel merely |
| 116 | // connects to the already established IPC object. |
| 117 | // |
| 118 | // Each mode has its own Create*() API to create the Channel object. |
| 119 | // |
| 120 | // TODO(morrita): Replace CreateByModeForProxy() with one of above Create*(). |
| 121 | // |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 122 | // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 123 | // make the upcoming refactor decomposable into smaller CLs. |
| 124 | // http://crbug.com/493414. |
| 125 | static scoped_ptr<Channel> Create(const IPC::ChannelHandle& channel_handle, |
| 126 | Mode mode, |
| 127 | Listener* listener, |
| 128 | AttachmentBroker* broker = nullptr); |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 129 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 130 | // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 131 | // make the upcoming refactor decomposable into smaller CLs. |
| 132 | // http://crbug.com/493414. |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 133 | static scoped_ptr<Channel> CreateClient( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 134 | const IPC::ChannelHandle& channel_handle, |
| 135 | Listener* listener, |
| 136 | AttachmentBroker* broker = nullptr); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 137 | |
| 138 | // Channels on Windows are named by default and accessible from other |
| 139 | // processes. On POSIX channels are anonymous by default and not accessible |
| 140 | // from other processes. Named channels work via named unix domain sockets. |
| 141 | // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and |
| 142 | // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. |
| 143 | static scoped_ptr<Channel> CreateNamedServer( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 144 | const IPC::ChannelHandle& channel_handle, |
| 145 | Listener* listener, |
| 146 | AttachmentBroker* broker); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 147 | static scoped_ptr<Channel> CreateNamedClient( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 148 | const IPC::ChannelHandle& channel_handle, |
| 149 | Listener* listener, |
| 150 | AttachmentBroker* broker); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 151 | #if defined(OS_POSIX) |
| 152 | // An "open" named server accepts connections from ANY client. |
| 153 | // The caller must then implement their own access-control based on the |
| 154 | // client process' user Id. |
| 155 | static scoped_ptr<Channel> CreateOpenNamedServer( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 156 | const IPC::ChannelHandle& channel_handle, |
| 157 | Listener* listener, |
| 158 | AttachmentBroker* broker); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 159 | #endif |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 160 | // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 161 | // make the upcoming refactor decomposable into smaller CLs. |
| 162 | // http://crbug.com/493414. |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 163 | static scoped_ptr<Channel> CreateServer( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 164 | const IPC::ChannelHandle& channel_handle, |
| 165 | Listener* listener, |
| 166 | AttachmentBroker* broker = nullptr); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 167 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 168 | ~Channel() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | |
| 170 | // Connect the pipe. On the server side, this will initiate |
| 171 | // waiting for connections. On the client, it attempts to |
| 172 | // connect to a pre-existing pipe. Note, calling Connect() |
| 173 | // will not block the calling thread and may complete |
| 174 | // asynchronously. |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 175 | virtual bool Connect() WARN_UNUSED_RESULT = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | |
| 177 | // Close this Channel explicitly. May be called multiple times. |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 178 | // On POSIX calling close on an IPC channel that listens for connections will |
| 179 | // cause it to close any accepted connections, and it will stop listening for |
| 180 | // new connections. If you just want to close the currently accepted |
| 181 | // connection and listen for new ones, use ResetToAcceptingConnectionState. |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 182 | virtual void Close() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | |
jschuh@chromium.org | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 184 | // Get the process ID for the connected peer. |
brettw@chromium.org | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 185 | // |
| 186 | // Returns base::kNullProcessId if the peer is not connected yet. Watch out |
| 187 | // for race conditions. You can easily get a channel to another process, but |
| 188 | // if your process has not yet processed the "hello" message from the remote |
| 189 | // side, this will fail. You should either make sure calling this is either |
| 190 | // in response to a message from the remote side (which guarantees that it's |
| 191 | // been connected), or you wait for the "connected" notification on the |
| 192 | // listener. |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 193 | virtual base::ProcessId GetPeerPID() const = 0; |
jschuh@chromium.org | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 194 | |
morrita@chromium.org | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 195 | // Get its own process id. This value is told to the peer. |
| 196 | virtual base::ProcessId GetSelfPID() const = 0; |
| 197 | |
thakis | 6d1bd47 | 2014-10-29 00:30:41 | [diff] [blame] | 198 | // Overridden from ipc::Sender. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | // Send a message over the Channel to the listener on the other end. |
| 200 | // |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 201 | // |message| must be allocated using operator new. This object will be |
| 202 | // deleted once the contents of the Message have been sent. |
dcheng | 1c3d9ac | 2014-12-23 19:59:59 | [diff] [blame] | 203 | bool Send(Message* message) override = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 205 | // IsSendThreadSafe returns true iff it's safe to call |Send| from non-IO |
| 206 | // threads. This is constant for the lifetime of the |Channel|. |
| 207 | virtual bool IsSendThreadSafe() const; |
| 208 | |
hidehiko | c2eec0d | 2014-10-24 03:49:27 | [diff] [blame] | 209 | // NaCl in Non-SFI mode runs on Linux directly, and the following functions |
| 210 | // compiled on Linux are also needed. Please see also comments in |
| 211 | // components/nacl_nonsfi.gyp for more details. |
hidehiko | f7295f2 | 2014-10-28 11:57:21 | [diff] [blame] | 212 | #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 213 | // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
agl@chromium.org | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 214 | // FD # for the client end of the socket. |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 215 | // This method may only be called on the server side of a channel. |
phajdan.jr@chromium.org | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 216 | // This method can be called on any thread. |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 217 | virtual int GetClientFileDescriptor() const = 0; |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 218 | |
phajdan.jr@chromium.org | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 219 | // Same as GetClientFileDescriptor, but transfers the ownership of the |
| 220 | // file descriptor to the caller. |
| 221 | // This method can be called on any thread. |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 222 | virtual base::ScopedFD TakeClientFileDescriptor() = 0; |
hidehiko | c2eec0d | 2014-10-24 03:49:27 | [diff] [blame] | 223 | #endif |
neb@chromium.org | 74f2754 | 2010-03-31 17:44:57 | [diff] [blame] | 224 | |
kkania@chromium.org | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 225 | // Returns true if a named server channel is initialized on the given channel |
| 226 | // ID. Even if true, the server may have already accepted a connection. |
| 227 | static bool IsNamedServerInitialized(const std::string& channel_id); |
| 228 | |
hidehiko | f7295f2 | 2014-10-28 11:57:21 | [diff] [blame] | 229 | #if !defined(OS_NACL_SFI) |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 230 | // Generates a channel ID that's non-predictable and unique. |
| 231 | static std::string GenerateUniqueRandomChannelID(); |
| 232 | |
| 233 | // Generates a channel ID that, if passed to the client as a shared secret, |
| 234 | // will validate that the client's authenticity. On platforms that do not |
| 235 | // require additional this is simply calls GenerateUniqueRandomChannelID(). |
| 236 | // For portability the prefix should not include the \ character. |
| 237 | static std::string GenerateVerifiedChannelID(const std::string& prefix); |
bbudge@chromium.org | bccbaf2 | 2012-09-28 21:46:01 | [diff] [blame] | 238 | #endif |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 239 | |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 240 | #if defined(OS_LINUX) |
| 241 | // Sandboxed processes live in a PID namespace, so when sending the IPC hello |
| 242 | // message from client to server we need to send the PID from the global |
| 243 | // PID namespace. |
| 244 | static void SetGlobalPid(int pid); |
| 245 | #endif |
| 246 | |
epenner@chromium.org | e097b93 | 2014-03-19 06:34:52 | [diff] [blame] | 247 | #if defined(OS_ANDROID) |
| 248 | // Most tests are single process and work the same on all platforms. However |
| 249 | // in some cases we want to test multi-process, and Android differs in that it |
| 250 | // can't 'exec' after forking. This callback resets any data in the forked |
| 251 | // process such that it acts similar to if it was exec'd, for tests. |
| 252 | static void NotifyProcessForkedForTesting(); |
| 253 | #endif |
| 254 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | }; |
| 256 | |
hidehiko@chromium.org | 2648d5f9 | 2014-02-21 15:05:25 | [diff] [blame] | 257 | #if defined(OS_POSIX) |
| 258 | // SocketPair() creates a pair of socket FDs suitable for using with |
| 259 | // IPC::Channel. |
| 260 | IPC_EXPORT bool SocketPair(int* fd1, int* fd2); |
| 261 | #endif |
| 262 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 263 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 265 | #endif // IPC_IPC_CHANNEL_H_ |