[go: nahoru, domu]

blob: a7982e5554f1f719270831680748c8d5ef2f0d87 [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_
initial.commit09911bf2008-07-26 23:55:297
jschuh@chromium.org5c41e6e12012-03-17 02:20:468#include <string>
9
shenhan@google.com3fcbd4b2012-06-05 01:54:4610#if defined(OS_POSIX)
11#include <sys/types.h>
12#endif
13
evan@chromium.org39703fb2010-10-19 19:11:1514#include "base/compiler_specific.h"
morritaa409ccc2014-10-20 23:53:2515#include "base/files/scoped_file.h"
rsesek@chromium.orge66ef602013-07-24 05:15:2416#include "base/process/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"
brettw@chromium.org57319ce2012-06-11 22:35:2619#include "ipc/ipc_sender.h"
20
initial.commit09911bf2008-07-26 23:55:2921namespace IPC {
brettw@chromium.org57319ce2012-06-11 22:35:2622
23class Listener;
initial.commit09911bf2008-07-26 23:55:2924
25//------------------------------------------------------------------------------
dmaclach@chromium.org22b42c52010-12-20 06:59:2326// See
27// http://www.chromium.org/developers/design-documents/inter-process-communication
28// for overview of IPC in Chromium.
29
30// Channels are implemented using named pipes on Windows, and
31// socket pairs (or in some special cases unix domain sockets) on POSIX.
32// On Windows we access pipes in various processes by name.
33// On POSIX we pass file descriptors to child processes and assign names to them
34// in a lookup table.
35// In general on POSIX we do not use unix domain sockets due to security
36// concerns and the fact that they can leave garbage around the file system
37// (MacOS does not support abstract named unix domain sockets).
38// You can use unix domain sockets if you like on POSIX by constructing the
39// the channel with the mode set to one of the NAMED modes. NAMED modes are
40// currently used by automation and service processes.
initial.commit09911bf2008-07-26 23:55:2941
brettw@chromium.org57319ce2012-06-11 22:35:2642class IPC_EXPORT Channel : public Sender {
initial.commit09911bf2008-07-26 23:55:2943 // Security tests need access to the pipe handle.
44 friend class ChannelTest;
45
46 public:
dmaclach@chromium.org1707726c2011-02-03 20:35:0947 // Flags to test modes
48 enum ModeFlags {
49 MODE_NO_FLAG = 0x0,
50 MODE_SERVER_FLAG = 0x1,
51 MODE_CLIENT_FLAG = 0x2,
wez@chromium.org8ec3fbe2011-04-06 12:01:4452 MODE_NAMED_FLAG = 0x4,
53#if defined(OS_POSIX)
54 MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID.
55#endif
dmaclach@chromium.org1707726c2011-02-03 20:35:0956 };
57
58 // Some Standard Modes
morrita@chromium.orge482111a82014-05-30 03:58:5959 // TODO(morrita): These are under deprecation work. You should use Create*()
60 // functions instead.
initial.commit09911bf2008-07-26 23:55:2961 enum Mode {
dmaclach@chromium.org1707726c2011-02-03 20:35:0962 MODE_NONE = MODE_NO_FLAG,
63 MODE_SERVER = MODE_SERVER_FLAG,
64 MODE_CLIENT = MODE_CLIENT_FLAG,
dmaclach@chromium.org1707726c2011-02-03 20:35:0965 MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG,
66 MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG,
wez@chromium.org8ec3fbe2011-04-06 12:01:4467#if defined(OS_POSIX)
wez@chromium.org8ec3fbe2011-04-06 12:01:4468 MODE_OPEN_NAMED_SERVER = MODE_OPEN_ACCESS_FLAG | MODE_SERVER_FLAG |
69 MODE_NAMED_FLAG
70#endif
brettw@chromium.orgd805c6a2012-03-08 12:30:2871 };
72
hubbe@chromium.orgdc875dc2013-10-15 00:07:0073 // Messages internal to the IPC implementation are defined here.
74 // Uses Maximum value of message type (uint16), to avoid conflicting
75 // with normal message types, which are enumeration constants starting from 0.
brettw@chromium.orgd805c6a2012-03-08 12:30:2876 enum {
hubbe@chromium.orgdc875dc2013-10-15 00:07:0077 // The Hello message is sent by the peer when the channel is connected.
78 // The message contains just the process id (pid).
79 // The message has a special routing_id (MSG_ROUTING_NONE)
80 // and type (HELLO_MESSAGE_TYPE).
81 HELLO_MESSAGE_TYPE = kuint16max,
82 // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to
83 // work around a bug in sendmsg() on Mac. When an FD is sent
84 // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2.
85 // The client will return the message with hops = 1, *after* it
86 // has received the message that contains the FD. When we
87 // receive it again on the sender side, we close the FD.
88 CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1
initial.commit09911bf2008-07-26 23:55:2989 };
90
pkasting@chromium.org05094a32011-09-01 00:50:1391 // The maximum message size in bytes. Attempting to receive a message of this
92 // size or bigger results in a channel error.
93 static const size_t kMaximumMessageSize = 128 * 1024 * 1024;
jeremy@chromium.org514411fc2008-12-10 22:28:1194
viettrungluu@chromium.org4c4c0dc2013-01-05 02:13:0495 // Amount of data to read at once from the pipe.
pkasting@chromium.org05094a32011-09-01 00:50:1396 static const size_t kReadBufferSize = 4 * 1024;
initial.commit09911bf2008-07-26 23:55:2997
98 // Initialize a Channel.
99 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09100 // |channel_handle| identifies the communication Channel. For POSIX, if
101 // the file descriptor in the channel handle is != -1, the channel takes
102 // ownership of the file descriptor and will close it appropriately, otherwise
103 // it will create a new descriptor internally.
rvargas@google.comc1afbd2c2008-10-13 19:19:36104 // |listener| receives a callback on the current thread for each newly
105 // received message.
initial.commit09911bf2008-07-26 23:55:29106 //
morrita@chromium.orge482111a82014-05-30 03:58:59107 // There are four type of modes how channels operate:
108 //
109 // - Server and named server: In these modes, the Channel is
110 // responsible for settingb up the IPC object
111 // - An "open" named server: It accepts connections from ANY client.
112 // The caller must then implement their own access-control based on the
113 // client process' user Id.
114 // - Client and named client: In these mode, the Channel merely
115 // connects to the already established IPC object.
116 //
117 // Each mode has its own Create*() API to create the Channel object.
118 //
119 // TODO(morrita): Replace CreateByModeForProxy() with one of above Create*().
120 //
morrita@chromium.org2f60c9b2014-06-06 20:13:51121 static scoped_ptr<Channel> Create(
morrita373af032014-09-09 19:35:24122 const IPC::ChannelHandle &channel_handle, Mode mode, Listener* listener);
morrita@chromium.org2f60c9b2014-06-06 20:13:51123
morrita@chromium.orge482111a82014-05-30 03:58:59124 static scoped_ptr<Channel> CreateClient(
125 const IPC::ChannelHandle &channel_handle, Listener* listener);
126
127 // Channels on Windows are named by default and accessible from other
128 // processes. On POSIX channels are anonymous by default and not accessible
129 // from other processes. Named channels work via named unix domain sockets.
130 // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and
131 // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT.
132 static scoped_ptr<Channel> CreateNamedServer(
133 const IPC::ChannelHandle &channel_handle, Listener* listener);
134 static scoped_ptr<Channel> CreateNamedClient(
135 const IPC::ChannelHandle &channel_handle, Listener* listener);
136#if defined(OS_POSIX)
137 // An "open" named server accepts connections from ANY client.
138 // The caller must then implement their own access-control based on the
139 // client process' user Id.
140 static scoped_ptr<Channel> CreateOpenNamedServer(
141 const IPC::ChannelHandle &channel_handle, Listener* listener);
142#endif
143 static scoped_ptr<Channel> CreateServer(
144 const IPC::ChannelHandle &channel_handle, Listener* listener);
145
initial.commit09911bf2008-07-26 23:55:29146
hans@chromium.org3690ebe02011-05-25 09:08:19147 virtual ~Channel();
initial.commit09911bf2008-07-26 23:55:29148
149 // Connect the pipe. On the server side, this will initiate
150 // waiting for connections. On the client, it attempts to
151 // connect to a pre-existing pipe. Note, calling Connect()
152 // will not block the calling thread and may complete
153 // asynchronously.
morrita@chromium.org2f60c9b2014-06-06 20:13:51154 virtual bool Connect() WARN_UNUSED_RESULT = 0;
initial.commit09911bf2008-07-26 23:55:29155
156 // Close this Channel explicitly. May be called multiple times.
dmaclach@chromium.org22b42c52010-12-20 06:59:23157 // On POSIX calling close on an IPC channel that listens for connections will
158 // cause it to close any accepted connections, and it will stop listening for
159 // new connections. If you just want to close the currently accepted
160 // connection and listen for new ones, use ResetToAcceptingConnectionState.
morrita@chromium.org2f60c9b2014-06-06 20:13:51161 virtual void Close() = 0;
initial.commit09911bf2008-07-26 23:55:29162
jschuh@chromium.org0a6fc4b2012-04-05 02:38:34163 // Get the process ID for the connected peer.
brettw@chromium.org108fd342013-01-04 20:46:54164 //
165 // Returns base::kNullProcessId if the peer is not connected yet. Watch out
166 // for race conditions. You can easily get a channel to another process, but
167 // if your process has not yet processed the "hello" message from the remote
168 // side, this will fail. You should either make sure calling this is either
169 // in response to a message from the remote side (which guarantees that it's
170 // been connected), or you wait for the "connected" notification on the
171 // listener.
morrita@chromium.org2f60c9b2014-06-06 20:13:51172 virtual base::ProcessId GetPeerPID() const = 0;
jschuh@chromium.org0a6fc4b2012-04-05 02:38:34173
morrita@chromium.org64860882014-08-04 23:44:17174 // Get its own process id. This value is told to the peer.
175 virtual base::ProcessId GetSelfPID() const = 0;
176
initial.commit09911bf2008-07-26 23:55:29177 // Send a message over the Channel to the listener on the other end.
178 //
rvargas@google.comc1afbd2c2008-10-13 19:19:36179 // |message| must be allocated using operator new. This object will be
180 // deleted once the contents of the Message have been sent.
morrita@chromium.org2f60c9b2014-06-06 20:13:51181 virtual bool Send(Message* message) = 0;
initial.commit09911bf2008-07-26 23:55:29182
morrita@chromium.org2f60c9b2014-06-06 20:13:51183#if defined(OS_POSIX) && !defined(OS_NACL)
jeremy@chromium.orgdf3c1ca12008-12-19 21:37:01184 // On POSIX an IPC::Channel wraps a socketpair(), this method returns the
agl@chromium.orgcc8f1462009-06-12 17:36:55185 // FD # for the client end of the socket.
jeremy@chromium.orgdf3c1ca12008-12-19 21:37:01186 // This method may only be called on the server side of a channel.
phajdan.jr@chromium.org2ce26c432011-09-19 17:08:12187 // This method can be called on any thread.
morrita@chromium.org2f60c9b2014-06-06 20:13:51188 virtual int GetClientFileDescriptor() const = 0;
dmaclach@chromium.org22b42c52010-12-20 06:59:23189
phajdan.jr@chromium.org2ce26c432011-09-19 17:08:12190 // Same as GetClientFileDescriptor, but transfers the ownership of the
191 // file descriptor to the caller.
192 // This method can be called on any thread.
morritaa409ccc2014-10-20 23:53:25193 virtual base::ScopedFD TakeClientFileDescriptor() = 0;
wez@chromium.org8ec3fbe2011-04-06 12:01:44194#endif // defined(OS_POSIX) && !defined(OS_NACL)
neb@chromium.org74f27542010-03-31 17:44:57195
kkania@chromium.org313c00e52011-08-09 06:46:06196 // Returns true if a named server channel is initialized on the given channel
197 // ID. Even if true, the server may have already accepted a connection.
198 static bool IsNamedServerInitialized(const std::string& channel_id);
199
bbudge@chromium.orgbccbaf22012-09-28 21:46:01200#if !defined(OS_NACL)
jschuh@chromium.org5c41e6e12012-03-17 02:20:46201 // Generates a channel ID that's non-predictable and unique.
202 static std::string GenerateUniqueRandomChannelID();
203
204 // Generates a channel ID that, if passed to the client as a shared secret,
205 // will validate that the client's authenticity. On platforms that do not
206 // require additional this is simply calls GenerateUniqueRandomChannelID().
207 // For portability the prefix should not include the \ character.
208 static std::string GenerateVerifiedChannelID(const std::string& prefix);
bbudge@chromium.orgbccbaf22012-09-28 21:46:01209#endif
jschuh@chromium.org5c41e6e12012-03-17 02:20:46210
jamescook@chromium.orge1d67a882011-08-31 21:11:04211#if defined(OS_LINUX)
212 // Sandboxed processes live in a PID namespace, so when sending the IPC hello
213 // message from client to server we need to send the PID from the global
214 // PID namespace.
215 static void SetGlobalPid(int pid);
216#endif
217
epenner@chromium.orge097b932014-03-19 06:34:52218#if defined(OS_ANDROID)
219 // Most tests are single process and work the same on all platforms. However
220 // in some cases we want to test multi-process, and Android differs in that it
221 // can't 'exec' after forking. This callback resets any data in the forked
222 // process such that it acts similar to if it was exec'd, for tests.
223 static void NotifyProcessForkedForTesting();
224#endif
225
initial.commit09911bf2008-07-26 23:55:29226};
227
hidehiko@chromium.org2648d5f92014-02-21 15:05:25228#if defined(OS_POSIX)
229// SocketPair() creates a pair of socket FDs suitable for using with
230// IPC::Channel.
231IPC_EXPORT bool SocketPair(int* fd1, int* fd2);
232#endif
233
jeremy@chromium.org514411fc2008-12-10 22:28:11234} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29235
agl@chromium.org946d1b22009-07-22 23:57:21236#endif // IPC_IPC_CHANNEL_H_