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