[go: nahoru, domu]

blob: 14c375bbdee6c2e636d48362569031af37fbe3af [file] [log] [blame]
brettw@chromium.orgd805c6a2012-03-08 12:30:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
agl@chromium.org946d1b22009-07-22 23:57:215#ifndef IPC_IPC_CHANNEL_H_
6#define IPC_IPC_CHANNEL_H_
thakis@chromium.org32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
jschuh@chromium.org5c41e6e12012-03-17 02:20:469#include <string>
10
shenhan@google.com3fcbd4b2012-06-05 01:54:4611#if defined(OS_POSIX)
12#include <sys/types.h>
13#endif
14
evan@chromium.org39703fb2010-10-19 19:11:1515#include "base/compiler_specific.h"
jschuh@chromium.org0a6fc4b2012-04-05 02:38:3416#include "base/process.h"
dmaclach@chromium.org42ce94e2010-12-08 19:28:0917#include "ipc/ipc_channel_handle.h"
agl@chromium.org946d1b22009-07-22 23:57:2118#include "ipc/ipc_message.h"
initial.commit09911bf2008-07-26 23:55:2919
20namespace IPC {
21
22//------------------------------------------------------------------------------
dmaclach@chromium.org22b42c52010-12-20 06:59:2323// 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.commit09911bf2008-07-26 23:55:2938
darin@chromium.org7c854372011-08-15 20:41:4639class IPC_EXPORT Channel : public Message::Sender {
initial.commit09911bf2008-07-26 23:55:2940 // 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.org7c854372011-08-15 20:41:4645 class IPC_EXPORT Listener {
initial.commit09911bf2008-07-26 23:55:2946 public:
darin@google.comc8ea1ef2008-09-06 18:07:1047 virtual ~Listener() {}
48
jam@chromium.orga95986a2010-12-24 06:19:2849 // Called when a message is received. Returns true iff the message was
50 // handled.
51 virtual bool OnMessageReceived(const Message& message) = 0;
initial.commit09911bf2008-07-26 23:55:2952
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.org22b42c52010-12-20 06:59:2360
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.commit09911bf2008-07-26 23:55:2970 };
71
dmaclach@chromium.org1707726c2011-02-03 20:35:0972 // Flags to test modes
73 enum ModeFlags {
74 MODE_NO_FLAG = 0x0,
75 MODE_SERVER_FLAG = 0x1,
76 MODE_CLIENT_FLAG = 0x2,
wez@chromium.org8ec3fbe2011-04-06 12:01:4477 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.org1707726c2011-02-03 20:35:0981 };
82
83 // Some Standard Modes
initial.commit09911bf2008-07-26 23:55:2984 enum Mode {
dmaclach@chromium.org1707726c2011-02-03 20:35:0985 MODE_NONE = MODE_NO_FLAG,
86 MODE_SERVER = MODE_SERVER_FLAG,
87 MODE_CLIENT = MODE_CLIENT_FLAG,
dmaclach@chromium.org22b42c52010-12-20 06:59:2388 // 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.org1707726c2011-02-03 20:35:0991 // 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.org8ec3fbe2011-04-06 12:01:4495#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.orgd805c6a2012-03-08 12:30:28102 };
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.commit09911bf2008-07-26 23:55:29113 };
114
pkasting@chromium.org05094a32011-09-01 00:50:13115 // 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.org514411fc2008-12-10 22:28:11118
pkasting@chromium.org05094a32011-09-01 00:50:13119 // Ammount of data to read at once from the pipe.
120 static const size_t kReadBufferSize = 4 * 1024;
initial.commit09911bf2008-07-26 23:55:29121
122 // Initialize a Channel.
123 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09124 // |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.comc1afbd2c2008-10-13 19:19:36128 // |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.commit09911bf2008-07-26 23:55:29134 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09135 Channel(const IPC::ChannelHandle &channel_handle, Mode mode,
136 Listener* listener);
initial.commit09911bf2008-07-26 23:55:29137
hans@chromium.org3690ebe02011-05-25 09:08:19138 virtual ~Channel();
initial.commit09911bf2008-07-26 23:55:29139
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.org39703fb2010-10-19 19:11:15145 bool Connect() WARN_UNUSED_RESULT;
initial.commit09911bf2008-07-26 23:55:29146
147 // Close this Channel explicitly. May be called multiple times.
dmaclach@chromium.org22b42c52010-12-20 06:59:23148 // 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.commit09911bf2008-07-26 23:55:29152 void Close();
153
154 // Modify the Channel's listener.
jeremy@chromium.org514411fc2008-12-10 22:28:11155 void set_listener(Listener* listener);
initial.commit09911bf2008-07-26 23:55:29156
jschuh@chromium.org0a6fc4b2012-04-05 02:38:34157 // 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.commit09911bf2008-07-26 23:55:29161 // Send a message over the Channel to the listener on the other end.
162 //
rvargas@google.comc1afbd2c2008-10-13 19:19:36163 // |message| must be allocated using operator new. This object will be
164 // deleted once the contents of the Message have been sent.
avi@chromium.org2a026e52011-11-17 16:09:44165 virtual bool Send(Message* message) OVERRIDE;
initial.commit09911bf2008-07-26 23:55:29166
bbudge@chromium.orgfe5d4062012-04-23 21:18:19167#if defined(OS_POSIX)
jeremy@chromium.orgdf3c1ca12008-12-19 21:37:01168 // On POSIX an IPC::Channel wraps a socketpair(), this method returns the
agl@chromium.orgcc8f1462009-06-12 17:36:55169 // FD # for the client end of the socket.
jeremy@chromium.orgdf3c1ca12008-12-19 21:37:01170 // This method may only be called on the server side of a channel.
phajdan.jr@chromium.org2ce26c432011-09-19 17:08:12171 // This method can be called on any thread.
agl@chromium.orgcc8f1462009-06-12 17:36:55172 int GetClientFileDescriptor() const;
dmaclach@chromium.org22b42c52010-12-20 06:59:23173
phajdan.jr@chromium.org2ce26c432011-09-19 17:08:12174 // 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.org22b42c52010-12-20 06:59:23179 // 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.org8ec3fbe2011-04-06 12:01:44191 // 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.org22b42c52010-12-20 06:59:23195 // Closes any currently connected socket, and returns to a listening state
196 // for more connections.
197 void ResetToAcceptingConnectionState();
wez@chromium.org8ec3fbe2011-04-06 12:01:44198#endif // defined(OS_POSIX) && !defined(OS_NACL)
neb@chromium.org74f27542010-03-31 17:44:57199
kkania@chromium.org313c00e52011-08-09 06:46:06200 // 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.org5c41e6e12012-03-17 02:20:46204 // 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.orge1d67a882011-08-31 21:11:04213#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.org90b721e62010-04-05 17:35:01220 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.commit09911bf2008-07-26 23:55:29227 private:
jeremy@chromium.org514411fc2008-12-10 22:28:11228 // PIMPL to which all channel calls are delegated.
229 class ChannelImpl;
230 ChannelImpl *channel_impl_;
initial.commit09911bf2008-07-26 23:55:29231};
232
jeremy@chromium.org514411fc2008-12-10 22:28:11233} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29234
agl@chromium.org946d1b22009-07-22 23:57:21235#endif // IPC_IPC_CHANNEL_H_