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_ |
thakis@chromium.org | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 9 | #include <string> |
| 10 | |
shenhan@google.com | 3fcbd4b | 2012-06-05 01:54:46 | [diff] [blame^] | 11 | #if defined(OS_POSIX) |
| 12 | #include <sys/types.h> |
| 13 | #endif |
| 14 | |
evan@chromium.org | 39703fb | 2010-10-19 19:11:15 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
jschuh@chromium.org | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 16 | #include "base/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" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
| 20 | namespace IPC { |
| 21 | |
| 22 | //------------------------------------------------------------------------------ |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 23 | // See |
| 24 | // http://www.chromium.org/developers/design-documents/inter-process-communication |
| 25 | // for overview of IPC in Chromium. |
| 26 | |
| 27 | // Channels are implemented using named pipes on Windows, and |
| 28 | // socket pairs (or in some special cases unix domain sockets) on POSIX. |
| 29 | // On Windows we access pipes in various processes by name. |
| 30 | // On POSIX we pass file descriptors to child processes and assign names to them |
| 31 | // in a lookup table. |
| 32 | // In general on POSIX we do not use unix domain sockets due to security |
| 33 | // concerns and the fact that they can leave garbage around the file system |
| 34 | // (MacOS does not support abstract named unix domain sockets). |
| 35 | // You can use unix domain sockets if you like on POSIX by constructing the |
| 36 | // the channel with the mode set to one of the NAMED modes. NAMED modes are |
| 37 | // currently used by automation and service processes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
darin@chromium.org | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 39 | class IPC_EXPORT Channel : public Message::Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | // Security tests need access to the pipe handle. |
| 41 | friend class ChannelTest; |
| 42 | |
| 43 | public: |
| 44 | // Implemented by consumers of a Channel to receive messages. |
darin@chromium.org | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 45 | class IPC_EXPORT Listener { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | public: |
darin@google.com | c8ea1ef | 2008-09-06 18:07:10 | [diff] [blame] | 47 | virtual ~Listener() {} |
| 48 | |
jam@chromium.org | a95986a | 2010-12-24 06:19:28 | [diff] [blame] | 49 | // Called when a message is received. Returns true iff the message was |
| 50 | // handled. |
| 51 | virtual bool OnMessageReceived(const Message& message) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
| 53 | // Called when the channel is connected and we have received the internal |
| 54 | // Hello message from the peer. |
| 55 | virtual void OnChannelConnected(int32 peer_pid) {} |
| 56 | |
| 57 | // Called when an error is detected that causes the channel to close. |
| 58 | // This method is not called when a channel is closed normally. |
| 59 | virtual void OnChannelError() {} |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 60 | |
| 61 | #if defined(OS_POSIX) |
| 62 | // Called on the server side when a channel that listens for connections |
| 63 | // denies an attempt to connect. |
| 64 | virtual void OnChannelDenied() {} |
| 65 | |
| 66 | // Called on the server side when a channel that listens for connections |
| 67 | // has an error that causes the listening channel to close. |
| 68 | virtual void OnChannelListenError() {} |
| 69 | #endif // OS_POSIX |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | }; |
| 71 | |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 72 | // Flags to test modes |
| 73 | enum ModeFlags { |
| 74 | MODE_NO_FLAG = 0x0, |
| 75 | MODE_SERVER_FLAG = 0x1, |
| 76 | MODE_CLIENT_FLAG = 0x2, |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 77 | MODE_NAMED_FLAG = 0x4, |
| 78 | #if defined(OS_POSIX) |
| 79 | MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID. |
| 80 | #endif |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Some Standard Modes |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | enum Mode { |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 85 | MODE_NONE = MODE_NO_FLAG, |
| 86 | MODE_SERVER = MODE_SERVER_FLAG, |
| 87 | MODE_CLIENT = MODE_CLIENT_FLAG, |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 88 | // Channels on Windows are named by default and accessible from other |
| 89 | // processes. On POSIX channels are anonymous by default and not accessible |
| 90 | // from other processes. Named channels work via named unix domain sockets. |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 91 | // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and |
| 92 | // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. |
| 93 | MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG, |
| 94 | MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG, |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 95 | #if defined(OS_POSIX) |
| 96 | // An "open" named server accepts connections from ANY client. |
| 97 | // The caller must then implement their own access-control based on the |
| 98 | // client process' user Id. |
| 99 | MODE_OPEN_NAMED_SERVER = MODE_OPEN_ACCESS_FLAG | MODE_SERVER_FLAG | |
| 100 | MODE_NAMED_FLAG |
| 101 | #endif |
brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // The Hello message is internal to the Channel class. It is sent |
| 105 | // by the peer when the channel is connected. The message contains |
| 106 | // just the process id (pid). The message has a special routing_id |
| 107 | // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE). |
| 108 | enum { |
| 109 | HELLO_MESSAGE_TYPE = kuint16max // Maximum value of message type (uint16), |
| 110 | // to avoid conflicting with normal |
| 111 | // message types, which are enumeration |
| 112 | // constants starting from 0. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | }; |
| 114 | |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 115 | // The maximum message size in bytes. Attempting to receive a message of this |
| 116 | // size or bigger results in a channel error. |
| 117 | static const size_t kMaximumMessageSize = 128 * 1024 * 1024; |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 118 | |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 119 | // Ammount of data to read at once from the pipe. |
| 120 | static const size_t kReadBufferSize = 4 * 1024; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
| 122 | // Initialize a Channel. |
| 123 | // |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 124 | // |channel_handle| identifies the communication Channel. For POSIX, if |
| 125 | // the file descriptor in the channel handle is != -1, the channel takes |
| 126 | // ownership of the file descriptor and will close it appropriately, otherwise |
| 127 | // it will create a new descriptor internally. |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 128 | // |mode| specifies whether this Channel is to operate in server mode or |
| 129 | // client mode. In server mode, the Channel is responsible for setting up the |
| 130 | // IPC object, whereas in client mode, the Channel merely connects to the |
| 131 | // already established IPC object. |
| 132 | // |listener| receives a callback on the current thread for each newly |
| 133 | // received message. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | // |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 135 | Channel(const IPC::ChannelHandle &channel_handle, Mode mode, |
| 136 | Listener* listener); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
hans@chromium.org | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 138 | virtual ~Channel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | |
| 140 | // Connect the pipe. On the server side, this will initiate |
| 141 | // waiting for connections. On the client, it attempts to |
| 142 | // connect to a pre-existing pipe. Note, calling Connect() |
| 143 | // will not block the calling thread and may complete |
| 144 | // asynchronously. |
evan@chromium.org | 39703fb | 2010-10-19 19:11:15 | [diff] [blame] | 145 | bool Connect() WARN_UNUSED_RESULT; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
| 147 | // Close this Channel explicitly. May be called multiple times. |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 148 | // On POSIX calling close on an IPC channel that listens for connections will |
| 149 | // cause it to close any accepted connections, and it will stop listening for |
| 150 | // new connections. If you just want to close the currently accepted |
| 151 | // connection and listen for new ones, use ResetToAcceptingConnectionState. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | void Close(); |
| 153 | |
| 154 | // Modify the Channel's listener. |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 155 | void set_listener(Listener* listener); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
jschuh@chromium.org | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 157 | // Get the process ID for the connected peer. |
| 158 | // Returns base::kNullProcessId if the peer is not connected yet. |
| 159 | base::ProcessId peer_pid() const; |
| 160 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | // Send a message over the Channel to the listener on the other end. |
| 162 | // |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 163 | // |message| must be allocated using operator new. This object will be |
| 164 | // deleted once the contents of the Message have been sent. |
avi@chromium.org | 2a026e5 | 2011-11-17 16:09:44 | [diff] [blame] | 165 | virtual bool Send(Message* message) OVERRIDE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | |
bbudge@chromium.org | fe5d406 | 2012-04-23 21:18:19 | [diff] [blame] | 167 | #if defined(OS_POSIX) |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 168 | // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
agl@chromium.org | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 169 | // FD # for the client end of the socket. |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 170 | // 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] | 171 | // This method can be called on any thread. |
agl@chromium.org | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 172 | int GetClientFileDescriptor() const; |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 173 | |
phajdan.jr@chromium.org | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 174 | // Same as GetClientFileDescriptor, but transfers the ownership of the |
| 175 | // file descriptor to the caller. |
| 176 | // This method can be called on any thread. |
| 177 | int TakeClientFileDescriptor(); |
| 178 | |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 179 | // On POSIX an IPC::Channel can either wrap an established socket, or it |
| 180 | // can wrap a socket that is listening for connections. Currently an |
| 181 | // IPC::Channel that listens for connections can only accept one connection |
| 182 | // at a time. |
| 183 | |
| 184 | // Returns true if the channel supports listening for connections. |
| 185 | bool AcceptsConnections() const; |
| 186 | |
| 187 | // Returns true if the channel supports listening for connections and is |
| 188 | // currently connected. |
| 189 | bool HasAcceptedConnection() const; |
| 190 | |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 191 | // Returns true if the peer process' effective user id can be determined, in |
| 192 | // which case the supplied client_euid is updated with it. |
| 193 | bool GetClientEuid(uid_t* client_euid) const; |
| 194 | |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 195 | // Closes any currently connected socket, and returns to a listening state |
| 196 | // for more connections. |
| 197 | void ResetToAcceptingConnectionState(); |
wez@chromium.org | 8ec3fbe | 2011-04-06 12:01:44 | [diff] [blame] | 198 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
neb@chromium.org | 74f2754 | 2010-03-31 17:44:57 | [diff] [blame] | 199 | |
kkania@chromium.org | 313c00e5 | 2011-08-09 06:46:06 | [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 | |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 204 | // Generates a channel ID that's non-predictable and unique. |
| 205 | static std::string GenerateUniqueRandomChannelID(); |
| 206 | |
| 207 | // Generates a channel ID that, if passed to the client as a shared secret, |
| 208 | // will validate that the client's authenticity. On platforms that do not |
| 209 | // require additional this is simply calls GenerateUniqueRandomChannelID(). |
| 210 | // For portability the prefix should not include the \ character. |
| 211 | static std::string GenerateVerifiedChannelID(const std::string& prefix); |
| 212 | |
jamescook@chromium.org | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 213 | #if defined(OS_LINUX) |
| 214 | // Sandboxed processes live in a PID namespace, so when sending the IPC hello |
| 215 | // message from client to server we need to send the PID from the global |
| 216 | // PID namespace. |
| 217 | static void SetGlobalPid(int pid); |
| 218 | #endif |
| 219 | |
neb@chromium.org | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 220 | protected: |
| 221 | // Used in Chrome by the TestSink to provide a dummy channel implementation |
| 222 | // for testing. TestSink overrides the "interesting" functions in Channel so |
| 223 | // no actual implementation is needed. This will cause un-overridden calls to |
| 224 | // segfault. Do not use outside of test code! |
| 225 | Channel() : channel_impl_(0) { } |
| 226 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | private: |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 228 | // PIMPL to which all channel calls are delegated. |
| 229 | class ChannelImpl; |
| 230 | ChannelImpl *channel_impl_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | }; |
| 232 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 233 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 235 | #endif // IPC_IPC_CHANNEL_H_ |