blob: 14bb95c48e2228701960f943a454c4a5349c503f [file] [log] [blame]
[email protected]334f3022012-02-29 22:48:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]514411fc2008-12-10 22:28:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]946d1b22009-07-22 23:57:215#ifndef IPC_IPC_CHANNEL_POSIX_H_
6#define IPC_IPC_CHANNEL_POSIX_H_
[email protected]514411fc2008-12-10 22:28:117
[email protected]946d1b22009-07-22 23:57:218#include "ipc/ipc_channel.h"
[email protected]514411fc2008-12-10 22:28:119
avi246998d82015-12-22 02:39:0410#include <stddef.h>
[email protected]526776c2009-02-07 00:39:2611#include <sys/socket.h> // for CMSG macros
12
[email protected]514411fc2008-12-10 22:28:1113#include <queue>
[email protected]dc875dc2013-10-15 00:07:0014#include <set>
[email protected]514411fc2008-12-10 22:28:1115#include <string>
[email protected]526776c2009-02-07 00:39:2616#include <vector>
[email protected]514411fc2008-12-10 22:28:1117
morritace44fef2014-09-29 22:25:5418#include "base/files/scoped_file.h"
avi246998d82015-12-22 02:39:0419#include "base/macros.h"
[email protected]2a9ec0e2013-07-17 23:00:3020#include "base/message_loop/message_loop.h"
[email protected]e66ef602013-07-24 05:15:2421#include "base/process/process.h"
avi246998d82015-12-22 02:39:0422#include "build/build_config.h"
[email protected]d805c6a2012-03-08 12:30:2823#include "ipc/ipc_channel_reader.h"
morrita4b5c28e22015-01-14 21:17:0624#include "ipc/ipc_message_attachment_set.h"
[email protected]514411fc2008-12-10 22:28:1125
26namespace IPC {
27
[email protected]5210d592014-06-07 06:15:5328class IPC_EXPORT ChannelPosix : public Channel,
29 public internal::ChannelReader,
30 public base::MessageLoopForIO::Watcher {
[email protected]514411fc2008-12-10 22:28:1131 public:
erikchen27aa7d82015-06-16 21:21:0432 // |broker| must outlive the newly created object.
33 ChannelPosix(const IPC::ChannelHandle& channel_handle,
34 Mode mode,
erikchen5708aae2015-09-14 17:45:1235 Listener* listener);
dchengfe61fca2014-10-22 02:29:5236 ~ChannelPosix() override;
[email protected]2f60c9b2014-06-06 20:13:5137
38 // Channel implementation
dchengfe61fca2014-10-22 02:29:5239 bool Connect() override;
40 void Close() override;
41 bool Send(Message* message) override;
erikchen27aa7d82015-06-16 21:21:0442 AttachmentBroker* GetAttachmentBroker() override;
dchengfe61fca2014-10-22 02:29:5243 base::ProcessId GetPeerPID() const override;
44 base::ProcessId GetSelfPID() const override;
45 int GetClientFileDescriptor() const override;
46 base::ScopedFD TakeClientFileDescriptor() override;
[email protected]5210d592014-06-07 06:15:5347
48 // Returns true if the channel supports listening for connections.
49 bool AcceptsConnections() const;
50
51 // Returns true if the channel supports listening for connections and is
52 // currently connected.
53 bool HasAcceptedConnection() const;
54
55 // Closes any currently connected socket, and returns to a listening state
56 // for more connections.
57 void ResetToAcceptingConnectionState();
58
59 // Returns true if the peer process' effective user id can be determined, in
60 // which case the supplied peer_euid is updated with it.
61 bool GetPeerEuid(uid_t* peer_euid) const;
[email protected]2f60c9b2014-06-06 20:13:5162
[email protected]2ce26c432011-09-19 17:08:1263 void CloseClientFileDescriptor();
[email protected]2f60c9b2014-06-06 20:13:5164
[email protected]313c00e52011-08-09 06:46:0665 static bool IsNamedServerInitialized(const std::string& channel_id);
perkjdbcac352014-12-11 17:27:5866#if defined(OS_LINUX)
67 static void SetGlobalPid(int pid);
sammce4d0abd2016-03-07 22:38:0468 static int GetGlobalPid();
perkjdbcac352014-12-11 17:27:5869#endif // OS_LINUX
[email protected]df3c1ca12008-12-19 21:37:0170
[email protected]514411fc2008-12-10 22:28:1171 private:
[email protected]1707726c2011-02-03 20:35:0972 bool CreatePipe(const IPC::ChannelHandle& channel_handle);
[email protected]514411fc2008-12-10 22:28:1173
erikchenf81b10a2015-10-06 21:46:3774 // Returns false on recoverable error.
75 // There are two reasons why this method might leave messages in the
76 // output_queue_.
77 // 1. |waiting_connect_| is |true|.
78 // 2. |is_blocked_on_write_| is |true|.
79 // If any of these conditionals change, this method should be called, as
80 // previously blocked messages may no longer be blocked.
[email protected]514411fc2008-12-10 22:28:1181 bool ProcessOutgoingMessages();
82
[email protected]22b42c52010-12-20 06:59:2383 bool AcceptConnection();
84 void ClosePipeOnError();
[email protected]64860882014-08-04 23:44:1785 int GetHelloMessageProcId() const;
[email protected]22b42c52010-12-20 06:59:2386 void QueueHelloMessage();
[email protected]dc875dc2013-10-15 00:07:0087 void CloseFileDescriptors(Message* msg);
88 void QueueCloseFDMessage(int fd, int hops);
[email protected]22b42c52010-12-20 06:59:2389
[email protected]d805c6a2012-03-08 12:30:2890 // ChannelReader implementation.
dchengfe61fca2014-10-22 02:29:5291 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override;
erikchende9412b82015-07-27 18:26:1492 bool ShouldDispatchInputMessage(Message* msg) override;
93 bool GetNonBrokeredAttachments(Message* msg) override;
dchengfe61fca2014-10-22 02:29:5294 bool DidEmptyInputBuffers() override;
95 void HandleInternalMessage(const Message& msg) override;
erikchen3c175a32015-07-28 23:16:4896 base::ProcessId GetSenderPID() override;
erikchen8c73f832015-07-30 22:26:0897 bool IsAttachmentBrokerEndpoint() override;
[email protected]334f3022012-02-29 22:48:1498
[email protected]334f3022012-02-29 22:48:1499 // Finds the set of file descriptors in the given message. On success,
100 // appends the descriptors to the input_fds_ member and returns true
101 //
102 // Returns false if the message was truncated. In this case, any handles that
103 // were sent will be closed.
104 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg);
105
106 // Closes all handles in the input_fds_ list and clears the list. This is
107 // used to clean up handles in error conditions to avoid leaking the handles.
108 void ClearInputFDs();
109
[email protected]5f594c02009-05-01 22:37:59110 // MessageLoopForIO::Watcher implementation.
dchengfe61fca2014-10-22 02:29:52111 void OnFileCanReadWithoutBlocking(int fd) override;
112 void OnFileCanWriteWithoutBlocking(int fd) override;
[email protected]514411fc2008-12-10 22:28:11113
erikchenf81b10a2015-10-06 21:46:37114 // Returns |false| on channel error.
115 // If |message| has brokerable attachments, those attachments are passed to
116 // the AttachmentBroker (which in turn invokes Send()), so this method must
117 // be re-entrant.
118 // Adds |message| to |output_queue_| and calls ProcessOutgoingMessages().
119 bool ProcessMessageForDelivery(Message* message);
120
121 // Moves all messages from |prelim_queue_| to |output_queue_| by calling
122 // ProcessMessageForDelivery().
123 // Returns |false| on channel error.
124 bool FlushPrelimQueue();
125
[email protected]514411fc2008-12-10 22:28:11126 Mode mode_;
127
[email protected]0a6fc4b2012-04-05 02:38:34128 base::ProcessId peer_pid_;
129
[email protected]e45e6c02008-12-15 22:02:17130 // After accepting one client connection on our server socket we want to
131 // stop listening.
[email protected]fd0a773a2013-04-30 20:55:03132 base::MessageLoopForIO::FileDescriptorWatcher
133 server_listen_connection_watcher_;
134 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_;
135 base::MessageLoopForIO::FileDescriptorWatcher write_watcher_;
[email protected]514411fc2008-12-10 22:28:11136
[email protected]e45e6c02008-12-15 22:02:17137 // Indicates whether we're currently blocked waiting for a write to complete.
138 bool is_blocked_on_write_;
[email protected]22b42c52010-12-20 06:59:23139 bool waiting_connect_;
[email protected]514411fc2008-12-10 22:28:11140
141 // If sending a message blocks then we use this variable
142 // to keep track of where we are.
143 size_t message_send_bytes_written_;
144
[email protected]22b42c52010-12-20 06:59:23145 // File descriptor we're listening on for new connections if we listen
146 // for connections.
morritace44fef2014-09-29 22:25:54147 base::ScopedFD server_listen_pipe_;
[email protected]d2e884d2009-06-22 20:37:52148
149 // The pipe used for communication.
morritace44fef2014-09-29 22:25:54150 base::ScopedFD pipe_;
[email protected]d2e884d2009-06-22 20:37:52151
152 // For a server, the client end of our socketpair() -- the other end of our
153 // pipe_ that is passed to the client.
morritace44fef2014-09-29 22:25:54154 base::ScopedFD client_pipe_;
[email protected]2f60c9b2014-06-06 20:13:51155 mutable base::Lock client_pipe_lock_; // Lock that protects |client_pipe_|.
[email protected]5f594c02009-05-01 22:37:59156
157 // The "name" of our pipe. On Windows this is the global identifier for
158 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
[email protected]514411fc2008-12-10 22:28:11159 std::string pipe_name_;
160
erikchenf81b10a2015-10-06 21:46:37161 // Messages not yet ready to be sent are queued here. Messages removed from
162 // this queue are placed in the output_queue_. The double queue is
163 // unfortunate, but is necessary because messages with brokerable attachments
164 // can generate multiple messages to be sent (possibly from other channels).
165 // Some of these generated messages cannot be sent until |peer_pid_| has been
166 // configured.
167 // As soon as |peer_pid| has been configured, there is no longer any need for
168 // |prelim_queue_|. All messages are flushed, and no new messages are added.
169 std::queue<Message*> prelim_queue_;
170
[email protected]514411fc2008-12-10 22:28:11171 // Messages to be sent are queued here.
erikchenf81b10a2015-10-06 21:46:37172 std::queue<OutputElement*> output_queue_;
[email protected]514411fc2008-12-10 22:28:11173
[email protected]05094a32011-09-01 00:50:13174 // We assume a worst case: kReadBufferSize bytes of messages, where each
175 // message has no payload and a full complement of descriptors.
176 static const size_t kMaxReadFDs =
177 (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) *
morrita4b5c28e22015-01-14 21:17:06178 MessageAttachmentSet::kMaxDescriptorsPerMessage;
[email protected]526776c2009-02-07 00:39:26179
[email protected]334f3022012-02-29 22:48:14180 // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros
bratell5937d45672015-05-29 13:19:01181 // are not constant so we have to pick a "large enough" padding for headers.
[email protected]337c6bf2009-02-07 00:51:58182#if defined(OS_MACOSX)
bratell5937d45672015-05-29 13:19:01183 static const size_t kMaxReadFDBuffer = 1024 + sizeof(int) * kMaxReadFDs;
[email protected]337c6bf2009-02-07 00:51:58184#else
[email protected]334f3022012-02-29 22:48:14185 static const size_t kMaxReadFDBuffer = CMSG_SPACE(sizeof(int) * kMaxReadFDs);
[email protected]337c6bf2009-02-07 00:51:58186#endif
bratell5937d45672015-05-29 13:19:01187 static_assert(kMaxReadFDBuffer <= 8192,
188 "kMaxReadFDBuffer too big for a stack buffer");
[email protected]334f3022012-02-29 22:48:14189
190 // File descriptors extracted from messages coming off of the channel. The
191 // handles may span messages and come off different channels from the message
192 // data (in the case of READWRITE), and are processed in FIFO here.
[email protected]7e9eecb62012-04-09 21:40:44193 // NOTE: The implementation assumes underlying storage here is contiguous, so
194 // don't change to something like std::deque<> without changing the
195 // implementation!
196 std::vector<int> input_fds_;
[email protected]514411fc2008-12-10 22:28:11197
morrita2ad0be42015-01-27 20:05:53198
199 void ResetSafely(base::ScopedFD* fd);
200 bool in_dtor_;
201
[email protected]dc875dc2013-10-15 00:07:00202#if defined(OS_MACOSX)
203 // On OSX, sent FDs must not be closed until we get an ack.
204 // Keep track of sent FDs here to make sure the remote is not
205 // trying to bamboozle us.
206 std::set<int> fds_to_close_;
207#endif
208
[email protected]22b42c52010-12-20 06:59:23209 // True if we are responsible for unlinking the unix domain socket file.
210 bool must_unlink_;
[email protected]514411fc2008-12-10 22:28:11211
perkjdbcac352014-12-11 17:27:58212#if defined(OS_LINUX)
213 // If non-zero, overrides the process ID sent in the hello message.
214 static int global_pid_;
215#endif // OS_LINUX
216
[email protected]2f60c9b2014-06-06 20:13:51217 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelPosix);
[email protected]514411fc2008-12-10 22:28:11218};
219
220} // namespace IPC
221
[email protected]946d1b22009-07-22 23:57:21222#endif // IPC_IPC_CHANNEL_POSIX_H_