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