[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_CHANNEL_POSIX_H_ |
| 6 | #define IPC_IPC_CHANNEL_POSIX_H_ |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 7 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 8 | #include "ipc/ipc_channel.h" |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 9 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 10 | #include <stddef.h> |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 11 | #include <sys/socket.h> // for CMSG macros |
| 12 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 13 | #include <queue> |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 14 | #include <set> |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 15 | #include <string> |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 16 | #include <vector> |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 17 | |
morrita | ce44fef | 2014-09-29 22:25:54 | [diff] [blame] | 18 | #include "base/files/scoped_file.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 19 | #include "base/macros.h" |
[email protected] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 20 | #include "base/message_loop/message_loop.h" |
[email protected] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 21 | #include "base/process/process.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 22 | #include "build/build_config.h" |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 23 | #include "ipc/ipc_channel_reader.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 24 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 25 | |
| 26 | namespace IPC { |
| 27 | |
[email protected] | 5210d59 | 2014-06-07 06:15:53 | [diff] [blame] | 28 | class IPC_EXPORT ChannelPosix : public Channel, |
| 29 | public internal::ChannelReader, |
| 30 | public base::MessageLoopForIO::Watcher { |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 31 | public: |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 32 | // |broker| must outlive the newly created object. |
| 33 | ChannelPosix(const IPC::ChannelHandle& channel_handle, |
| 34 | Mode mode, |
erikchen | 5708aae | 2015-09-14 17:45:12 | [diff] [blame] | 35 | Listener* listener); |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 36 | ~ChannelPosix() override; |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 37 | |
| 38 | // Channel implementation |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 39 | bool Connect() override; |
| 40 | void Close() override; |
| 41 | bool Send(Message* message) override; |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 42 | AttachmentBroker* GetAttachmentBroker() override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 43 | base::ProcessId GetPeerPID() const override; |
| 44 | base::ProcessId GetSelfPID() const override; |
| 45 | int GetClientFileDescriptor() const override; |
| 46 | base::ScopedFD TakeClientFileDescriptor() override; |
[email protected] | 5210d59 | 2014-06-07 06:15:53 | [diff] [blame] | 47 | |
| 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] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 62 | |
[email protected] | 2ce26c43 | 2011-09-19 17:08:12 | [diff] [blame] | 63 | void CloseClientFileDescriptor(); |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 64 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 65 | static bool IsNamedServerInitialized(const std::string& channel_id); |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 66 | #if defined(OS_LINUX) |
| 67 | static void SetGlobalPid(int pid); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 68 | static int GetGlobalPid(); |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 69 | #endif // OS_LINUX |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 70 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 71 | private: |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 72 | bool CreatePipe(const IPC::ChannelHandle& channel_handle); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 73 | |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 74 | // 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] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 81 | bool ProcessOutgoingMessages(); |
| 82 | |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 83 | bool AcceptConnection(); |
| 84 | void ClosePipeOnError(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 85 | int GetHelloMessageProcId() const; |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 86 | void QueueHelloMessage(); |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 87 | void CloseFileDescriptors(Message* msg); |
| 88 | void QueueCloseFDMessage(int fd, int hops); |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 89 | |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 90 | // ChannelReader implementation. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 91 | ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 92 | bool ShouldDispatchInputMessage(Message* msg) override; |
| 93 | bool GetNonBrokeredAttachments(Message* msg) override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 94 | bool DidEmptyInputBuffers() override; |
| 95 | void HandleInternalMessage(const Message& msg) override; |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 96 | base::ProcessId GetSenderPID() override; |
erikchen | 8c73f83 | 2015-07-30 22:26:08 | [diff] [blame] | 97 | bool IsAttachmentBrokerEndpoint() override; |
[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 98 | |
[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 99 | // 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] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 110 | // MessageLoopForIO::Watcher implementation. |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 111 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 112 | void OnFileCanWriteWithoutBlocking(int fd) override; |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 113 | |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 114 | // 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] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 126 | Mode mode_; |
| 127 | |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 128 | base::ProcessId peer_pid_; |
| 129 | |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 130 | // After accepting one client connection on our server socket we want to |
| 131 | // stop listening. |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 132 | base::MessageLoopForIO::FileDescriptorWatcher |
| 133 | server_listen_connection_watcher_; |
| 134 | base::MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
| 135 | base::MessageLoopForIO::FileDescriptorWatcher write_watcher_; |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 136 | |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 137 | // Indicates whether we're currently blocked waiting for a write to complete. |
| 138 | bool is_blocked_on_write_; |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 139 | bool waiting_connect_; |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 140 | |
| 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] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 145 | // File descriptor we're listening on for new connections if we listen |
| 146 | // for connections. |
morrita | ce44fef | 2014-09-29 22:25:54 | [diff] [blame] | 147 | base::ScopedFD server_listen_pipe_; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 148 | |
| 149 | // The pipe used for communication. |
morrita | ce44fef | 2014-09-29 22:25:54 | [diff] [blame] | 150 | base::ScopedFD pipe_; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 151 | |
| 152 | // For a server, the client end of our socketpair() -- the other end of our |
| 153 | // pipe_ that is passed to the client. |
morrita | ce44fef | 2014-09-29 22:25:54 | [diff] [blame] | 154 | base::ScopedFD client_pipe_; |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 155 | mutable base::Lock client_pipe_lock_; // Lock that protects |client_pipe_|. |
[email protected] | 5f594c0 | 2009-05-01 22:37:59 | [diff] [blame] | 156 | |
| 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] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 159 | std::string pipe_name_; |
| 160 | |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 161 | // 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] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 171 | // Messages to be sent are queued here. |
erikchen | f81b10a | 2015-10-06 21:46:37 | [diff] [blame] | 172 | std::queue<OutputElement*> output_queue_; |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 173 | |
[email protected] | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 174 | // 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)) * |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 178 | MessageAttachmentSet::kMaxDescriptorsPerMessage; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 179 | |
[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 180 | // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros |
bratell | 5937d4567 | 2015-05-29 13:19:01 | [diff] [blame] | 181 | // are not constant so we have to pick a "large enough" padding for headers. |
[email protected] | 337c6bf | 2009-02-07 00:51:58 | [diff] [blame] | 182 | #if defined(OS_MACOSX) |
bratell | 5937d4567 | 2015-05-29 13:19:01 | [diff] [blame] | 183 | static const size_t kMaxReadFDBuffer = 1024 + sizeof(int) * kMaxReadFDs; |
[email protected] | 337c6bf | 2009-02-07 00:51:58 | [diff] [blame] | 184 | #else |
[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 185 | static const size_t kMaxReadFDBuffer = CMSG_SPACE(sizeof(int) * kMaxReadFDs); |
[email protected] | 337c6bf | 2009-02-07 00:51:58 | [diff] [blame] | 186 | #endif |
bratell | 5937d4567 | 2015-05-29 13:19:01 | [diff] [blame] | 187 | static_assert(kMaxReadFDBuffer <= 8192, |
| 188 | "kMaxReadFDBuffer too big for a stack buffer"); |
[email protected] | 334f302 | 2012-02-29 22:48:14 | [diff] [blame] | 189 | |
| 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] | 7e9eecb6 | 2012-04-09 21:40:44 | [diff] [blame] | 193 | // 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] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 197 | |
morrita | 2ad0be4 | 2015-01-27 20:05:53 | [diff] [blame] | 198 | |
| 199 | void ResetSafely(base::ScopedFD* fd); |
| 200 | bool in_dtor_; |
| 201 | |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 202 | #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] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 209 | // True if we are responsible for unlinking the unix domain socket file. |
| 210 | bool must_unlink_; |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 211 | |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 212 | #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] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 217 | DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelPosix); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | } // namespace IPC |
| 221 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 222 | #endif // IPC_IPC_CHANNEL_POSIX_H_ |